7 "golang.org/x/crypto/bcrypt"
14 var clientConnSortFunc = func(a, b *ClientConn) int {
16 binary.BigEndian.Uint16(a.ID[:]),
17 binary.BigEndian.Uint16(b.ID[:]),
21 // ClientConn represents a client connected to a Server
22 type ClientConn struct {
23 Connection io.ReadWriteCloser
26 Icon []byte // TODO: make fixed size of 2
27 Version []byte // TODO: make fixed size of 2
29 FlagsMU sync.Mutex // TODO: move into UserFlags struct
35 Server *Server // TODO: consider adding methods to interact with server
38 ClientFileTransferMgr ClientFileTransferMgr
45 func (cc *ClientConn) FileRoot() string {
46 if cc.Account.FileRoot != "" {
47 return cc.Account.FileRoot
49 return cc.Server.Config.FileRoot
52 type ClientFileTransferMgr struct {
53 transfers map[FileTransferType]map[FileTransferID]*FileTransfer
58 func NewClientFileTransferMgr() ClientFileTransferMgr {
59 return ClientFileTransferMgr{
60 transfers: map[FileTransferType]map[FileTransferID]*FileTransfer{
70 func (cftm *ClientFileTransferMgr) Add(ftType FileTransferType, ft *FileTransfer) {
72 defer cftm.mu.Unlock()
74 cftm.transfers[ftType][ft.RefNum] = ft
77 func (cftm *ClientFileTransferMgr) Get(ftType FileTransferType) []FileTransfer {
79 defer cftm.mu.Unlock()
81 fts := cftm.transfers[ftType]
83 var transfers []FileTransfer
84 for _, ft := range fts {
85 transfers = append(transfers, *ft)
91 func (cftm *ClientFileTransferMgr) Delete(ftType FileTransferType, id FileTransferID) {
93 defer cftm.mu.Unlock()
95 delete(cftm.transfers[ftType], id)
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...)
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])
110 for _, t := range handler(cc, &transaction) {
111 cc.Server.outbox <- t
115 if transaction.Type != TranKeepAlive {
119 // reset the user idle timer
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)
128 TranNotifyChangeUser,
129 NewField(FieldUserID, cc.ID[:]),
130 NewField(FieldUserFlags, cc.Flags[:]),
131 NewField(FieldUserName, cc.UserName),
132 NewField(FieldUserIconID, cc.Icon),
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
146 // Authorize checks if the user account has the specified permission
147 func (cc *ClientConn) Authorize(access int) bool {
148 if cc.Account == nil {
151 return cc.Account.Access.IsSet(access)
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)
158 for _, t := range cc.NotifyOthers(NewTransaction(TranNotifyDeleteUser, [2]byte{}, NewField(FieldUserID, cc.ID[:]))) {
159 cc.Server.outbox <- t
162 if err := cc.Connection.Close(); err != nil {
163 cc.Server.Logger.Debug("error closing client connection", "RemoteAddr", cc.RemoteAddr)
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() {
172 trans = append(trans, t)
178 // NewReply returns a reply Transaction with fields for the ClientConn
179 func (cc *ClientConn) NewReply(t *Transaction, fields ...Field) Transaction {
188 // NewErrReply returns an error reply Transaction with errMsg
189 func (cc *ClientConn) NewErrReply(t *Transaction, errMsg string) []Transaction {
190 return []Transaction{
195 ErrorCode: [4]byte{0, 0, 0, 1},
197 NewField(FieldError, []byte(errMsg)),
203 const userInfoTemplate = `Nickname: %s
208 -------- File Downloads ---------
211 ------- Folder Downloads --------
214 --------- File Uploads ----------
217 -------- Folder Uploads ---------
220 ------- Waiting Downloads -------
225 func formatDownloadList(fts []FileTransfer) (s string) {
230 for _, dl := range fts {
237 func (cc *ClientConn) String() string {
238 template := fmt.Sprintf(
244 formatDownloadList(cc.ClientFileTransferMgr.Get(FileDownload)),
245 formatDownloadList(cc.ClientFileTransferMgr.Get(FolderDownload)),
246 formatDownloadList(cc.ClientFileTransferMgr.Get(FileUpload)),
247 formatDownloadList(cc.ClientFileTransferMgr.Get(FolderUpload)),
251 return strings.ReplaceAll(template, "\n", "\r")