1
0
Fork 0
photos/cmd/proxy/proxy.go

107 lines
2.3 KiB
Go

// This is honestly terrible. It only exists to replace the AWS S3 website hosting feature missing in MinIO
package main
import (
"log"
"net"
"net/http"
"net/http/httputil"
"os"
"strings"
"time"
"github.com/miekg/dns"
)
var endpoint string
var endpointSecure bool
var domain string
var behindProxy bool
func main() {
// Read configuration
endpoint = os.Getenv("MINIO_ENDPOINT")
endpointSecure = os.Getenv("MINIO_ENDPOINT_SECURE") == "true"
domain = os.Getenv("MINIO_DOMAIN")
behindProxy = os.Getenv("BEHIND_PROXY") == "true"
transport := &http.Transport{
MaxIdleConns: 4,
MaxIdleConnsPerHost: 4,
IdleConnTimeout: 60 * time.Second,
DisableCompression: true,
}
http.DefaultClient = &http.Client{
Transport: transport,
Timeout: 60 * time.Second,
}
server := &http.Server{
Addr: ":80",
ReadTimeout: 5 * time.Second,
WriteTimeout: 60 * time.Second,
Handler: &httputil.ReverseProxy{Director: director},
}
err := server.ListenAndServe()
if err != nil {
panic(err)
}
}
func director(req *http.Request) {
// Validate host
host, port, err := net.SplitHostPort(req.Host)
if err != nil {
// Assumption: IsDomainName will handle other cases
host = req.Host
}
if _, ok := dns.IsDomainName(host); !ok {
req.URL.Scheme = ""
return
}
// Rewrite URL
req.URL.Scheme = "http"
if endpointSecure {
req.URL.Scheme = "https"
}
req.URL.Host = endpoint
req.URL.Path = pathFromHost(mapPath(*req), host)
// Prevent MINIO from issuing redirects
userAgent := req.Header.Get("User-Agent")
userAgent = strings.ReplaceAll(userAgent, "Mozilla", "M-o-z-i-l-l-a")
req.Header.Set("User-Agent", userAgent)
if !behindProxy {
// Clear existing unsafe headers
req.Header.Del("Forwarded")
req.Header.Del("X-Forwarded-For")
// Unnecessary, but might as well
req.Header.Set("X-Forwarded-Proto", req.URL.Scheme)
req.Header.Set("X-Forwarded-Host", host)
req.Header.Set("X-Forwarded-Port", port)
}
log.Println(req.Header.Get("Host"), req.URL)
}
func mapPath(req http.Request) string {
path := req.URL.Path
if req.FormValue("list-type") == "2" {
return path
}
if strings.HasSuffix(path, "/") {
path += "index.html"
}
return path
}
func pathFromHost(path, host string) string {
if len(host) > 0 {
return "/" + host + path
}
return path
}