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

validate tcp and udp connections #31

Merged
merged 4 commits into from
Sep 5, 2023
Merged
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
21 changes: 21 additions & 0 deletions internal/gameServer/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ func (g *GameServer) tcpSendReg(conn *net.TCPConn) {
}

func (g *GameServer) processTCP(conn *net.TCPConn) {
defer conn.Close()

tcpData := &TCPData{Request: RequestNone}
incomingBuffer := make([]byte, 1500) //nolint:gomnd
for {
Expand Down Expand Up @@ -350,6 +352,25 @@ func (g *GameServer) watchTCP() {
} else if g.isConnClosed(err) {
return
}

validated := false
remoteAddr, err := net.ResolveTCPAddr(conn.RemoteAddr().Network(), conn.RemoteAddr().String())
if err != nil {
g.Logger.Error(err, "could not resolve remote IP")
conn.Close()
continue
}
for _, v := range g.Players {
if remoteAddr.IP.Equal(net.ParseIP(v.IP)) {
validated = true
}
}
if !validated {
g.Logger.Error(fmt.Errorf("invalid tcp connection"), "bad IP", "IP", conn.RemoteAddr().String())
conn.Close()
continue
}

g.Logger.Info("received TCP connection", "address", conn.RemoteAddr().String())
go g.processTCP(conn)
}
Expand Down
12 changes: 12 additions & 0 deletions internal/gameServer/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ func (g *GameServer) watchUDP() {
} else if g.isConnClosed(err) {
return
}

validated := false
for _, v := range g.Players {
if addr.IP.Equal(net.ParseIP(v.IP)) {
validated = true
}
}
if !validated {
g.Logger.Error(fmt.Errorf("invalid udp connection"), "bad IP", "IP", addr.IP)
continue
}

g.processUDP(addr, buf)
}
}
Expand Down
12 changes: 10 additions & 2 deletions internal/lobbyServer/lobby.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,12 @@ func (s *LobbyServer) wsHandler(ws *websocket.Conn) {
g.Emulator = receivedMessage.Emulator
g.Players = make(map[string]gameserver.Client)
g.Features = receivedMessage.Features
ip, _, err := net.SplitHostPort(ws.Request().RemoteAddr)
if err != nil {
s.Logger.Error(err, "could not parse IP", "IP", ws.Request().RemoteAddr)
}
g.Players[receivedMessage.PlayerName] = gameserver.Client{
IP: ws.Request().RemoteAddr,
IP: ip,
Number: 0,
Socket: ws,
}
Expand Down Expand Up @@ -434,9 +438,13 @@ func (s *LobbyServer) wsHandler(ws *websocket.Conn) {
}
}

ip, _, err := net.SplitHostPort(ws.Request().RemoteAddr)
if err != nil {
s.Logger.Error(err, "could not parse IP", "IP", ws.Request().RemoteAddr)
}
g.PlayersMutex.Lock() // any player can modify this from their own thread
g.Players[receivedMessage.PlayerName] = gameserver.Client{
IP: ws.Request().RemoteAddr,
IP: ip,
Socket: ws,
Number: number,
}
Expand Down