-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.py
executable file
·57 lines (49 loc) · 1.38 KB
/
check.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/python3
import os
import random
import requests
import re
import logging
from sys import exc_info
attempts = 3
timeout = 5
# 'http://ipinfo.io/ip',
# 'http://ifconfig.me/ip',
services = {
'http://ipv4.icanhazip.com/',
'http://v4.ident.me/',
'http://ipecho.net/plain',
'http://api.ipify.org',
'http://checkip.amazonaws.com',
'http://ifconfig.co/ip'
}
proxy_url = 'socks5://{}:{}'.format(os.environ['HAPROXY_SERVER_ADDR'], os.environ['HAPROXY_SERVER_PORT'])
proxies = {'http': proxy_url, 'https': proxy_url}
logging.basicConfig(
format='%(asctime)s [check:%(levelname)s] %(message)s',
level=logging.INFO
)
success = False
for try_count in range(attempts):
req_url = random.sample(services, 1)[0]
# do not try again this service
services.remove(req_url)
try:
request = requests.get(req_url, timeout=timeout, proxies=proxies)
except Exception as e:
logging.warning(os.environ['HAPROXY_SERVER_NAME'] + ' -> ' + req_url + ' ' + str(exc_info()[0]))
else:
if request.status_code < 300:
success = True
break
if len(services) == 0:
break
if success:
ipaddress = request.text.strip()
if not re.match('([0-9]{1,3}.){3}[0-9]{1,3}', ipaddress):
ipaddress = None
logging.debug(ipaddress)
exit(0)
else:
logging.error(os.environ['HAPROXY_SERVER_NAME'])
exit(1)