forked from goftp/ftpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (108 loc) · 2.78 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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"github.com/Unknwon/goconfig"
"github.com/goftp/file-driver"
"github.com/goftp/leveldb-perm"
"github.com/goftp/qiniu-driver"
"github.com/goftp/server"
"github.com/lunny/log"
"github.com/syndtr/goleveldb/leveldb"
"github.com/wenhuang1003/ftpd/web"
"github.com/wenhuang1003/leveldb-auth"
)
var (
version = "v0.1.1104"
)
var (
cfg *goconfig.ConfigFile
cfgPath string
customPath string
)
func main() {
flag.StringVar(&cfgPath, "config", "config.ini",
"config file path, default is config.ini and custom.ini")
flag.Parse()
if len(cfgPath) <= 0 {
cfgPath = "config.ini"
customPath = "custom.ini"
} else {
f, _ := filepath.Abs(cfgPath)
customPath = filepath.Join(filepath.Dir(f), "custom.ini")
}
var err error
cfg, err = goconfig.LoadConfigFile(cfgPath, customPath)
if err != nil {
fmt.Println(err)
return
}
log.Info("Loaded config files:", cfgPath, customPath)
port, _ := cfg.Int("server", "port")
db, err := leveldb.OpenFile("./authperm.db", nil)
if err != nil {
fmt.Println(err)
return
}
var auth = &ldbauth.LDBAuth{db}
var perm server.Perm
if cfg.MustValue("perm", "type") == "leveldb" {
perm = ldbperm.NewLDBPerm(db, "root", "root", os.ModePerm)
} else {
perm = server.NewSimplePerm("root", "root")
}
typ, _ := cfg.GetValue("driver", "type")
var factory server.DriverFactory
if typ == "file" {
rootPath, _ := cfg.GetValue("file", "rootpath")
_, err = os.Lstat(rootPath)
if os.IsNotExist(err) {
os.MkdirAll(rootPath, os.ModePerm)
} else if err != nil {
fmt.Println(err)
return
}
factory = &filedriver.FileDriverFactory{
rootPath,
perm,
}
} else if typ == "qiniu" {
accessKey, _ := cfg.GetValue("qiniu", "accessKey")
secretKey, _ := cfg.GetValue("qiniu", "secretKey")
bucket, _ := cfg.GetValue("qiniu", "bucket")
factory = qiniudriver.NewQiniuDriverFactory(accessKey,
secretKey, bucket)
} else {
log.Fatal("no driver type input")
}
// start web manage UI
useweb, _ := cfg.Bool("web", "enable")
if useweb {
web.DB = auth
web.Perm = perm
web.Factory = factory
weblisten, _ := cfg.GetValue("web", "listen")
admin, _ := cfg.GetValue("admin", "user")
pass, _ := cfg.GetValue("admin", "pass")
tls, _ := cfg.Bool("web", "tls")
certFile, _ := cfg.GetValue("web", "certFile")
keyFile, _ := cfg.GetValue("web", "keyFile")
go web.Web(weblisten, "static", "templates", admin, pass, tls, certFile, keyFile)
}
ftpName, _ := cfg.GetValue("server", "name")
opt := &server.ServerOpts{
Name: ftpName,
Factory: factory,
Port: port,
Auth: auth,
}
// start ftp server
ftpServer := server.NewServer(opt)
log.Info("FTP Server", version)
err = ftpServer.ListenAndServe()
if err != nil {
log.Fatal("Error starting server:", err)
}
}