This commit is contained in:
parent
7d40f898a6
commit
40436dcf92
1 changed files with 40 additions and 11 deletions
51
main.go
51
main.go
|
@ -12,6 +12,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
@ -251,6 +252,12 @@ func root(w http.ResponseWriter, req *http.Request) {
|
||||||
// CustomHandler wraps the default promhttp.Handler with custom logic
|
// CustomHandler wraps the default promhttp.Handler with custom logic
|
||||||
func metricsHandler() http.Handler {
|
func metricsHandler() http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
// To prevent accessing from the bare IP address
|
||||||
|
if req.Host == "" || net.ParseIP(strings.Split(req.Host, ":")[0]) != nil {
|
||||||
|
w.WriteHeader(444)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
metrics.Uptime.Set(float64(time.Duration(time.Since(programInit).Seconds())))
|
metrics.Uptime.Set(float64(time.Duration(time.Since(programInit).Seconds())))
|
||||||
promhttp.Handler().ServeHTTP(w, req)
|
promhttp.Handler().ServeHTTP(w, req)
|
||||||
})
|
})
|
||||||
|
@ -296,10 +303,30 @@ func requestPerMinute() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func beforeAll(next http.HandlerFunc) http.HandlerFunc {
|
func beforeMisc(next http.HandlerFunc) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, req *http.Request) {
|
return func(w http.ResponseWriter, req *http.Request) {
|
||||||
defer panicHandler(w)
|
defer panicHandler(w)
|
||||||
|
|
||||||
|
// To prevent accessing from the bare IP address
|
||||||
|
if req.Host == "" || net.ParseIP(strings.Split(req.Host, ":")[0]) != nil {
|
||||||
|
w.WriteHeader(444)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
next(w, req)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func beforeProxy(next http.HandlerFunc) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
defer panicHandler(w)
|
||||||
|
|
||||||
|
// To prevent accessing from the bare IP address
|
||||||
|
if req.Host == "" || net.ParseIP(strings.Split(req.Host, ":")[0]) != nil {
|
||||||
|
w.WriteHeader(444)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if h3s {
|
if h3s {
|
||||||
w.Header().Set("Alt-Svc", "h3=\":8443\"; ma=86400")
|
w.Header().Set("Alt-Svc", "h3=\":8443\"; ma=86400")
|
||||||
}
|
}
|
||||||
|
@ -378,9 +405,10 @@ func main() {
|
||||||
|
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
mux.HandleFunc("/", root)
|
// MISC ROUTES
|
||||||
mux.HandleFunc("/health", health)
|
mux.HandleFunc("/", beforeMisc(root))
|
||||||
mux.HandleFunc("/stats", stats)
|
mux.HandleFunc("/health", beforeMisc(health))
|
||||||
|
mux.HandleFunc("/stats", beforeMisc(stats))
|
||||||
|
|
||||||
prometheus.MustRegister(metrics.Uptime)
|
prometheus.MustRegister(metrics.Uptime)
|
||||||
prometheus.MustRegister(metrics.ActiveConnections)
|
prometheus.MustRegister(metrics.ActiveConnections)
|
||||||
|
@ -396,13 +424,14 @@ func main() {
|
||||||
|
|
||||||
mux.Handle("/metrics", metricsHandler())
|
mux.Handle("/metrics", metricsHandler())
|
||||||
|
|
||||||
mux.HandleFunc("/videoplayback", beforeAll(videoplayback))
|
// PROXY ROUTES
|
||||||
mux.HandleFunc("/vi/", beforeAll(vi))
|
mux.HandleFunc("/videoplayback", beforeProxy(videoplayback))
|
||||||
mux.HandleFunc("/vi_webp/", beforeAll(vi))
|
mux.HandleFunc("/vi/", beforeProxy(vi))
|
||||||
mux.HandleFunc("/sb/", beforeAll(vi))
|
mux.HandleFunc("/vi_webp/", beforeProxy(vi))
|
||||||
mux.HandleFunc("/ggpht/", beforeAll(ggpht))
|
mux.HandleFunc("/sb/", beforeProxy(vi))
|
||||||
mux.HandleFunc("/a/", beforeAll(ggpht))
|
mux.HandleFunc("/ggpht/", beforeProxy(ggpht))
|
||||||
mux.HandleFunc("/ytc/", beforeAll(ggpht))
|
mux.HandleFunc("/a/", beforeProxy(ggpht))
|
||||||
|
mux.HandleFunc("/ytc/", beforeProxy(ggpht))
|
||||||
|
|
||||||
go requestPerSecond()
|
go requestPerSecond()
|
||||||
go requestPerMinute()
|
go requestPerMinute()
|
||||||
|
|
Loading…
Reference in a new issue