diff options
author | pg9182 <96569817+pg9182@users.noreply.github.com> | 2022-10-15 05:25:51 -0400 |
---|---|---|
committer | pg9182 <96569817+pg9182@users.noreply.github.com> | 2022-10-15 05:25:51 -0400 |
commit | b135e321c2253caf532e5e107350d2af3b97fc3d (patch) | |
tree | 75377c7e3b320924b0e36e727d3e1982a4aefba6 | |
parent | 6f424c8b6f4f005cee1e9c2f729e62dd18fc299b (diff) | |
download | Atlas-b135e321c2253caf532e5e107350d2af3b97fc3d.tar.gz Atlas-b135e321c2253caf532e5e107350d2af3b97fc3d.zip |
pkg/api/api0: Refactor ServerList UpdateServerByID
* Support verifying the server IP.
* Return an error instead of a bool.
-rw-r--r-- | pkg/api/api0/serverlist.go | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/pkg/api/api0/serverlist.go b/pkg/api/api0/serverlist.go index f077fbe..f7f2726 100644 --- a/pkg/api/api0/serverlist.go +++ b/pkg/api/api0/serverlist.go @@ -494,10 +494,17 @@ func (s *ServerList) PutServerByAddr(x *Server) (string, bool, error) { return nsrv.ID, replaced, nil } -// UpdateServerByID updates values for the server with the provided ID, -// returning false if it is dead (and couldn't be revived by the heartbeat, if -// any). -func (s *ServerList) UpdateServerByID(id string, u *ServerUpdate) bool { +var ( + ErrServerListUpdateServerDead = errors.New("no server found") + ErrServerListUpdateWrongIP = errors.New("wrong server update ip") +) + +// UpdateServerByID updates values for the server with the provided ID. If the +// error nil, the server was updated. If no live server (or a ghost server which +// could be made alive from u.Heartbeat) was found, errors.Is(err, +// ErrServerListUpdateServerDead). If ip is valid and doesn't match the target +// server, errors.Is(err, ErrServerListUpdateWrongIP). +func (s *ServerList) UpdateServerByID(id string, ip netip.Addr, u *ServerUpdate) error { t := s.now() // take a write lock on the server list @@ -506,7 +513,7 @@ func (s *ServerList) UpdateServerByID(id string, u *ServerUpdate) bool { // if the map isn't initialized, we don't have any servers if s.servers2 == nil { - return false + return ErrServerListUpdateServerDead } // force an update when we're finished @@ -519,13 +526,17 @@ func (s *ServerList) UpdateServerByID(id string, u *ServerUpdate) bool { if s.isServerGone(esrv, t) { s.freeServer(esrv) } - return false + return ErrServerListUpdateServerDead } // ensure another server hasn't already taken the auth port (which can // happen if it was a ghost) if osrv, exists := s.servers3[esrv.AuthAddr()]; exists && esrv != osrv { - return false + return ErrServerListUpdateServerDead + } + + if ip.IsValid() && esrv.Addr.Addr() != ip { + return ErrServerListUpdateWrongIP } // do the update @@ -552,7 +563,7 @@ func (s *ServerList) UpdateServerByID(id string, u *ServerUpdate) bool { esrv.MaxPlayers = *u.MaxPlayers } s.csForceUpdate() - return true + return nil } // ReapServers deletes dead servers from memory. |