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