fixup! feat: func to rotate the IP address from gluetun automatically depending of the traffic
All checks were successful
CI / build (push) Successful in 53s

This commit is contained in:
Fijxu 2025-02-13 02:15:47 -03:00
parent 2f88b8487b
commit 538aa2e7e8
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4

22
main.go
View file

@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"log"
"net"
@ -14,6 +13,7 @@ import (
"os"
"regexp"
"runtime"
"strconv"
"strings"
"sync/atomic"
"syscall"
@ -334,9 +334,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) {
// Sleep for 60 seconds before commencing the loop
time.Sleep(60 * time.Second)
func blockChecker(gh string, cooldown int) {
log.Println("[INFO] Starting blockchecker")
// Sleep for 30 seconds before commencing the loop
time.Sleep(30 * time.Second)
url := "http://" + gh + "/v1/openvpn/status"
p, err := procfs.Self()
@ -354,7 +355,6 @@ func blockChecker(gh string) {
current := stat.Total().TxBytes
aux := current - last
if float64(aux)*0.000008 < 3.0 {
fmt.Printf("xd\n")
body := "{\"status\":\"stopped\"}\""
// This should never fail too
request, _ := http.NewRequest("PUT", url, strings.NewReader(body))
@ -364,7 +364,7 @@ func blockChecker(gh string) {
} else {
log.Printf("[INFO] Request to change IP sent to gluetun successfully")
}
time.Sleep(60 * time.Second)
time.Sleep(time.Duration(cooldown) * time.Second)
}
last = current
}
@ -478,6 +478,10 @@ func main() {
if gh == "" {
gh = "127.0.0.1:8000"
}
bc_cooldown := os.Getenv("BLOCK_CHECKER_COOLDOWN")
if bc_cooldown == "" {
bc_cooldown = "20"
}
proxy = os.Getenv("PROXY")
flag.BoolVar(&https, "https", https, "Use built-in https server (recommended)")
@ -542,7 +546,11 @@ func main() {
go requestPerSecond()
go requestPerMinute()
if bc {
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)