-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (69 loc) · 1.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"net"
"crypto/tls"
"fmt"
"os"
"path/filepath"
fs "github.com/dearrude/easygoftp/ftpserver"
ws "github.com/dearrude/easygoftp/webserver"
)
func main() {
c := GenConfig()
// Get current directory
cur_dir, err := os.Getwd()
if err != nil {
c.StderrLogger.Println("Failed to get current directory:", err)
return
}
// Inilize the database
db, err := initDb(cur_dir, c.DbAdminName, c.DbAdminPass)
if err != nil {
c.StderrLogger.Println("Failed to init the db: ", err)
}
defer db.Close()
// If secure, handle TLS
tlsConfig := &tls.Config{}
if c.UseTLS {
tlsConfig = fs.GetTLSConfig(c.Domain)
}
// Init web server
go ws.Setup(
c.WebPort,
&c.StdoutLogger,
&c.StderrLogger,
filepath.Join(cur_dir, "files"),
db,
)
// Init FTP server
go func() {
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", c.FTPPort))
if err != nil {
c.StderrLogger.Println("Failed to start the FTP server on designated port:", err)
return
}
defer listener.Close()
c.StdoutLogger.Printf("FTP server started on port %d\n", c.FTPPort)
for {
conn, err := listener.Accept()
if err != nil {
c.StderrLogger.Println("Connection could not be accepted", err)
continue
}
c.StdoutLogger.Printf("Connection accepted from %s", conn.RemoteAddr())
_, _ = conn.Write([]byte("220 Service ready for new user\r\n"))
ftpConn := fs.FTPServer{
Logger: &c.StdoutLogger,
ErrLogger: &c.StderrLogger,
UseTLS: c.UseTLS,
TLSConf: tlsConfig,
Conn: conn,
MainDir: filepath.Join(cur_dir, "files"),
DB: db,
}
// Handle the requests concurrently
go fs.HandleFTPCommands(&ftpConn)
}
}()
select {} // keep the main goroutine running
}