diff --git a/.infra/group_vars/all/_frey.yml b/.infra/group_vars/all/_frey.yml new file mode 100644 index 0000000..9eb5f20 --- /dev/null +++ b/.infra/group_vars/all/_frey.yml @@ -0,0 +1,6 @@ +# Frey specific overrides +--- +# apt role +apt_manage_sources_list: true +apt_src_enable: false +apt_install_state: present diff --git a/README.md b/README.md index a5062b1..fe9651e 100644 --- a/README.md +++ b/README.md @@ -54,14 +54,17 @@ your own Go program: package main import ( + "fmt" + "net/http" + "github.com/tus/tusd" "github.com/tus/tusd/filestore" - "net/http" ) func main() { // Create a new FileStore instance which is responsible for // storing the uploaded file on disk in the specified directory. + // This path _must_ exist before tusd will store uploads in it. // If you want to save them on a different medium, for example // a remote FTP server, you can implement your own storage backend // by implementing the tusd.DataStore interface. @@ -79,22 +82,23 @@ func main() { // Create a new HTTP handler for the tusd server by providing a configuration. // The StoreComposer property must be set to allow the handler to function. handler, err := tusd.NewHandler(tusd.Config{ - BasePath: "files/", + BasePath: "/files/", StoreComposer: composer, }) if err != nil { - panic("Unable to create handler: %s", err) + panic(fmt.Errorf("Unable to create handler: %s", err)) } // Right now, nothing has happened since we need to start the HTTP server on // our own. In the end, tusd will start listening on and accept request at // http://localhost:8080/files - http.Handle("files/", http.StripPrefix("files/", handler)) + http.Handle("/files/", http.StripPrefix("/files/", handler)) err = http.ListenAndServe(":8080", nil) if err != nil { - panic("Unable to listen: %s", err) + panic(fmt.Errorf("Unable to listen: %s", err)) } } + ``` Please consult the [online documentation](https://godoc.org/github.com/tus/tusd) @@ -134,6 +138,23 @@ useful tools: go test -v ./... ``` +## FAQ + +### How can I access tusd using HTTPS? + +The tusd binary, once executed, listens on the provided port for only non-encrypted HTTP requests and *does not accept* HTTPS connections. This decision has been made to limit the functionality inside this repository which has to be developed, tested and maintained. If you want to send requests to tusd in a secure fashion - what we absolutely encourage, we recommend you to utilize a reverse proxy in front of tusd which accepts incoming HTTPS connections and forwards them to tusd using plain HTTP. More information about this topic, including sample configurations for Nginx and Apache, can be found in [issue #86](https://github.com/tus/tusd/issues/86#issuecomment-269569077). + +### Can I run tusd behind a reverse proxy? + +Yes, it is absolutely possible to do so. Firstly, you should execute the tusd binary using the `-behind-proxy` flag indicating it to pay attention to special headers which are only relevent when used in conjunction with a proxy. Furthermore, there are addtional details which should be kept in mind, depending on the used software: + +- *Disable request buffering.* Nginx, for example, reads the entire incoming HTTP request, including its body, before sending it to the backend, by default. This behavior defeats the purpose of resumability where an upload is processed while it's being transfered. Therefore, such as feature should be disabled. + +- *Adjust maximum request size.* Some proxies have default values for how big a request may be in order to protect your services. Be sure to check these settings to match the requirements of your application. + +- *Forward hostname and scheme.* If the proxy rewrites the request URL, the tusd server does not know the original URL which was used to reach the proxy. This behavior can lead to situations, where tusd returns a redirect to a URL which can not be reached by the client. To avoid this confusion, you can explicitly tell tusd which hostname and scheme to use by supplying the `X-Forwarded-Host` and `X-Forwarded-Proto` headers. + +Explicit examples for the above points can be found in the [Nginx configuration](https://github.com/tus/tusd/blob/master/.infra/files/nginx.conf) which is used to power the [master.tus.io](https://master.tus.io) instace. ## License This project is licensed under the MIT license, see `LICENSE.txt`. diff --git a/cmd/tusd/cli/listener.go b/cmd/tusd/cli/listener.go index 4438c19..054a39f 100644 --- a/cmd/tusd/cli/listener.go +++ b/cmd/tusd/cli/listener.go @@ -37,6 +37,11 @@ type Conn struct { net.Conn ReadTimeout time.Duration WriteTimeout time.Duration + + // closeRecorded will be true if the connection has been closed and the + // corresponding prometheus counter has been decremented. It will be used to + // avoid duplicated modifications to this metric. + closeRecorded bool } func (c *Conn) Read(b []byte) (int, error) { @@ -70,7 +75,13 @@ func (c *Conn) Write(b []byte) (int, error) { } func (c *Conn) Close() error { - go MetricsOpenConnections.Dec() + // Only decremented the prometheus counter if the Close function has not been + // invoked before to avoid duplicated modifications. + if !c.closeRecorded { + c.closeRecorded = true + MetricsOpenConnections.Dec() + } + return c.Conn.Close() } diff --git a/filestore/filestore.go b/filestore/filestore.go index ad52a7e..5808cac 100644 --- a/filestore/filestore.go +++ b/filestore/filestore.go @@ -171,7 +171,7 @@ func (store FileStore) UnlockUpload(id string) error { // newLock contructs a new Lockfile instance. func (store FileStore) newLock(id string) (lockfile.Lockfile, error) { - path, err := filepath.Abs(store.Path + "/" + id + ".lock") + path, err := filepath.Abs(filepath.Join(store.Path, id+".lock")) if err != nil { return lockfile.Lockfile(""), err } @@ -184,12 +184,12 @@ func (store FileStore) newLock(id string) (lockfile.Lockfile, error) { // binPath returns the path to the .bin storing the binary data. func (store FileStore) binPath(id string) string { - return store.Path + "/" + id + ".bin" + return filepath.Join(store.Path, id+".bin") } // infoPath returns the path to the .info file storing the file's info. func (store FileStore) infoPath(id string) string { - return store.Path + "/" + id + ".info" + return filepath.Join(store.Path, id+".info") } // writeInfo updates the entire information. Everything will be overwritten.