Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prevent authentication proxy option #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions server/proxy/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/http"
"net/url"
)

type GameProfile struct {
Expand All @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions server/proxy/main/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -196,6 +197,7 @@ func DefaultConfig() (config *Config) {
Motd: "A LilyPad Server",
MaxPlayers: 1,
Authenticate: true,
PreventProxy: true,
}
return
}
Expand Down
2 changes: 1 addition & 1 deletion server/proxy/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion server/proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@ type Server struct {
maxPlayers *uint16
syncMaxPlayers *bool
authenticate *bool
preventProxy *bool
router Router
localizer Localizer
connect *connect.ProxyConnect
privateKey *rsa.PrivateKey
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
Expand Down Expand Up @@ -102,3 +104,8 @@ func (this *Server) Authenticate() (val bool) {
val = *this.authenticate
return
}

func (this *Server) PreventProxy() (val bool) {
val = *this.preventProxy
return
}
8 changes: 7 additions & 1 deletion server/proxy/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down