Skip to content

Commit 12447a8

Browse files
committed
support https
1 parent 4d27fe2 commit 12447a8

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed

fileserver.go

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ var (
8080
CONST_LOG_LEVELDB_FILE_NAME = DATA_DIR + "/log.db"
8181
CONST_STAT_FILE_NAME = DATA_DIR + "/stat.json"
8282
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"
8385
CONST_SEARCH_FILE_NAME = DATA_DIR + "/search.txt"
8486
CONST_UPLOAD_COUNTER_KEY = "__CONST_UPLOAD_COUNTER_KEY__"
8587
logConfigStr = `
@@ -126,6 +128,8 @@ const (
126128
cfgJson = `{
127129
"绑定端号": "端口",
128130
"addr": ":8080",
131+
"是否开启https": "默认不开启,如需启开启,请在conf目录中增加证书文件 server.crt 私钥 文件 server.key",
132+
"enable_https": false,
129133
"PeerID": "集群内唯一,请使用0-9的单字符,默认自动生成",
130134
"peer_id": "%s",
131135
"本主机地址": "本机http地址,默认自动生成(注意端口必须与addr中的端口一致),必段为内网,自动生成不为内网请自行修改,下同",
@@ -271,6 +275,7 @@ type StatDateFileInfo struct {
271275
type GloablConfig struct {
272276
Addr string `json:"addr"`
273277
Peers []string `json:"peers"`
278+
EnableHttps bool `json:"enable_https"`
274279
Group string `json:"group"`
275280
RenameFile bool `json:"rename_file"`
276281
ShowDir bool `json:"show_dir"`
@@ -2047,10 +2052,11 @@ func (this *Server) SyncFileInfo(w http.ResponseWriter, r *http.Request) {
20472052
filename string
20482053
)
20492054
r.ParseForm()
2055+
fileInfoStr = r.FormValue("fileInfo")
20502056
if !this.IsPeer(r) {
2057+
log.Info("isn't peer fileInfo:", fileInfo)
20512058
return
20522059
}
2053-
fileInfoStr = r.FormValue("fileInfo")
20542060
if err = json.Unmarshal([]byte(fileInfoStr), &fileInfo); err != nil {
20552061
w.Write([]byte(this.GetClusterNotPermitMessage(r)))
20562062
log.Error(err)
@@ -2218,22 +2224,28 @@ func (this *Server) BuildFileResult(fileInfo *FileInfo, r *http.Request) FileRes
22182224
downloadUrl string
22192225
domain string
22202226
host string
2227+
protocol string
22212228
)
2229+
if Config().EnableHttps {
2230+
protocol = "https"
2231+
} else {
2232+
protocol = "http"
2233+
}
22222234
host = strings.Replace(Config().Host, "http://", "", -1)
22232235
if r != nil {
22242236
host = r.Host
22252237
}
22262238
if !strings.HasPrefix(Config().DownloadDomain, "http") {
22272239
if Config().DownloadDomain == "" {
2228-
Config().DownloadDomain = fmt.Sprintf("http://%s", host)
2240+
Config().DownloadDomain = fmt.Sprintf("%s://%s", protocol, host)
22292241
} else {
2230-
Config().DownloadDomain = fmt.Sprintf("http://%s", Config().DownloadDomain)
2242+
Config().DownloadDomain = fmt.Sprintf("%s://%s", protocol, Config().DownloadDomain)
22312243
}
22322244
}
22332245
if Config().DownloadDomain != "" {
22342246
domain = Config().DownloadDomain
22352247
} else {
2236-
domain = fmt.Sprintf("http://%s", host)
2248+
domain = fmt.Sprintf("%s://%s", protocol, host)
22372249
}
22382250
outname = fileInfo.Name
22392251
if fileInfo.ReName != "" {
@@ -2245,7 +2257,7 @@ func (this *Server) BuildFileResult(fileInfo *FileInfo, r *http.Request) FileRes
22452257
} else {
22462258
p = p + "/" + outname
22472259
}
2248-
downloadUrl = fmt.Sprintf("http://%s/%s", host, p)
2260+
downloadUrl = fmt.Sprintf("%s://%s/%s", protocol, host, p)
22492261
if Config().DownloadDomain != "" {
22502262
downloadUrl = fmt.Sprintf("%s/%s", Config().DownloadDomain, p)
22512263
}
@@ -2274,7 +2286,6 @@ func (this *Server) SaveUploadFile(file multipart.File, header *multipart.FileHe
22742286
if len(Config().Extensions) > 0 && !this.util.Contains(path.Ext(fileInfo.Name), Config().Extensions) {
22752287
return fileInfo, errors.New("(error)file extension mismatch")
22762288
}
2277-
22782289
if Config().RenameFile {
22792290
fileInfo.ReName = this.util.MD5(this.util.GetUUID()) + path.Ext(fileInfo.Name)
22802291
}
@@ -3757,6 +3768,8 @@ func init() {
37573768
CONST_LOG_LEVELDB_FILE_NAME = DATA_DIR + "/log.db"
37583769
CONST_STAT_FILE_NAME = DATA_DIR + "/stat.json"
37593770
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"
37603773
CONST_SEARCH_FILE_NAME = DATA_DIR + "/search.txt"
37613774
FOLDERS = []string{DATA_DIR, STORE_DIR, CONF_DIR, STATIC_DIR}
37623775
logAccessConfigStr = strings.Replace(logAccessConfigStr, "{DOCKER_DIR}", DOCKER_DIR, -1)
@@ -4343,17 +4356,23 @@ func (this *Server) Start() {
43434356
http.Handle(fmt.Sprintf("%s/static/", groupRoute), http.StripPrefix(fmt.Sprintf("%s/static/", groupRoute), http.FileServer(http.Dir("./static"))))
43444357
http.HandleFunc("/"+Config().Group+"/", this.Download)
43454358
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+
}
43574376
}
43584377
func main() {
43594378
server.Start()

0 commit comments

Comments
 (0)