]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "bufio" | |
5 | "bytes" | |
6 | "context" | |
7 | "encoding/binary" | |
8 | "fmt" | |
9 | "io" | |
10 | "log/slog" | |
11 | "net" | |
12 | "time" | |
13 | ) | |
14 | ||
15 | type ClientPrefs struct { | |
16 | Username string `yaml:"Username"` | |
17 | IconID int `yaml:"IconID"` | |
18 | Tracker string `yaml:"Tracker"` | |
19 | EnableBell bool `yaml:"EnableBell"` | |
20 | } | |
21 | ||
22 | func (cp *ClientPrefs) IconBytes() []byte { | |
23 | iconBytes := make([]byte, 2) | |
24 | binary.BigEndian.PutUint16(iconBytes, uint16(cp.IconID)) | |
25 | return iconBytes | |
26 | } | |
27 | ||
28 | type Client struct { | |
29 | Connection net.Conn | |
30 | Logger *slog.Logger | |
31 | Pref *ClientPrefs | |
32 | Handlers map[[2]byte]ClientHandler | |
33 | activeTasks map[[4]byte]*Transaction | |
34 | } | |
35 | ||
36 | type ClientHandler func(context.Context, *Client, *Transaction) ([]Transaction, error) | |
37 | ||
38 | func (c *Client) HandleFunc(tranType [2]byte, handler ClientHandler) { | |
39 | c.Handlers[tranType] = handler | |
40 | } | |
41 | ||
42 | func NewClient(username string, logger *slog.Logger) *Client { | |
43 | c := &Client{ | |
44 | Logger: logger, | |
45 | activeTasks: make(map[[4]byte]*Transaction), | |
46 | Handlers: make(map[[2]byte]ClientHandler), | |
47 | Pref: &ClientPrefs{Username: username}, | |
48 | } | |
49 | ||
50 | return c | |
51 | } | |
52 | ||
53 | type ClientTransaction struct { | |
54 | Name string | |
55 | Handler func(*Client, *Transaction) ([]Transaction, error) | |
56 | } | |
57 | ||
58 | func (ch ClientTransaction) Handle(cc *Client, t *Transaction) ([]Transaction, error) { | |
59 | return ch.Handler(cc, t) | |
60 | } | |
61 | ||
62 | type ClientTHandler interface { | |
63 | Handle(*Client, *Transaction) ([]Transaction, error) | |
64 | } | |
65 | ||
66 | // JoinServer connects to a Hotline server and completes the login flow | |
67 | func (c *Client) Connect(address, login, passwd string) (err error) { | |
68 | // Establish TCP connection to server | |
69 | c.Connection, err = net.DialTimeout("tcp", address, 5*time.Second) | |
70 | if err != nil { | |
71 | return err | |
72 | } | |
73 | ||
74 | // Send handshake sequence | |
75 | if err := c.Handshake(); err != nil { | |
76 | return err | |
77 | } | |
78 | ||
79 | // Authenticate (send TranLogin 107) | |
80 | ||
81 | err = c.Send( | |
82 | NewTransaction( | |
83 | TranLogin, [2]byte{0, 0}, | |
84 | NewField(FieldUserName, []byte(c.Pref.Username)), | |
85 | NewField(FieldUserIconID, c.Pref.IconBytes()), | |
86 | NewField(FieldUserLogin, encodeString([]byte(login))), | |
87 | NewField(FieldUserPassword, encodeString([]byte(passwd))), | |
88 | ), | |
89 | ) | |
90 | if err != nil { | |
91 | return fmt.Errorf("error sending login transaction: %w", err) | |
92 | } | |
93 | ||
94 | // start keepalive go routine | |
95 | go func() { _ = c.keepalive() }() | |
96 | ||
97 | return nil | |
98 | } | |
99 | ||
100 | const keepaliveInterval = 300 * time.Second | |
101 | ||
102 | func (c *Client) keepalive() error { | |
103 | for { | |
104 | time.Sleep(keepaliveInterval) | |
105 | _ = c.Send(NewTransaction(TranKeepAlive, [2]byte{})) | |
106 | } | |
107 | } | |
108 | ||
109 | var ClientHandshake = []byte{ | |
110 | 0x54, 0x52, 0x54, 0x50, // TRTP | |
111 | 0x48, 0x4f, 0x54, 0x4c, // HOTL | |
112 | 0x00, 0x01, | |
113 | 0x00, 0x02, | |
114 | } | |
115 | ||
116 | var ServerHandshake = []byte{ | |
117 | 0x54, 0x52, 0x54, 0x50, // TRTP | |
118 | 0x00, 0x00, 0x00, 0x00, // ErrorCode | |
119 | } | |
120 | ||
121 | func (c *Client) Handshake() error { | |
122 | // Protocol ID 4 ‘TRTP’ 0x54 52 54 50 | |
123 | // Sub-protocol ID 4 User defined | |
124 | // Version 2 1 Currently 1 | |
125 | // Sub-version 2 User defined | |
126 | if _, err := c.Connection.Write(ClientHandshake); err != nil { | |
127 | return fmt.Errorf("handshake write err: %s", err) | |
128 | } | |
129 | ||
130 | replyBuf := make([]byte, 8) | |
131 | _, err := c.Connection.Read(replyBuf) | |
132 | if err != nil { | |
133 | return err | |
134 | } | |
135 | ||
136 | if bytes.Equal(replyBuf, ServerHandshake) { | |
137 | return nil | |
138 | } | |
139 | ||
140 | // In the case of an error, client and server close the connection. | |
141 | return fmt.Errorf("handshake response err: %s", err) | |
142 | } | |
143 | ||
144 | func (c *Client) Send(t Transaction) error { | |
145 | requestNum := binary.BigEndian.Uint16(t.Type[:]) | |
146 | ||
147 | // if transaction is NOT reply, add it to the list to transactions we're expecting a response for | |
148 | if t.IsReply == 0 { | |
149 | c.activeTasks[t.ID] = &t | |
150 | } | |
151 | ||
152 | n, err := io.Copy(c.Connection, &t) | |
153 | if err != nil { | |
154 | return fmt.Errorf("error sending transaction: %w", err) | |
155 | } | |
156 | ||
157 | c.Logger.Debug("Sent Transaction", | |
158 | "IsReply", t.IsReply, | |
159 | "type", requestNum, | |
160 | "sentBytes", n, | |
161 | ) | |
162 | return nil | |
163 | } | |
164 | ||
165 | func (c *Client) HandleTransaction(ctx context.Context, t *Transaction) error { | |
166 | var origT Transaction | |
167 | if t.IsReply == 1 { | |
168 | origT = *c.activeTasks[t.ID] | |
169 | t.Type = origT.Type | |
170 | } | |
171 | ||
172 | if handler, ok := c.Handlers[t.Type]; ok { | |
173 | c.Logger.Debug( | |
174 | "Received transaction", | |
175 | "IsReply", t.IsReply, | |
176 | "type", t.Type[:], | |
177 | ) | |
178 | outT, err := handler(ctx, c, t) | |
179 | if err != nil { | |
180 | c.Logger.Error("error handling transaction", "err", err) | |
181 | } | |
182 | for _, t := range outT { | |
183 | if err := c.Send(t); err != nil { | |
184 | return err | |
185 | } | |
186 | } | |
187 | } else { | |
188 | c.Logger.Debug( | |
189 | "Unimplemented transaction type", | |
190 | "IsReply", t.IsReply, | |
191 | "type", t.Type[:], | |
192 | ) | |
193 | } | |
194 | ||
195 | return nil | |
196 | } | |
197 | ||
198 | func (c *Client) Disconnect() error { | |
199 | return c.Connection.Close() | |
200 | } | |
201 | ||
202 | func (c *Client) HandleTransactions(ctx context.Context) error { | |
203 | // Create a new scanner for parsing incoming bytes into transaction tokens | |
204 | scanner := bufio.NewScanner(c.Connection) | |
205 | scanner.Split(transactionScanner) | |
206 | ||
207 | // Scan for new transactions and handle them as they come in. | |
208 | for scanner.Scan() { | |
209 | // Make a new []byte slice and copy the scanner bytes to it. This is critical to avoid a data race as the | |
210 | // scanner re-uses the buffer for subsequent scans. | |
211 | buf := make([]byte, len(scanner.Bytes())) | |
212 | copy(buf, scanner.Bytes()) | |
213 | ||
214 | var t Transaction | |
215 | _, err := t.Write(buf) | |
216 | if err != nil { | |
217 | break | |
218 | } | |
219 | ||
220 | if err := c.HandleTransaction(ctx, &t); err != nil { | |
221 | c.Logger.Error("Error handling transaction", "err", err) | |
222 | } | |
223 | } | |
224 | ||
225 | if scanner.Err() == nil { | |
226 | return scanner.Err() | |
227 | } | |
228 | return nil | |
229 | } |