diff options
| author | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2024-06-15 11:13:16 -0700 |
|---|---|---|
| committer | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2024-06-15 11:13:16 -0700 |
| commit | 95159e5585762c06c654945070ba54262b7dcec9 (patch) | |
| tree | 23609018c1460b056ce22067290ea12ee851d483 /hotline/server.go | |
| parent | a6216dd89252fa01dc176f98f1e4ecfd3f637566 (diff) | |
Refactoring and cleanup
* Split CLI client into separate project
* Convert more functions to follow common Golang idioms e.g io.Reader, io.Writer
* Use ldflags for versioning
* Misc cleanup and simplification
Diffstat (limited to 'hotline/server.go')
| -rw-r--r-- | hotline/server.go | 38 |
1 files changed, 11 insertions, 27 deletions
diff --git a/hotline/server.go b/hotline/server.go index f2a69ad..b4841a7 100644 --- a/hotline/server.go +++ b/hotline/server.go @@ -152,24 +152,19 @@ func (s *Server) ServeFileTransfers(ctx context.Context, ln net.Listener) error func (s *Server) sendTransaction(t Transaction) error { clientID, err := byteToInt(*t.clientID) if err != nil { - return err + return fmt.Errorf("invalid client ID: %v", err) } s.mux.Lock() - client := s.Clients[uint16(clientID)] + client, ok := s.Clients[uint16(clientID)] s.mux.Unlock() - if client == nil { + if !ok || client == nil { return fmt.Errorf("invalid client id %v", *t.clientID) } - b, err := t.MarshalBinary() - if err != nil { - return err - } - - _, err = client.Connection.Write(b) + _, err = io.Copy(client.Connection, &t) if err != nil { - return err + return fmt.Errorf("failed to send transaction to client %v: %v", clientID, err) } return nil @@ -620,12 +615,7 @@ func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser NewField(FieldChatOptions, []byte{0, 0}), ) - b, err := t.MarshalBinary() - if err != nil { - return err - } - - _, err = rwc.Write(b) + _, err := io.Copy(rwc, t) if err != nil { return err } @@ -642,12 +632,8 @@ func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser NewField(FieldData, []byte("You are temporarily banned on this server")), NewField(FieldChatOptions, []byte{0, 0}), ) - b, err := t.MarshalBinary() - if err != nil { - return err - } - _, err = rwc.Write(b) + _, err := io.Copy(rwc, t) if err != nil { return err } @@ -677,13 +663,11 @@ func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser // If authentication fails, send error reply and close connection if !c.Authenticate(login, encodedPassword) { t := c.NewErrReply(&clientLogin, "Incorrect login.") - b, err := t.MarshalBinary() + + _, err := io.Copy(rwc, &t) if err != nil { return err } - if _, err := rwc.Write(b); err != nil { - return err - } c.logger.Info("Login failed", "clientVersion", fmt.Sprintf("%x", c.Version)) @@ -734,7 +718,7 @@ func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser if len(c.UserName) != 0 { // Add the client username to the logger. For 1.5+ clients, we don't have this information yet as it comes as // part of TranAgreed - c.logger = c.logger.With("name", string(c.UserName)) + c.logger = c.logger.With("Name", string(c.UserName)) c.logger.Info("Login successful", "clientVersion", "Not sent (probably 1.2.3)") @@ -838,7 +822,7 @@ func (s *Server) handleFileTransfer(ctx context.Context, rwc io.ReadWriter) erro rLogger := s.Logger.With( "remoteAddr", ctx.Value(contextKeyReq).(requestCtx).remoteAddr, "login", fileTransfer.ClientConn.Account.Login, - "name", string(fileTransfer.ClientConn.UserName), + "Name", string(fileTransfer.ClientConn.UserName), ) fullPath, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName) |