-const (
- userIdleSeconds = 300 // time in seconds before an inactive user is marked idle
- idleCheckInterval = 10 // time in seconds to check for idle users
- trackerUpdateFrequency = 300 // time in seconds between tracker re-registration
-)
+// Converts bytes from Mac Roman encoding to UTF-8
+var txtDecoder = charmap.Macintosh.NewDecoder()
-var nostalgiaVersion = []byte{0, 0, 2, 0x2c} // version ID used by the Nostalgia client
+// Converts bytes from UTF-8 to Mac Roman encoding
+var txtEncoder = charmap.Macintosh.NewEncoder()
Port int
Accounts map[string]*Account
Agreement []byte
Clients map[uint16]*ClientConn
fileTransfers map[[4]byte]*FileTransfer
Port int
Accounts map[string]*Account
Agreement []byte
Clients map[uint16]*ClientConn
fileTransfers map[[4]byte]*FileTransfer
- Config *Config
- ConfigDir string
- Logger *zap.SugaredLogger
- PrivateChats map[uint32]*PrivateChat
+ Config *Config
+ ConfigDir string
+ Logger *zap.SugaredLogger
+
+ PrivateChatsMu sync.Mutex
+ PrivateChats map[uint32]*PrivateChat
+
func (s *Server) ListenAndServe(ctx context.Context, cancelRoot context.CancelFunc) error {
s.Logger.Infow("Hotline server started",
"version", VERSION,
func (s *Server) ListenAndServe(ctx context.Context, cancelRoot context.CancelFunc) error {
s.Logger.Infow("Hotline server started",
"version", VERSION,
- "API port", fmt.Sprintf(":%v", s.Port),
- "Transfer port", fmt.Sprintf(":%v", s.Port+1),
+ "API port", fmt.Sprintf("%s:%v", s.NetInterface, s.Port),
+ "Transfer port", fmt.Sprintf("%s:%v", s.NetInterface, s.Port+1),
- ln, err := net.Listen("tcp", fmt.Sprintf("%s:%v", "", s.Port))
+ ln, err := net.Listen("tcp", fmt.Sprintf("%s:%v", s.NetInterface, s.Port))
- ln, err := net.Listen("tcp", fmt.Sprintf("%s:%v", "", s.Port+1))
+ ln, err := net.Listen("tcp", fmt.Sprintf("%s:%v", s.NetInterface, s.Port+1))
-func NewServer(configDir string, netPort int, logger *zap.SugaredLogger, FS FileStore) (*Server, error) {
+func NewServer(configDir, netInterface string, netPort int, logger *zap.SugaredLogger, fs FileStore) (*Server, error) {
Logger: logger,
NextGuestID: new(uint16),
outbox: make(chan Transaction),
Logger: logger,
NextGuestID: new(uint16),
outbox: make(chan Transaction),
- server.Config.FileRoot = filepath.Join(configDir, "Files")
+ // If the FileRoot is an absolute path, use it, otherwise treat as a relative path to the config dir.
+ if !filepath.IsAbs(server.Config.FileRoot) {
+ server.Config.FileRoot = filepath.Join(configDir, server.Config.FileRoot)
+ }
- tranNotifyChangeUser,
- NewField(fieldUserID, *c.ID),
- NewField(fieldUserFlags, c.Flags),
- NewField(fieldUserName, c.UserName),
- NewField(fieldUserIconID, c.Icon),
+ TranNotifyChangeUser,
+ NewField(FieldUserID, *c.ID),
+ NewField(FieldUserFlags, c.Flags),
+ NewField(FieldUserName, c.UserName),
+ NewField(FieldUserIconID, c.Icon),
var connectedUsers []Field
for _, c := range sortedClients(s.Clients) {
var connectedUsers []Field
for _, c := range sortedClients(s.Clients) {
-// dontPanic logs panics instead of crashing
-func dontPanic(logger *zap.SugaredLogger) {
- if r := recover(); r != nil {
- fmt.Println("stacktrace from panic: \n" + string(debug.Stack()))
- logger.Errorw("PANIC", "err", r, "trace", string(debug.Stack()))
- }
-}
-
// handleNewConnection takes a new net.Conn and performs the initial login sequence
func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser, remoteAddr string) error {
defer dontPanic(s.Logger)
// handleNewConnection takes a new net.Conn and performs the initial login sequence
func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser, remoteAddr string) error {
defer dontPanic(s.Logger)
- clientLogin, _, err := ReadTransaction(scanner.Bytes())
- if err != nil {
- panic(err)
- }
+ // Make a new []byte slice and copy the scanner bytes to it. This is critical to avoid a data race as the
+ // scanner re-uses the buffer for subsequent scans.
+ buf := make([]byte, len(scanner.Bytes()))
+ copy(buf, scanner.Bytes())
// check if remoteAddr is present in the ban list
if banUntil, ok := s.banList[strings.Split(remoteAddr, ":")[0]]; ok {
// permaban
if banUntil == nil {
// check if remoteAddr is present in the ban list
if banUntil, ok := s.banList[strings.Split(remoteAddr, ":")[0]]; ok {
// permaban
if banUntil == nil {
- s.outbox <- *NewTransaction(
- tranServerMsg,
- c.ID,
- NewField(fieldData, []byte("You are permanently banned on this server")),
- NewField(fieldChatOptions, []byte{0, 0}),
+ t := NewTransaction(
+ TranServerMsg,
+ &[]byte{0, 0},
+ NewField(FieldData, []byte("You are permanently banned on this server")),
+ NewField(FieldChatOptions, []byte{0, 0}),
- } else if time.Now().Before(*banUntil) {
- s.outbox <- *NewTransaction(
- tranServerMsg,
- c.ID,
- NewField(fieldData, []byte("You are temporarily banned on this server")),
- NewField(fieldChatOptions, []byte{0, 0}),
+ }
+
+ // temporary ban
+ if time.Now().Before(*banUntil) {
+ t := NewTransaction(
+ TranServerMsg,
+ &[]byte{0, 0},
+ NewField(FieldData, []byte("You are temporarily banned on this server")),
+ NewField(FieldChatOptions, []byte{0, 0}),
- encodedLogin := clientLogin.GetField(fieldUserLogin).Data
- encodedPassword := clientLogin.GetField(fieldUserPassword).Data
- c.Version = clientLogin.GetField(fieldVersion).Data
+ encodedLogin := clientLogin.GetField(FieldUserLogin).Data
+ encodedPassword := clientLogin.GetField(FieldUserPassword).Data
+ c.Version = clientLogin.GetField(FieldVersion).Data
// If authentication fails, send error reply and close connection
if !c.Authenticate(login, encodedPassword) {
// If authentication fails, send error reply and close connection
if !c.Authenticate(login, encodedPassword) {
- if clientLogin.GetField(fieldUserIconID).Data != nil {
- c.Icon = clientLogin.GetField(fieldUserIconID).Data
+ if clientLogin.GetField(FieldUserIconID).Data != nil {
+ c.Icon = clientLogin.GetField(FieldUserIconID).Data
- s.outbox <- c.NewReply(clientLogin,
- NewField(fieldVersion, []byte{0x00, 0xbe}),
- NewField(fieldCommunityBannerID, []byte{0, 0}),
- NewField(fieldServerName, []byte(s.Config.Name)),
+ s.outbox <- c.NewReply(&clientLogin,
+ NewField(FieldVersion, []byte{0x00, 0xbe}),
+ NewField(FieldCommunityBannerID, []byte{0, 0}),
+ NewField(FieldServerName, []byte(s.Config.Name)),
- c.Server.outbox <- *NewTransaction(tranUserAccess, c.ID, NewField(fieldUserAccess, c.Account.Access[:]))
+ c.Server.outbox <- *NewTransaction(TranUserAccess, c.ID, NewField(FieldUserAccess, c.Account.Access[:]))
- // client versions. For 1.2.3 client, we do not send tranShowAgreement. For other client versions, we send
- // tranShowAgreement but with the NoServerAgreement field set to 1.
+ // client versions. For 1.2.3 client, we do not send TranShowAgreement. For other client versions, we send
+ // TranShowAgreement but with the NoServerAgreement field set to 1.
if c.Authorize(accessNoAgreement) {
// If client version is nil, then the client uses the 1.2.3 login behavior
if c.Version != nil {
if c.Authorize(accessNoAgreement) {
// If client version is nil, then the client uses the 1.2.3 login behavior
if c.Version != nil {
- c.Server.outbox <- *NewTransaction(tranShowAgreement, c.ID, NewField(fieldNoServerAgreement, []byte{1}))
+ c.Server.outbox <- *NewTransaction(TranShowAgreement, c.ID, NewField(FieldNoServerAgreement, []byte{1}))
- c.Server.outbox <- *NewTransaction(tranShowAgreement, c.ID, NewField(fieldData, s.Agreement))
+ c.Server.outbox <- *NewTransaction(TranShowAgreement, c.ID, NewField(FieldData, s.Agreement))
- // Used simplified hotline v1.2.3 login flow for clients that do not send login info in tranAgreed
- if c.Version == nil || bytes.Equal(c.Version, nostalgiaVersion) {
- c.Agreed = true
+ // If the client has provided a username as part of the login, we can infer that it is using the 1.2.3 login
+ // flow and not the 1.5+ flow.
+ 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.Infow("Login successful", "clientVersion", "Not sent (probably 1.2.3)")
+
+ // Notify other clients on the server that the new user has logged in. For 1.5+ clients we don't have this
+ // information yet, so we do it in TranAgreed instead
- tranNotifyChangeUser, nil,
- NewField(fieldUserName, c.UserName),
- NewField(fieldUserID, *c.ID),
- NewField(fieldUserIconID, c.Icon),
- NewField(fieldUserFlags, c.Flags),
+ TranNotifyChangeUser, nil,
+ NewField(FieldUserName, c.UserName),
+ NewField(FieldUserID, *c.ID),
+ NewField(FieldUserIconID, c.Icon),
+ NewField(FieldUserFlags, c.Flags),
buf := make([]byte, len(scanner.Bytes()))
copy(buf, scanner.Bytes())
buf := make([]byte, len(scanner.Bytes()))
copy(buf, scanner.Bytes())
ClientConn: make(map[uint16]*ClientConn),
}
s.PrivateChats[data].ClientConn[cc.uint16ID()] = cc
ClientConn: make(map[uint16]*ClientConn),
}
s.PrivateChats[data].ClientConn[cc.uint16ID()] = cc
+ // Wait a few seconds before closing the connection: this is a workaround for problems
+ // observed with Windows clients where the client must initiate close of the TCP connection before
+ // the server does. This is gross and seems unnecessary. TODO: Revisit?
+ time.Sleep(3 * time.Second)
- if err := receiveFile(rwc, file, ioutil.Discard, ioutil.Discard, fileTransfer.bytesSentCounter); err != nil {
+ if err := receiveFile(rwc, file, io.Discard, io.Discard, fileTransfer.bytesSentCounter); err != nil {