parent
70232f56bf
commit
60c481c9a7
1 changed files with 24 additions and 14 deletions
38
main.go
38
main.go
|
@ -13,6 +13,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"syscall"
|
"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
|
// Detects if a backend has been blocked based on the amount of bandwidth
|
||||||
// reported by procfs.
|
// reported by procfs.
|
||||||
// This may be the best way to detect if the IP has been blocked from googlevideo
|
// 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
|
// 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
|
// exclusive to IP blocks, it's also returned for other reasons like a wrong
|
||||||
// query parameter like `pot` (po_token) or anything like that.
|
// 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")
|
log.Println("[INFO] Starting blockchecker")
|
||||||
// Sleep for 60 seconds before commencing the loop
|
// Sleep for 60 seconds before commencing the loop
|
||||||
time.Sleep(60 * time.Second)
|
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!")
|
log.Println("[INFO] Blockchecker will not run, so if the VPN IP used on gluetun gets blocked, it will not be rotated!")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
go blockCheckerCalc(&p)
|
||||||
|
|
||||||
var last uint64
|
|
||||||
for {
|
for {
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(time.Duration(cooldown) * time.Second)
|
||||||
// p.NetDev should never fail.
|
if float64(tx)*0.000008 < 2.0 {
|
||||||
stat, _ := p.NetDev()
|
|
||||||
current := stat.Total().TxBytes
|
|
||||||
aux := current - last
|
|
||||||
if float64(aux)*0.000008 < 2.0 {
|
|
||||||
body := "{\"status\":\"stopped\"}\""
|
body := "{\"status\":\"stopped\"}\""
|
||||||
// This should never fail too
|
// This should never fail too
|
||||||
request, _ := http.NewRequest("PUT", url, strings.NewReader(body))
|
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")
|
log.Printf("[INFO] Request to change IP sent to gluetun successfully")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
last = current
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -544,11 +554,11 @@ func main() {
|
||||||
go requestPerSecond()
|
go requestPerSecond()
|
||||||
go requestPerMinute()
|
go requestPerMinute()
|
||||||
if bc {
|
if bc {
|
||||||
// num, err := strconv.Atoi(bc_cooldown)
|
num, err := strconv.Atoi(bc_cooldown)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// log.Fatalf("[FATAL] Error while setting BLOCK_CHECKER_COOLDOWN: %s", err)
|
log.Fatalf("[FATAL] Error while setting BLOCK_CHECKER_COOLDOWN: %s", err)
|
||||||
// }
|
}
|
||||||
go blockChecker(gh)
|
go blockChecker(gh, num)
|
||||||
}
|
}
|
||||||
|
|
||||||
ln, err := net.Listen("tcp", host+":"+port)
|
ln, err := net.Listen("tcp", host+":"+port)
|
||||||
|
|
Loading…
Add table
Reference in a new issue