]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "bufio" | |
5 | "context" | |
6 | "encoding/binary" | |
7 | "errors" | |
8 | "fmt" | |
9 | "github.com/go-playground/validator/v10" | |
10 | "go.uber.org/zap" | |
11 | "gopkg.in/yaml.v3" | |
12 | "io" | |
13 | "io/fs" | |
14 | "io/ioutil" | |
15 | "math/big" | |
16 | "math/rand" | |
17 | "net" | |
18 | "os" | |
19 | "path/filepath" | |
20 | "strings" | |
21 | "sync" | |
22 | "time" | |
23 | ) | |
24 | ||
25 | type contextKey string | |
26 | ||
27 | var contextKeyReq = contextKey("req") | |
28 | ||
29 | type requestCtx struct { | |
30 | remoteAddr string | |
31 | login string | |
32 | name string | |
33 | } | |
34 | ||
35 | type Server struct { | |
36 | Port int | |
37 | Accounts map[string]*Account | |
38 | Agreement []byte | |
39 | Clients map[uint16]*ClientConn | |
40 | fileTransfers map[[4]byte]*FileTransfer | |
41 | ||
42 | Config *Config | |
43 | ConfigDir string | |
44 | Logger *zap.SugaredLogger | |
45 | ||
46 | PrivateChatsMu sync.Mutex | |
47 | PrivateChats map[uint32]*PrivateChat | |
48 | ||
49 | NextGuestID *uint16 | |
50 | TrackerPassID [4]byte | |
51 | ||
52 | StatsMu sync.Mutex | |
53 | Stats *Stats | |
54 | ||
55 | FS FileStore // Storage backend to use for File storage | |
56 | ||
57 | outbox chan Transaction | |
58 | mux sync.Mutex | |
59 | ||
60 | threadedNewsMux sync.Mutex | |
61 | ThreadedNews *ThreadedNews | |
62 | ||
63 | flatNewsMux sync.Mutex | |
64 | FlatNews []byte | |
65 | ||
66 | banListMU sync.Mutex | |
67 | banList map[string]*time.Time | |
68 | } | |
69 | ||
70 | func (s *Server) CurrentStats() Stats { | |
71 | s.StatsMu.Lock() | |
72 | defer s.StatsMu.Unlock() | |
73 | ||
74 | stats := s.Stats | |
75 | stats.CurrentlyConnected = len(s.Clients) | |
76 | ||
77 | return *stats | |
78 | } | |
79 | ||
80 | type PrivateChat struct { | |
81 | Subject string | |
82 | ClientConn map[uint16]*ClientConn | |
83 | } | |
84 | ||
85 | func (s *Server) ListenAndServe(ctx context.Context, cancelRoot context.CancelFunc) error { | |
86 | s.Logger.Infow("Hotline server started", | |
87 | "version", VERSION, | |
88 | "API port", fmt.Sprintf(":%v", s.Port), | |
89 | "Transfer port", fmt.Sprintf(":%v", s.Port+1), | |
90 | ) | |
91 | ||
92 | var wg sync.WaitGroup | |
93 | ||
94 | wg.Add(1) | |
95 | go func() { | |
96 | ln, err := net.Listen("tcp", fmt.Sprintf("%s:%v", "", s.Port)) | |
97 | if err != nil { | |
98 | s.Logger.Fatal(err) | |
99 | } | |
100 | ||
101 | s.Logger.Fatal(s.Serve(ctx, ln)) | |
102 | }() | |
103 | ||
104 | wg.Add(1) | |
105 | go func() { | |
106 | ln, err := net.Listen("tcp", fmt.Sprintf("%s:%v", "", s.Port+1)) | |
107 | if err != nil { | |
108 | s.Logger.Fatal(err) | |
109 | ||
110 | } | |
111 | ||
112 | s.Logger.Fatal(s.ServeFileTransfers(ctx, ln)) | |
113 | }() | |
114 | ||
115 | wg.Wait() | |
116 | ||
117 | return nil | |
118 | } | |
119 | ||
120 | func (s *Server) ServeFileTransfers(ctx context.Context, ln net.Listener) error { | |
121 | for { | |
122 | conn, err := ln.Accept() | |
123 | if err != nil { | |
124 | return err | |
125 | } | |
126 | ||
127 | go func() { | |
128 | defer func() { _ = conn.Close() }() | |
129 | ||
130 | err = s.handleFileTransfer( | |
131 | context.WithValue(ctx, contextKeyReq, requestCtx{ | |
132 | remoteAddr: conn.RemoteAddr().String(), | |
133 | }), | |
134 | conn, | |
135 | ) | |
136 | ||
137 | if err != nil { | |
138 | s.Logger.Errorw("file transfer error", "reason", err) | |
139 | } | |
140 | }() | |
141 | } | |
142 | } | |
143 | ||
144 | func (s *Server) sendTransaction(t Transaction) error { | |
145 | clientID, err := byteToInt(*t.clientID) | |
146 | if err != nil { | |
147 | return err | |
148 | } | |
149 | ||
150 | s.mux.Lock() | |
151 | client := s.Clients[uint16(clientID)] | |
152 | s.mux.Unlock() | |
153 | if client == nil { | |
154 | return fmt.Errorf("invalid client id %v", *t.clientID) | |
155 | } | |
156 | ||
157 | b, err := t.MarshalBinary() | |
158 | if err != nil { | |
159 | return err | |
160 | } | |
161 | ||
162 | _, err = client.Connection.Write(b) | |
163 | if err != nil { | |
164 | return err | |
165 | } | |
166 | ||
167 | return nil | |
168 | } | |
169 | ||
170 | func (s *Server) processOutbox() { | |
171 | for { | |
172 | t := <-s.outbox | |
173 | go func() { | |
174 | if err := s.sendTransaction(t); err != nil { | |
175 | s.Logger.Errorw("error sending transaction", "err", err) | |
176 | } | |
177 | }() | |
178 | } | |
179 | } | |
180 | ||
181 | func (s *Server) Serve(ctx context.Context, ln net.Listener) error { | |
182 | go s.processOutbox() | |
183 | ||
184 | for { | |
185 | conn, err := ln.Accept() | |
186 | if err != nil { | |
187 | s.Logger.Errorw("error accepting connection", "err", err) | |
188 | } | |
189 | connCtx := context.WithValue(ctx, contextKeyReq, requestCtx{ | |
190 | remoteAddr: conn.RemoteAddr().String(), | |
191 | }) | |
192 | ||
193 | go func() { | |
194 | s.Logger.Infow("Connection established", "RemoteAddr", conn.RemoteAddr()) | |
195 | ||
196 | defer conn.Close() | |
197 | if err := s.handleNewConnection(connCtx, conn, conn.RemoteAddr().String()); err != nil { | |
198 | if err == io.EOF { | |
199 | s.Logger.Infow("Client disconnected", "RemoteAddr", conn.RemoteAddr()) | |
200 | } else { | |
201 | s.Logger.Errorw("error serving request", "RemoteAddr", conn.RemoteAddr(), "err", err) | |
202 | } | |
203 | } | |
204 | }() | |
205 | } | |
206 | } | |
207 | ||
208 | const ( | |
209 | agreementFile = "Agreement.txt" | |
210 | ) | |
211 | ||
212 | // NewServer constructs a new Server from a config dir | |
213 | func NewServer(configDir string, netPort int, logger *zap.SugaredLogger, FS FileStore) (*Server, error) { | |
214 | server := Server{ | |
215 | Port: netPort, | |
216 | Accounts: make(map[string]*Account), | |
217 | Config: new(Config), | |
218 | Clients: make(map[uint16]*ClientConn), | |
219 | fileTransfers: make(map[[4]byte]*FileTransfer), | |
220 | PrivateChats: make(map[uint32]*PrivateChat), | |
221 | ConfigDir: configDir, | |
222 | Logger: logger, | |
223 | NextGuestID: new(uint16), | |
224 | outbox: make(chan Transaction), | |
225 | Stats: &Stats{Since: time.Now()}, | |
226 | ThreadedNews: &ThreadedNews{}, | |
227 | FS: FS, | |
228 | banList: make(map[string]*time.Time), | |
229 | } | |
230 | ||
231 | var err error | |
232 | ||
233 | // generate a new random passID for tracker registration | |
234 | if _, err := rand.Read(server.TrackerPassID[:]); err != nil { | |
235 | return nil, err | |
236 | } | |
237 | ||
238 | server.Agreement, err = os.ReadFile(filepath.Join(configDir, agreementFile)) | |
239 | if err != nil { | |
240 | return nil, err | |
241 | } | |
242 | ||
243 | if server.FlatNews, err = os.ReadFile(filepath.Join(configDir, "MessageBoard.txt")); err != nil { | |
244 | return nil, err | |
245 | } | |
246 | ||
247 | // try to load the ban list, but ignore errors as this file may not be present or may be empty | |
248 | _ = server.loadBanList(filepath.Join(configDir, "Banlist.yaml")) | |
249 | ||
250 | if err := server.loadThreadedNews(filepath.Join(configDir, "ThreadedNews.yaml")); err != nil { | |
251 | return nil, err | |
252 | } | |
253 | ||
254 | if err := server.loadConfig(filepath.Join(configDir, "config.yaml")); err != nil { | |
255 | return nil, err | |
256 | } | |
257 | ||
258 | if err := server.loadAccounts(filepath.Join(configDir, "Users/")); err != nil { | |
259 | return nil, err | |
260 | } | |
261 | ||
262 | server.Config.FileRoot = filepath.Join(configDir, "Files") | |
263 | ||
264 | *server.NextGuestID = 1 | |
265 | ||
266 | if server.Config.EnableTrackerRegistration { | |
267 | server.Logger.Infow( | |
268 | "Tracker registration enabled", | |
269 | "frequency", fmt.Sprintf("%vs", trackerUpdateFrequency), | |
270 | "trackers", server.Config.Trackers, | |
271 | ) | |
272 | ||
273 | go func() { | |
274 | for { | |
275 | tr := &TrackerRegistration{ | |
276 | UserCount: server.userCount(), | |
277 | PassID: server.TrackerPassID[:], | |
278 | Name: server.Config.Name, | |
279 | Description: server.Config.Description, | |
280 | } | |
281 | binary.BigEndian.PutUint16(tr.Port[:], uint16(server.Port)) | |
282 | for _, t := range server.Config.Trackers { | |
283 | if err := register(t, tr); err != nil { | |
284 | server.Logger.Errorw("unable to register with tracker %v", "error", err) | |
285 | } | |
286 | server.Logger.Debugw("Sent Tracker registration", "addr", t) | |
287 | } | |
288 | ||
289 | time.Sleep(trackerUpdateFrequency * time.Second) | |
290 | } | |
291 | }() | |
292 | } | |
293 | ||
294 | // Start Client Keepalive go routine | |
295 | go server.keepaliveHandler() | |
296 | ||
297 | return &server, nil | |
298 | } | |
299 | ||
300 | func (s *Server) userCount() int { | |
301 | s.mux.Lock() | |
302 | defer s.mux.Unlock() | |
303 | ||
304 | return len(s.Clients) | |
305 | } | |
306 | ||
307 | func (s *Server) keepaliveHandler() { | |
308 | for { | |
309 | time.Sleep(idleCheckInterval * time.Second) | |
310 | s.mux.Lock() | |
311 | ||
312 | for _, c := range s.Clients { | |
313 | c.IdleTime += idleCheckInterval | |
314 | if c.IdleTime > userIdleSeconds && !c.Idle { | |
315 | c.Idle = true | |
316 | ||
317 | flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(c.Flags))) | |
318 | flagBitmap.SetBit(flagBitmap, userFlagAway, 1) | |
319 | binary.BigEndian.PutUint16(c.Flags, uint16(flagBitmap.Int64())) | |
320 | ||
321 | c.sendAll( | |
322 | tranNotifyChangeUser, | |
323 | NewField(fieldUserID, *c.ID), | |
324 | NewField(fieldUserFlags, c.Flags), | |
325 | NewField(fieldUserName, c.UserName), | |
326 | NewField(fieldUserIconID, c.Icon), | |
327 | ) | |
328 | } | |
329 | } | |
330 | s.mux.Unlock() | |
331 | } | |
332 | } | |
333 | ||
334 | func (s *Server) writeBanList() error { | |
335 | s.banListMU.Lock() | |
336 | defer s.banListMU.Unlock() | |
337 | ||
338 | out, err := yaml.Marshal(s.banList) | |
339 | if err != nil { | |
340 | return err | |
341 | } | |
342 | err = ioutil.WriteFile( | |
343 | filepath.Join(s.ConfigDir, "Banlist.yaml"), | |
344 | out, | |
345 | 0666, | |
346 | ) | |
347 | return err | |
348 | } | |
349 | ||
350 | func (s *Server) writeThreadedNews() error { | |
351 | s.threadedNewsMux.Lock() | |
352 | defer s.threadedNewsMux.Unlock() | |
353 | ||
354 | out, err := yaml.Marshal(s.ThreadedNews) | |
355 | if err != nil { | |
356 | return err | |
357 | } | |
358 | err = s.FS.WriteFile( | |
359 | filepath.Join(s.ConfigDir, "ThreadedNews.yaml"), | |
360 | out, | |
361 | 0666, | |
362 | ) | |
363 | return err | |
364 | } | |
365 | ||
366 | func (s *Server) NewClientConn(conn io.ReadWriteCloser, remoteAddr string) *ClientConn { | |
367 | s.mux.Lock() | |
368 | defer s.mux.Unlock() | |
369 | ||
370 | clientConn := &ClientConn{ | |
371 | ID: &[]byte{0, 0}, | |
372 | Icon: []byte{0, 0}, | |
373 | Flags: []byte{0, 0}, | |
374 | UserName: []byte{}, | |
375 | Connection: conn, | |
376 | Server: s, | |
377 | Version: []byte{}, | |
378 | AutoReply: []byte{}, | |
379 | transfers: map[int]map[[4]byte]*FileTransfer{}, | |
380 | RemoteAddr: remoteAddr, | |
381 | } | |
382 | clientConn.transfers = map[int]map[[4]byte]*FileTransfer{ | |
383 | FileDownload: {}, | |
384 | FileUpload: {}, | |
385 | FolderDownload: {}, | |
386 | FolderUpload: {}, | |
387 | bannerDownload: {}, | |
388 | } | |
389 | ||
390 | *s.NextGuestID++ | |
391 | ID := *s.NextGuestID | |
392 | ||
393 | binary.BigEndian.PutUint16(*clientConn.ID, ID) | |
394 | s.Clients[ID] = clientConn | |
395 | ||
396 | return clientConn | |
397 | } | |
398 | ||
399 | // NewUser creates a new user account entry in the server map and config file | |
400 | func (s *Server) NewUser(login, name, password string, access accessBitmap) error { | |
401 | s.mux.Lock() | |
402 | defer s.mux.Unlock() | |
403 | ||
404 | account := Account{ | |
405 | Login: login, | |
406 | Name: name, | |
407 | Password: hashAndSalt([]byte(password)), | |
408 | Access: access, | |
409 | } | |
410 | out, err := yaml.Marshal(&account) | |
411 | if err != nil { | |
412 | return err | |
413 | } | |
414 | s.Accounts[login] = &account | |
415 | ||
416 | return s.FS.WriteFile(filepath.Join(s.ConfigDir, "Users", login+".yaml"), out, 0666) | |
417 | } | |
418 | ||
419 | func (s *Server) UpdateUser(login, newLogin, name, password string, access accessBitmap) error { | |
420 | s.mux.Lock() | |
421 | defer s.mux.Unlock() | |
422 | ||
423 | // update renames the user login | |
424 | if login != newLogin { | |
425 | err := os.Rename(filepath.Join(s.ConfigDir, "Users", login+".yaml"), filepath.Join(s.ConfigDir, "Users", newLogin+".yaml")) | |
426 | if err != nil { | |
427 | return err | |
428 | } | |
429 | s.Accounts[newLogin] = s.Accounts[login] | |
430 | delete(s.Accounts, login) | |
431 | } | |
432 | ||
433 | account := s.Accounts[newLogin] | |
434 | account.Access = access | |
435 | account.Name = name | |
436 | account.Password = password | |
437 | ||
438 | out, err := yaml.Marshal(&account) | |
439 | if err != nil { | |
440 | return err | |
441 | } | |
442 | ||
443 | if err := os.WriteFile(filepath.Join(s.ConfigDir, "Users", newLogin+".yaml"), out, 0666); err != nil { | |
444 | return err | |
445 | } | |
446 | ||
447 | return nil | |
448 | } | |
449 | ||
450 | // DeleteUser deletes the user account | |
451 | func (s *Server) DeleteUser(login string) error { | |
452 | s.mux.Lock() | |
453 | defer s.mux.Unlock() | |
454 | ||
455 | delete(s.Accounts, login) | |
456 | ||
457 | return s.FS.Remove(filepath.Join(s.ConfigDir, "Users", login+".yaml")) | |
458 | } | |
459 | ||
460 | func (s *Server) connectedUsers() []Field { | |
461 | s.mux.Lock() | |
462 | defer s.mux.Unlock() | |
463 | ||
464 | var connectedUsers []Field | |
465 | for _, c := range sortedClients(s.Clients) { | |
466 | user := User{ | |
467 | ID: *c.ID, | |
468 | Icon: c.Icon, | |
469 | Flags: c.Flags, | |
470 | Name: string(c.UserName), | |
471 | } | |
472 | connectedUsers = append(connectedUsers, NewField(fieldUsernameWithInfo, user.Payload())) | |
473 | } | |
474 | return connectedUsers | |
475 | } | |
476 | ||
477 | func (s *Server) loadBanList(path string) error { | |
478 | fh, err := os.Open(path) | |
479 | if err != nil { | |
480 | return err | |
481 | } | |
482 | decoder := yaml.NewDecoder(fh) | |
483 | ||
484 | return decoder.Decode(s.banList) | |
485 | } | |
486 | ||
487 | // loadThreadedNews loads the threaded news data from disk | |
488 | func (s *Server) loadThreadedNews(threadedNewsPath string) error { | |
489 | fh, err := os.Open(threadedNewsPath) | |
490 | if err != nil { | |
491 | return err | |
492 | } | |
493 | decoder := yaml.NewDecoder(fh) | |
494 | ||
495 | return decoder.Decode(s.ThreadedNews) | |
496 | } | |
497 | ||
498 | // loadAccounts loads account data from disk | |
499 | func (s *Server) loadAccounts(userDir string) error { | |
500 | matches, err := filepath.Glob(filepath.Join(userDir, "*.yaml")) | |
501 | if err != nil { | |
502 | return err | |
503 | } | |
504 | ||
505 | if len(matches) == 0 { | |
506 | return errors.New("no user accounts found in " + userDir) | |
507 | } | |
508 | ||
509 | for _, file := range matches { | |
510 | fh, err := s.FS.Open(file) | |
511 | if err != nil { | |
512 | return err | |
513 | } | |
514 | ||
515 | account := Account{} | |
516 | decoder := yaml.NewDecoder(fh) | |
517 | if err := decoder.Decode(&account); err != nil { | |
518 | return err | |
519 | } | |
520 | ||
521 | s.Accounts[account.Login] = &account | |
522 | } | |
523 | return nil | |
524 | } | |
525 | ||
526 | func (s *Server) loadConfig(path string) error { | |
527 | fh, err := s.FS.Open(path) | |
528 | if err != nil { | |
529 | return err | |
530 | } | |
531 | ||
532 | decoder := yaml.NewDecoder(fh) | |
533 | err = decoder.Decode(s.Config) | |
534 | if err != nil { | |
535 | return err | |
536 | } | |
537 | ||
538 | validate := validator.New() | |
539 | err = validate.Struct(s.Config) | |
540 | if err != nil { | |
541 | return err | |
542 | } | |
543 | return nil | |
544 | } | |
545 | ||
546 | // handleNewConnection takes a new net.Conn and performs the initial login sequence | |
547 | func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser, remoteAddr string) error { | |
548 | defer dontPanic(s.Logger) | |
549 | ||
550 | if err := Handshake(rwc); err != nil { | |
551 | return err | |
552 | } | |
553 | ||
554 | // Create a new scanner for parsing incoming bytes into transaction tokens | |
555 | scanner := bufio.NewScanner(rwc) | |
556 | scanner.Split(transactionScanner) | |
557 | ||
558 | scanner.Scan() | |
559 | ||
560 | // Make a new []byte slice and copy the scanner bytes to it. This is critical to avoid a data race as the | |
561 | // scanner re-uses the buffer for subsequent scans. | |
562 | buf := make([]byte, len(scanner.Bytes())) | |
563 | copy(buf, scanner.Bytes()) | |
564 | ||
565 | var clientLogin Transaction | |
566 | if _, err := clientLogin.Write(buf); err != nil { | |
567 | return err | |
568 | } | |
569 | ||
570 | c := s.NewClientConn(rwc, remoteAddr) | |
571 | ||
572 | // check if remoteAddr is present in the ban list | |
573 | if banUntil, ok := s.banList[strings.Split(remoteAddr, ":")[0]]; ok { | |
574 | // permaban | |
575 | if banUntil == nil { | |
576 | s.outbox <- *NewTransaction( | |
577 | tranServerMsg, | |
578 | c.ID, | |
579 | NewField(fieldData, []byte("You are permanently banned on this server")), | |
580 | NewField(fieldChatOptions, []byte{0, 0}), | |
581 | ) | |
582 | time.Sleep(1 * time.Second) | |
583 | return nil | |
584 | } else if time.Now().Before(*banUntil) { | |
585 | s.outbox <- *NewTransaction( | |
586 | tranServerMsg, | |
587 | c.ID, | |
588 | NewField(fieldData, []byte("You are temporarily banned on this server")), | |
589 | NewField(fieldChatOptions, []byte{0, 0}), | |
590 | ) | |
591 | time.Sleep(1 * time.Second) | |
592 | return nil | |
593 | } | |
594 | ||
595 | } | |
596 | defer c.Disconnect() | |
597 | ||
598 | encodedLogin := clientLogin.GetField(fieldUserLogin).Data | |
599 | encodedPassword := clientLogin.GetField(fieldUserPassword).Data | |
600 | c.Version = clientLogin.GetField(fieldVersion).Data | |
601 | ||
602 | var login string | |
603 | for _, char := range encodedLogin { | |
604 | login += string(rune(255 - uint(char))) | |
605 | } | |
606 | if login == "" { | |
607 | login = GuestAccount | |
608 | } | |
609 | ||
610 | c.logger = s.Logger.With("remoteAddr", remoteAddr, "login", login) | |
611 | ||
612 | // If authentication fails, send error reply and close connection | |
613 | if !c.Authenticate(login, encodedPassword) { | |
614 | t := c.NewErrReply(&clientLogin, "Incorrect login.") | |
615 | b, err := t.MarshalBinary() | |
616 | if err != nil { | |
617 | return err | |
618 | } | |
619 | if _, err := rwc.Write(b); err != nil { | |
620 | return err | |
621 | } | |
622 | ||
623 | c.logger.Infow("Login failed", "clientVersion", fmt.Sprintf("%x", c.Version)) | |
624 | ||
625 | return nil | |
626 | } | |
627 | ||
628 | if clientLogin.GetField(fieldUserIconID).Data != nil { | |
629 | c.Icon = clientLogin.GetField(fieldUserIconID).Data | |
630 | } | |
631 | ||
632 | c.Account = c.Server.Accounts[login] | |
633 | ||
634 | if clientLogin.GetField(fieldUserName).Data != nil { | |
635 | if c.Authorize(accessAnyName) { | |
636 | c.UserName = clientLogin.GetField(fieldUserName).Data | |
637 | } else { | |
638 | c.UserName = []byte(c.Account.Name) | |
639 | } | |
640 | } | |
641 | ||
642 | if c.Authorize(accessDisconUser) { | |
643 | c.Flags = []byte{0, 2} | |
644 | } | |
645 | ||
646 | s.outbox <- c.NewReply(&clientLogin, | |
647 | NewField(fieldVersion, []byte{0x00, 0xbe}), | |
648 | NewField(fieldCommunityBannerID, []byte{0, 0}), | |
649 | NewField(fieldServerName, []byte(s.Config.Name)), | |
650 | ) | |
651 | ||
652 | // Send user access privs so client UI knows how to behave | |
653 | c.Server.outbox <- *NewTransaction(tranUserAccess, c.ID, NewField(fieldUserAccess, c.Account.Access[:])) | |
654 | ||
655 | // Accounts with accessNoAgreement do not receive the server agreement on login. The behavior is different between | |
656 | // client versions. For 1.2.3 client, we do not send tranShowAgreement. For other client versions, we send | |
657 | // tranShowAgreement but with the NoServerAgreement field set to 1. | |
658 | if c.Authorize(accessNoAgreement) { | |
659 | // If client version is nil, then the client uses the 1.2.3 login behavior | |
660 | if c.Version != nil { | |
661 | c.Server.outbox <- *NewTransaction(tranShowAgreement, c.ID, NewField(fieldNoServerAgreement, []byte{1})) | |
662 | } | |
663 | } else { | |
664 | c.Server.outbox <- *NewTransaction(tranShowAgreement, c.ID, NewField(fieldData, s.Agreement)) | |
665 | } | |
666 | ||
667 | // If the client has provided a username as part of the login, we can infer that it is using the 1.2.3 login | |
668 | // flow and not the 1.5+ flow. | |
669 | if len(c.UserName) != 0 { | |
670 | // Add the client username to the logger. For 1.5+ clients, we don't have this information yet as it comes as | |
671 | // part of tranAgreed | |
672 | c.logger = c.logger.With("name", string(c.UserName)) | |
673 | ||
674 | c.logger.Infow("Login successful", "clientVersion", "Not sent (probably 1.2.3)") | |
675 | ||
676 | // Notify other clients on the server that the new user has logged in. For 1.5+ clients we don't have this | |
677 | // information yet, so we do it in tranAgreed instead | |
678 | for _, t := range c.notifyOthers( | |
679 | *NewTransaction( | |
680 | tranNotifyChangeUser, nil, | |
681 | NewField(fieldUserName, c.UserName), | |
682 | NewField(fieldUserID, *c.ID), | |
683 | NewField(fieldUserIconID, c.Icon), | |
684 | NewField(fieldUserFlags, c.Flags), | |
685 | ), | |
686 | ) { | |
687 | c.Server.outbox <- t | |
688 | } | |
689 | } | |
690 | ||
691 | c.Server.Stats.ConnectionCounter += 1 | |
692 | if len(s.Clients) > c.Server.Stats.ConnectionPeak { | |
693 | c.Server.Stats.ConnectionPeak = len(s.Clients) | |
694 | } | |
695 | ||
696 | // Scan for new transactions and handle them as they come in. | |
697 | for scanner.Scan() { | |
698 | // Make a new []byte slice and copy the scanner bytes to it. This is critical to avoid a data race as the | |
699 | // scanner re-uses the buffer for subsequent scans. | |
700 | buf := make([]byte, len(scanner.Bytes())) | |
701 | copy(buf, scanner.Bytes()) | |
702 | ||
703 | var t Transaction | |
704 | if _, err := t.Write(buf); err != nil { | |
705 | return err | |
706 | } | |
707 | ||
708 | if err := c.handleTransaction(t); err != nil { | |
709 | c.logger.Errorw("Error handling transaction", "err", err) | |
710 | } | |
711 | } | |
712 | return nil | |
713 | } | |
714 | ||
715 | func (s *Server) NewPrivateChat(cc *ClientConn) []byte { | |
716 | s.PrivateChatsMu.Lock() | |
717 | defer s.PrivateChatsMu.Unlock() | |
718 | ||
719 | randID := make([]byte, 4) | |
720 | rand.Read(randID) | |
721 | data := binary.BigEndian.Uint32(randID[:]) | |
722 | ||
723 | s.PrivateChats[data] = &PrivateChat{ | |
724 | ClientConn: make(map[uint16]*ClientConn), | |
725 | } | |
726 | s.PrivateChats[data].ClientConn[cc.uint16ID()] = cc | |
727 | ||
728 | return randID | |
729 | } | |
730 | ||
731 | const dlFldrActionSendFile = 1 | |
732 | const dlFldrActionResumeFile = 2 | |
733 | const dlFldrActionNextFile = 3 | |
734 | ||
735 | // handleFileTransfer receives a client net.Conn from the file transfer server, performs the requested transfer type, then closes the connection | |
736 | func (s *Server) handleFileTransfer(ctx context.Context, rwc io.ReadWriter) error { | |
737 | defer dontPanic(s.Logger) | |
738 | ||
739 | txBuf := make([]byte, 16) | |
740 | if _, err := io.ReadFull(rwc, txBuf); err != nil { | |
741 | return err | |
742 | } | |
743 | ||
744 | var t transfer | |
745 | if _, err := t.Write(txBuf); err != nil { | |
746 | return err | |
747 | } | |
748 | ||
749 | defer func() { | |
750 | s.mux.Lock() | |
751 | delete(s.fileTransfers, t.ReferenceNumber) | |
752 | s.mux.Unlock() | |
753 | ||
754 | // Wait a few seconds before closing the connection: this is a workaround for problems | |
755 | // observed with Windows clients where the client must initiate close of the TCP connection before | |
756 | // the server does. This is gross and seems unnecessary. TODO: Revisit? | |
757 | time.Sleep(3 * time.Second) | |
758 | }() | |
759 | ||
760 | s.mux.Lock() | |
761 | fileTransfer, ok := s.fileTransfers[t.ReferenceNumber] | |
762 | s.mux.Unlock() | |
763 | if !ok { | |
764 | return errors.New("invalid transaction ID") | |
765 | } | |
766 | ||
767 | defer func() { | |
768 | fileTransfer.ClientConn.transfersMU.Lock() | |
769 | delete(fileTransfer.ClientConn.transfers[fileTransfer.Type], t.ReferenceNumber) | |
770 | fileTransfer.ClientConn.transfersMU.Unlock() | |
771 | }() | |
772 | ||
773 | rLogger := s.Logger.With( | |
774 | "remoteAddr", ctx.Value(contextKeyReq).(requestCtx).remoteAddr, | |
775 | "login", fileTransfer.ClientConn.Account.Login, | |
776 | "name", string(fileTransfer.ClientConn.UserName), | |
777 | ) | |
778 | ||
779 | fullPath, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName) | |
780 | if err != nil { | |
781 | return err | |
782 | } | |
783 | ||
784 | switch fileTransfer.Type { | |
785 | case bannerDownload: | |
786 | if err := s.bannerDownload(rwc); err != nil { | |
787 | return err | |
788 | } | |
789 | case FileDownload: | |
790 | s.Stats.DownloadCounter += 1 | |
791 | s.Stats.DownloadsInProgress += 1 | |
792 | defer func() { | |
793 | s.Stats.DownloadsInProgress -= 1 | |
794 | }() | |
795 | ||
796 | var dataOffset int64 | |
797 | if fileTransfer.fileResumeData != nil { | |
798 | dataOffset = int64(binary.BigEndian.Uint32(fileTransfer.fileResumeData.ForkInfoList[0].DataSize[:])) | |
799 | } | |
800 | ||
801 | fw, err := newFileWrapper(s.FS, fullPath, 0) | |
802 | if err != nil { | |
803 | return err | |
804 | } | |
805 | ||
806 | rLogger.Infow("File download started", "filePath", fullPath) | |
807 | ||
808 | // if file transfer options are included, that means this is a "quick preview" request from a 1.5+ client | |
809 | if fileTransfer.options == nil { | |
810 | // Start by sending flat file object to client | |
811 | if _, err := rwc.Write(fw.ffo.BinaryMarshal()); err != nil { | |
812 | return err | |
813 | } | |
814 | } | |
815 | ||
816 | file, err := fw.dataForkReader() | |
817 | if err != nil { | |
818 | return err | |
819 | } | |
820 | ||
821 | br := bufio.NewReader(file) | |
822 | if _, err := br.Discard(int(dataOffset)); err != nil { | |
823 | return err | |
824 | } | |
825 | ||
826 | if _, err = io.Copy(rwc, io.TeeReader(br, fileTransfer.bytesSentCounter)); err != nil { | |
827 | return err | |
828 | } | |
829 | ||
830 | // if the client requested to resume transfer, do not send the resource fork header, or it will be appended into the fileWrapper data | |
831 | if fileTransfer.fileResumeData == nil { | |
832 | err = binary.Write(rwc, binary.BigEndian, fw.rsrcForkHeader()) | |
833 | if err != nil { | |
834 | return err | |
835 | } | |
836 | } | |
837 | ||
838 | rFile, err := fw.rsrcForkFile() | |
839 | if err != nil { | |
840 | return nil | |
841 | } | |
842 | ||
843 | if _, err = io.Copy(rwc, io.TeeReader(rFile, fileTransfer.bytesSentCounter)); err != nil { | |
844 | return err | |
845 | } | |
846 | ||
847 | case FileUpload: | |
848 | s.Stats.UploadCounter += 1 | |
849 | s.Stats.UploadsInProgress += 1 | |
850 | defer func() { s.Stats.UploadsInProgress -= 1 }() | |
851 | ||
852 | var file *os.File | |
853 | ||
854 | // A file upload has three possible cases: | |
855 | // 1) Upload a new file | |
856 | // 2) Resume a partially transferred file | |
857 | // 3) Replace a fully uploaded file | |
858 | // We have to infer which case applies by inspecting what is already on the filesystem | |
859 | ||
860 | // 1) Check for existing file: | |
861 | _, err = os.Stat(fullPath) | |
862 | if err == nil { | |
863 | return errors.New("existing file found at " + fullPath) | |
864 | } | |
865 | if errors.Is(err, fs.ErrNotExist) { | |
866 | // If not found, open or create a new .incomplete file | |
867 | file, err = os.OpenFile(fullPath+incompleteFileSuffix, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) | |
868 | if err != nil { | |
869 | return err | |
870 | } | |
871 | } | |
872 | ||
873 | f, err := newFileWrapper(s.FS, fullPath, 0) | |
874 | if err != nil { | |
875 | return err | |
876 | } | |
877 | ||
878 | rLogger.Infow("File upload started", "dstFile", fullPath) | |
879 | ||
880 | rForkWriter := io.Discard | |
881 | iForkWriter := io.Discard | |
882 | if s.Config.PreserveResourceForks { | |
883 | rForkWriter, err = f.rsrcForkWriter() | |
884 | if err != nil { | |
885 | return err | |
886 | } | |
887 | ||
888 | iForkWriter, err = f.infoForkWriter() | |
889 | if err != nil { | |
890 | return err | |
891 | } | |
892 | } | |
893 | ||
894 | if err := receiveFile(rwc, file, rForkWriter, iForkWriter, fileTransfer.bytesSentCounter); err != nil { | |
895 | s.Logger.Error(err) | |
896 | } | |
897 | ||
898 | if err := file.Close(); err != nil { | |
899 | return err | |
900 | } | |
901 | ||
902 | if err := s.FS.Rename(fullPath+".incomplete", fullPath); err != nil { | |
903 | return err | |
904 | } | |
905 | ||
906 | rLogger.Infow("File upload complete", "dstFile", fullPath) | |
907 | ||
908 | case FolderDownload: | |
909 | s.Stats.DownloadCounter += 1 | |
910 | s.Stats.DownloadsInProgress += 1 | |
911 | defer func() { s.Stats.DownloadsInProgress -= 1 }() | |
912 | ||
913 | // Folder Download flow: | |
914 | // 1. Get filePath from the transfer | |
915 | // 2. Iterate over files | |
916 | // 3. For each fileWrapper: | |
917 | // Send fileWrapper header to client | |
918 | // The client can reply in 3 ways: | |
919 | // | |
920 | // 1. If type is an odd number (unknown type?), or fileWrapper download for the current fileWrapper is completed: | |
921 | // client sends []byte{0x00, 0x03} to tell the server to continue to the next fileWrapper | |
922 | // | |
923 | // 2. If download of a fileWrapper is to be resumed: | |
924 | // client sends: | |
925 | // []byte{0x00, 0x02} // download folder action | |
926 | // [2]byte // Resume data size | |
927 | // []byte fileWrapper resume data (see myField_FileResumeData) | |
928 | // | |
929 | // 3. Otherwise, download of the fileWrapper is requested and client sends []byte{0x00, 0x01} | |
930 | // | |
931 | // When download is requested (case 2 or 3), server replies with: | |
932 | // [4]byte - fileWrapper size | |
933 | // []byte - Flattened File Object | |
934 | // | |
935 | // After every fileWrapper download, client could request next fileWrapper with: | |
936 | // []byte{0x00, 0x03} | |
937 | // | |
938 | // This notifies the server to send the next item header | |
939 | ||
940 | basePathLen := len(fullPath) | |
941 | ||
942 | rLogger.Infow("Start folder download", "path", fullPath) | |
943 | ||
944 | nextAction := make([]byte, 2) | |
945 | if _, err := io.ReadFull(rwc, nextAction); err != nil { | |
946 | return err | |
947 | } | |
948 | ||
949 | i := 0 | |
950 | err = filepath.Walk(fullPath+"/", func(path string, info os.FileInfo, err error) error { | |
951 | s.Stats.DownloadCounter += 1 | |
952 | i += 1 | |
953 | ||
954 | if err != nil { | |
955 | return err | |
956 | } | |
957 | ||
958 | // skip dot files | |
959 | if strings.HasPrefix(info.Name(), ".") { | |
960 | return nil | |
961 | } | |
962 | ||
963 | hlFile, err := newFileWrapper(s.FS, path, 0) | |
964 | if err != nil { | |
965 | return err | |
966 | } | |
967 | ||
968 | subPath := path[basePathLen+1:] | |
969 | rLogger.Debugw("Sending fileheader", "i", i, "path", path, "fullFilePath", fullPath, "subPath", subPath, "IsDir", info.IsDir()) | |
970 | ||
971 | if i == 1 { | |
972 | return nil | |
973 | } | |
974 | ||
975 | fileHeader := NewFileHeader(subPath, info.IsDir()) | |
976 | ||
977 | // Send the fileWrapper header to client | |
978 | if _, err := rwc.Write(fileHeader.Payload()); err != nil { | |
979 | s.Logger.Errorf("error sending file header: %v", err) | |
980 | return err | |
981 | } | |
982 | ||
983 | // Read the client's Next Action request | |
984 | if _, err := io.ReadFull(rwc, nextAction); err != nil { | |
985 | return err | |
986 | } | |
987 | ||
988 | rLogger.Debugw("Client folder download action", "action", fmt.Sprintf("%X", nextAction[0:2])) | |
989 | ||
990 | var dataOffset int64 | |
991 | ||
992 | switch nextAction[1] { | |
993 | case dlFldrActionResumeFile: | |
994 | // get size of resumeData | |
995 | resumeDataByteLen := make([]byte, 2) | |
996 | if _, err := io.ReadFull(rwc, resumeDataByteLen); err != nil { | |
997 | return err | |
998 | } | |
999 | ||
1000 | resumeDataLen := binary.BigEndian.Uint16(resumeDataByteLen) | |
1001 | resumeDataBytes := make([]byte, resumeDataLen) | |
1002 | if _, err := io.ReadFull(rwc, resumeDataBytes); err != nil { | |
1003 | return err | |
1004 | } | |
1005 | ||
1006 | var frd FileResumeData | |
1007 | if err := frd.UnmarshalBinary(resumeDataBytes); err != nil { | |
1008 | return err | |
1009 | } | |
1010 | dataOffset = int64(binary.BigEndian.Uint32(frd.ForkInfoList[0].DataSize[:])) | |
1011 | case dlFldrActionNextFile: | |
1012 | // client asked to skip this file | |
1013 | return nil | |
1014 | } | |
1015 | ||
1016 | if info.IsDir() { | |
1017 | return nil | |
1018 | } | |
1019 | ||
1020 | rLogger.Infow("File download started", | |
1021 | "fileName", info.Name(), | |
1022 | "TransferSize", fmt.Sprintf("%x", hlFile.ffo.TransferSize(dataOffset)), | |
1023 | ) | |
1024 | ||
1025 | // Send file size to client | |
1026 | if _, err := rwc.Write(hlFile.ffo.TransferSize(dataOffset)); err != nil { | |
1027 | s.Logger.Error(err) | |
1028 | return err | |
1029 | } | |
1030 | ||
1031 | // Send ffo bytes to client | |
1032 | if _, err := rwc.Write(hlFile.ffo.BinaryMarshal()); err != nil { | |
1033 | s.Logger.Error(err) | |
1034 | return err | |
1035 | } | |
1036 | ||
1037 | file, err := s.FS.Open(path) | |
1038 | if err != nil { | |
1039 | return err | |
1040 | } | |
1041 | ||
1042 | // wr := bufio.NewWriterSize(rwc, 1460) | |
1043 | if _, err = io.Copy(rwc, io.TeeReader(file, fileTransfer.bytesSentCounter)); err != nil { | |
1044 | return err | |
1045 | } | |
1046 | ||
1047 | if nextAction[1] != 2 && hlFile.ffo.FlatFileHeader.ForkCount[1] == 3 { | |
1048 | err = binary.Write(rwc, binary.BigEndian, hlFile.rsrcForkHeader()) | |
1049 | if err != nil { | |
1050 | return err | |
1051 | } | |
1052 | ||
1053 | rFile, err := hlFile.rsrcForkFile() | |
1054 | if err != nil { | |
1055 | return err | |
1056 | } | |
1057 | ||
1058 | if _, err = io.Copy(rwc, io.TeeReader(rFile, fileTransfer.bytesSentCounter)); err != nil { | |
1059 | return err | |
1060 | } | |
1061 | } | |
1062 | ||
1063 | // Read the client's Next Action request. This is always 3, I think? | |
1064 | if _, err := io.ReadFull(rwc, nextAction); err != nil { | |
1065 | return err | |
1066 | } | |
1067 | ||
1068 | return nil | |
1069 | }) | |
1070 | ||
1071 | if err != nil { | |
1072 | return err | |
1073 | } | |
1074 | ||
1075 | case FolderUpload: | |
1076 | s.Stats.UploadCounter += 1 | |
1077 | s.Stats.UploadsInProgress += 1 | |
1078 | defer func() { s.Stats.UploadsInProgress -= 1 }() | |
1079 | rLogger.Infow( | |
1080 | "Folder upload started", | |
1081 | "dstPath", fullPath, | |
1082 | "TransferSize", binary.BigEndian.Uint32(fileTransfer.TransferSize), | |
1083 | "FolderItemCount", fileTransfer.FolderItemCount, | |
1084 | ) | |
1085 | ||
1086 | // Check if the target folder exists. If not, create it. | |
1087 | if _, err := s.FS.Stat(fullPath); os.IsNotExist(err) { | |
1088 | if err := s.FS.Mkdir(fullPath, 0777); err != nil { | |
1089 | return err | |
1090 | } | |
1091 | } | |
1092 | ||
1093 | // Begin the folder upload flow by sending the "next file action" to client | |
1094 | if _, err := rwc.Write([]byte{0, dlFldrActionNextFile}); err != nil { | |
1095 | return err | |
1096 | } | |
1097 | ||
1098 | fileSize := make([]byte, 4) | |
1099 | ||
1100 | for i := 0; i < fileTransfer.ItemCount(); i++ { | |
1101 | s.Stats.UploadCounter += 1 | |
1102 | ||
1103 | var fu folderUpload | |
1104 | if _, err := io.ReadFull(rwc, fu.DataSize[:]); err != nil { | |
1105 | return err | |
1106 | } | |
1107 | if _, err := io.ReadFull(rwc, fu.IsFolder[:]); err != nil { | |
1108 | return err | |
1109 | } | |
1110 | if _, err := io.ReadFull(rwc, fu.PathItemCount[:]); err != nil { | |
1111 | return err | |
1112 | } | |
1113 | ||
1114 | fu.FileNamePath = make([]byte, binary.BigEndian.Uint16(fu.DataSize[:])-4) // -4 to subtract the path separator bytes | |
1115 | ||
1116 | if _, err := io.ReadFull(rwc, fu.FileNamePath); err != nil { | |
1117 | return err | |
1118 | } | |
1119 | ||
1120 | rLogger.Infow( | |
1121 | "Folder upload continued", | |
1122 | "FormattedPath", fu.FormattedPath(), | |
1123 | "IsFolder", fmt.Sprintf("%x", fu.IsFolder), | |
1124 | "PathItemCount", binary.BigEndian.Uint16(fu.PathItemCount[:]), | |
1125 | ) | |
1126 | ||
1127 | if fu.IsFolder == [2]byte{0, 1} { | |
1128 | if _, err := os.Stat(filepath.Join(fullPath, fu.FormattedPath())); os.IsNotExist(err) { | |
1129 | if err := os.Mkdir(filepath.Join(fullPath, fu.FormattedPath()), 0777); err != nil { | |
1130 | return err | |
1131 | } | |
1132 | } | |
1133 | ||
1134 | // Tell client to send next file | |
1135 | if _, err := rwc.Write([]byte{0, dlFldrActionNextFile}); err != nil { | |
1136 | return err | |
1137 | } | |
1138 | } else { | |
1139 | nextAction := dlFldrActionSendFile | |
1140 | ||
1141 | // Check if we have the full file already. If so, send dlFldrAction_NextFile to client to skip. | |
1142 | _, err = os.Stat(filepath.Join(fullPath, fu.FormattedPath())) | |
1143 | if err != nil && !errors.Is(err, fs.ErrNotExist) { | |
1144 | return err | |
1145 | } | |
1146 | if err == nil { | |
1147 | nextAction = dlFldrActionNextFile | |
1148 | } | |
1149 | ||
1150 | // Check if we have a partial file already. If so, send dlFldrAction_ResumeFile to client to resume upload. | |
1151 | incompleteFile, err := os.Stat(filepath.Join(fullPath, fu.FormattedPath()+incompleteFileSuffix)) | |
1152 | if err != nil && !errors.Is(err, fs.ErrNotExist) { | |
1153 | return err | |
1154 | } | |
1155 | if err == nil { | |
1156 | nextAction = dlFldrActionResumeFile | |
1157 | } | |
1158 | ||
1159 | if _, err := rwc.Write([]byte{0, uint8(nextAction)}); err != nil { | |
1160 | return err | |
1161 | } | |
1162 | ||
1163 | switch nextAction { | |
1164 | case dlFldrActionNextFile: | |
1165 | continue | |
1166 | case dlFldrActionResumeFile: | |
1167 | offset := make([]byte, 4) | |
1168 | binary.BigEndian.PutUint32(offset, uint32(incompleteFile.Size())) | |
1169 | ||
1170 | file, err := os.OpenFile(fullPath+"/"+fu.FormattedPath()+incompleteFileSuffix, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | |
1171 | if err != nil { | |
1172 | return err | |
1173 | } | |
1174 | ||
1175 | fileResumeData := NewFileResumeData([]ForkInfoList{*NewForkInfoList(offset)}) | |
1176 | ||
1177 | b, _ := fileResumeData.BinaryMarshal() | |
1178 | ||
1179 | bs := make([]byte, 2) | |
1180 | binary.BigEndian.PutUint16(bs, uint16(len(b))) | |
1181 | ||
1182 | if _, err := rwc.Write(append(bs, b...)); err != nil { | |
1183 | return err | |
1184 | } | |
1185 | ||
1186 | if _, err := io.ReadFull(rwc, fileSize); err != nil { | |
1187 | return err | |
1188 | } | |
1189 | ||
1190 | if err := receiveFile(rwc, file, ioutil.Discard, ioutil.Discard, fileTransfer.bytesSentCounter); err != nil { | |
1191 | s.Logger.Error(err) | |
1192 | } | |
1193 | ||
1194 | err = os.Rename(fullPath+"/"+fu.FormattedPath()+".incomplete", fullPath+"/"+fu.FormattedPath()) | |
1195 | if err != nil { | |
1196 | return err | |
1197 | } | |
1198 | ||
1199 | case dlFldrActionSendFile: | |
1200 | if _, err := io.ReadFull(rwc, fileSize); err != nil { | |
1201 | return err | |
1202 | } | |
1203 | ||
1204 | filePath := filepath.Join(fullPath, fu.FormattedPath()) | |
1205 | ||
1206 | hlFile, err := newFileWrapper(s.FS, filePath, 0) | |
1207 | if err != nil { | |
1208 | return err | |
1209 | } | |
1210 | ||
1211 | rLogger.Infow("Starting file transfer", "path", filePath, "fileNum", i+1, "fileSize", binary.BigEndian.Uint32(fileSize)) | |
1212 | ||
1213 | incWriter, err := hlFile.incFileWriter() | |
1214 | if err != nil { | |
1215 | return err | |
1216 | } | |
1217 | ||
1218 | rForkWriter := io.Discard | |
1219 | iForkWriter := io.Discard | |
1220 | if s.Config.PreserveResourceForks { | |
1221 | iForkWriter, err = hlFile.infoForkWriter() | |
1222 | if err != nil { | |
1223 | return err | |
1224 | } | |
1225 | ||
1226 | rForkWriter, err = hlFile.rsrcForkWriter() | |
1227 | if err != nil { | |
1228 | return err | |
1229 | } | |
1230 | } | |
1231 | if err := receiveFile(rwc, incWriter, rForkWriter, iForkWriter, fileTransfer.bytesSentCounter); err != nil { | |
1232 | return err | |
1233 | } | |
1234 | ||
1235 | if err := os.Rename(filePath+".incomplete", filePath); err != nil { | |
1236 | return err | |
1237 | } | |
1238 | } | |
1239 | ||
1240 | // Tell client to send next fileWrapper | |
1241 | if _, err := rwc.Write([]byte{0, dlFldrActionNextFile}); err != nil { | |
1242 | return err | |
1243 | } | |
1244 | } | |
1245 | } | |
1246 | rLogger.Infof("Folder upload complete") | |
1247 | } | |
1248 | ||
1249 | return nil | |
1250 | } |