Skip to content

Commit

Permalink
fix: server status with proxy protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
haveachin committed Dec 25, 2022
1 parent d08fc4e commit 3c21a7e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ tests:

devenv:
docker-compose -p infrared -f deployments/docker-compose.dev.yml up
docker-compose -p infrared -f deployments/docker-compose.dev.yml down

run:
go run -race . -c config.yml -w dev/ -e dev
Expand Down
3 changes: 2 additions & 1 deletion deployments/docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ services:
- 25566:25565/tcp
environment:
EULA: "TRUE"
VERSION: "1.19.2"
VERSION: "1.19.3"
TYPE: PAPER
#TYPE: "FORGE"
#FORGE_VERSION: "43.1.1"
networks:
Expand Down
41 changes: 27 additions & 14 deletions internal/pkg/java/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ func (s *statusResponseJSONProvider) isStatusCacheValid() bool {
return s.cacheSetAt.Add(s.cacheTTL).After(time.Now())
}

func (s *statusResponseJSONProvider) requestNewStatusResponseJSON() (status.ResponseJSON, error) {
func (s *statusResponseJSONProvider) requestNewStatusResponseJSON(pc *ProcessedConn) (status.ResponseJSON, error) {
rc, err := s.server.dial()
if err != nil {
return status.ResponseJSON{}, err
}

if err := s.server.prepareConns(pc, rc); err != nil {
rc.Close()
return status.ResponseJSON{}, err
}

if err := rc.WritePackets(s.handshakePk, s.statusRequestPk); err != nil {
return status.ResponseJSON{}, err
}
Expand All @@ -61,15 +66,15 @@ func (s *statusResponseJSONProvider) requestNewStatusResponseJSON() (status.Resp
return respJSON, nil
}

func (s *statusResponseJSONProvider) StatusResponseJSON() (status.ResponseJSON, error) {
func (s *statusResponseJSONProvider) StatusResponseJSON(pc *ProcessedConn) (status.ResponseJSON, error) {
s.mu.Lock()
defer s.mu.Unlock()

if s.isStatusCacheValid() {
return s.statusResponseJSONCache, nil
}

respJSON, err := s.requestNewStatusResponseJSON()
respJSON, err := s.requestNewStatusResponseJSON(pc)
if err != nil {
return status.ResponseJSON{}, err
}
Expand All @@ -81,7 +86,7 @@ func (s *statusResponseJSONProvider) StatusResponseJSON() (status.ResponseJSON,
}

type StatusResponseJSONProvider interface {
StatusResponseJSON() (status.ResponseJSON, error)
StatusResponseJSON(pc *ProcessedConn) (status.ResponseJSON, error)
}

type Server struct {
Expand Down Expand Up @@ -133,10 +138,24 @@ func (s Server) handleConn(c net.Conn) (infrared.Conn, error) {
return nil, err
}

if err := s.prepareConns(pc, rc); err != nil {
rc.Close()
return nil, err
}

// Sends the handshake and the request or login packet to the server
if err := rc.WritePackets(pc.readPks...); err != nil {
rc.Close()
return nil, err
}

return rc, nil
}

func (s Server) prepareConns(pc *ProcessedConn, rc net.Conn) error {
if s.SendProxyProtocol {
if err := writeProxyProtocolHeader(pc.RemoteAddr(), rc); err != nil {
defer rc.Close()
return nil, err
return err
}
}

Expand All @@ -151,13 +170,7 @@ func (s Server) handleConn(c net.Conn) (infrared.Conn, error) {
pc.readPks[0] = pc.handshake.Marshal()
}

// Sends the handshake and the request or login packet to the server
if err := rc.WritePackets(pc.readPks...); err != nil {
defer rc.Close()
return nil, err
}

return rc, nil
return nil
}

func (s Server) handleDialTimeoutStatusRequest(pc *ProcessedConn) error {
Expand Down Expand Up @@ -211,7 +224,7 @@ func (s Server) handleStatusPing(pc *ProcessedConn) error {
}

func (s Server) overrideStatusResponse(pc *ProcessedConn) error {
respJSON, err := s.statusResponseJSONProvider.StatusResponseJSON()
respJSON, err := s.statusResponseJSONProvider.StatusResponseJSON(pc)
if err != nil {
return err
}
Expand Down

0 comments on commit 3c21a7e

Please sign in to comment.