-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
236 lines (195 loc) · 5.9 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package main
import (
"crypto/tls"
"net/http"
"flag"
"log"
"time"
"context"
"os"
"os/signal"
"path"
"path/filepath"
"strings"
"errors"
webmap "local/webmap"
)
var (
gzipLv = flag.Int("gz", 5, "gzip disable = 0, DefaultCompression = -1, BestSpeed = 1, BestCompression = 9")
upto = flag.Int("upto", 10*60, "upload timeout in Seconds")
dwto = flag.Int("dwto", 10*60, "download timeout in Seconds")
addr = flag.String("l", ":4040", "bind addr & port")
crtFile = flag.String("crt", "", "https certificate file")
keyFile = flag.String("key", "", "https private key file")
verbosity = flag.Int("v", 3, "verbosity for app")
logDir = flag.String("logdir", "log/", "path to save log file")
logFile = flag.String("log", "access-%v.log", "access log file for all http request, '%v' for date")
syslogFile = flag.String("syslog", "system-%v.log", "system log file, '%v' for date")
dbFile = flag.String("db", "webmap.db", "json database file")
ssusr = flag.String("ssusr", "", "temporary super user login acc")
)
func main() {
flag.Parse()
if err := createDirIfNotExist(*logDir); err != nil {
log.Println("[dir]create", *logDir, err)
return
}
if err := createDirIfNotExist("./upload"); err != nil { // TODO: not hardcode
log.Println("[dir]create", "upload", err)
return
}
if err := createDirIfNotExist("./cache"); err != nil { // TODO: not hardcode
log.Println("[dir]create", "cache", err)
return
}
db := webmap.NewDataStore()
err := db.Open(*dbFile)
if err != nil {
log.Println("[db]error", err)
return
}
if *ssusr != "" {
pwd := webmap.GenPWD(10)
db.AddShadowUser(*ssusr, pwd)
log.Println("[warn]enable temporary super user:", *ssusr, pwd)
}
webmap.GZIP_LV = *gzipLv
webmap.Verbosity = *verbosity
webmap.SetFileOutput(filepath.Join(*logDir, *syslogFile))
webmap.SetWebOutput(filepath.Join(*logDir, *logFile))
web := webmap.NewWebAPI(db)
web.Handle("/admin/", http.FileServer(NewSPADir("./www", "./www/admin/index.html", "admin")))
web.Handle("/res/", webmap.ReqGz(webmap.ReqCache(http.StripPrefix("/res/", http.FileServer(http.Dir("./www/res"))), "public, no-cache, max-age=0, must-revalidate")))
// web.Handle("/res/", http.StripPrefix("/res/", http.FileServer(http.Dir("./www/res"))))
srv := &http.Server{
ReadTimeout: time.Duration(*upto) * time.Second,
WriteTimeout: time.Duration(*dwto) * time.Second,
Addr: *addr,
Handler: webmap.ReqLog(web),
ReadHeaderTimeout: 20 * time.Second,
IdleTimeout: 60 * time.Second,
MaxHeaderBytes: 1024*1024, // 1MB
}
idleConnsClosed := make(chan struct{})
go func() {
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
<-sigint
// We received an interrupt signal, shut down.
if err := srv.Shutdown(context.Background()); err != nil {
// Error from closing listeners, or context timeout:
log.Printf("HTTP server Shutdown: %v", err)
}
close(idleConnsClosed)
}()
startServer(srv, *crtFile, *keyFile)
<-idleConnsClosed
// flush & exit
db.Close()
}
func startServer(srv *http.Server, crt string, key string) {
var err error
// check tls
if crt != "" && key != "" {
cfg := &tls.Config{
MinVersion: tls.VersionTLS12,
CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
PreferServerCipherSuites: true,
CipherSuites: []uint16{
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, // http/2 must
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, // http/2 must
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_GCM_SHA384, // weak
tls.TLS_RSA_WITH_AES_256_CBC_SHA, // waek
},
}
srv.TLSConfig = cfg
//srv.TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler), 0) // disable http/2
log.Printf("[server] HTTPS server Listen on: %v", srv.Addr)
err = srv.ListenAndServeTLS(crt, key)
} else {
log.Printf("[server] HTTP server Listen on: %v", srv.Addr)
err = srv.ListenAndServe()
}
if err != http.ErrServerClosed {
log.Printf("[server] ListenAndServe error: %v", err)
}
}
// An empty Dir is treated as ".".
type SPADir struct {
dir string
base string
//tag string
}
func NewSPADir(dir string, base string, tag string) SPADir {
return SPADir{
dir: dir,
base: base,
//tag: tag,
}
}
func mapDirOpenError(originalErr error, name string) error {
if os.IsNotExist(originalErr) || os.IsPermission(originalErr) {
return originalErr
}
parts := strings.Split(name, string(filepath.Separator))
for i := range parts {
if parts[i] == "" {
continue
}
fi, err := os.Stat(strings.Join(parts[:i+1], string(filepath.Separator)))
if err != nil {
return originalErr
}
if !fi.IsDir() {
return os.ErrNotExist
}
}
return originalErr
}
func (d SPADir) Open(name string) (http.File, error) {
if filepath.Separator != '/' && strings.ContainsRune(name, filepath.Separator) {
return nil, errors.New("http: invalid character in file path")
}
dir := d.dir
if dir == "" {
dir = "."
}
fullName := filepath.Join(dir, filepath.FromSlash(path.Clean("/"+name)))
f, err := os.Open(fullName)
if err != nil {
err2 := mapDirOpenError(err, fullName)
if strings.HasSuffix(name, "/index.html") { // try open index.html or list dir
return nil, err2
}
if err2 != os.ErrNotExist {
f2, err := os.Open(d.base)
if err != nil {
return nil, err2
}
f = f2
}
}
return f, nil
}
func createDirIfNotExist(dirName string) error {
src, err := os.Stat(dirName)
if os.IsNotExist(err) {
errDir := os.MkdirAll(dirName, 0755)
if errDir != nil {
return err
}
return nil
}
if src.Mode().IsRegular() {
return errors.New("already exist as a file!")
}
return nil
}