diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..c15838d --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,3 @@ +# Contributing + +under development diff --git a/README.md b/README.md index fa36698..ae085cb 100644 --- a/README.md +++ b/README.md @@ -1 +1,46 @@ -# oscdn +# oscdn - Open Source Content Delivery Network + +[![Go Reference](https://pkg.go.dev/badge/github.com/orkunkaraduman/oscdn.svg)](https://pkg.go.dev/github.com/orkunkaraduman/oscdn) +[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=orkunkaraduman_oscdn&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=orkunkaraduman_oscdn) + +oscdn is an open-source Content Delivery Network (CDN) software designed to optimize website performance by efficiently +delivering static assets to users worldwide. It supports HTTP/2, partial content serving, and rate limiting, making it +a powerful and versatile solution for content delivery needs. + +## Features + +- HTTP/2 Support: oscdn leverages the latest HTTP/2 protocol to improve the loading speed and performance of websites, +reducing latency and increasing concurrent connections. + +- Partial Content Serving: oscdn allows clients to request only parts of a resource, making it ideal for large files +like videos, audio, or images. This feature enhances the overall user experience and saves bandwidth. + +- Rate Limiting: Control and manage traffic flow with rate limiting. oscdn provides configurable rate limits to prevent +abuse and ensure fair usage of resources. + +## Getting Started + +Follow the steps below to get oscdn up and running on your server: + +1. Prerequisites: Ensure you have go with version 1.20 installed on your system. + +2. Installation: Install oscdn by running the following command: `go get github.com/orkunkaraduman/oscdn@latest` + +3. Configuration: You can view command-line flags and `config.yaml` at this time. + +4. Start the Server: Launch the oscdn server by running the following command: `oscdn --store-path=/example/store/path` + +## Contributing + +We welcome contributions from the community to improve and expand oscdn's capabilities. If you find a bug, have a +feature request, or want to contribute code, please follow our guidelines for contributing (CONTRIBUTING.md) and submit +a pull request. + +## License + +oscdn is open-source software released under the [BSD 3-Clause License](https://opensource.org/licenses/BSD-3-Clause). + +## Acknowledgments + +We would like to thank the open-source community and the developers of the libraries and tools that oscdn depends on. +Your contributions help make oscdn a reliable and powerful CDN solution. diff --git a/cdn/handler.go b/cdn/handler.go index bb6ebec..47ce969 100644 --- a/cdn/handler.go +++ b/cdn/handler.go @@ -21,6 +21,7 @@ import ( type Handler struct { Logger *logng.Logger Store *store.Store + ServerHeader string GetHostConfig func(scheme, host string) *HostConfig } @@ -50,6 +51,11 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) { return } + w.Header().Set("Server", "oscdn") + if h.ServerHeader != "" { + w.Header().Set("Server", h.ServerHeader) + } + if (req.URL.Scheme != "http" && req.URL.Scheme != "https") || req.URL.Opaque != "" || req.URL.User != nil || @@ -73,6 +79,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) { contentRange, err := getContentRange(req.Header) if err != nil { + err = fmt.Errorf("invalid content range: %w", err) logger.V(1).Error(err) http.Error(w, "invalid content range", http.StatusBadRequest) return @@ -106,7 +113,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) { if hostConfig.HttpsRedirectPort > 0 && hostConfig.HttpsRedirectPort != 443 { storeURL.Host = fmt.Sprintf("%s:%d", domain, hostConfig.HttpsRedirectPort) } - //http.Redirect(w, req, storeURL.String(), http.StatusFound) + w.Header().Set("Location", storeURL.String()) http.Error(w, http.StatusText(http.StatusFound), http.StatusFound) return } diff --git a/config.yaml b/config.yaml index 431769d..7f2a91f 100644 --- a/config.yaml +++ b/config.yaml @@ -2,7 +2,7 @@ origins: speed.hetzner.de: useHttps: true maxSize: 0 - maxAge: 60s + maxAge: 5m downloadBurst: 0 downloadRate: 0 diff --git a/internal/flags/flags.go b/internal/flags/flags.go index 835df7a..44b0e92 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -17,6 +17,7 @@ type _Flags struct { StorePath string `default:""` MaxIdleConns int `default:"100"` UserAgent string `default:"oscdn"` + ServerHeader string `default:"oscdn"` Http string `default:":8080"` Https string `default:":8443"` Mgmt string `default:":8000"` diff --git a/main.go b/main.go index 8c98ef8..20cc529 100644 --- a/main.go +++ b/main.go @@ -121,8 +121,9 @@ func main() { }(_store) handler := &cdn.Handler{ - Logger: logng.WithFieldKeyVals("logger", "cdn handler"), - Store: _store, + Logger: logng.WithFieldKeyVals("logger", "cdn handler"), + Store: _store, + ServerHeader: flags.Flags.ServerHeader, GetHostConfig: func(scheme, host string) *cdn.HostConfig { domain, _, _ := httputil.SplitHostPort(host) d, ok := _config.Domains[domain]