80
80
CONST_LOG_LEVELDB_FILE_NAME = DATA_DIR + "/log.db"
81
81
CONST_STAT_FILE_NAME = DATA_DIR + "/stat.json"
82
82
CONST_CONF_FILE_NAME = CONF_DIR + "/cfg.json"
83
+ CONST_SERVER_CRT_FILE_NAME = CONF_DIR + "/server.crt"
84
+ CONST_SERVER_KEY_FILE_NAME = CONF_DIR + "/server.key"
83
85
CONST_SEARCH_FILE_NAME = DATA_DIR + "/search.txt"
84
86
CONST_UPLOAD_COUNTER_KEY = "__CONST_UPLOAD_COUNTER_KEY__"
85
87
logConfigStr = `
@@ -126,6 +128,8 @@ const (
126
128
cfgJson = `{
127
129
"绑定端号": "端口",
128
130
"addr": ":8080",
131
+ "是否开启https": "默认不开启,如需启开启,请在conf目录中增加证书文件 server.crt 私钥 文件 server.key",
132
+ "enable_https": false,
129
133
"PeerID": "集群内唯一,请使用0-9的单字符,默认自动生成",
130
134
"peer_id": "%s",
131
135
"本主机地址": "本机http地址,默认自动生成(注意端口必须与addr中的端口一致),必段为内网,自动生成不为内网请自行修改,下同",
@@ -271,6 +275,7 @@ type StatDateFileInfo struct {
271
275
type GloablConfig struct {
272
276
Addr string `json:"addr"`
273
277
Peers []string `json:"peers"`
278
+ EnableHttps bool `json:"enable_https"`
274
279
Group string `json:"group"`
275
280
RenameFile bool `json:"rename_file"`
276
281
ShowDir bool `json:"show_dir"`
@@ -2047,10 +2052,11 @@ func (this *Server) SyncFileInfo(w http.ResponseWriter, r *http.Request) {
2047
2052
filename string
2048
2053
)
2049
2054
r .ParseForm ()
2055
+ fileInfoStr = r .FormValue ("fileInfo" )
2050
2056
if ! this .IsPeer (r ) {
2057
+ log .Info ("isn't peer fileInfo:" , fileInfo )
2051
2058
return
2052
2059
}
2053
- fileInfoStr = r .FormValue ("fileInfo" )
2054
2060
if err = json .Unmarshal ([]byte (fileInfoStr ), & fileInfo ); err != nil {
2055
2061
w .Write ([]byte (this .GetClusterNotPermitMessage (r )))
2056
2062
log .Error (err )
@@ -2218,22 +2224,28 @@ func (this *Server) BuildFileResult(fileInfo *FileInfo, r *http.Request) FileRes
2218
2224
downloadUrl string
2219
2225
domain string
2220
2226
host string
2227
+ protocol string
2221
2228
)
2229
+ if Config ().EnableHttps {
2230
+ protocol = "https"
2231
+ } else {
2232
+ protocol = "http"
2233
+ }
2222
2234
host = strings .Replace (Config ().Host , "http://" , "" , - 1 )
2223
2235
if r != nil {
2224
2236
host = r .Host
2225
2237
}
2226
2238
if ! strings .HasPrefix (Config ().DownloadDomain , "http" ) {
2227
2239
if Config ().DownloadDomain == "" {
2228
- Config ().DownloadDomain = fmt .Sprintf ("http ://%s" , host )
2240
+ Config ().DownloadDomain = fmt .Sprintf ("%s ://%s" , protocol , host )
2229
2241
} else {
2230
- Config ().DownloadDomain = fmt .Sprintf ("http ://%s" , Config ().DownloadDomain )
2242
+ Config ().DownloadDomain = fmt .Sprintf ("%s ://%s" , protocol , Config ().DownloadDomain )
2231
2243
}
2232
2244
}
2233
2245
if Config ().DownloadDomain != "" {
2234
2246
domain = Config ().DownloadDomain
2235
2247
} else {
2236
- domain = fmt .Sprintf ("http ://%s" , host )
2248
+ domain = fmt .Sprintf ("%s ://%s" , protocol , host )
2237
2249
}
2238
2250
outname = fileInfo .Name
2239
2251
if fileInfo .ReName != "" {
@@ -2245,7 +2257,7 @@ func (this *Server) BuildFileResult(fileInfo *FileInfo, r *http.Request) FileRes
2245
2257
} else {
2246
2258
p = p + "/" + outname
2247
2259
}
2248
- downloadUrl = fmt .Sprintf ("http ://%s/%s" , host , p )
2260
+ downloadUrl = fmt .Sprintf ("%s ://%s/%s" , protocol , host , p )
2249
2261
if Config ().DownloadDomain != "" {
2250
2262
downloadUrl = fmt .Sprintf ("%s/%s" , Config ().DownloadDomain , p )
2251
2263
}
@@ -2274,7 +2286,6 @@ func (this *Server) SaveUploadFile(file multipart.File, header *multipart.FileHe
2274
2286
if len (Config ().Extensions ) > 0 && ! this .util .Contains (path .Ext (fileInfo .Name ), Config ().Extensions ) {
2275
2287
return fileInfo , errors .New ("(error)file extension mismatch" )
2276
2288
}
2277
-
2278
2289
if Config ().RenameFile {
2279
2290
fileInfo .ReName = this .util .MD5 (this .util .GetUUID ()) + path .Ext (fileInfo .Name )
2280
2291
}
@@ -3757,6 +3768,8 @@ func init() {
3757
3768
CONST_LOG_LEVELDB_FILE_NAME = DATA_DIR + "/log.db"
3758
3769
CONST_STAT_FILE_NAME = DATA_DIR + "/stat.json"
3759
3770
CONST_CONF_FILE_NAME = CONF_DIR + "/cfg.json"
3771
+ CONST_SERVER_CRT_FILE_NAME = CONF_DIR + "/server.crt"
3772
+ CONST_SERVER_KEY_FILE_NAME = CONF_DIR + "/server.key"
3760
3773
CONST_SEARCH_FILE_NAME = DATA_DIR + "/search.txt"
3761
3774
FOLDERS = []string {DATA_DIR , STORE_DIR , CONF_DIR , STATIC_DIR }
3762
3775
logAccessConfigStr = strings .Replace (logAccessConfigStr , "{DOCKER_DIR}" , DOCKER_DIR , - 1 )
@@ -4343,17 +4356,23 @@ func (this *Server) Start() {
4343
4356
http .Handle (fmt .Sprintf ("%s/static/" , groupRoute ), http .StripPrefix (fmt .Sprintf ("%s/static/" , groupRoute ), http .FileServer (http .Dir ("./static" ))))
4344
4357
http .HandleFunc ("/" + Config ().Group + "/" , this .Download )
4345
4358
fmt .Println ("Listen on " + Config ().Addr )
4346
- srv := & http.Server {
4347
- Addr : Config ().Addr ,
4348
- Handler : new (HttpHandler ),
4349
- ReadTimeout : time .Duration (Config ().ReadTimeout ) * time .Second ,
4350
- ReadHeaderTimeout : time .Duration (Config ().ReadHeaderTimeout ) * time .Second ,
4351
- WriteTimeout : time .Duration (Config ().WriteTimeout ) * time .Second ,
4352
- IdleTimeout : time .Duration (Config ().IdleTimeout ) * time .Second ,
4353
- }
4354
- err := srv .ListenAndServe ()
4355
- log .Error (err )
4356
- fmt .Println (err )
4359
+ if Config ().EnableHttps {
4360
+ err := http .ListenAndServeTLS (Config ().Addr , CONST_SERVER_CRT_FILE_NAME , CONST_SERVER_KEY_FILE_NAME , new (HttpHandler ))
4361
+ log .Error (err )
4362
+ fmt .Println (err )
4363
+ } else {
4364
+ srv := & http.Server {
4365
+ Addr : Config ().Addr ,
4366
+ Handler : new (HttpHandler ),
4367
+ ReadTimeout : time .Duration (Config ().ReadTimeout ) * time .Second ,
4368
+ ReadHeaderTimeout : time .Duration (Config ().ReadHeaderTimeout ) * time .Second ,
4369
+ WriteTimeout : time .Duration (Config ().WriteTimeout ) * time .Second ,
4370
+ IdleTimeout : time .Duration (Config ().IdleTimeout ) * time .Second ,
4371
+ }
4372
+ err := srv .ListenAndServe ()
4373
+ log .Error (err )
4374
+ fmt .Println (err )
4375
+ }
4357
4376
}
4358
4377
func main () {
4359
4378
server .Start ()
0 commit comments