diff --git a/server/proxy/auth/auth.go b/server/proxy/auth/auth.go index 3e67433..fe980ab 100644 --- a/server/proxy/auth/auth.go +++ b/server/proxy/auth/auth.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "net/http" + "net/url" ) type GameProfile struct { @@ -18,8 +19,14 @@ type GameProfileProperty struct { Signature string `json:"signature"` } -func Authenticate(name string, serverId string, sharedSecret []byte, publicKey []byte) (profile GameProfile, err error) { - response, err := http.Get(fmt.Sprintf(URL, name, MojangSha1Hex([]byte(serverId), sharedSecret, publicKey))) +func Authenticate(name string, serverId string, sharedSecret []byte, publicKey []byte, ip string) (profile GameProfile, err error) { + httpUrl := fmt.Sprintf(URL, name, MojangSha1Hex([]byte(serverId), sharedSecret, publicKey)) + if len(ip) > 0 { + //escape the ip to it correctly parse IPv6 addresses + httpUrl += "&ip=" + url.QueryEscape(ip) + } + + response, err := http.Get(httpUrl) if err != nil { return } diff --git a/server/proxy/main/config/config.go b/server/proxy/main/config/config.go index 4308ab5..be7012f 100644 --- a/server/proxy/main/config/config.go +++ b/server/proxy/main/config/config.go @@ -147,6 +147,7 @@ type ConfigProxy struct { MaxPlayers uint16 `yaml:"maxPlayers"` SyncMaxPlayers bool `yaml:"syncMaxPlayers"` Authenticate bool `yaml:"authenticate"` + PreventProxy bool `yaml:"preventProxy"` } type ConfigProxyLocale struct { @@ -196,6 +197,7 @@ func DefaultConfig() (config *Config) { Motd: "A LilyPad Server", MaxPlayers: 1, Authenticate: true, + PreventProxy: true, } return } diff --git a/server/proxy/main/main.go b/server/proxy/main/main.go index 654ebe4..14022cd 100644 --- a/server/proxy/main/main.go +++ b/server/proxy/main/main.go @@ -56,7 +56,7 @@ func main() { var server *proxy.Server go func() { var err error - server, err = proxy.NewServer(&cfg.Proxy.Motd, &cfg.Proxy.MaxPlayers, &cfg.Proxy.SyncMaxPlayers, &cfg.Proxy.Authenticate, cfg, cfg, proxyConnect) + server, err = proxy.NewServer(&cfg.Proxy.Motd, &cfg.Proxy.MaxPlayers, &cfg.Proxy.SyncMaxPlayers, &cfg.Proxy.Authenticate, &cfg.Proxy.PreventProxy, cfg, cfg, proxyConnect) if err != nil { serverErr <- err return diff --git a/server/proxy/server.go b/server/proxy/server.go index 6ea9cb2..646c49c 100644 --- a/server/proxy/server.go +++ b/server/proxy/server.go @@ -17,6 +17,7 @@ type Server struct { maxPlayers *uint16 syncMaxPlayers *bool authenticate *bool + preventProxy *bool router Router localizer Localizer connect *connect.ProxyConnect @@ -24,13 +25,14 @@ type Server struct { publicKey []byte } -func NewServer(motd *string, maxPlayers *uint16, syncMaxPlayers *bool, authenticate *bool, router Router, localizer Localizer, connect *connect.ProxyConnect) (this *Server, err error) { +func NewServer(motd *string, maxPlayers *uint16, syncMaxPlayers *bool, authenticate *bool, preventProxy *bool, router Router, localizer Localizer, connect *connect.ProxyConnect) (this *Server, err error) { this = new(Server) this.SessionRegistry = NewSessionRegistry() this.motd = motd this.maxPlayers = maxPlayers this.syncMaxPlayers = syncMaxPlayers this.authenticate = authenticate + this.preventProxy = preventProxy this.router = router this.localizer = localizer this.connect = connect @@ -102,3 +104,8 @@ func (this *Server) Authenticate() (val bool) { val = *this.authenticate return } + +func (this *Server) PreventProxy() (val bool) { + val = *this.preventProxy + return +} diff --git a/server/proxy/session.go b/server/proxy/session.go index 247d264..4cda929 100644 --- a/server/proxy/session.go +++ b/server/proxy/session.go @@ -402,7 +402,13 @@ func (this *Session) HandlePacket(packet packet.Packet) (err error) { return } var authErr error - this.profile, authErr = auth.Authenticate(this.name, this.serverId, sharedSecret, this.server.publicKey) + + ip := this.remoteIp + if !this.server.PreventProxy() { + ip = "" + } + + this.profile, authErr = auth.Authenticate(this.name, this.serverId, sharedSecret, this.server.publicKey, ip) if authErr != nil { this.SetAuthenticated(false) fmt.Println("Proxy server, failed to authorize:", this.name, "ip:", this.remoteIp, "err:", authErr)