34
34
logging.error(f"Error retrieving configuration values: {e}")
38
'https://httpbin.org/ip', # returns {"origin": "x.x.x.x"}
39
'https://api.ipify.org?format=json', # returns {"ip": "x.x.x.x"}
40
'https://ipinfo.io/json', # returns {"ip": "x.x.x.x", ...}
41
'https://icanhazip.com', # returns plain text "x.x.x.x\n"
42
'https://checkip.amazonaws.com' # returns plain text "x.x.x.x\n"
38
46
log_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s", datefmt="%H:%M:%S")
328
336
logging.warning("Timeout reached waiting for VPN initialization.")
339
def fetch_ip_from_service(url):
341
resp = requests.get(url, timeout=5)
342
resp.raise_for_status()
343
if url.endswith('/ip'):
344
return resp.json()['origin'].strip()
345
elif 'ipify' in url or 'ipinfo' in url:
346
return resp.json()['ip'].strip()
348
return resp.text.strip()
349
except Exception as e:
350
logging.warning(f"Failed to get IP from {url}: {e}")
331
353
def get_current_ip(real_ip, retry_interval=1, max_retries=30):
333
355
while retries < max_retries:
335
resp = requests.get('https://httpbin.org/ip', timeout=5)
336
current_ip = resp.json()['origin'].strip()
337
logging.info(f"Fetched IP: {current_ip}")
338
if current_ip != real_ip:
356
for service in IP_SERVICES:
357
current_ip = fetch_ip_from_service(service)
358
if current_ip and current_ip != real_ip:
359
logging.info(f"Fetched new IP: {current_ip} from {service}")
339
360
return current_ip
340
logging.warning("Current IP still matches REAL_IP. Waiting for new IP...")
341
except Exception as e:
342
logging.error(f"Error fetching IP (retrying): {e}")
361
elif current_ip == real_ip:
362
logging.warning(f"IP from {service} still matches REAL_IP. Retrying...")
343
363
time.sleep(retry_interval)
365
logging.error("Max retries reached. Failed to get a new IP.")
347
368
def connect_until_new_ip(real_ip, banned_ip=None):