package hotline
import (
+ "bufio"
"bytes"
- "embed"
+ "context"
"encoding/binary"
- "errors"
"fmt"
- "github.com/gdamore/tcell/v2"
- "github.com/rivo/tview"
- "github.com/stretchr/testify/mock"
- "go.uber.org/zap"
- "gopkg.in/yaml.v2"
- "math/big"
- "math/rand"
+ "io"
+ "log/slog"
"net"
- "os"
- "strings"
"time"
)
-const (
- trackerListPage = "trackerList"
-)
-
-//go:embed banners/*.txt
-var bannerDir embed.FS
-
-type Bookmark struct {
- Name string `yaml:"Name"`
- Addr string `yaml:"Addr"`
- Login string `yaml:"Login"`
- Password string `yaml:"Password"`
-}
-
type ClientPrefs struct {
- Username string `yaml:"Username"`
- IconID int `yaml:"IconID"`
- Bookmarks []Bookmark `yaml:"Bookmarks"`
- Tracker string `yaml:"Tracker"`
+ Username string `yaml:"Username"`
+ IconID int `yaml:"IconID"`
+ Tracker string `yaml:"Tracker"`
+ EnableBell bool `yaml:"EnableBell"`
}
func (cp *ClientPrefs) IconBytes() []byte {
return iconBytes
}
-func readConfig(cfgPath string) (*ClientPrefs, error) {
- fh, err := os.Open(cfgPath)
- if err != nil {
- return nil, err
- }
-
- prefs := ClientPrefs{}
- decoder := yaml.NewDecoder(fh)
- decoder.SetStrict(true)
- if err := decoder.Decode(&prefs); err != nil {
- return nil, err
- }
- return &prefs, nil
-}
-
type Client struct {
- cfgPath string
- DebugBuf *DebugBuffer
Connection net.Conn
- Login *[]byte
- Password *[]byte
- Flags *[]byte
- ID *[]byte
- Version []byte
- UserAccess []byte
- filePath []string
+ Logger *slog.Logger
+ Pref *ClientPrefs
+ Handlers map[[2]byte]ClientHandler
+ activeTasks map[[4]byte]*Transaction
UserList []User
- Logger *zap.SugaredLogger
- activeTasks map[uint32]*Transaction
-
- pref *ClientPrefs
-
- Handlers map[uint16]clientTHandler
+}
- UI *UI
+type ClientHandler func(context.Context, *Client, *Transaction) ([]Transaction, error)
- outbox chan *Transaction
- Inbox chan *Transaction
+func (c *Client) HandleFunc(tranType [2]byte, handler ClientHandler) {
+ c.Handlers[tranType] = handler
}
-func NewClient(cfgPath string, logger *zap.SugaredLogger) *Client {
+func NewClient(username string, logger *slog.Logger) *Client {
c := &Client{
- cfgPath: cfgPath,
Logger: logger,
- activeTasks: make(map[uint32]*Transaction),
- Handlers: clientHandlers,
- }
- c.UI = NewUI(c)
-
- prefs, err := readConfig(cfgPath)
- if err != nil {
- fmt.Printf("unable to read config file %s", cfgPath)
- os.Exit(1)
+ activeTasks: make(map[[4]byte]*Transaction),
+ Handlers: make(map[[2]byte]ClientHandler),
+ Pref: &ClientPrefs{Username: username},
}
- c.pref = prefs
return c
}
-// DebugBuffer wraps a *tview.TextView and adds a Sync() method to make it available as a Zap logger
-type DebugBuffer struct {
- TextView *tview.TextView
-}
-
-func (db *DebugBuffer) Write(p []byte) (int, error) {
- return db.TextView.Write(p)
-}
-
-// Sync is a noop function that exists to satisfy the zapcore.WriteSyncer interface
-func (db *DebugBuffer) Sync() error {
- return nil
-}
-
-func randomBanner() string {
- rand.Seed(time.Now().UnixNano())
-
- bannerFiles, _ := bannerDir.ReadDir("banners")
- file, _ := bannerDir.ReadFile("banners/" + bannerFiles[rand.Intn(len(bannerFiles))].Name())
-
- return fmt.Sprintf("\n\n\nWelcome to...\n\n[red::b]%s[-:-:-]\n\n", file)
-}
-
-type clientTransaction struct {
+type ClientTransaction struct {
Name string
Handler func(*Client, *Transaction) ([]Transaction, error)
}
-func (ch clientTransaction) Handle(cc *Client, t *Transaction) ([]Transaction, error) {
+func (ch ClientTransaction) Handle(cc *Client, t *Transaction) ([]Transaction, error) {
return ch.Handler(cc, t)
}
-type clientTHandler interface {
+type ClientTHandler interface {
Handle(*Client, *Transaction) ([]Transaction, error)
}
-type mockClientHandler struct {
- mock.Mock
-}
-
-func (mh *mockClientHandler) Handle(cc *Client, t *Transaction) ([]Transaction, error) {
- args := mh.Called(cc, t)
- return args.Get(0).([]Transaction), args.Error(1)
-}
-
-var clientHandlers = map[uint16]clientTHandler{
- // Server initiated
- tranChatMsg: clientTransaction{
- Name: "tranChatMsg",
- Handler: handleClientChatMsg,
- },
- tranLogin: clientTransaction{
- Name: "tranLogin",
- Handler: handleClientTranLogin,
- },
- tranShowAgreement: clientTransaction{
- Name: "tranShowAgreement",
- Handler: handleClientTranShowAgreement,
- },
- tranUserAccess: clientTransaction{
- Name: "tranUserAccess",
- Handler: handleClientTranUserAccess,
- },
- tranGetUserNameList: clientTransaction{
- Name: "tranGetUserNameList",
- Handler: handleClientGetUserNameList,
- },
- tranNotifyChangeUser: clientTransaction{
- Name: "tranNotifyChangeUser",
- Handler: handleNotifyChangeUser,
- },
- tranNotifyDeleteUser: clientTransaction{
- Name: "tranNotifyDeleteUser",
- Handler: handleNotifyDeleteUser,
- },
- tranGetMsgs: clientTransaction{
- Name: "tranNotifyDeleteUser",
- Handler: handleGetMsgs,
- },
- tranGetFileNameList: clientTransaction{
- Name: "tranGetFileNameList",
- Handler: handleGetFileNameList,
- },
-}
-
-func handleGetFileNameList(c *Client, t *Transaction) (res []Transaction, err error) {
- fTree := tview.NewTreeView().SetTopLevel(1)
- root := tview.NewTreeNode("Root")
- fTree.SetRoot(root).SetCurrentNode(root)
- fTree.SetBorder(true).SetTitle("| Files |")
- fTree.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
- switch event.Key() {
- case tcell.KeyEscape:
- c.UI.Pages.RemovePage("files")
- c.filePath = []string{}
- case tcell.KeyEnter:
- selectedNode := fTree.GetCurrentNode()
-
- if selectedNode.GetText() == "<- Back" {
- c.filePath = c.filePath[:len(c.filePath)-1]
- f := NewField(fieldFilePath, EncodeFilePath(strings.Join(c.filePath, "/")))
-
- if err := c.UI.HLClient.Send(*NewTransaction(tranGetFileNameList, nil, f)); err != nil {
- c.UI.HLClient.Logger.Errorw("err", "err", err)
- }
- return event
- }
-
- entry := selectedNode.GetReference().(*FileNameWithInfo)
-
- if bytes.Equal(entry.Type, []byte("fldr")) {
- c.Logger.Infow("get new directory listing", "name", string(entry.Name))
-
- c.filePath = append(c.filePath, string(entry.Name))
- f := NewField(fieldFilePath, EncodeFilePath(strings.Join(c.filePath, "/")))
-
- if err := c.UI.HLClient.Send(*NewTransaction(tranGetFileNameList, nil, f)); err != nil {
- c.UI.HLClient.Logger.Errorw("err", "err", err)
- }
- } else {
- // TODO: initiate file download
- c.Logger.Infow("download file", "name", string(entry.Name))
- }
- }
-
- return event
- })
-
- if len(c.filePath) > 0 {
- node := tview.NewTreeNode("<- Back")
- root.AddChild(node)
- }
-
- var fileList []FileNameWithInfo
- for _, f := range t.Fields {
- var fn FileNameWithInfo
- _, _ = fn.Read(f.Data)
- fileList = append(fileList, fn)
-
- if bytes.Equal(fn.Type, []byte("fldr")) {
- node := tview.NewTreeNode(fmt.Sprintf("[blue::]📁 %s[-:-:-]", fn.Name))
- node.SetReference(&fn)
- root.AddChild(node)
- } else {
- size := binary.BigEndian.Uint32(fn.FileSize) / 1024
-
- node := tview.NewTreeNode(fmt.Sprintf(" %-10s %10v KB", fn.Name, size))
- node.SetReference(&fn)
- root.AddChild(node)
- }
-
- }
-
- centerFlex := tview.NewFlex().
- AddItem(nil, 0, 1, false).
- AddItem(tview.NewFlex().
- SetDirection(tview.FlexRow).
- AddItem(nil, 0, 1, false).
- AddItem(fTree, 20, 1, true).
- AddItem(nil, 0, 1, false), 40, 1, true).
- AddItem(nil, 0, 1, false)
-
- c.UI.Pages.AddPage("files", centerFlex, true, true)
- c.UI.App.Draw()
-
- return res, err
-}
-
-func handleGetMsgs(c *Client, t *Transaction) (res []Transaction, err error) {
- newsText := string(t.GetField(fieldData).Data)
- newsText = strings.ReplaceAll(newsText, "\r", "\n")
-
- newsTextView := tview.NewTextView().
- SetText(newsText).
- SetDoneFunc(func(key tcell.Key) {
- c.UI.Pages.SwitchToPage("serverUI")
- c.UI.App.SetFocus(c.UI.chatInput)
- })
- newsTextView.SetBorder(true).SetTitle("News")
-
- c.UI.Pages.AddPage("news", newsTextView, true, true)
- //c.UI.Pages.SwitchToPage("news")
- //c.UI.App.SetFocus(newsTextView)
- c.UI.App.Draw()
-
- return res, err
-}
-
-func handleNotifyChangeUser(c *Client, t *Transaction) (res []Transaction, err error) {
- newUser := User{
- ID: t.GetField(fieldUserID).Data,
- Name: string(t.GetField(fieldUserName).Data),
- Icon: t.GetField(fieldUserIconID).Data,
- Flags: t.GetField(fieldUserFlags).Data,
- }
-
- // Possible cases:
- // user is new to the server
- // user is already on the server but has a new name
-
- var oldName string
- var newUserList []User
- updatedUser := false
- for _, u := range c.UserList {
- c.Logger.Debugw("Comparing Users", "userToUpdate", newUser.ID, "myID", u.ID, "userToUpdateName", newUser.Name, "myname", u.Name)
- if bytes.Equal(newUser.ID, u.ID) {
- oldName = u.Name
- u.Name = newUser.Name
- if u.Name != newUser.Name {
- _, _ = fmt.Fprintf(c.UI.chatBox, " <<< "+oldName+" is now known as "+newUser.Name+" >>>\n")
- }
- updatedUser = true
- }
- newUserList = append(newUserList, u)
- }
-
- if !updatedUser {
- newUserList = append(newUserList, newUser)
- }
-
- c.UserList = newUserList
-
- c.renderUserList()
-
- return res, err
-}
-
-func handleNotifyDeleteUser(c *Client, t *Transaction) (res []Transaction, err error) {
- exitUser := t.GetField(fieldUserID).Data
-
- var newUserList []User
- for _, u := range c.UserList {
- if !bytes.Equal(exitUser, u.ID) {
- newUserList = append(newUserList, u)
- }
- }
-
- c.UserList = newUserList
-
- c.renderUserList()
-
- return res, err
-}
-
-const readBuffSize = 1024000 // 1KB - TODO: what should this be?
-
-func (c *Client) ReadLoop() error {
- 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:]
-
- readLen, err := c.Connection.Read(buf)
- if err != nil {
- return 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.Logger.Errorw("Error handling transaction", "err", err)
- }
-
- // iterate over all of the transactions that were parsed from the byte slice and handle them
- for _, t := range transactions {
- if err := c.HandleTransaction(&t); err != nil {
- c.Logger.Errorw("Error handling transaction", "err", err)
- }
- }
- }
-}
-
-func (c *Client) GetTransactions() error {
- tranBuff := make([]byte, 0)
- tReadlen := 0
-
- buf := make([]byte, readBuffSize)
- tranBuff = tranBuff[tReadlen:]
-
- readLen, err := c.Connection.Read(buf)
- if err != nil {
- return err
- }
- tranBuff = append(tranBuff, buf[:readLen]...)
-
- return nil
-}
-
-func handleClientGetUserNameList(c *Client, t *Transaction) (res []Transaction, err error) {
- var users []User
- for _, field := range t.Fields {
- // The Hotline protocol docs say that ClientGetUserNameList should only return fieldUsernameWithInfo (300)
- // fields, but shxd sneaks in fieldChatSubject (115) so it's important to filter explicitly for the expected
- // field type. Probably a good idea to do everywhere.
- if bytes.Equal(field.ID, []byte{0x01, 0x2c}) {
- u, err := ReadUser(field.Data)
- if err != nil {
- return res, err
- }
- users = append(users, *u)
- }
- }
- c.UserList = users
-
- c.renderUserList()
-
- return res, err
-}
-
-func (c *Client) renderUserList() {
- c.UI.userList.Clear()
- for _, u := range c.UserList {
- flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(u.Flags)))
- if flagBitmap.Bit(userFlagAdmin) == 1 {
- _, _ = fmt.Fprintf(c.UI.userList, "[red::b]%s[-:-:-]\n", u.Name)
- } else {
- _, _ = fmt.Fprintf(c.UI.userList, "%s\n", u.Name)
- }
- // TODO: fade if user is away
- }
-}
-
-func handleClientChatMsg(c *Client, t *Transaction) (res []Transaction, err error) {
- _, _ = fmt.Fprintf(c.UI.chatBox, "%s \n", t.GetField(fieldData).Data)
-
- return res, err
-}
-
-func handleClientTranUserAccess(c *Client, t *Transaction) (res []Transaction, err error) {
- c.UserAccess = t.GetField(fieldUserAccess).Data
-
- return res, err
-}
-
-func handleClientTranShowAgreement(c *Client, t *Transaction) (res []Transaction, err error) {
- agreement := string(t.GetField(fieldData).Data)
- agreement = strings.ReplaceAll(agreement, "\r", "\n")
-
- c.UI.agreeModal = tview.NewModal().
- SetText(agreement).
- AddButtons([]string{"Agree", "Disagree"}).
- SetDoneFunc(func(buttonIndex int, buttonLabel string) {
- if buttonIndex == 0 {
- res = append(res,
- *NewTransaction(
- tranAgreed, nil,
- NewField(fieldUserName, []byte(c.pref.Username)),
- NewField(fieldUserIconID, c.pref.IconBytes()),
- NewField(fieldUserFlags, []byte{0x00, 0x00}),
- NewField(fieldOptions, []byte{0x00, 0x00}),
- ),
- )
- c.UI.Pages.HidePage("agreement")
- c.UI.App.SetFocus(c.UI.chatInput)
- } else {
- _ = c.Disconnect()
- c.UI.Pages.SwitchToPage("home")
- }
- },
- )
-
- c.Logger.Debug("show agreement page")
- c.UI.Pages.AddPage("agreement", c.UI.agreeModal, false, true)
- c.UI.Pages.ShowPage("agreement ")
- c.UI.App.Draw()
-
- return res, err
-}
-
-func handleClientTranLogin(c *Client, t *Transaction) (res []Transaction, err error) {
- if !bytes.Equal(t.ErrorCode, []byte{0, 0, 0, 0}) {
- errMsg := string(t.GetField(fieldError).Data)
- errModal := tview.NewModal()
- errModal.SetText(errMsg)
- errModal.AddButtons([]string{"Oh no"})
- errModal.SetDoneFunc(func(buttonIndex int, buttonLabel string) {
- c.UI.Pages.RemovePage("errModal")
- })
- c.UI.Pages.RemovePage("joinServer")
- c.UI.Pages.AddPage("errModal", errModal, false, true)
-
- c.UI.App.Draw() // TODO: errModal doesn't render without this. wtf?
-
- c.Logger.Error(string(t.GetField(fieldError).Data))
- return nil, errors.New("login error: " + string(t.GetField(fieldError).Data))
- }
- c.UI.Pages.AddAndSwitchToPage("serverUI", c.UI.renderServerUI(), true)
- c.UI.App.SetFocus(c.UI.chatInput)
-
- if err := c.Send(*NewTransaction(tranGetUserNameList, nil)); err != nil {
- c.Logger.Errorw("err", "err", err)
- }
- return res, err
-}
-
// JoinServer connects to a Hotline server and completes the login flow
-func (c *Client) JoinServer(address, login, passwd string) error {
+func (c *Client) Connect(address, login, passwd string) (err error) {
// Establish TCP connection to server
- if err := c.connect(address); err != nil {
+ c.Connection, err = net.DialTimeout("tcp", address, 5*time.Second)
+ if err != nil {
return err
}
return err
}
- // Authenticate (send tranLogin 107)
- if err := c.LogIn(login, passwd); err != nil {
- return err
+ // Authenticate (send TranLogin 107)
+
+ err = c.Send(
+ NewTransaction(
+ TranLogin, [2]byte{0, 0},
+ NewField(FieldUserName, []byte(c.Pref.Username)),
+ NewField(FieldUserIconID, c.Pref.IconBytes()),
+ NewField(FieldUserLogin, EncodeString([]byte(login))),
+ NewField(FieldUserPassword, EncodeString([]byte(passwd))),
+ ),
+ )
+ if err != nil {
+ return fmt.Errorf("error sending login transaction: %w", err)
}
+ // start keepalive go routine
+ go func() { _ = c.keepalive() }()
+
return nil
}
-// connect establishes a connection with a Server by sending handshake sequence
-func (c *Client) connect(address string) error {
- var err error
- c.Connection, err = net.DialTimeout("tcp", address, 5*time.Second)
- if err != nil {
- return err
+const keepaliveInterval = 300 * time.Second
+
+func (c *Client) keepalive() error {
+ for {
+ time.Sleep(keepaliveInterval)
+ _ = c.Send(NewTransaction(TranKeepAlive, [2]byte{}))
}
- return nil
}
var ClientHandshake = []byte{
}
func (c *Client) Handshake() error {
- //Protocol ID 4 ‘TRTP’ 0x54 52 54 50
- //Sub-protocol ID 4 User defined
- //Version 2 1 Currently 1
- //Sub-version 2 User defined
+ // Protocol Type 4 ‘TRTP’ 0x54 52 54 50
+ // Sub-protocol Type 4 User defined
+ // Version 2 1 Currently 1
+ // Sub-version 2 User defined
if _, err := c.Connection.Write(ClientHandshake); err != nil {
return fmt.Errorf("handshake write err: %s", err)
}
return err
}
- if bytes.Compare(replyBuf, ServerHandshake) == 0 {
+ if bytes.Equal(replyBuf, ServerHandshake) {
return nil
}
return fmt.Errorf("handshake response err: %s", err)
}
-func (c *Client) LogIn(login string, password string) error {
- return c.Send(
- *NewTransaction(
- tranLogin, nil,
- NewField(fieldUserName, []byte(c.pref.Username)),
- NewField(fieldUserIconID, c.pref.IconBytes()),
- NewField(fieldUserLogin, []byte(NegatedUserString([]byte(login)))),
- NewField(fieldUserPassword, []byte(NegatedUserString([]byte(password)))),
- NewField(fieldVersion, []byte{0, 2}),
- ),
- )
-}
-
func (c *Client) Send(t Transaction) error {
- requestNum := binary.BigEndian.Uint16(t.Type)
- tID := binary.BigEndian.Uint32(t.ID)
-
- //handler := TransactionHandlers[requestNum]
+ requestNum := binary.BigEndian.Uint16(t.Type[:])
// if transaction is NOT reply, add it to the list to transactions we're expecting a response for
if t.IsReply == 0 {
- c.activeTasks[tID] = &t
+ c.activeTasks[t.ID] = &t
}
- var n int
- var err error
- if n, err = c.Connection.Write(t.Payload()); err != nil {
- return err
+ n, err := io.Copy(c.Connection, &t)
+ if err != nil {
+ return fmt.Errorf("error sending transaction: %w", err)
}
- c.Logger.Debugw("Sent Transaction",
+
+ c.Logger.Debug("Sent Transaction",
"IsReply", t.IsReply,
"type", requestNum,
"sentBytes", n,
return nil
}
-func (c *Client) HandleTransaction(t *Transaction) error {
+func (c *Client) HandleTransaction(ctx context.Context, t *Transaction) error {
var origT Transaction
if t.IsReply == 1 {
- requestID := binary.BigEndian.Uint32(t.ID)
- origT = *c.activeTasks[requestID]
+ origT = *c.activeTasks[t.ID]
t.Type = origT.Type
}
- requestNum := binary.BigEndian.Uint16(t.Type)
- c.Logger.Infow(
- "Received Transaction",
- "RequestType", requestNum,
- )
-
- if handler, ok := c.Handlers[requestNum]; ok {
- outT, _ := handler.Handle(c, t)
+ if handler, ok := c.Handlers[t.Type]; ok {
+ c.Logger.Debug(
+ "Received transaction",
+ "IsReply", t.IsReply,
+ "type", t.Type[:],
+ )
+ outT, err := handler(ctx, c, t)
+ if err != nil {
+ c.Logger.Error("error handling transaction", "err", err)
+ }
for _, t := range outT {
- c.Send(t)
+ if err := c.Send(t); err != nil {
+ return err
+ }
}
- } else {
- c.Logger.Errorw(
- "Unimplemented transaction type received",
- "RequestID", requestNum,
- "TransactionID", t.ID,
- )
}
return nil
}
-func (c *Client) Connected() bool {
- // c.Agreed == true &&
- if c.UserAccess != nil {
- return true
- }
- return false
+func (c *Client) Disconnect() error {
+ return c.Connection.Close()
}
-func (c *Client) Disconnect() error {
- err := c.Connection.Close()
- if err != nil {
- return err
+func (c *Client) HandleTransactions(ctx context.Context) error {
+ // Create a new scanner for parsing incoming bytes into transaction tokens
+ scanner := bufio.NewScanner(c.Connection)
+ scanner.Split(transactionScanner)
+
+ // 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())
+
+ var t Transaction
+ _, err := t.Write(buf)
+ if err != nil {
+ break
+ }
+
+ if err := c.HandleTransaction(ctx, &t); err != nil {
+ c.Logger.Error("Error handling transaction", "err", err)
+ }
+ }
+
+ if scanner.Err() == nil {
+ return scanner.Err()
}
return nil
}