2020-06-14 18:40:55 +08:00
|
|
|
// This is honestly terrible. It only exists to replace the AWS S3 website hosting feature missing in MinIO
|
2020-05-31 03:13:22 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2020-05-31 14:15:24 +08:00
|
|
|
"net"
|
2020-05-31 03:13:22 +08:00
|
|
|
"net/http"
|
2020-05-31 14:15:24 +08:00
|
|
|
"net/http/httputil"
|
2020-05-31 03:13:22 +08:00
|
|
|
"os"
|
2020-05-31 12:38:15 +08:00
|
|
|
"strings"
|
2020-05-31 03:13:22 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
var endpoint string
|
|
|
|
var endpointSecure bool
|
2020-05-31 18:24:35 +08:00
|
|
|
var domain string
|
2020-05-31 12:38:15 +08:00
|
|
|
var behindProxy bool
|
2020-05-31 03:13:22 +08:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
// Read configuration
|
|
|
|
endpoint = os.Getenv("MINIO_ENDPOINT")
|
|
|
|
endpointSecure = os.Getenv("MINIO_ENDPOINT_SECURE") == "true"
|
2020-05-31 18:24:35 +08:00
|
|
|
domain = os.Getenv("MINIO_DOMAIN")
|
2020-05-31 12:38:15 +08:00
|
|
|
behindProxy = os.Getenv("BEHIND_PROXY") == "true"
|
2020-05-31 03:13:22 +08:00
|
|
|
|
2020-06-14 18:40:55 +08:00
|
|
|
transport := &http.Transport{
|
|
|
|
MaxIdleConns: 4,
|
|
|
|
MaxIdleConnsPerHost: 4,
|
2020-06-14 19:25:24 +08:00
|
|
|
IdleConnTimeout: 60 * time.Second,
|
2020-06-14 18:40:55 +08:00
|
|
|
DisableCompression: true,
|
|
|
|
}
|
|
|
|
http.DefaultClient = &http.Client{
|
|
|
|
Transport: transport,
|
2020-06-14 19:25:24 +08:00
|
|
|
Timeout: 60 * time.Second,
|
2020-06-14 18:40:55 +08:00
|
|
|
}
|
|
|
|
|
2020-05-31 03:13:22 +08:00
|
|
|
server := &http.Server{
|
|
|
|
Addr: ":80",
|
|
|
|
ReadTimeout: 5 * time.Second,
|
2020-06-14 19:25:24 +08:00
|
|
|
WriteTimeout: 60 * time.Second,
|
2020-05-31 14:15:24 +08:00
|
|
|
Handler: &httputil.ReverseProxy{Director: director},
|
2020-05-31 03:13:22 +08:00
|
|
|
}
|
|
|
|
err := server.ListenAndServe()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 14:15:24 +08:00
|
|
|
func director(req *http.Request) {
|
2020-05-31 12:38:15 +08:00
|
|
|
// Validate host
|
2020-05-31 14:15:24 +08:00
|
|
|
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 = ""
|
2020-05-31 03:13:22 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-31 12:38:15 +08:00
|
|
|
// Rewrite URL
|
2020-05-31 14:15:24 +08:00
|
|
|
req.URL.Scheme = "http"
|
2020-05-31 03:13:22 +08:00
|
|
|
if endpointSecure {
|
2020-05-31 14:15:24 +08:00
|
|
|
req.URL.Scheme = "https"
|
2020-05-31 03:13:22 +08:00
|
|
|
}
|
2020-05-31 14:15:24 +08:00
|
|
|
req.URL.Host = endpoint
|
2020-06-14 18:40:55 +08:00
|
|
|
req.URL.Path = pathFromHost(mapPath(*req), host)
|
2020-05-31 18:24:35 +08:00
|
|
|
|
|
|
|
// 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)
|
2020-05-31 03:13:22 +08:00
|
|
|
|
2020-05-31 14:15:24 +08:00
|
|
|
if !behindProxy {
|
|
|
|
// Clear existing unsafe headers
|
|
|
|
req.Header.Del("Forwarded")
|
|
|
|
req.Header.Del("X-Forwarded-For")
|
2020-05-31 03:13:22 +08:00
|
|
|
|
2020-05-31 14:15:24 +08:00
|
|
|
// 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)
|
2020-05-31 03:13:22 +08:00
|
|
|
}
|
|
|
|
|
2020-05-31 18:24:35 +08:00
|
|
|
log.Println(req.Header.Get("Host"), req.URL)
|
2020-05-31 12:38:15 +08:00
|
|
|
}
|
|
|
|
|
2020-05-31 18:24:35 +08:00
|
|
|
func mapPath(req http.Request) string {
|
|
|
|
path := req.URL.Path
|
|
|
|
if req.FormValue("list-type") == "2" {
|
|
|
|
return path
|
|
|
|
}
|
2020-05-31 12:38:15 +08:00
|
|
|
if strings.HasSuffix(path, "/") {
|
|
|
|
path += "index.html"
|
|
|
|
}
|
2020-05-31 18:24:35 +08:00
|
|
|
return path
|
2020-05-31 12:38:15 +08:00
|
|
|
}
|
2020-06-14 18:40:55 +08:00
|
|
|
|
|
|
|
func pathFromHost(path, host string) string {
|
|
|
|
if len(host) > 0 {
|
|
|
|
return "/" + host + path
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|