Revert "Add Total upload and download stats"
All checks were successful
CI / build (push) Successful in 4m8s

This reverts commit 7793a6938c.
This commit is contained in:
Fijxu 2024-11-02 02:53:58 -03:00
parent 7793a6938c
commit 9480654bff
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4
3 changed files with 26 additions and 77 deletions

6
go.mod
View file

@ -4,12 +4,10 @@ go 1.22.0
toolchain go1.23.0
require (
github.com/conduitio/bwlimit v0.1.0
github.com/quic-go/quic-go v0.48.1
)
require github.com/quic-go/quic-go v0.48.1
require (
github.com/conduitio/bwlimit v0.1.0 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/google/pprof v0.0.0-20241029010322-833c56d90c8e // indirect
github.com/onsi/ginkgo/v2 v2.20.2 // indirect

View file

@ -5,42 +5,12 @@ import (
"io"
"log"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"sync/atomic"
)
type ResponseWriterWrapper struct {
http.ResponseWriter
size int
}
func (w *ResponseWriterWrapper) Size() int {
return w.size
}
// This overrides the http.ResponseWriter Write() function. Do not remove!
func (w *ResponseWriterWrapper) Write(b []byte) (int, error) {
n, err := w.ResponseWriter.Write(b)
w.size += n
return n, err
}
func doRequest(request *http.Request) *http.Response {
resp, err := client.Do(request)
if err != nil {
log.Panic(err)
}
size, _ := httputil.DumpResponse(resp, true)
atomic.AddInt64(&totalBandwidth.TotalDownload, int64(len(size)))
return resp
}
func videoplayback(w http.ResponseWriter, req *http.Request) {
w_ := &ResponseWriterWrapper{ResponseWriter: w}
q := req.URL.Query()
host := q.Get("host")
q.Del("host")
@ -96,14 +66,16 @@ func videoplayback(w http.ResponseWriter, req *http.Request) {
body := []byte{0x78, 0} // protobuf body
request, err := http.NewRequest("POST", proxyURL.String(), bytes.NewReader(body))
copyHeaders(req.Header, request.Header, false)
request.Header.Set("User-Agent", ua)
if err != nil {
log.Panic(err)
}
copyHeaders(req.Header, request.Header, false)
request.Header.Set("User-Agent", ua)
resp := doRequest(request)
resp, err := client.Do(request)
if err != nil {
log.Panic(err)
}
if resp.StatusCode == 403 {
atomic.AddInt64(&stats_.RequestsForbidden.Videoplayback, 1)
@ -146,8 +118,7 @@ func videoplayback(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, strings.Join(lines, "\n"))
} else {
io.Copy(w_, resp.Body)
atomic.AddInt64(&totalBandwidth.TotalUpload, int64(w_.Size()))
io.Copy(w, resp.Body)
}
}
@ -174,14 +145,17 @@ func vi(w http.ResponseWriter, req *http.Request) {
proxyURL.RawQuery = q.Encode()
request, err := http.NewRequest(req.Method, proxyURL.String(), nil)
copyHeaders(req.Header, request.Header, false)
request.Header.Set("User-Agent", ua)
if err != nil {
log.Panic(err)
}
copyHeaders(req.Header, request.Header, false)
request.Header.Set("User-Agent", ua)
resp := doRequest(request)
resp, err := client.Do(request)
if err != nil {
log.Panic(err)
}
w.WriteHeader(resp.StatusCode)
if resp.StatusCode == 403 {
@ -211,14 +185,16 @@ func ggpht(w http.ResponseWriter, req *http.Request) {
}
request, err := http.NewRequest(req.Method, proxyURL.String(), nil)
copyHeaders(req.Header, request.Header, false)
request.Header.Set("User-Agent", ua)
if err != nil {
log.Panic(err)
}
copyHeaders(req.Header, request.Header, false)
request.Header.Set("User-Agent", ua)
resp := doRequest(request)
resp, err := client.Do(request)
if err != nil {
log.Panic(err)
}
w.WriteHeader(resp.StatusCode)
if resp.StatusCode == 403 {

33
main.go
View file

@ -93,25 +93,11 @@ var ipv6_only = false
var version string
var programInit = time.Now()
type TotalBandwidth struct {
TotalUpload int64
TotalDownload int64
}
var totalBandwidth = TotalBandwidth{TotalUpload: 0, TotalDownload: 0}
type statusJson struct {
Version string `json:"version"`
Uptime time.Duration `json:"uptime"`
RequestCount int64 `json:"requestCount"`
RequestPerSecond int64 `json:"requestPerSecond"`
RequestPerMinute int64 `json:"requestPerMinute"`
// CurrentUpload any `json:"currentUpload"`
// CurrentDownload any `json:"currentDownload"`
TotalUpload string `json:"totalUpload"`
TotalDownload string `json:"totalDownload"`
Version string `json:"version"`
RequestCount int64 `json:"requestCount"`
RequestPerSecond int64 `json:"requestPerSecond"`
RequestPerMinute int64 `json:"requestPerMinute"`
RequestsForbidden struct {
Videoplayback int64 `json:"videoplayback"`
Vi int64 `json:"vi"`
@ -121,14 +107,9 @@ type statusJson struct {
var stats_ = statusJson{
Version: version + "-" + runtime.GOARCH,
Uptime: 0,
RequestCount: 0,
RequestPerSecond: 0,
RequestPerMinute: 0,
// CurrentUpload: nil,
// CurrentDownload: nil,
TotalUpload: "",
TotalDownload: "",
RequestsForbidden: struct {
Videoplayback int64 `json:"videoplayback"`
Vi int64 `json:"vi"`
@ -154,12 +135,6 @@ 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())
tu := float64(totalBandwidth.TotalUpload) / float64(1_048_576) // 1MiB = 1_048_576 (DON'T CONFUSE THEM WITH 1MB WHICH IS 1_000_000 BYTES)
td := float64(totalBandwidth.TotalDownload) / float64(1_048_576)
stats_.TotalUpload = fmt.Sprintf("%.3gMiB", tu)
stats_.TotalDownload = fmt.Sprintf("%.3gMiB", td)
if err := json.NewEncoder(w).Encode(stats_); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}