forked from fclairamb/ftpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
162 lines (126 loc) · 3.55 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// ftpserver allows to create your own FTP(S) server
package main
import (
"flag"
"io"
"io/ioutil"
"os"
"os/signal"
"syscall"
"time"
ftpserver "github.com/fclairamb/ftpserverlib"
gkwrap "github.com/fclairamb/go-log/gokit"
gokit "github.com/go-kit/log"
"github.com/fclairamb/ftpserver/config"
"github.com/fclairamb/ftpserver/server"
)
var (
ftpServer *ftpserver.FtpServer
driver *server.Server
)
func main() {
// Arguments vars
var confFile string
var onlyConf bool
// Parsing arguments
flag.StringVar(&confFile, "conf", "", "Configuration file")
flag.BoolVar(&onlyConf, "conf-only", false, "Only create the conf")
flag.Parse()
// Setting up the logger
logger := gkwrap.New()
logger.Info("FTP server", "version", BuildVersion, "date", BuildDate, "commit", Commit)
autoCreate := onlyConf
// The general idea here is that if you start it without any arg, you're probably doing a local quick&dirty run
// possibly on a windows machine, so we're better of just using a default file name and create the file.
if confFile == "" {
confFile = "ftpserver.json"
autoCreate = true
}
if autoCreate {
if _, err := os.Stat(confFile); err != nil && os.IsNotExist(err) {
logger.Warn("No conf file, creating one", "confFile", confFile)
if err := ioutil.WriteFile(confFile, confFileContent(), 0600); err != nil { //nolint: gomnd
logger.Warn("Couldn't create conf file", "confFile", confFile)
}
}
}
conf, errConfig := config.NewConfig(confFile, logger)
if errConfig != nil {
logger.Error("Can't load conf", "err", errConfig)
return
}
// Now is a good time to open a logging file
if conf.Content.Logging.File != "" {
writer, err := os.OpenFile(conf.Content.Logging.File, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600) //nolint:gomnd
if err != nil {
logger.Error("Can't open log file", "err", err)
return
}
logger = gkwrap.NewWrap(gokit.NewLogfmtLogger(io.MultiWriter(writer, os.Stdout))).With(
"ts", gokit.DefaultTimestampUTC,
"caller", gokit.DefaultCaller,
)
}
// Loading the driver
var errNewServer error
driver, errNewServer = server.NewServer(conf, logger.With("component", "driver"))
if errNewServer != nil {
logger.Error("Could not load the driver", "err", errNewServer)
return
}
// Instantiating the server by passing our driver implementation
ftpServer = ftpserver.NewFtpServer(driver)
// Overriding the server default silent logger by a sub-logger (component: server)
ftpServer.Logger = logger.With("component", "server")
// Preparing the SIGTERM handling
go signalHandler()
// Blocking call, behaving similarly to the http.ListenAndServe
if onlyConf {
logger.Warn("Only creating conf")
return
}
if err := ftpServer.ListenAndServe(); err != nil {
logger.Error("Problem listening", "err", err)
}
// We wait at most 1 minutes for all clients to disconnect
if err := driver.WaitGracefully(time.Minute); err != nil {
ftpServer.Logger.Warn("Problem stopping server", "err", err)
}
}
func stop() {
driver.Stop()
if err := ftpServer.Stop(); err != nil {
ftpServer.Logger.Error("Problem stopping server", "err", err)
}
}
func signalHandler() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGTERM)
for {
sig := <-ch
if sig == syscall.SIGTERM {
stop()
break
}
}
}
func confFileContent() []byte {
str := `{
"version": 1,
"accesses": [
{
"user": "test",
"pass": "test",
"fs": "os",
"params": {
"basePath": "/tmp"
}
}
],
"passive_transfer_port_range": {
"start": 2122,
"end": 2130
}
}`
return []byte(str)
}