fuck
Some checks failed
CI / build (push) Has been cancelled

This commit is contained in:
Fijxu 2025-02-13 02:37:09 -03:00
parent 70232f56bf
commit 1ef42a2b53
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4

42
main.go
View file

@ -13,6 +13,7 @@ import (
"os"
"regexp"
"runtime"
"strconv"
"strings"
"sync/atomic"
"syscall"
@ -326,6 +327,20 @@ func requestPerMinute() {
}
}
var tx uint64
func blockCheckerCalc(p *procfs.Proc) {
var last uint64
for {
time.Sleep(1 * time.Second)
// p.NetDev should never fail.
stat, _ := p.NetDev()
current := stat.Total().TxBytes
tx = current - last
last = current
}
}
// Detects if a backend has been blocked based on the amount of bandwidth
// reported by procfs.
// This may be the best way to detect if the IP has been blocked from googlevideo
@ -333,10 +348,10 @@ func requestPerMinute() {
// returns, which most of the time is 403 (Forbidden). But this error code is not
// exclusive to IP blocks, it's also returned for other reasons like a wrong
// query parameter like `pot` (po_token) or anything like that.
func blockChecker(gh string) {
func blockChecker(gh string, cooldown int) {
log.Println("[INFO] Starting blockchecker")
// Sleep for 60 seconds before commencing the loop
time.Sleep(60 * time.Second)
// time.Sleep(60 * time.Second)
url := "http://" + gh + "/v1/openvpn/status"
p, err := procfs.Self()
@ -345,15 +360,13 @@ func blockChecker(gh string) {
log.Println("[INFO] Blockchecker will not run, so if the VPN IP used on gluetun gets blocked, it will not be rotated!")
return
}
go blockCheckerCalc(&p)
var last uint64
for {
time.Sleep(1 * time.Second)
// p.NetDev should never fail.
stat, _ := p.NetDev()
current := stat.Total().TxBytes
aux := current - last
if float64(aux)*0.000008 < 2.0 {
// time.Sleep(time.Duration(cooldown) * time.Second)
time.Sleep(4 * time.Second)
log.Println(float64(tx) * 0.000008)
if float64(tx)*0.000008 < 2.0 {
body := "{\"status\":\"stopped\"}\""
// This should never fail too
request, _ := http.NewRequest("PUT", url, strings.NewReader(body))
@ -364,7 +377,6 @@ func blockChecker(gh string) {
log.Printf("[INFO] Request to change IP sent to gluetun successfully")
}
}
last = current
}
}
@ -544,11 +556,11 @@ func main() {
go requestPerSecond()
go requestPerMinute()
if bc {
// num, err := strconv.Atoi(bc_cooldown)
// if err != nil {
// log.Fatalf("[FATAL] Error while setting BLOCK_CHECKER_COOLDOWN: %s", err)
// }
go blockChecker(gh)
num, err := strconv.Atoi(bc_cooldown)
if err != nil {
log.Fatalf("[FATAL] Error while setting BLOCK_CHECKER_COOLDOWN: %s", err)
}
go blockChecker(gh, num)
}
ln, err := net.Listen("tcp", host+":"+port)