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
All checks were successful
CI / build (push) Successful in 53s
This commit is contained in:
parent
2f88b8487b
commit
538aa2e7e8
1 changed files with 15 additions and 7 deletions
22
main.go
22
main.go
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue