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