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 type ClientFileTransferMgr struct {
46 transfers map[FileTransferType]map[FileTransferID]*FileTransfer
51 func NewClientFileTransferMgr() ClientFileTransferMgr {
52 return ClientFileTransferMgr{
53 transfers: map[FileTransferType]map[FileTransferID]*FileTransfer{
63 func (cftm *ClientFileTransferMgr) Add(ftType FileTransferType, ft *FileTransfer) {
65 defer cftm.mu.Unlock()
67 cftm.transfers[ftType][ft.refNum] = ft
70 func (cftm *ClientFileTransferMgr) Get(ftType FileTransferType) []FileTransfer {
72 defer cftm.mu.Unlock()
74 fts := cftm.transfers[ftType]
76 var transfers []FileTransfer
77 for _, ft := range fts {
78 transfers = append(transfers, *ft)
84 func (cftm *ClientFileTransferMgr) Delete(ftType FileTransferType, id FileTransferID) {
86 defer cftm.mu.Unlock()
88 delete(cftm.transfers[ftType], id)
91 func (cc *ClientConn) SendAll(t [2]byte, fields ...Field) {
92 for _, c := range cc.Server.ClientMgr.List() {
93 cc.Server.outbox <- NewTransaction(t, c.ID, fields...)
97 func (cc *ClientConn) handleTransaction(transaction Transaction) {
98 if handler, ok := TransactionHandlers[transaction.Type]; ok {
99 if transaction.Type != TranKeepAlive {
100 cc.logger.Info(tranTypeNames[transaction.Type])
103 for _, t := range handler(cc, &transaction) {
104 cc.Server.outbox <- t
108 if transaction.Type != TranKeepAlive {
112 // reset the user idle timer
115 // if user was previously idle, mark as not idle and notify other connected clients that
116 // the user is no longer away
117 if cc.Flags.IsSet(UserFlagAway) {
118 cc.Flags.Set(UserFlagAway, 0)
121 TranNotifyChangeUser,
122 NewField(FieldUserID, cc.ID[:]),
123 NewField(FieldUserFlags, cc.Flags[:]),
124 NewField(FieldUserName, cc.UserName),
125 NewField(FieldUserIconID, cc.Icon),
131 func (cc *ClientConn) Authenticate(login string, password []byte) bool {
132 if account := cc.Server.AccountManager.Get(login); account != nil {
133 return bcrypt.CompareHashAndPassword([]byte(account.Password), password) == nil
139 // Authorize checks if the user account has the specified permission
140 func (cc *ClientConn) Authorize(access int) bool {
141 if cc.Account == nil {
144 return cc.Account.Access.IsSet(access)
147 // Disconnect notifies other clients that a client has disconnected
148 func (cc *ClientConn) Disconnect() {
149 cc.Server.ClientMgr.Delete(cc.ID)
151 for _, t := range cc.NotifyOthers(NewTransaction(TranNotifyDeleteUser, [2]byte{}, NewField(FieldUserID, cc.ID[:]))) {
152 cc.Server.outbox <- t
155 if err := cc.Connection.Close(); err != nil {
156 cc.Server.Logger.Error("error closing client connection", "RemoteAddr", cc.RemoteAddr)
160 // NotifyOthers sends transaction t to other clients connected to the server
161 func (cc *ClientConn) NotifyOthers(t Transaction) (trans []Transaction) {
162 for _, c := range cc.Server.ClientMgr.List() {
165 trans = append(trans, t)
171 // NewReply returns a reply Transaction with fields for the ClientConn
172 func (cc *ClientConn) NewReply(t *Transaction, fields ...Field) Transaction {
181 // NewErrReply returns an error reply Transaction with errMsg
182 func (cc *ClientConn) NewErrReply(t *Transaction, errMsg string) []Transaction {
183 return []Transaction{
188 ErrorCode: [4]byte{0, 0, 0, 1},
190 NewField(FieldError, []byte(errMsg)),
196 const userInfoTemplate = `Nickname: %s
201 -------- File Downloads ---------
204 ------- Folder Downloads --------
207 --------- File Uploads ----------
210 -------- Folder Uploads ---------
213 ------- Waiting Downloads -------
218 func formatDownloadList(fts []FileTransfer) (s string) {
223 for _, dl := range fts {
230 func (cc *ClientConn) String() string {
231 template := fmt.Sprintf(
237 formatDownloadList(cc.ClientFileTransferMgr.Get(FileDownload)),
238 formatDownloadList(cc.ClientFileTransferMgr.Get(FolderDownload)),
239 formatDownloadList(cc.ClientFileTransferMgr.Get(FileUpload)),
240 formatDownloadList(cc.ClientFileTransferMgr.Get(FolderUpload)),
244 return strings.ReplaceAll(template, "\n", "\r")