Skip to content

Commit

Permalink
add TLS support
Browse files Browse the repository at this point in the history
fix #8
  • Loading branch information
mayth committed Feb 11, 2019
1 parent be039fa commit 57ace6f
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions simple_upload_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ var logger *logrus.Logger
func run(args []string) int {
bindAddress := flag.String("ip", "0.0.0.0", "IP address to bind")
listenPort := flag.Int("port", 25478, "port number to listen on")
tlsListenPort := flag.Int("tlsport", 25443, "port number to listen on with TLS")
// 5,242,880 bytes == 5 MiB
maxUploadSize := flag.Int64("upload_limit", 5242880, "max size of uploaded file (byte)")
tokenFlag := flag.String("token", "", "specify the security token (it is automatically generated if empty)")
logLevelFlag := flag.String("loglevel", "info", "logging level")
certFile := flag.String("cert", "", "path to certificate file")
keyFile := flag.String("key", "", "path to key file")
flag.Parse()
serverRoot := flag.Arg(0)
if len(serverRoot) == 0 {
Expand All @@ -42,17 +45,44 @@ func run(args []string) int {
token = fmt.Sprintf("%x", b)
logger.WithField("token", token).Warn("token generated")
}
logger.WithFields(logrus.Fields{
"ip": *bindAddress,
"port": *listenPort,
"token": token,
"upload_limit": *maxUploadSize,
"root": serverRoot,
}).Info("start listening")
tlsEnabled := *certFile != "" && *keyFile != ""
server := NewServer(serverRoot, *maxUploadSize, token)
http.Handle("/upload", server)
http.Handle("/files/", server)
http.ListenAndServe(fmt.Sprintf("%s:%d", *bindAddress, *listenPort), nil)

errors := make(chan error)

go func() {
logger.WithFields(logrus.Fields{
"ip": *bindAddress,
"port": *listenPort,
"token": token,
"upload_limit": *maxUploadSize,
"root": serverRoot,
}).Info("start listening")

if err := http.ListenAndServe(fmt.Sprintf("%s:%d", *bindAddress, *listenPort), nil); err != nil {
errors <- err
}
}()

if tlsEnabled {
go func() {
logger.WithFields(logrus.Fields{
"cert": *certFile,
"key": *keyFile,
"port": *tlsListenPort,
}).Info("start listening TLS")

if err := http.ListenAndServeTLS(fmt.Sprintf("%s:%d", *bindAddress, *tlsListenPort), *certFile, *keyFile, nil); err != nil {
errors <- err
}
}()
}

err := <-errors
logger.WithError(err).Info("closing server")

return 0
}

Expand Down

0 comments on commit 57ace6f

Please # to comment.