diff --git a/cmd/http3-ytproxy/main.go b/cmd/http3-ytproxy/main.go index 0080ef7..05fc476 100644 --- a/cmd/http3-ytproxy/main.go +++ b/cmd/http3-ytproxy/main.go @@ -12,7 +12,6 @@ import ( "runtime" "strconv" "strings" - "sync/atomic" "syscall" "time" @@ -31,12 +30,12 @@ var ( rl = flag.Int("r", 8000, "Read limit in Kbps") ) -var version string - var h3s bool var domain_only_access bool = false +var version string + type ConnectionWatcher struct { totalEstablished int64 established int64 @@ -45,60 +44,23 @@ type ConnectionWatcher struct { } // https://stackoverflow.com/questions/51317122/how-to-get-number-of-idle-and-active-connections-in-go - // OnStateChange records open connections in response to connection // state changes. Set net/http Server.ConnState to this method // as value. func (cw *ConnectionWatcher) OnStateChange(conn net.Conn, state http.ConnState) { switch state { case http.StateNew: - atomic.AddInt64(&stats_.EstablishedConnections, 1) metrics.Metrics.EstablishedConnections.Inc() - atomic.AddInt64(&stats_.TotalConnEstablished, 1) metrics.Metrics.TotalConnEstablished.Inc() // case http.StateActive: // atomic.AddInt64(&cw.active, 1) case http.StateClosed, http.StateHijacked: - atomic.AddInt64(&stats_.EstablishedConnections, -1) metrics.Metrics.EstablishedConnections.Dec() } } -// // Count returns the number of connections at the time -// // the call. -// func (cw *ConnectionWatcher) Count() int { -// return int(atomic.LoadInt64(&cw.n)) -// } - -// // Add adds c to the number of active connections. -// func (cw *ConnectionWatcher) Add(c int64) { -// atomic.AddInt64(&cw.n, c) -// } - var cw ConnectionWatcher -func requestPerSecond() { - var last int64 - for { - time.Sleep(1 * time.Second) - current := stats_.RequestCount - stats_.RequestPerSecond = current - last - metrics.Metrics.RequestPerSecond.Set(float64(stats_.RequestPerSecond)) - last = current - } -} - -func requestPerMinute() { - var last int64 - for { - time.Sleep(60 * time.Second) - current := stats_.RequestCount - stats_.RequestPerMinute = current - last - metrics.Metrics.RequestPerMinute.Set(float64(stats_.RequestPerMinute)) - last = current - } -} - var tx uint64 func blockCheckerCalc(p *procfs.Proc) { @@ -196,7 +158,6 @@ func beforeProxy(next http.HandlerFunc) http.HandlerFunc { return } - atomic.AddInt64(&stats_.RequestCount, 1) metrics.Metrics.RequestCount.Inc() next(w, req) } @@ -297,7 +258,6 @@ func main() { // MISC ROUTES mux.HandleFunc("/", beforeMisc(paths.Root)) mux.HandleFunc("/health", beforeMisc(paths.Health)) - mux.HandleFunc("/stats", beforeMisc(paths.Stats)) metrics.Register() @@ -312,8 +272,6 @@ func main() { mux.HandleFunc("/a/", beforeProxy(paths.Ggpht)) mux.HandleFunc("/ytc/", beforeProxy(paths.Ggpht)) - go requestPerSecond() - go requestPerMinute() if bc { num, err := strconv.Atoi(bc_cooldown) if err != nil { diff --git a/internal/metrics/stats.go b/internal/metrics/stats.go deleted file mode 100644 index 74d8b02..0000000 --- a/internal/metrics/stats.go +++ /dev/null @@ -1,52 +0,0 @@ -package metrics - -import ( - "runtime" - "time" -) - -type statusJson struct { - Version string `json:"version"` - Uptime time.Duration `json:"uptime"` - RequestCount int64 `json:"requestCount"` - RequestPerSecond int64 `json:"requestPerSecond"` - RequestPerMinute int64 `json:"requestPerMinute"` - TotalConnEstablished int64 `json:"totalEstablished"` - EstablishedConnections int64 `json:"establishedConnections"` - ActiveConnections int64 `json:"activeConnections"` - IdleConnections int64 `json:"idleConnections"` - RequestsForbiddenPerSec struct { - Videoplayback int64 `json:"videoplayback"` - } - RequestsForbidden struct { - Videoplayback int64 `json:"videoplayback"` - Vi int64 `json:"vi"` - Ggpht int64 `json:"ggpht"` - } `json:"requestsForbidden"` -} - -var stats_ = statusJson{ - Version: version + "-" + runtime.GOARCH, - Uptime: 0, - RequestCount: 0, - RequestPerSecond: 0, - RequestPerMinute: 0, - TotalConnEstablished: 0, - EstablishedConnections: 0, - ActiveConnections: 0, - IdleConnections: 0, - RequestsForbiddenPerSec: struct { - Videoplayback int64 `json:"videoplayback"` - }{ - Videoplayback: 0, - }, - RequestsForbidden: struct { - Videoplayback int64 `json:"videoplayback"` - Vi int64 `json:"vi"` - Ggpht int64 `json:"ggpht"` - }{ - Videoplayback: 0, - Vi: 0, - Ggpht: 0, - }, -} diff --git a/internal/paths/ggpht.go b/internal/paths/ggpht.go index a2344cf..dcbc3d1 100644 --- a/internal/paths/ggpht.go +++ b/internal/paths/ggpht.go @@ -6,9 +6,9 @@ import ( "net/http" "net/url" "strings" - "sync/atomic" "git.nadeko.net/Fijxu/http3-ytproxy/internal/httpc" + "git.nadeko.net/Fijxu/http3-ytproxy/internal/metrics" "git.nadeko.net/Fijxu/http3-ytproxy/internal/utils" ) @@ -35,8 +35,7 @@ func Ggpht(w http.ResponseWriter, req *http.Request) { } if err := forbiddenChecker(resp, w); err != nil { - atomic.AddInt64(&stats_.RequestsForbidden.Ggpht, 1) - metrics.RequestForbidden.Ggpht.Inc() + metrics.Metrics.RequestForbidden.Ggpht.Inc() return } diff --git a/internal/paths/stats.go b/internal/paths/stats.go deleted file mode 100644 index 3c34439..0000000 --- a/internal/paths/stats.go +++ /dev/null @@ -1,20 +0,0 @@ -package paths - -import ( - "encoding/json" - "net/http" - "time" -) - -func Stats(w http.ResponseWriter, req *http.Request) { - w.Header().Set("Content-Type", "application/json") - stats_.Uptime = time.Duration(time.Since(programInit).Seconds()) - // stats_.TotalEstablished = int64(cw.totalEstablished) - // stats_.EstablishedConnections = int64(cw.established) - // stats_.ActiveConnections = int64(cw.active) - // stats_.IdleConnections = int64(cw.idle) - - if err := json.NewEncoder(w).Encode(stats_); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - } -} diff --git a/internal/paths/vi.go b/internal/paths/vi.go index 4ec22d2..a665f46 100644 --- a/internal/paths/vi.go +++ b/internal/paths/vi.go @@ -6,9 +6,9 @@ import ( "net/http" "net/url" "strings" - "sync/atomic" "git.nadeko.net/Fijxu/http3-ytproxy/internal/httpc" + "git.nadeko.net/Fijxu/http3-ytproxy/internal/metrics" "git.nadeko.net/Fijxu/http3-ytproxy/internal/utils" ) @@ -47,8 +47,7 @@ func Vi(w http.ResponseWriter, req *http.Request) { } if err := forbiddenChecker(resp, w); err != nil { - atomic.AddInt64(&stats_.RequestsForbidden.Vi, 1) - metrics.RequestForbidden.Vi.Inc() + metrics.Metrics.RequestForbidden.Vi.Inc() return } diff --git a/internal/paths/videoplayback.go b/internal/paths/videoplayback.go index f26e8cb..ec63514 100644 --- a/internal/paths/videoplayback.go +++ b/internal/paths/videoplayback.go @@ -9,10 +9,10 @@ import ( "net/url" "strconv" "strings" - "sync/atomic" "time" "git.nadeko.net/Fijxu/http3-ytproxy/internal/httpc" + "git.nadeko.net/Fijxu/http3-ytproxy/internal/metrics" "git.nadeko.net/Fijxu/http3-ytproxy/internal/utils" ) @@ -178,8 +178,7 @@ func Videoplayback(w http.ResponseWriter, req *http.Request) { } if err := forbiddenChecker(resp, w); err != nil { - atomic.AddInt64(&stats_.RequestsForbidden.Videoplayback, 1) - metrics.RequestForbidden.Videoplayback.Inc() + metrics.Metrics.RequestForbidden.Videoplayback.Inc() return }