]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "cmp" | |
5 | "encoding/binary" | |
6 | "fmt" | |
7 | "golang.org/x/crypto/bcrypt" | |
8 | "io" | |
9 | "log/slog" | |
10 | "strings" | |
11 | "sync" | |
12 | ) | |
13 | ||
14 | var clientConnSortFunc = func(a, b *ClientConn) int { | |
15 | return cmp.Compare( | |
16 | binary.BigEndian.Uint16(a.ID[:]), | |
17 | binary.BigEndian.Uint16(b.ID[:]), | |
18 | ) | |
19 | } | |
20 | ||
21 | // ClientConn represents a client connected to a Server | |
22 | type ClientConn struct { | |
23 | Connection io.ReadWriteCloser | |
24 | RemoteAddr string | |
25 | ID ClientID | |
26 | Icon []byte // TODO: make fixed size of 2 | |
27 | Version []byte // TODO: make fixed size of 2 | |
28 | ||
29 | FlagsMU sync.Mutex // TODO: move into UserFlags struct | |
30 | Flags UserFlags | |
31 | ||
32 | UserName []byte | |
33 | Account *Account | |
34 | IdleTime int | |
35 | Server *Server // TODO: consider adding methods to interact with server | |
36 | AutoReply []byte | |
37 | ||
38 | ClientFileTransferMgr ClientFileTransferMgr | |
39 | ||
40 | Logger *slog.Logger | |
41 | ||
42 | mu sync.RWMutex | |
43 | } | |
44 | ||
45 | func (cc *ClientConn) FileRoot() string { | |
46 | if cc.Account.FileRoot != "" { | |
47 | return cc.Account.FileRoot | |
48 | } | |
49 | return cc.Server.Config.FileRoot | |
50 | } | |
51 | ||
52 | type ClientFileTransferMgr struct { | |
53 | transfers map[FileTransferType]map[FileTransferID]*FileTransfer | |
54 | ||
55 | mu sync.RWMutex | |
56 | } | |
57 | ||
58 | func NewClientFileTransferMgr() ClientFileTransferMgr { | |
59 | return ClientFileTransferMgr{ | |
60 | transfers: map[FileTransferType]map[FileTransferID]*FileTransfer{ | |
61 | FileDownload: {}, | |
62 | FileUpload: {}, | |
63 | FolderDownload: {}, | |
64 | FolderUpload: {}, | |
65 | BannerDownload: {}, | |
66 | }, | |
67 | } | |
68 | } | |
69 | ||
70 | func (cftm *ClientFileTransferMgr) Add(ftType FileTransferType, ft *FileTransfer) { | |
71 | cftm.mu.Lock() | |
72 | defer cftm.mu.Unlock() | |
73 | ||
74 | cftm.transfers[ftType][ft.RefNum] = ft | |
75 | } | |
76 | ||
77 | func (cftm *ClientFileTransferMgr) Get(ftType FileTransferType) []FileTransfer { | |
78 | cftm.mu.Lock() | |
79 | defer cftm.mu.Unlock() | |
80 | ||
81 | fts := cftm.transfers[ftType] | |
82 | ||
83 | var transfers []FileTransfer | |
84 | for _, ft := range fts { | |
85 | transfers = append(transfers, *ft) | |
86 | } | |
87 | ||
88 | return transfers | |
89 | } | |
90 | ||
91 | func (cftm *ClientFileTransferMgr) Delete(ftType FileTransferType, id FileTransferID) { | |
92 | cftm.mu.Lock() | |
93 | defer cftm.mu.Unlock() | |
94 | ||
95 | delete(cftm.transfers[ftType], id) | |
96 | } | |
97 | ||
98 | func (cc *ClientConn) SendAll(t [2]byte, fields ...Field) { | |
99 | for _, c := range cc.Server.ClientMgr.List() { | |
100 | cc.Server.outbox <- NewTransaction(t, c.ID, fields...) | |
101 | } | |
102 | } | |
103 | ||
104 | func (cc *ClientConn) handleTransaction(transaction Transaction) { | |
105 | if handler, ok := cc.Server.handlers[transaction.Type]; ok { | |
106 | if transaction.Type != TranKeepAlive { | |
107 | cc.Logger.Info(tranTypeNames[transaction.Type]) | |
108 | } | |
109 | ||
110 | for _, t := range handler(cc, &transaction) { | |
111 | cc.Server.outbox <- t | |
112 | } | |
113 | } | |
114 | ||
115 | if transaction.Type != TranKeepAlive { | |
116 | cc.mu.Lock() | |
117 | defer cc.mu.Unlock() | |
118 | ||
119 | // reset the user idle timer | |
120 | cc.IdleTime = 0 | |
121 | ||
122 | // if user was previously idle, mark as not idle and notify other connected clients that | |
123 | // the user is no longer away | |
124 | if cc.Flags.IsSet(UserFlagAway) { | |
125 | cc.Flags.Set(UserFlagAway, 0) | |
126 | ||
127 | cc.SendAll( | |
128 | TranNotifyChangeUser, | |
129 | NewField(FieldUserID, cc.ID[:]), | |
130 | NewField(FieldUserFlags, cc.Flags[:]), | |
131 | NewField(FieldUserName, cc.UserName), | |
132 | NewField(FieldUserIconID, cc.Icon), | |
133 | ) | |
134 | } | |
135 | } | |
136 | } | |
137 | ||
138 | func (cc *ClientConn) Authenticate(login string, password []byte) bool { | |
139 | if account := cc.Server.AccountManager.Get(login); account != nil { | |
140 | return bcrypt.CompareHashAndPassword([]byte(account.Password), password) == nil | |
141 | } | |
142 | ||
143 | return false | |
144 | } | |
145 | ||
146 | // Authorize checks if the user account has the specified permission | |
147 | func (cc *ClientConn) Authorize(access int) bool { | |
148 | if cc.Account == nil { | |
149 | return false | |
150 | } | |
151 | return cc.Account.Access.IsSet(access) | |
152 | } | |
153 | ||
154 | // Disconnect notifies other clients that a client has disconnected and closes the connection. | |
155 | func (cc *ClientConn) Disconnect() { | |
156 | cc.Server.ClientMgr.Delete(cc.ID) | |
157 | ||
158 | for _, t := range cc.NotifyOthers(NewTransaction(TranNotifyDeleteUser, [2]byte{}, NewField(FieldUserID, cc.ID[:]))) { | |
159 | cc.Server.outbox <- t | |
160 | } | |
161 | ||
162 | if err := cc.Connection.Close(); err != nil { | |
163 | cc.Server.Logger.Debug("error closing client connection", "RemoteAddr", cc.RemoteAddr) | |
164 | } | |
165 | } | |
166 | ||
167 | // NotifyOthers sends transaction t to other clients connected to the server | |
168 | func (cc *ClientConn) NotifyOthers(t Transaction) (trans []Transaction) { | |
169 | for _, c := range cc.Server.ClientMgr.List() { | |
170 | if c.ID != cc.ID { | |
171 | t.ClientID = c.ID | |
172 | trans = append(trans, t) | |
173 | } | |
174 | } | |
175 | return trans | |
176 | } | |
177 | ||
178 | // NewReply returns a reply Transaction with fields for the ClientConn | |
179 | func (cc *ClientConn) NewReply(t *Transaction, fields ...Field) Transaction { | |
180 | return Transaction{ | |
181 | IsReply: 1, | |
182 | ID: t.ID, | |
183 | ClientID: cc.ID, | |
184 | Fields: fields, | |
185 | } | |
186 | } | |
187 | ||
188 | // NewErrReply returns an error reply Transaction with errMsg | |
189 | func (cc *ClientConn) NewErrReply(t *Transaction, errMsg string) []Transaction { | |
190 | return []Transaction{ | |
191 | { | |
192 | ClientID: cc.ID, | |
193 | IsReply: 1, | |
194 | ID: t.ID, | |
195 | ErrorCode: [4]byte{0, 0, 0, 1}, | |
196 | Fields: []Field{ | |
197 | NewField(FieldError, []byte(errMsg)), | |
198 | }, | |
199 | }, | |
200 | } | |
201 | } | |
202 | ||
203 | const userInfoTemplate = `Nickname: %s | |
204 | Name: %s | |
205 | Account: %s | |
206 | Address: %s | |
207 | ||
208 | -------- File Downloads --------- | |
209 | ||
210 | %s | |
211 | ------- Folder Downloads -------- | |
212 | ||
213 | %s | |
214 | --------- File Uploads ---------- | |
215 | ||
216 | %s | |
217 | -------- Folder Uploads --------- | |
218 | ||
219 | %s | |
220 | ------- Waiting Downloads ------- | |
221 | ||
222 | %s | |
223 | ` | |
224 | ||
225 | func formatDownloadList(fts []FileTransfer) (s string) { | |
226 | if len(fts) == 0 { | |
227 | return "None.\n" | |
228 | } | |
229 | ||
230 | for _, dl := range fts { | |
231 | s += dl.String() | |
232 | } | |
233 | ||
234 | return s | |
235 | } | |
236 | ||
237 | func (cc *ClientConn) String() string { | |
238 | template := fmt.Sprintf( | |
239 | userInfoTemplate, | |
240 | cc.UserName, | |
241 | cc.Account.Name, | |
242 | cc.Account.Login, | |
243 | cc.RemoteAddr, | |
244 | formatDownloadList(cc.ClientFileTransferMgr.Get(FileDownload)), | |
245 | formatDownloadList(cc.ClientFileTransferMgr.Get(FolderDownload)), | |
246 | formatDownloadList(cc.ClientFileTransferMgr.Get(FileUpload)), | |
247 | formatDownloadList(cc.ClientFileTransferMgr.Get(FolderUpload)), | |
248 | "None.\n", | |
249 | ) | |
250 | ||
251 | return strings.ReplaceAll(template, "\n", "\r") | |
252 | } |