89 lines
3.1 KiB
Go
89 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httputil"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const page = `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="robots" content="noindex"><title>Proxy Edge</title><style>body{margin:0;background:#0b1220;color:#dce7f7;font:16px system-ui,sans-serif}main{max-width:720px;margin:16vh auto;padding:32px}h1{font-size:42px;margin:0 0 12px;color:#fff}p{line-height:1.6;color:#9eb2cc}.badge{display:inline-block;padding:6px 10px;border:1px solid #274262;border-radius:999px;color:#86b7ea;font-size:13px}</style></head><body><main><span class="badge">Proxy Edge</span><h1>Private edge transport</h1><p>This endpoint provides resilient transport and availability services for authorized clients.</p></main></body></html>`
|
|
|
|
func requestHostname(hostport string) string {
|
|
host := strings.TrimSpace(hostport)
|
|
if parsed, _, err := net.SplitHostPort(host); err == nil {
|
|
return strings.ToLower(parsed)
|
|
}
|
|
return strings.ToLower(strings.Trim(host, "[]"))
|
|
}
|
|
|
|
func main() {
|
|
hostname := strings.ToLower(strings.TrimSpace(os.Getenv("TPROXY_HOSTNAME")))
|
|
if hostname == "" {
|
|
log.Fatal("TPROXY_HOSTNAME is required")
|
|
}
|
|
|
|
target, err := url.Parse("http://127.0.0.1:8080")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
proxy := &httputil.ReverseProxy{
|
|
Rewrite: func(request *httputil.ProxyRequest) {
|
|
request.SetURL(target)
|
|
request.Out.Host = request.In.Host
|
|
|
|
clientIP := strings.TrimSpace(request.In.Header.Get("X-Real-IP"))
|
|
if net.ParseIP(clientIP) == nil {
|
|
clientIP, _, _ = net.SplitHostPort(request.In.RemoteAddr)
|
|
}
|
|
request.Out.Header.Set("X-Real-IP", clientIP)
|
|
request.Out.Header.Set("X-Forwarded-For", clientIP)
|
|
if proto := request.In.Header.Get("X-Forwarded-Proto"); proto != "" {
|
|
request.Out.Header.Set("X-Forwarded-Proto", proto)
|
|
}
|
|
},
|
|
ErrorHandler: func(response http.ResponseWriter, _ *http.Request, proxyErr error) {
|
|
log.Printf("relay error: %v", proxyErr)
|
|
http.Error(response, "upstream unavailable", http.StatusBadGateway)
|
|
},
|
|
}
|
|
|
|
handler := http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
|
|
if requestHostname(request.Host) != hostname {
|
|
http.Error(response, "misdirected request", http.StatusMisdirectedRequest)
|
|
return
|
|
}
|
|
|
|
response.Header().Set("X-Content-Type-Options", "nosniff")
|
|
response.Header().Set("Referrer-Policy", "no-referrer")
|
|
|
|
staticPath := request.URL.Path == "/" ||
|
|
request.URL.Path == "/about" ||
|
|
request.URL.Path == "/privacy"
|
|
if request.Method == http.MethodGet && request.URL.RawQuery == "" && staticPath {
|
|
response.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
response.Header().Set("Cache-Control", "public, max-age=300")
|
|
_, _ = fmt.Fprint(response, page)
|
|
return
|
|
}
|
|
|
|
proxy.ServeHTTP(response, request)
|
|
})
|
|
|
|
server := &http.Server{
|
|
Addr: ":8088",
|
|
Handler: handler,
|
|
ReadHeaderTimeout: 10 * time.Second,
|
|
IdleTimeout: 90 * time.Second,
|
|
}
|
|
|
|
log.Printf("ingress listening on %s for %s", server.Addr, hostname)
|
|
log.Fatal(server.ListenAndServe())
|
|
}
|