]> git.r.bdr.sh - rbdr/mobius/blame - hotline/client_conn.go
Delete cmd/mobius-hotline-server/mobius/config/Files/hello.txt
[rbdr/mobius] / hotline / client_conn.go
CommitLineData
6988a057
JH
1package hotline
2
3import (
a2ef262a 4 "cmp"
6988a057 5 "encoding/binary"
df1ade54 6 "fmt"
6988a057 7 "golang.org/x/crypto/bcrypt"
d4c152a4 8 "io"
a6216dd8 9 "log/slog"
df1ade54
JH
10 "strings"
11 "sync"
6988a057
JH
12)
13
d9bc63a1
JH
14var 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
6988a057
JH
21// ClientConn represents a client connected to a Server
22type ClientConn struct {
d4c152a4
JH
23 Connection io.ReadWriteCloser
24 RemoteAddr string
d9bc63a1
JH
25 ID ClientID
26 Icon []byte // TODO: make fixed size of 2
27 Version []byte // TODO: make fixed size of 2
28
fd740bc4 29 FlagsMU sync.Mutex // TODO: move into UserFlags struct
d9bc63a1
JH
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
df1ade54 39
fd740bc4 40 Logger *slog.Logger
a2ef262a 41
d9bc63a1
JH
42 mu sync.RWMutex
43}
44
dcd23d53
JH
45func (cc *ClientConn) FileRoot() string {
46 if cc.Account.FileRoot != "" {
47 return cc.Account.FileRoot
48 }
49 return cc.Server.Config.FileRoot
50}
51
d9bc63a1
JH
52type ClientFileTransferMgr struct {
53 transfers map[FileTransferType]map[FileTransferID]*FileTransfer
54
55 mu sync.RWMutex
56}
57
58func 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
70func (cftm *ClientFileTransferMgr) Add(ftType FileTransferType, ft *FileTransfer) {
71 cftm.mu.Lock()
72 defer cftm.mu.Unlock()
73
fd740bc4 74 cftm.transfers[ftType][ft.RefNum] = ft
d9bc63a1
JH
75}
76
77func (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
6988a057
JH
89}
90
d9bc63a1
JH
91func (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
98func (cc *ClientConn) SendAll(t [2]byte, fields ...Field) {
99 for _, c := range cc.Server.ClientMgr.List() {
a2ef262a 100 cc.Server.outbox <- NewTransaction(t, c.ID, fields...)
6988a057
JH
101 }
102}
103
a2ef262a 104func (cc *ClientConn) handleTransaction(transaction Transaction) {
fd740bc4 105 if handler, ok := cc.Server.handlers[transaction.Type]; ok {
d9bc63a1 106 if transaction.Type != TranKeepAlive {
fd740bc4 107 cc.Logger.Info(tranTypeNames[transaction.Type])
d9bc63a1 108 }
6988a057 109
a2ef262a 110 for _, t := range handler(cc, &transaction) {
6988a057
JH
111 cc.Server.outbox <- t
112 }
6988a057
JH
113 }
114
a2ef262a 115 if transaction.Type != TranKeepAlive {
d9bc63a1
JH
116 cc.mu.Lock()
117 defer cc.mu.Unlock()
118
61c272e1
JH
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
d9bc63a1 124 if cc.Flags.IsSet(UserFlagAway) {
a2ef262a 125 cc.Flags.Set(UserFlagAway, 0)
61c272e1 126
d9bc63a1 127 cc.SendAll(
d005ef04 128 TranNotifyChangeUser,
a2ef262a
JH
129 NewField(FieldUserID, cc.ID[:]),
130 NewField(FieldUserFlags, cc.Flags[:]),
d005ef04
JH
131 NewField(FieldUserName, cc.UserName),
132 NewField(FieldUserIconID, cc.Icon),
61c272e1
JH
133 )
134 }
6988a057 135 }
6988a057
JH
136}
137
138func (cc *ClientConn) Authenticate(login string, password []byte) bool {
d9bc63a1 139 if account := cc.Server.AccountManager.Get(login); account != nil {
6988a057
JH
140 return bcrypt.CompareHashAndPassword([]byte(account.Password), password) == nil
141 }
142
143 return false
144}
145
6988a057
JH
146// Authorize checks if the user account has the specified permission
147func (cc *ClientConn) Authorize(access int) bool {
a2ef262a
JH
148 if cc.Account == nil {
149 return false
150 }
187d6dc5 151 return cc.Account.Access.IsSet(access)
6988a057
JH
152}
153
fd740bc4 154// Disconnect notifies other clients that a client has disconnected and closes the connection.
0a92e50b 155func (cc *ClientConn) Disconnect() {
d9bc63a1 156 cc.Server.ClientMgr.Delete(cc.ID)
6988a057 157
d9bc63a1 158 for _, t := range cc.NotifyOthers(NewTransaction(TranNotifyDeleteUser, [2]byte{}, NewField(FieldUserID, cc.ID[:]))) {
21581958
JH
159 cc.Server.outbox <- t
160 }
6988a057
JH
161
162 if err := cc.Connection.Close(); err != nil {
fd740bc4 163 cc.Server.Logger.Debug("error closing client connection", "RemoteAddr", cc.RemoteAddr)
6988a057
JH
164 }
165}
166
d9bc63a1
JH
167// NotifyOthers sends transaction t to other clients connected to the server
168func (cc *ClientConn) NotifyOthers(t Transaction) (trans []Transaction) {
169 for _, c := range cc.Server.ClientMgr.List() {
5853654f 170 if c.ID != cc.ID {
fd740bc4 171 t.ClientID = c.ID
21581958 172 trans = append(trans, t)
6988a057
JH
173 }
174 }
21581958 175 return trans
6988a057
JH
176}
177
6988a057
JH
178// NewReply returns a reply Transaction with fields for the ClientConn
179func (cc *ClientConn) NewReply(t *Transaction, fields ...Field) Transaction {
95159e55 180 return Transaction{
a2ef262a
JH
181 IsReply: 1,
182 ID: t.ID,
fd740bc4 183 ClientID: cc.ID,
a2ef262a 184 Fields: fields,
6988a057 185 }
6988a057
JH
186}
187
188// NewErrReply returns an error reply Transaction with errMsg
a2ef262a
JH
189func (cc *ClientConn) NewErrReply(t *Transaction, errMsg string) []Transaction {
190 return []Transaction{
191 {
fd740bc4 192 ClientID: cc.ID,
a2ef262a
JH
193 IsReply: 1,
194 ID: t.ID,
195 ErrorCode: [4]byte{0, 0, 0, 1},
196 Fields: []Field{
197 NewField(FieldError, []byte(errMsg)),
198 },
6988a057
JH
199 },
200 }
201}
7cd900d6 202
df1ade54
JH
203const userInfoTemplate = `Nickname: %s
204Name: %s
205Account: %s
206Address: %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
d9bc63a1 225func formatDownloadList(fts []FileTransfer) (s string) {
df1ade54
JH
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
237func (cc *ClientConn) String() string {
df1ade54
JH
238 template := fmt.Sprintf(
239 userInfoTemplate,
240 cc.UserName,
241 cc.Account.Name,
242 cc.Account.Login,
243 cc.RemoteAddr,
d9bc63a1
JH
244 formatDownloadList(cc.ClientFileTransferMgr.Get(FileDownload)),
245 formatDownloadList(cc.ClientFileTransferMgr.Get(FolderDownload)),
246 formatDownloadList(cc.ClientFileTransferMgr.Get(FileUpload)),
247 formatDownloadList(cc.ClientFileTransferMgr.Get(FolderUpload)),
df1ade54
JH
248 "None.\n",
249 )
250
c8bfd606 251 return strings.ReplaceAll(template, "\n", "\r")
df1ade54 252}