2018-02-15 04:39:11 +08:00
|
|
|
// Package httplib provides common functionality for http servers
|
|
|
|
package httplib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
2018-02-15 05:06:34 +08:00
|
|
|
auth "github.com/abbot/go-http-auth"
|
|
|
|
"github.com/ncw/rclone/fs"
|
2018-02-15 04:39:11 +08:00
|
|
|
"github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Globals
|
|
|
|
var (
|
2018-02-15 05:06:34 +08:00
|
|
|
bindAddress = "localhost:8080"
|
2018-02-15 07:14:14 +08:00
|
|
|
htPasswdFile = ""
|
|
|
|
realm = "rclone"
|
|
|
|
basicUser = ""
|
|
|
|
basicPass = ""
|
2018-02-15 04:39:11 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// AddFlags adds the http server specific flags
|
|
|
|
func AddFlags(flagSet *pflag.FlagSet) {
|
|
|
|
flagSet.StringVarP(&bindAddress, "addr", "", bindAddress, "IPaddress:Port to bind server to.")
|
2018-02-15 05:06:34 +08:00
|
|
|
flagSet.StringVarP(&htPasswdFile, "htpasswd", "", htPasswdFile, "File to use for htpasswd authentication.")
|
2018-02-15 07:14:14 +08:00
|
|
|
flagSet.StringVarP(&realm, "realm", "", realm, "Realm name for authentication.")
|
|
|
|
flagSet.StringVarP(&basicUser, "user", "", basicUser, "User name for authentication.")
|
|
|
|
flagSet.StringVarP(&basicPass, "pass", "", basicPass, "Password for authentication.")
|
2018-02-15 04:39:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Help contains text describing the http server to add to the command
|
|
|
|
// help.
|
|
|
|
var Help = `
|
|
|
|
### Server options
|
|
|
|
|
|
|
|
Use --addr to specify which IP address and port the server should
|
|
|
|
listen on, eg --addr 1.2.3.4:8000 or --addr :8080 to listen to all
|
|
|
|
IPs. By default it only listens on localhost.
|
2018-02-15 05:06:34 +08:00
|
|
|
|
2018-02-15 07:14:14 +08:00
|
|
|
#### Authentication
|
|
|
|
|
|
|
|
By default this will serve files without needing a login.
|
|
|
|
|
|
|
|
You can either use an htpasswd file which can take lots of users, or
|
|
|
|
set a single username and password with the --user and --pass flags.
|
|
|
|
|
2018-02-15 05:06:34 +08:00
|
|
|
Use --htpasswd /path/to/htpasswd to provide an htpasswd file. This is
|
|
|
|
in standard apache format and supports MD5, SHA1 and BCrypt for basic
|
|
|
|
authentication. Bcrypt is recommended.
|
|
|
|
|
|
|
|
To create an htpasswd file:
|
|
|
|
|
|
|
|
touch htpasswd
|
|
|
|
htpasswd -B htpasswd user
|
|
|
|
htpasswd -B htpasswd anotherUser
|
2018-02-15 07:14:14 +08:00
|
|
|
|
|
|
|
Use --realm to set the authentication realm.`
|
2018-02-15 04:39:11 +08:00
|
|
|
|
|
|
|
// Server contains info about the running http server
|
|
|
|
type Server struct {
|
2018-02-15 07:14:14 +08:00
|
|
|
bindAddress string
|
|
|
|
httpServer *http.Server
|
|
|
|
basicUser string
|
|
|
|
basicPassHashed string
|
|
|
|
}
|
|
|
|
|
|
|
|
// singleUserProvider provides the encrypted password for a single user
|
|
|
|
func (s *Server) singleUserProvider(user, realm string) string {
|
|
|
|
if user == s.basicUser {
|
|
|
|
return s.basicPassHashed
|
|
|
|
}
|
|
|
|
return ""
|
2018-02-15 04:39:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer creates an http server
|
|
|
|
func NewServer(handler http.Handler) *Server {
|
2018-02-15 07:14:14 +08:00
|
|
|
s := &Server{
|
|
|
|
bindAddress: bindAddress,
|
|
|
|
}
|
|
|
|
|
2018-02-15 05:06:34 +08:00
|
|
|
// Use htpasswd if required on everything
|
2018-02-15 07:14:14 +08:00
|
|
|
if htPasswdFile != "" || basicUser != "" {
|
|
|
|
var secretProvider auth.SecretProvider
|
|
|
|
if htPasswdFile != "" {
|
|
|
|
fs.Infof(nil, "Using %q as htpasswd storage", htPasswdFile)
|
|
|
|
secretProvider = auth.HtpasswdFileProvider(htPasswdFile)
|
|
|
|
} else {
|
|
|
|
fs.Infof(nil, "Using --user %s --pass XXXX as authenticated user", basicUser)
|
|
|
|
s.basicUser = basicUser
|
|
|
|
s.basicPassHashed = string(auth.MD5Crypt([]byte(basicPass), []byte("dlPL2MqE"), []byte("$1$")))
|
|
|
|
secretProvider = s.singleUserProvider
|
|
|
|
}
|
|
|
|
authenticator := auth.NewBasicAuthenticator(realm, secretProvider)
|
2018-02-15 05:06:34 +08:00
|
|
|
handler = auth.JustCheck(authenticator, handler.ServeHTTP)
|
|
|
|
}
|
|
|
|
|
2018-02-15 04:39:11 +08:00
|
|
|
// FIXME make a transport?
|
|
|
|
s.httpServer = &http.Server{
|
|
|
|
Addr: s.bindAddress,
|
|
|
|
Handler: handler,
|
|
|
|
MaxHeaderBytes: 1 << 20,
|
|
|
|
}
|
|
|
|
// go version specific initialisation
|
|
|
|
initServer(s.httpServer)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBindAddress overrides the config flag
|
|
|
|
func (s *Server) SetBindAddress(addr string) {
|
|
|
|
s.bindAddress = addr
|
|
|
|
s.httpServer.Addr = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serve runs the server - doesn't return
|
|
|
|
func (s *Server) Serve() {
|
|
|
|
log.Fatal(s.httpServer.ListenAndServe())
|
|
|
|
}
|
|
|
|
|
|
|
|
// URL returns the serving address of this server
|
|
|
|
func (s *Server) URL() string {
|
|
|
|
return fmt.Sprintf("http://%s/", s.bindAddress)
|
|
|
|
}
|