From 3b89ea41e7b83e32ec482188a11a5591b1364248 Mon Sep 17 00:00:00 2001 From: Fijxu Date: Mon, 4 Nov 2024 12:05:59 -0300 Subject: [PATCH] Add uptime to /stats --- httppaths.go | 4 ++++ main.go | 21 +++++++++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/httppaths.go b/httppaths.go index 003ab34..eeaea19 100644 --- a/httppaths.go +++ b/httppaths.go @@ -20,11 +20,13 @@ func videoplayback(w http.ResponseWriter, req *http.Request) { mn := strings.Split(q.Get("mn"), ",") if len(mvi) <= 0 { + w.WriteHeader(400) io.WriteString(w, "No `mvi` in query parameters") return } if len(mn) <= 0 { + w.WriteHeader(400) io.WriteString(w, "No `mn` in query parameters") return } @@ -35,6 +37,7 @@ func videoplayback(w http.ResponseWriter, req *http.Request) { parts := strings.Split(strings.ToLower(host), ".") if len(parts) < 2 { + w.WriteHeader(400) io.WriteString(w, "Invalid hostname.") return } @@ -49,6 +52,7 @@ func videoplayback(w http.ResponseWriter, req *http.Request) { } if disallowed { + w.WriteHeader(401) io.WriteString(w, "Non YouTube domains are not supported.") return } diff --git a/main.go b/main.go index cc2797d..bd01b5b 100644 --- a/main.go +++ b/main.go @@ -96,6 +96,8 @@ var version string var h3s bool +var programInit = time.Now() + type ConnectionWatcher struct { totalEstablished int64 established int64 @@ -134,14 +136,15 @@ func (cw *ConnectionWatcher) OnStateChange(conn net.Conn, state http.ConnState) var cw ConnectionWatcher type statusJson struct { - Version string `json:"version"` - RequestCount int64 `json:"requestCount"` - RequestPerSecond int64 `json:"requestPerSecond"` - RequestPerMinute int64 `json:"requestPerMinute"` - TotalEstablished int64 `json:"totalEstablished"` - EstablishedConnections int64 `json:"establishedConnections"` - ActiveConnections int64 `json:"activeConnections"` - IdleConnections int64 `json:"idleConnections"` + Version string `json:"version"` + Uptime time.Duration `json:"uptime"` + RequestCount int64 `json:"requestCount"` + RequestPerSecond int64 `json:"requestPerSecond"` + RequestPerMinute int64 `json:"requestPerMinute"` + TotalEstablished int64 `json:"totalEstablished"` + EstablishedConnections int64 `json:"establishedConnections"` + ActiveConnections int64 `json:"activeConnections"` + IdleConnections int64 `json:"idleConnections"` RequestsForbidden struct { Videoplayback int64 `json:"videoplayback"` Vi int64 `json:"vi"` @@ -151,6 +154,7 @@ type statusJson struct { var stats_ = statusJson{ Version: version + "-" + runtime.GOARCH, + Uptime: 0, RequestCount: 0, RequestPerSecond: 0, RequestPerMinute: 0, @@ -182,6 +186,7 @@ func root(w http.ResponseWriter, req *http.Request) { 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)