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