mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 14:44:02 +03:00
Add support of proxy protocol
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/9seconds/mtg/v2/antireplay"
|
||||
"github.com/9seconds/mtg/v2/events"
|
||||
"github.com/9seconds/mtg/v2/internal/config"
|
||||
"github.com/9seconds/mtg/v2/internal/proxyprotocol"
|
||||
"github.com/9seconds/mtg/v2/internal/utils"
|
||||
"github.com/9seconds/mtg/v2/ipblocklist"
|
||||
"github.com/9seconds/mtg/v2/ipblocklist/files"
|
||||
@@ -17,6 +18,7 @@ import (
|
||||
"github.com/9seconds/mtg/v2/mtglib"
|
||||
"github.com/9seconds/mtg/v2/network"
|
||||
"github.com/9seconds/mtg/v2/stats"
|
||||
"github.com/pires/go-proxyproto"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/yl2chen/cidranger"
|
||||
)
|
||||
@@ -275,6 +277,14 @@ func runProxy(conf *config.Config, version string) error { //nolint: funlen
|
||||
return fmt.Errorf("cannot start proxy: %w", err)
|
||||
}
|
||||
|
||||
if conf.ProxyProtocolListener.Get(false) {
|
||||
listener = &proxyprotocol.ListenerAdapter{
|
||||
Listener: proxyproto.Listener{
|
||||
Listener: listener,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
ctx := utils.RootContext()
|
||||
|
||||
go proxy.Serve(listener) //nolint: errcheck
|
||||
|
||||
@@ -25,6 +25,7 @@ type Config struct {
|
||||
AllowFallbackOnUnknownDC TypeBool `json:"allowFallbackOnUnknownDc"`
|
||||
Secret mtglib.Secret `json:"secret"`
|
||||
BindTo TypeHostPort `json:"bindTo"`
|
||||
ProxyProtocolListener TypeBool `json:"proxyProtocolListener"`
|
||||
PreferIP TypePreferIP `json:"preferIp"`
|
||||
DomainFrontingPort TypePort `json:"domainFrontingPort"`
|
||||
TolerateTimeSkewness TypeDuration `json:"tolerateTimeSkewness"`
|
||||
|
||||
@@ -13,6 +13,7 @@ type tomlConfig struct {
|
||||
AllowFallbackOnUnknownDC bool `toml:"allow-fallback-on-unknown-dc" json:"allowFallbackOnUnknownDc,omitempty"`
|
||||
Secret string `toml:"secret" json:"secret"`
|
||||
BindTo string `toml:"bind-to" json:"bindTo"`
|
||||
ProxyProtocolListener bool `toml:"proxy-protocol-listener" json:"proxyProtocolListener"`
|
||||
PreferIP string `toml:"prefer-ip" json:"preferIp,omitempty"`
|
||||
DomainFrontingPort uint `toml:"domain-fronting-port" json:"domainFrontingPort,omitempty"`
|
||||
TolerateTimeSkewness string `toml:"tolerate-time-skewness" json:"tolerateTimeSkewness,omitempty"`
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package proxyprotocol
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/pires/go-proxyproto"
|
||||
)
|
||||
|
||||
type ListenerAdapter struct {
|
||||
proxyproto.Listener
|
||||
}
|
||||
|
||||
func (l *ListenerAdapter) Accept() (net.Conn, error) {
|
||||
conn, err := l.Listener.Accept()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return connWrapper{conn.(*proxyproto.Conn)}, nil
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package proxyprotocol
|
||||
|
||||
import "github.com/pires/go-proxyproto"
|
||||
|
||||
type connWrapper struct {
|
||||
*proxyproto.Conn
|
||||
}
|
||||
|
||||
func (c connWrapper) CloseRead() error {
|
||||
tcpConn, ok := c.TCPConn()
|
||||
if !ok {
|
||||
panic("we support only tcp connections")
|
||||
}
|
||||
|
||||
return tcpConn.CloseRead()
|
||||
}
|
||||
|
||||
func (c connWrapper) CloseWrite() error {
|
||||
tcpConn, ok := c.TCPConn()
|
||||
if !ok {
|
||||
panic("we support only tcp connections")
|
||||
}
|
||||
|
||||
return tcpConn.CloseWrite()
|
||||
}
|
||||
Reference in New Issue
Block a user