2022-11-18 02:54:37 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-02-16 00:27:36 +00:00
|
|
|
"log"
|
2022-11-18 02:54:37 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
2023-02-16 00:27:36 +00:00
|
|
|
"net/url"
|
2022-11-18 02:54:37 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-02-16 00:27:36 +00:00
|
|
|
func proxyTo(host string) *httputil.ReverseProxy {
|
|
|
|
rp := httputil.NewSingleHostReverseProxy(&url.URL{
|
|
|
|
Scheme: "http",
|
|
|
|
Host: host,
|
|
|
|
RawQuery: "",
|
|
|
|
})
|
|
|
|
rp.ModifyResponse = logTimeElapsed
|
|
|
|
return rp
|
|
|
|
}
|
2022-11-18 02:54:37 +00:00
|
|
|
|
2022-12-07 11:26:00 +00:00
|
|
|
// todo: this shouldn't be in this repo
|
2022-11-18 02:54:37 +00:00
|
|
|
var remotes = map[string]*httputil.ReverseProxy{
|
2023-03-09 17:06:30 +00:00
|
|
|
"api.pluralkit.me": proxyTo("[fdaa:0:ae33:a7b:8dd7:0:a:902]:5000"),
|
|
|
|
"dash.pluralkit.me": proxyTo("[fdaa:0:ae33:a7b:8dd7:0:a:902]:8080"),
|
|
|
|
"sentry.pluralkit.me": proxyTo("[fdaa:0:ae33:a7b:8dd7:0:a:902]:9000"),
|
|
|
|
"grafana.pluralkit.me": proxyTo("[fdaa:0:ae33:a7b:8dd7:0:a:802]:3000"),
|
2022-11-18 02:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ProxyHandler struct{}
|
|
|
|
|
|
|
|
func (p ProxyHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
remote, ok := remotes[r.Host]
|
|
|
|
if !ok {
|
|
|
|
// unknown domains redirect to landing page
|
|
|
|
http.Redirect(rw, r, "https://pluralkit.me", http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
r = r.WithContext(context.WithValue(r.Context(), "req-time", startTime))
|
|
|
|
|
|
|
|
remote.ServeHTTP(rw, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func logTimeElapsed(resp *http.Response) error {
|
|
|
|
r := resp.Request
|
|
|
|
|
|
|
|
startTime := r.Context().Value("req-time").(time.Time)
|
|
|
|
|
|
|
|
elapsed := time.Since(startTime)
|
|
|
|
|
2023-02-16 00:27:36 +00:00
|
|
|
log.Printf("[%s] \"%s %s%s\" %d - %vms %s\n", r.Header.Get("Fly-Client-IP"), r.Method, r.Host, r.URL.Path, resp.StatusCode, elapsed.Milliseconds(), r.Header.Get("User-Agent"))
|
2022-11-18 02:54:37 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
http.ListenAndServe(":8080", ProxyHandler{})
|
|
|
|
}
|