2018-02-14 20:39:11 +00:00
|
|
|
// Package httplib provides common functionality for http servers
|
|
|
|
package httplib
|
|
|
|
|
|
|
|
import (
|
2018-02-15 15:01:19 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2018-02-14 20:39:11 +00:00
|
|
|
"fmt"
|
2018-02-15 15:01:19 +00:00
|
|
|
"io/ioutil"
|
2018-02-14 20:39:11 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2018-02-15 15:01:19 +00:00
|
|
|
"time"
|
2018-02-14 20:39:11 +00:00
|
|
|
|
2018-02-14 21:06:34 +00:00
|
|
|
auth "github.com/abbot/go-http-auth"
|
|
|
|
"github.com/ncw/rclone/fs"
|
2018-02-14 20:39:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Globals
|
2018-02-15 15:01:19 +00:00
|
|
|
var ()
|
2018-02-14 20:39:11 +00: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-14 21:06:34 +00:00
|
|
|
|
2018-02-15 15:01:19 +00:00
|
|
|
--server-read-timeout and --server-write-timeout can be used to
|
|
|
|
control the timeouts on the server. Note that this is the total time
|
|
|
|
for a transfer.
|
|
|
|
|
|
|
|
--max-header-bytes controls the maximum number of bytes the server will
|
|
|
|
accept in the HTTP header.
|
|
|
|
|
2018-02-14 23:14:14 +00: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-14 21:06:34 +00: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-14 23:14:14 +00:00
|
|
|
|
2018-02-15 15:01:19 +00:00
|
|
|
The password file can be updated while rclone is running.
|
|
|
|
|
|
|
|
Use --realm to set the authentication realm.
|
|
|
|
|
|
|
|
#### SSL/TLS
|
|
|
|
|
|
|
|
By default this will serve over http. If you want you can serve over
|
|
|
|
https. You will need to supply the --ssl along with --cert and --key.
|
|
|
|
If you wish to do client side certificate validation then you will
|
|
|
|
need to supply --client-ca also.
|
|
|
|
`
|
|
|
|
|
|
|
|
// Options contains options for the http Server
|
|
|
|
type Options struct {
|
|
|
|
ListenAddr string // Port to listen on
|
|
|
|
ServerReadTimeout time.Duration // Timeout for server reading data
|
|
|
|
ServerWriteTimeout time.Duration // Timeout for server writing data
|
|
|
|
MaxHeaderBytes int // Maximum size of request header
|
|
|
|
SslCert string // SSL PEM key (concatenation of certificate and CA certificate)
|
|
|
|
SslKey string // SSL PEM Private key
|
|
|
|
ClientCA string // Client certificate authority to verify clients with
|
|
|
|
HtPasswd string // htpasswd file - if not provided no authentication is done
|
|
|
|
Realm string // realm for authentication
|
|
|
|
BasicUser string // single username for basic auth if not using Htpasswd
|
|
|
|
BasicPass string // password for BasicUser
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultOpt is the default values used for Options
|
|
|
|
var DefaultOpt = Options{
|
|
|
|
ListenAddr: "localhost:8080",
|
|
|
|
Realm: "rclone",
|
|
|
|
ServerReadTimeout: 1 * time.Hour,
|
|
|
|
ServerWriteTimeout: 1 * time.Hour,
|
|
|
|
MaxHeaderBytes: 4096,
|
|
|
|
}
|
2018-02-14 20:39:11 +00:00
|
|
|
|
|
|
|
// Server contains info about the running http server
|
|
|
|
type Server struct {
|
2018-02-15 15:01:19 +00:00
|
|
|
Opt Options
|
|
|
|
handler http.Handler // original handler
|
2018-02-14 23:14:14 +00:00
|
|
|
httpServer *http.Server
|
|
|
|
basicPassHashed string
|
2018-02-15 15:01:19 +00:00
|
|
|
useSSL bool // if server is configured for SSL/TLS
|
2018-02-14 23:14:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// singleUserProvider provides the encrypted password for a single user
|
|
|
|
func (s *Server) singleUserProvider(user, realm string) string {
|
2018-02-15 15:01:19 +00:00
|
|
|
if user == s.Opt.BasicUser {
|
2018-02-14 23:14:14 +00:00
|
|
|
return s.basicPassHashed
|
|
|
|
}
|
|
|
|
return ""
|
2018-02-14 20:39:11 +00:00
|
|
|
}
|
|
|
|
|
2018-02-15 15:01:19 +00:00
|
|
|
// NewServer creates an http server. The opt can be nil in which case
|
|
|
|
// the default options will be used.
|
|
|
|
func NewServer(handler http.Handler, opt *Options) *Server {
|
2018-02-14 23:14:14 +00:00
|
|
|
s := &Server{
|
2018-02-15 15:01:19 +00:00
|
|
|
handler: handler,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a copy of the options
|
|
|
|
if opt != nil {
|
|
|
|
s.Opt = *opt
|
|
|
|
} else {
|
|
|
|
s.Opt = DefaultOpt
|
2018-02-14 23:14:14 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 21:06:34 +00:00
|
|
|
// Use htpasswd if required on everything
|
2018-02-15 15:01:19 +00:00
|
|
|
if s.Opt.HtPasswd != "" || s.Opt.BasicUser != "" {
|
2018-02-14 23:14:14 +00:00
|
|
|
var secretProvider auth.SecretProvider
|
2018-02-15 15:01:19 +00:00
|
|
|
if s.Opt.HtPasswd != "" {
|
|
|
|
fs.Infof(nil, "Using %q as htpasswd storage", s.Opt.HtPasswd)
|
|
|
|
secretProvider = auth.HtpasswdFileProvider(s.Opt.HtPasswd)
|
2018-02-14 23:14:14 +00:00
|
|
|
} else {
|
2018-02-15 15:01:19 +00:00
|
|
|
fs.Infof(nil, "Using --user %s --pass XXXX as authenticated user", s.Opt.BasicUser)
|
|
|
|
s.basicPassHashed = string(auth.MD5Crypt([]byte(s.Opt.BasicPass), []byte("dlPL2MqE"), []byte("$1$")))
|
2018-02-14 23:14:14 +00:00
|
|
|
secretProvider = s.singleUserProvider
|
|
|
|
}
|
2018-02-15 15:01:19 +00:00
|
|
|
authenticator := auth.NewBasicAuthenticator(s.Opt.Realm, secretProvider)
|
2018-02-14 21:06:34 +00:00
|
|
|
handler = auth.JustCheck(authenticator, handler.ServeHTTP)
|
|
|
|
}
|
|
|
|
|
2018-02-15 15:01:19 +00:00
|
|
|
s.useSSL = s.Opt.SslKey != ""
|
|
|
|
if (s.Opt.SslCert != "") != s.useSSL {
|
|
|
|
log.Fatalf("Need both -cert and -key to use SSL")
|
|
|
|
}
|
|
|
|
|
2018-02-14 20:39:11 +00:00
|
|
|
// FIXME make a transport?
|
|
|
|
s.httpServer = &http.Server{
|
2018-02-15 15:01:19 +00:00
|
|
|
Addr: s.Opt.ListenAddr,
|
2018-02-14 20:39:11 +00:00
|
|
|
Handler: handler,
|
2018-02-15 15:01:19 +00:00
|
|
|
ReadTimeout: s.Opt.ServerReadTimeout,
|
|
|
|
WriteTimeout: s.Opt.ServerWriteTimeout,
|
|
|
|
MaxHeaderBytes: s.Opt.MaxHeaderBytes,
|
|
|
|
TLSConfig: &tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS10, // disable SSL v3.0 and earlier
|
|
|
|
},
|
2018-02-14 20:39:11 +00:00
|
|
|
}
|
|
|
|
// go version specific initialisation
|
|
|
|
initServer(s.httpServer)
|
|
|
|
|
2018-02-15 15:01:19 +00:00
|
|
|
if s.Opt.ClientCA != "" {
|
|
|
|
if !s.useSSL {
|
|
|
|
log.Fatalf("Can't use --client-ca without --cert and --key")
|
|
|
|
}
|
|
|
|
certpool := x509.NewCertPool()
|
|
|
|
pem, err := ioutil.ReadFile(s.Opt.ClientCA)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to read client certificate authority: %v", err)
|
|
|
|
}
|
|
|
|
if !certpool.AppendCertsFromPEM(pem) {
|
|
|
|
log.Fatalf("Can't parse client certificate authority")
|
|
|
|
}
|
|
|
|
s.httpServer.TLSConfig.ClientCAs = certpool
|
|
|
|
s.httpServer.TLSConfig.ClientAuth = tls.RequireAndVerifyClientCert
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
2018-02-14 20:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Serve runs the server - doesn't return
|
|
|
|
func (s *Server) Serve() {
|
2018-02-15 15:01:19 +00:00
|
|
|
var err error
|
|
|
|
if s.useSSL {
|
|
|
|
err = s.httpServer.ListenAndServeTLS(s.Opt.SslCert, s.Opt.SslKey)
|
|
|
|
} else {
|
|
|
|
err = s.httpServer.ListenAndServe()
|
|
|
|
}
|
|
|
|
log.Fatalf("Fatal error while serving HTTP: %v", err)
|
2018-02-14 20:39:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// URL returns the serving address of this server
|
|
|
|
func (s *Server) URL() string {
|
2018-02-15 15:01:19 +00:00
|
|
|
proto := "http"
|
|
|
|
if s.useSSL {
|
|
|
|
proto = "https"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s://%s/", proto, s.Opt.ListenAddr)
|
2018-02-14 20:39:11 +00:00
|
|
|
}
|