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