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