diff --git a/main.go b/main.go index c24359a..1c1f9cd 100644 --- a/main.go +++ b/main.go @@ -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,7 +348,7 @@ 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) @@ -345,15 +360,11 @@ 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) + 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 +375,6 @@ func blockChecker(gh string) { log.Printf("[INFO] Request to change IP sent to gluetun successfully") } } - last = current } } @@ -544,11 +554,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)