var nostalgiaVersion = []byte{0, 0, 2, 0x2c} // version ID used by the Nostalgia client
type Server struct {
- Port int
- Accounts map[string]*Account
- Agreement []byte
- Clients map[uint16]*ClientConn
- ThreadedNews *ThreadedNews
- FileTransfers map[uint32]*FileTransfer
+ Port int
+ Accounts map[string]*Account
+ Agreement []byte
+ Clients map[uint16]*ClientConn
+ ThreadedNews *ThreadedNews
+
+ fileTransfers map[[4]byte]*FileTransfer
+
Config *Config
ConfigDir string
Logger *zap.SugaredLogger
}
func (s *Server) sendTransaction(t Transaction) error {
- requestNum := binary.BigEndian.Uint16(t.Type)
clientID, err := byteToInt(*t.clientID)
if err != nil {
return err
s.mux.Lock()
client := s.Clients[uint16(clientID)]
- s.mux.Unlock()
if client == nil {
return fmt.Errorf("invalid client id %v", *t.clientID)
}
- userName := string(client.UserName)
- login := client.Account.Login
- handler := TransactionHandlers[requestNum]
+ s.mux.Unlock()
b, err := t.MarshalBinary()
if err != nil {
return err
}
- var n int
- if n, err = client.Connection.Write(b); err != nil {
+
+ if _, err := client.Connection.Write(b); err != nil {
return err
}
- s.Logger.Debugw("Sent Transaction",
- "name", userName,
- "login", login,
- "IsReply", t.IsReply,
- "type", handler.Name,
- "sentBytes", n,
- "remoteAddr", client.RemoteAddr,
- )
+
return nil
}
+func (s *Server) processOutbox() {
+ for {
+ t := <-s.outbox
+ go func() {
+ if err := s.sendTransaction(t); err != nil {
+ s.Logger.Errorw("error sending transaction", "err", err)
+ }
+ }()
+ }
+}
+
func (s *Server) Serve(ctx context.Context, ln net.Listener) error {
+ go s.processOutbox()
+
for {
conn, err := ln.Accept()
if err != nil {
s.Logger.Errorw("error accepting connection", "err", err)
}
+ connCtx := context.WithValue(ctx, contextKeyReq, requestCtx{
+ remoteAddr: conn.RemoteAddr().String(),
+ })
go func() {
- for {
- t := <-s.outbox
- go func() {
- if err := s.sendTransaction(t); err != nil {
- s.Logger.Errorw("error sending transaction", "err", err)
- }
- }()
- }
- }()
- go func() {
- if err := s.handleNewConnection(ctx, conn, conn.RemoteAddr().String()); err != nil {
- s.Logger.Infow("New client connection established", "RemoteAddr", conn.RemoteAddr())
+ s.Logger.Infow("Connection established", "RemoteAddr", conn.RemoteAddr())
+
+ if err := s.handleNewConnection(connCtx, conn, conn.RemoteAddr().String()); err != nil {
if err == io.EOF {
s.Logger.Infow("Client disconnected", "RemoteAddr", conn.RemoteAddr())
} else {
Accounts: make(map[string]*Account),
Config: new(Config),
Clients: make(map[uint16]*ClientConn),
- FileTransfers: make(map[uint32]*FileTransfer),
+ fileTransfers: make(map[[4]byte]*FileTransfer),
PrivateChats: make(map[uint32]*PrivateChat),
ConfigDir: configDir,
Logger: logger,
return err
}
-func (s *Server) NewClientConn(conn net.Conn, remoteAddr string) *ClientConn {
+func (s *Server) NewClientConn(conn io.ReadWriteCloser, remoteAddr string) *ClientConn {
s.mux.Lock()
defer s.mux.Unlock()
Server: s,
Version: &[]byte{},
AutoReply: []byte{},
- Transfers: make(map[int][]*FileTransfer),
+ transfers: map[int]map[[4]byte]*FileTransfer{},
Agreed: false,
RemoteAddr: remoteAddr,
}
+ clientConn.transfers = map[int]map[[4]byte]*FileTransfer{
+ FileDownload: {},
+ FileUpload: {},
+ FolderDownload: {},
+ FolderUpload: {},
+ bannerDownload: {},
+ }
+
*s.NextGuestID++
ID := *s.NextGuestID
return nil
}
-const (
- minTransactionLen = 22 // minimum length of any transaction
-)
-
-// dontPanic recovers and logs panics instead of crashing
-// TODO: remove this after known issues are fixed
+// 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()))
}
// handleNewConnection takes a new net.Conn and performs the initial login sequence
-func (s *Server) handleNewConnection(ctx context.Context, conn net.Conn, remoteAddr string) error {
+func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser, remoteAddr string) error {
defer dontPanic(s.Logger)
- if err := Handshake(conn); err != nil {
+ if err := Handshake(rwc); err != nil {
return err
}
- buf := make([]byte, 1024)
- // TODO: fix potential short read with io.ReadFull
- readLen, err := conn.Read(buf)
- if readLen < minTransactionLen {
- return err
- }
- if err != nil {
- return err
- }
+ // Create a new scanner for parsing incoming bytes into transaction tokens
+ scanner := bufio.NewScanner(rwc)
+ scanner.Split(transactionScanner)
+
+ scanner.Scan()
- clientLogin, _, err := ReadTransaction(buf[:readLen])
+ clientLogin, _, err := ReadTransaction(scanner.Bytes())
if err != nil {
- return err
+ panic(err)
}
- c := s.NewClientConn(conn, remoteAddr)
+ c := s.NewClientConn(rwc, remoteAddr)
defer c.Disconnect()
encodedLogin := clientLogin.GetField(fieldUserLogin).Data
login = GuestAccount
}
+ c.logger = s.Logger.With("remoteAddr", remoteAddr, "login", login)
+
// If authentication fails, send error reply and close connection
if !c.Authenticate(login, encodedPassword) {
t := c.NewErrReply(clientLogin, "Incorrect login.")
if err != nil {
return err
}
- if _, err := conn.Write(b); err != nil {
+ if _, err := rwc.Write(b); err != nil {
return err
}
- return fmt.Errorf("incorrect login")
+
+ c.logger.Infow("Login failed", "clientVersion", fmt.Sprintf("%x", *c.Version))
+
+ return nil
}
if clientLogin.GetField(fieldUserName).Data != nil {
*c.Flags = []byte{0, 2}
}
- s.Logger.Infow("Client connection received", "login", login, "version", *c.Version, "RemoteAddr", remoteAddr)
-
s.outbox <- c.NewReply(clientLogin,
NewField(fieldVersion, []byte{0x00, 0xbe}),
- NewField(fieldCommunityBannerID, []byte{0x00, 0x01}),
+ NewField(fieldCommunityBannerID, []byte{0, 0}),
NewField(fieldServerName, []byte(s.Config.Name)),
)
// 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
+ c.logger = c.logger.With("name", string(c.UserName))
+ c.logger.Infow("Login successful", "clientVersion", fmt.Sprintf("%x", *c.Version))
- c.notifyOthers(
+ for _, t := range c.notifyOthers(
*NewTransaction(
tranNotifyChangeUser, nil,
NewField(fieldUserName, c.UserName),
NewField(fieldUserIconID, *c.Icon),
NewField(fieldUserFlags, *c.Flags),
),
- )
+ ) {
+ c.Server.outbox <- t
+ }
}
c.Server.Stats.LoginCount += 1
- const readBuffSize = 1024000 // 1KB - TODO: what should this be?
- tranBuff := make([]byte, 0)
- tReadlen := 0
- // Infinite loop where take action on incoming client requests until the connection is closed
- for {
- buf = make([]byte, readBuffSize)
- tranBuff = tranBuff[tReadlen:]
+ // Scan for new transactions and handle them as they come in.
+ for scanner.Scan() {
+ // 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())
- readLen, err := c.Connection.Read(buf)
+ t, _, err := ReadTransaction(buf)
if err != nil {
- return err
+ panic(err)
}
- tranBuff = append(tranBuff, buf[:readLen]...)
-
- // We may have read multiple requests worth of bytes from Connection.Read. readTransactions splits them
- // into a slice of transactions
- var transactions []Transaction
- if transactions, tReadlen, err = readTransactions(tranBuff); err != nil {
- c.Server.Logger.Errorw("Error handling transaction", "err", err)
- }
-
- // iterate over all the transactions that were parsed from the byte slice and handle them
- for _, t := range transactions {
- if err := c.handleTransaction(&t); err != nil {
- c.Server.Logger.Errorw("Error handling transaction", "err", err)
- }
+ if err := c.handleTransaction(*t); err != nil {
+ c.logger.Errorw("Error handling transaction", "err", err)
}
}
-}
-
-// NewTransactionRef generates a random ID for the file transfer. The Hotline client includes this ID
-// in the transfer request payload, and the file transfer server will use it to map the request
-// to a transfer
-func (s *Server) NewTransactionRef() []byte {
- transactionRef := make([]byte, 4)
- rand.Read(transactionRef)
-
- return transactionRef
+ return nil
}
func (s *Server) NewPrivateChat(cc *ClientConn) []byte {
return err
}
- transferRefNum := binary.BigEndian.Uint32(t.ReferenceNumber[:])
defer func() {
s.mux.Lock()
- delete(s.FileTransfers, transferRefNum)
+ delete(s.fileTransfers, t.ReferenceNumber)
s.mux.Unlock()
+
}()
s.mux.Lock()
- fileTransfer, ok := s.FileTransfers[transferRefNum]
+ fileTransfer, ok := s.fileTransfers[t.ReferenceNumber]
s.mux.Unlock()
if !ok {
return errors.New("invalid transaction ID")
}
+ defer func() {
+ fileTransfer.ClientConn.transfersMU.Lock()
+ delete(fileTransfer.ClientConn.transfers[fileTransfer.Type], t.ReferenceNumber)
+ fileTransfer.ClientConn.transfersMU.Unlock()
+ }()
+
rLogger := s.Logger.With(
"remoteAddr", ctx.Value(contextKeyReq).(requestCtx).remoteAddr,
- "xferID", transferRefNum,
+ "login", fileTransfer.ClientConn.Account.Login,
+ "name", string(fileTransfer.ClientConn.UserName),
)
- switch fileTransfer.Type {
- case FileDownload:
- s.Stats.DownloadCounter += 1
+ fullPath, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName)
+ if err != nil {
+ return err
+ }
- fullFilePath, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName)
- if err != nil {
+ switch fileTransfer.Type {
+ case bannerDownload:
+ if err := s.bannerDownload(rwc); err != nil {
+ panic(err)
return err
}
+ case FileDownload:
+ s.Stats.DownloadCounter += 1
var dataOffset int64
if fileTransfer.fileResumeData != nil {
dataOffset = int64(binary.BigEndian.Uint32(fileTransfer.fileResumeData.ForkInfoList[0].DataSize[:]))
}
- fw, err := newFileWrapper(s.FS, fullFilePath, 0)
+ fw, err := newFileWrapper(s.FS, fullPath, 0)
if err != nil {
return err
}
- rLogger.Infow("File download started", "filePath", fullFilePath, "transactionRef", fileTransfer.ReferenceNumber)
-
- wr := bufio.NewWriterSize(rwc, 1460)
+ rLogger.Infow("File download started", "filePath", fullPath)
// if file transfer options are included, that means this is a "quick preview" request from a 1.5+ client
if fileTransfer.options == nil {
// Start by sending flat file object to client
- if _, err := wr.Write(fw.ffo.BinaryMarshal()); err != nil {
+ if _, err := rwc.Write(fw.ffo.BinaryMarshal()); err != nil {
return err
}
}
return err
}
- if err := sendFile(wr, file, int(dataOffset)); err != nil {
+ br := bufio.NewReader(file)
+ if _, err := br.Discard(int(dataOffset)); err != nil {
return err
}
- if err := wr.Flush(); err != nil {
+ if _, err = io.Copy(rwc, io.TeeReader(br, fileTransfer.bytesSentCounter)); err != nil {
return err
}
- // if the client requested to resume transfer, do not send the resource fork, or it will be appended into the fileWrapper data
+ // if the client requested to resume transfer, do not send the resource fork header, or it will be appended into the fileWrapper data
if fileTransfer.fileResumeData == nil {
- err = binary.Write(wr, binary.BigEndian, fw.rsrcForkHeader())
+ err = binary.Write(rwc, binary.BigEndian, fw.rsrcForkHeader())
if err != nil {
return err
}
- if err := wr.Flush(); err != nil {
- return err
- }
}
rFile, err := fw.rsrcForkFile()
return nil
}
- err = sendFile(wr, rFile, int(dataOffset))
-
- if err := wr.Flush(); err != nil {
+ if _, err = io.Copy(rwc, io.TeeReader(rFile, fileTransfer.bytesSentCounter)); err != nil {
return err
}
case FileUpload:
s.Stats.UploadCounter += 1
- destinationFile, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName)
- if err != nil {
- return err
- }
-
var file *os.File
// A file upload has three possible cases:
// We have to infer which case applies by inspecting what is already on the filesystem
// 1) Check for existing file:
- _, err = os.Stat(destinationFile)
+ _, err = os.Stat(fullPath)
if err == nil {
- // If found, that means this upload is intended to replace the file
- if err = os.Remove(destinationFile); err != nil {
- return err
- }
- file, err = os.Create(destinationFile + incompleteFileSuffix)
+ return errors.New("existing file found at " + fullPath)
}
if errors.Is(err, fs.ErrNotExist) {
// If not found, open or create a new .incomplete file
- file, err = os.OpenFile(destinationFile+incompleteFileSuffix, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
+ file, err = os.OpenFile(fullPath+incompleteFileSuffix, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
}
- f, err := newFileWrapper(s.FS, destinationFile, 0)
+ f, err := newFileWrapper(s.FS, fullPath, 0)
if err != nil {
return err
}
- s.Logger.Infow("File upload started", "transactionRef", fileTransfer.ReferenceNumber, "dstFile", destinationFile)
+ rLogger.Infow("File upload started", "dstFile", fullPath)
rForkWriter := io.Discard
iForkWriter := io.Discard
}
}
- if err := receiveFile(rwc, file, rForkWriter, iForkWriter); err != nil {
- return err
+ if err := receiveFile(rwc, file, rForkWriter, iForkWriter, fileTransfer.bytesSentCounter); err != nil {
+ s.Logger.Error(err)
}
if err := file.Close(); err != nil {
return err
}
-
- if err := s.FS.Rename(destinationFile+".incomplete", destinationFile); err != nil {
+
+ if err := s.FS.Rename(fullPath+".incomplete", fullPath); err != nil {
return err
}
- s.Logger.Infow("File upload complete", "transactionRef", fileTransfer.ReferenceNumber, "dstFile", destinationFile)
+ rLogger.Infow("File upload complete", "dstFile", fullPath)
case FolderDownload:
// Folder Download flow:
// 1. Get filePath from the transfer
//
// This notifies the server to send the next item header
- fullFilePath, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName)
- if err != nil {
- return err
- }
-
- basePathLen := len(fullFilePath)
+ basePathLen := len(fullPath)
- s.Logger.Infow("Start folder download", "path", fullFilePath, "ReferenceNumber", fileTransfer.ReferenceNumber)
+ rLogger.Infow("Start folder download", "path", fullPath)
nextAction := make([]byte, 2)
if _, err := io.ReadFull(rwc, nextAction); err != nil {
}
i := 0
- err = filepath.Walk(fullFilePath+"/", func(path string, info os.FileInfo, err error) error {
+ err = filepath.Walk(fullPath+"/", func(path string, info os.FileInfo, err error) error {
s.Stats.DownloadCounter += 1
i += 1
}
subPath := path[basePathLen+1:]
- s.Logger.Infow("Sending fileheader", "i", i, "path", path, "fullFilePath", fullFilePath, "subPath", subPath, "IsDir", info.IsDir())
+ rLogger.Debugw("Sending fileheader", "i", i, "path", path, "fullFilePath", fullPath, "subPath", subPath, "IsDir", info.IsDir())
if i == 1 {
return nil
return err
}
- s.Logger.Infow("Client folder download action", "action", fmt.Sprintf("%X", nextAction[0:2]))
+ rLogger.Debugw("Client folder download action", "action", fmt.Sprintf("%X", nextAction[0:2]))
var dataOffset int64
return nil
}
- s.Logger.Infow("File download started",
+ rLogger.Infow("File download started",
"fileName", info.Name(),
- "transactionRef", fileTransfer.ReferenceNumber,
"TransferSize", fmt.Sprintf("%x", hlFile.ffo.TransferSize(dataOffset)),
)
}
// wr := bufio.NewWriterSize(rwc, 1460)
- err = sendFile(rwc, file, int(dataOffset))
- if err != nil {
+ if _, err = io.Copy(rwc, io.TeeReader(file, fileTransfer.bytesSentCounter)); err != nil {
return err
}
return err
}
- err = sendFile(rwc, rFile, int(dataOffset))
- if err != nil {
+ if _, err = io.Copy(rwc, io.TeeReader(rFile, fileTransfer.bytesSentCounter)); err != nil {
return err
}
}
return nil
})
- case FolderUpload:
- dstPath, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName)
if err != nil {
return err
}
- s.Logger.Infow(
+ case FolderUpload:
+ rLogger.Infow(
"Folder upload started",
- "transactionRef", fileTransfer.ReferenceNumber,
- "dstPath", dstPath,
- "TransferSize", fmt.Sprintf("%x", fileTransfer.TransferSize),
+ "dstPath", fullPath,
+ "TransferSize", binary.BigEndian.Uint32(fileTransfer.TransferSize),
"FolderItemCount", fileTransfer.FolderItemCount,
)
// Check if the target folder exists. If not, create it.
- if _, err := s.FS.Stat(dstPath); os.IsNotExist(err) {
- if err := s.FS.Mkdir(dstPath, 0777); err != nil {
+ if _, err := s.FS.Stat(fullPath); os.IsNotExist(err) {
+ if err := s.FS.Mkdir(fullPath, 0777); err != nil {
return err
}
}
return err
}
- s.Logger.Infow(
+ rLogger.Infow(
"Folder upload continued",
- "transactionRef", fmt.Sprintf("%x", fileTransfer.ReferenceNumber),
"FormattedPath", fu.FormattedPath(),
"IsFolder", fmt.Sprintf("%x", fu.IsFolder),
"PathItemCount", binary.BigEndian.Uint16(fu.PathItemCount[:]),
)
if fu.IsFolder == [2]byte{0, 1} {
- if _, err := os.Stat(filepath.Join(dstPath, fu.FormattedPath())); os.IsNotExist(err) {
- if err := os.Mkdir(filepath.Join(dstPath, fu.FormattedPath()), 0777); err != nil {
+ if _, err := os.Stat(filepath.Join(fullPath, fu.FormattedPath())); os.IsNotExist(err) {
+ if err := os.Mkdir(filepath.Join(fullPath, fu.FormattedPath()), 0777); err != nil {
return err
}
}
nextAction := dlFldrActionSendFile
// Check if we have the full file already. If so, send dlFldrAction_NextFile to client to skip.
- _, err = os.Stat(filepath.Join(dstPath, fu.FormattedPath()))
+ _, err = os.Stat(filepath.Join(fullPath, fu.FormattedPath()))
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}
}
// Check if we have a partial file already. If so, send dlFldrAction_ResumeFile to client to resume upload.
- incompleteFile, err := os.Stat(filepath.Join(dstPath, fu.FormattedPath()+incompleteFileSuffix))
+ incompleteFile, err := os.Stat(filepath.Join(fullPath, fu.FormattedPath()+incompleteFileSuffix))
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}
offset := make([]byte, 4)
binary.BigEndian.PutUint32(offset, uint32(incompleteFile.Size()))
- file, err := os.OpenFile(dstPath+"/"+fu.FormattedPath()+incompleteFileSuffix, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
+ file, err := os.OpenFile(fullPath+"/"+fu.FormattedPath()+incompleteFileSuffix, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
return err
}
- if err := receiveFile(rwc, file, ioutil.Discard, ioutil.Discard); err != nil {
+ if err := receiveFile(rwc, file, ioutil.Discard, ioutil.Discard, fileTransfer.bytesSentCounter); err != nil {
s.Logger.Error(err)
}
- err = os.Rename(dstPath+"/"+fu.FormattedPath()+".incomplete", dstPath+"/"+fu.FormattedPath())
+ err = os.Rename(fullPath+"/"+fu.FormattedPath()+".incomplete", fullPath+"/"+fu.FormattedPath())
if err != nil {
return err
}
return err
}
- filePath := filepath.Join(dstPath, fu.FormattedPath())
+ filePath := filepath.Join(fullPath, fu.FormattedPath())
hlFile, err := newFileWrapper(s.FS, filePath, 0)
if err != nil {
return err
}
- s.Logger.Infow("Starting file transfer", "path", filePath, "fileNum", i+1, "fileSize", binary.BigEndian.Uint32(fileSize))
+ rLogger.Infow("Starting file transfer", "path", filePath, "fileNum", i+1, "fileSize", binary.BigEndian.Uint32(fileSize))
incWriter, err := hlFile.incFileWriter()
if err != nil {
return err
}
}
- if err := receiveFile(rwc, incWriter, rForkWriter, iForkWriter); err != nil {
+ if err := receiveFile(rwc, incWriter, rForkWriter, iForkWriter, fileTransfer.bytesSentCounter); err != nil {
return err
}
- // _ = newFile.Close()
+
if err := os.Rename(filePath+".incomplete", filePath); err != nil {
return err
}
}
}
}
- s.Logger.Infof("Folder upload complete")
+ rLogger.Infof("Folder upload complete")
}
return nil