From a98a4ba1bf1db8c4f77dab09f0f82b981214b3ed Mon Sep 17 00:00:00 2001 From: Fijxu Date: Sun, 22 Dec 2024 01:09:19 -0300 Subject: [PATCH] check if the connection has been closed before doing the request to google servers --- httppaths.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/httppaths.go b/httppaths.go index 86c3eda..50a98eb 100644 --- a/httppaths.go +++ b/httppaths.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "context" "fmt" "io" "log" @@ -23,6 +24,17 @@ func forbiddenChecker(resp *http.Response, w http.ResponseWriter) error { return nil } +func connectionChecker(ctx context.Context) bool { + // To check if the connection has been closed. To prevent + // doing a useless request to google servers + select { + case <-ctx.Done(): + return true + default: + return false + } +} + func videoplayback(w http.ResponseWriter, req *http.Request) { q := req.URL.Query() @@ -132,6 +144,10 @@ func videoplayback(w http.ResponseWriter, req *http.Request) { request.Header.Add("Origin", "https://www.youtube.com") request.Header.Add("Referer", "https://www.youtube.com/") + if connectionChecker(req.Context()) { + return + } + resp, err := client.Do(request) if err != nil { log.Panic(err) @@ -222,6 +238,10 @@ func vi(w http.ResponseWriter, req *http.Request) { request.Header.Set("User-Agent", default_ua) + if connectionChecker(req.Context()) { + return + } + resp, err := client.Do(request) if err != nil { log.Panic(err) @@ -261,6 +281,10 @@ func ggpht(w http.ResponseWriter, req *http.Request) { log.Panic(err) } + if connectionChecker(req.Context()) { + return + } + resp, err := client.Do(request) if err != nil { log.Panic(err)