]>
Commit | Line | Data |
---|---|---|
6988a057 JH |
1 | package hotline |
2 | ||
3 | import ( | |
6988a057 | 4 | "encoding/binary" |
df1ade54 | 5 | "fmt" |
67db911d | 6 | "go.uber.org/zap" |
6988a057 | 7 | "golang.org/x/crypto/bcrypt" |
d4c152a4 | 8 | "io" |
6988a057 | 9 | "math/big" |
7cd900d6 | 10 | "sort" |
df1ade54 JH |
11 | "strings" |
12 | "sync" | |
6988a057 JH |
13 | ) |
14 | ||
15 | type byClientID []*ClientConn | |
16 | ||
17 | func (s byClientID) Len() int { | |
18 | return len(s) | |
19 | } | |
20 | ||
21 | func (s byClientID) Swap(i, j int) { | |
22 | s[i], s[j] = s[j], s[i] | |
23 | } | |
24 | ||
25 | func (s byClientID) Less(i, j int) bool { | |
26 | return s[i].uint16ID() < s[j].uint16ID() | |
27 | } | |
28 | ||
29 | // ClientConn represents a client connected to a Server | |
30 | type ClientConn struct { | |
d4c152a4 JH |
31 | Connection io.ReadWriteCloser |
32 | RemoteAddr string | |
6988a057 | 33 | ID *[]byte |
a7216f67 JH |
34 | Icon []byte |
35 | Flags []byte | |
72dd37f1 | 36 | UserName []byte |
6988a057 | 37 | Account *Account |
61c272e1 | 38 | IdleTime int |
6988a057 | 39 | Server *Server |
a7216f67 | 40 | Version []byte |
6988a057 | 41 | Idle bool |
aebc4d36 | 42 | AutoReply []byte |
df1ade54 JH |
43 | |
44 | transfersMU sync.Mutex | |
45 | transfers map[int]map[[4]byte]*FileTransfer | |
46 | ||
df1ade54 | 47 | logger *zap.SugaredLogger |
6988a057 JH |
48 | } |
49 | ||
50 | func (cc *ClientConn) sendAll(t int, fields ...Field) { | |
51 | for _, c := range sortedClients(cc.Server.Clients) { | |
52 | cc.Server.outbox <- *NewTransaction(t, c.ID, fields...) | |
53 | } | |
54 | } | |
55 | ||
3178ae58 | 56 | func (cc *ClientConn) handleTransaction(transaction Transaction) error { |
6988a057 JH |
57 | requestNum := binary.BigEndian.Uint16(transaction.Type) |
58 | if handler, ok := TransactionHandlers[requestNum]; ok { | |
59 | for _, reqField := range handler.RequiredFields { | |
60 | field := transaction.GetField(reqField.ID) | |
61 | ||
62 | // Validate that required field is present | |
63 | if field.ID == nil { | |
0fcfa5d5 | 64 | cc.logger.Errorw( |
6988a057 | 65 | "Missing required field", |
0fcfa5d5 | 66 | "RequestType", handler.Name, "FieldID", reqField.ID, |
6988a057 JH |
67 | ) |
68 | return nil | |
69 | } | |
70 | ||
71 | if len(field.Data) < reqField.minLen { | |
0fcfa5d5 | 72 | cc.logger.Infow( |
6988a057 | 73 | "Field does not meet minLen", |
0fcfa5d5 | 74 | "RequestType", handler.Name, "FieldID", reqField.ID, |
6988a057 JH |
75 | ) |
76 | return nil | |
77 | } | |
78 | } | |
6988a057 | 79 | |
0da28a1f | 80 | cc.logger.Debugw("Received Transaction", "RequestType", handler.Name) |
6988a057 | 81 | |
3178ae58 | 82 | transactions, err := handler.Handler(cc, &transaction) |
6988a057 JH |
83 | if err != nil { |
84 | return err | |
85 | } | |
86 | for _, t := range transactions { | |
87 | cc.Server.outbox <- t | |
88 | } | |
89 | } else { | |
0fcfa5d5 JH |
90 | cc.logger.Errorw( |
91 | "Unimplemented transaction type received", "RequestID", requestNum) | |
6988a057 JH |
92 | } |
93 | ||
94 | cc.Server.mux.Lock() | |
95 | defer cc.Server.mux.Unlock() | |
96 | ||
d005ef04 | 97 | if requestNum != TranKeepAlive { |
61c272e1 JH |
98 | // reset the user idle timer |
99 | cc.IdleTime = 0 | |
100 | ||
101 | // if user was previously idle, mark as not idle and notify other connected clients that | |
102 | // the user is no longer away | |
103 | if cc.Idle { | |
a7216f67 | 104 | flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(cc.Flags))) |
b1658a46 | 105 | flagBitmap.SetBit(flagBitmap, UserFlagAway, 0) |
a7216f67 | 106 | binary.BigEndian.PutUint16(cc.Flags, uint16(flagBitmap.Int64())) |
61c272e1 JH |
107 | cc.Idle = false |
108 | ||
109 | cc.sendAll( | |
d005ef04 JH |
110 | TranNotifyChangeUser, |
111 | NewField(FieldUserID, *cc.ID), | |
112 | NewField(FieldUserFlags, cc.Flags), | |
113 | NewField(FieldUserName, cc.UserName), | |
114 | NewField(FieldUserIconID, cc.Icon), | |
61c272e1 JH |
115 | ) |
116 | } | |
6988a057 JH |
117 | } |
118 | ||
6988a057 JH |
119 | return nil |
120 | } | |
121 | ||
122 | func (cc *ClientConn) Authenticate(login string, password []byte) bool { | |
123 | if account, ok := cc.Server.Accounts[login]; ok { | |
124 | return bcrypt.CompareHashAndPassword([]byte(account.Password), password) == nil | |
125 | } | |
126 | ||
127 | return false | |
128 | } | |
129 | ||
130 | func (cc *ClientConn) uint16ID() uint16 { | |
131 | id, _ := byteToInt(*cc.ID) | |
132 | return uint16(id) | |
133 | } | |
134 | ||
135 | // Authorize checks if the user account has the specified permission | |
136 | func (cc *ClientConn) Authorize(access int) bool { | |
187d6dc5 | 137 | return cc.Account.Access.IsSet(access) |
6988a057 JH |
138 | } |
139 | ||
140 | // Disconnect notifies other clients that a client has disconnected | |
0a92e50b | 141 | func (cc *ClientConn) Disconnect() { |
6988a057 JH |
142 | cc.Server.mux.Lock() |
143 | defer cc.Server.mux.Unlock() | |
144 | ||
145 | delete(cc.Server.Clients, binary.BigEndian.Uint16(*cc.ID)) | |
146 | ||
d005ef04 | 147 | for _, t := range cc.notifyOthers(*NewTransaction(TranNotifyDeleteUser, nil, NewField(FieldUserID, *cc.ID))) { |
21581958 JH |
148 | cc.Server.outbox <- t |
149 | } | |
6988a057 JH |
150 | |
151 | if err := cc.Connection.Close(); err != nil { | |
d4c152a4 | 152 | cc.Server.Logger.Errorw("error closing client connection", "RemoteAddr", cc.RemoteAddr) |
6988a057 JH |
153 | } |
154 | } | |
155 | ||
003a743e | 156 | // notifyOthers sends transaction t to other clients connected to the server |
21581958 | 157 | func (cc *ClientConn) notifyOthers(t Transaction) (trans []Transaction) { |
6988a057 | 158 | for _, c := range sortedClients(cc.Server.Clients) { |
5853654f | 159 | if c.ID != cc.ID { |
6988a057 | 160 | t.clientID = c.ID |
21581958 | 161 | trans = append(trans, t) |
6988a057 JH |
162 | } |
163 | } | |
21581958 | 164 | return trans |
6988a057 JH |
165 | } |
166 | ||
6988a057 JH |
167 | // NewReply returns a reply Transaction with fields for the ClientConn |
168 | func (cc *ClientConn) NewReply(t *Transaction, fields ...Field) Transaction { | |
169 | reply := Transaction{ | |
170 | Flags: 0x00, | |
171 | IsReply: 0x01, | |
945b9813 | 172 | Type: []byte{0x00, 0x00}, |
6988a057 JH |
173 | ID: t.ID, |
174 | clientID: cc.ID, | |
175 | ErrorCode: []byte{0, 0, 0, 0}, | |
176 | Fields: fields, | |
177 | } | |
178 | ||
179 | return reply | |
180 | } | |
181 | ||
182 | // NewErrReply returns an error reply Transaction with errMsg | |
183 | func (cc *ClientConn) NewErrReply(t *Transaction, errMsg string) Transaction { | |
184 | return Transaction{ | |
185 | clientID: cc.ID, | |
186 | Flags: 0x00, | |
187 | IsReply: 0x01, | |
188 | Type: []byte{0, 0}, | |
189 | ID: t.ID, | |
190 | ErrorCode: []byte{0, 0, 0, 1}, | |
191 | Fields: []Field{ | |
d005ef04 | 192 | NewField(FieldError, []byte(errMsg)), |
6988a057 JH |
193 | }, |
194 | } | |
195 | } | |
7cd900d6 JH |
196 | |
197 | // sortedClients is a utility function that takes a map of *ClientConn and returns a sorted slice of the values. | |
198 | // The purpose of this is to ensure that the ordering of client connections is deterministic so that test assertions work. | |
199 | func sortedClients(unsortedClients map[uint16]*ClientConn) (clients []*ClientConn) { | |
200 | for _, c := range unsortedClients { | |
201 | clients = append(clients, c) | |
202 | } | |
203 | sort.Sort(byClientID(clients)) | |
204 | return clients | |
205 | } | |
df1ade54 JH |
206 | |
207 | const userInfoTemplate = `Nickname: %s | |
208 | Name: %s | |
209 | Account: %s | |
210 | Address: %s | |
211 | ||
212 | -------- File Downloads --------- | |
213 | ||
214 | %s | |
215 | ------- Folder Downloads -------- | |
216 | ||
217 | %s | |
218 | --------- File Uploads ---------- | |
219 | ||
220 | %s | |
221 | -------- Folder Uploads --------- | |
222 | ||
223 | %s | |
224 | ------- Waiting Downloads ------- | |
225 | ||
226 | %s | |
227 | ` | |
228 | ||
229 | func formatDownloadList(fts map[[4]byte]*FileTransfer) (s string) { | |
230 | if len(fts) == 0 { | |
231 | return "None.\n" | |
232 | } | |
233 | ||
234 | for _, dl := range fts { | |
235 | s += dl.String() | |
236 | } | |
237 | ||
238 | return s | |
239 | } | |
240 | ||
241 | func (cc *ClientConn) String() string { | |
242 | cc.transfersMU.Lock() | |
243 | defer cc.transfersMU.Unlock() | |
244 | template := fmt.Sprintf( | |
245 | userInfoTemplate, | |
246 | cc.UserName, | |
247 | cc.Account.Name, | |
248 | cc.Account.Login, | |
249 | cc.RemoteAddr, | |
250 | formatDownloadList(cc.transfers[FileDownload]), | |
251 | formatDownloadList(cc.transfers[FolderDownload]), | |
252 | formatDownloadList(cc.transfers[FileUpload]), | |
253 | formatDownloadList(cc.transfers[FolderUpload]), | |
254 | "None.\n", | |
255 | ) | |
256 | ||
c8bfd606 | 257 | return strings.ReplaceAll(template, "\n", "\r") |
df1ade54 | 258 | } |