-
Notifications
You must be signed in to change notification settings - Fork 34
/
proxy_validator.py
48 lines (36 loc) · 1.44 KB
/
proxy_validator.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
import re
from parsers.regex_parser import PROXY_VALIDATE_REGEX
class ValidationError(Exception):
pass
def retrieve(proxy) -> tuple:
protocol = auth_data = domain = port = None
if type(proxy) is str:
matches = re.match(PROXY_VALIDATE_REGEX, proxy)
if matches:
matches = matches.groupdict()
auth_data = matches["auth_data"]
domain = matches["domain"]
port = matches["port"]
else:
raise ValidationError("Proxy doesn't match regex")
elif type(proxy) is dict:
auth_data = proxy["auth_data"] if "auth_data" in proxy else None
domain = proxy["domain"] if "domain" in proxy else None
port = proxy["port"] if "port" in proxy else None
str_proxy = ""
if auth_data is not None and auth_data:
str_proxy += auth_data + "@"
str_proxy += domain + ":" + port
return retrieve(str_proxy)
else:
raise ValidationError('Bad type. Type is "{}"'.format(type(proxy), proxy))
if protocol is not None:
if protocol not in ("socks", "socks4", "socks5", "http"):
raise ValidationError("Bad protocol")
if auth_data is None:
auth_data = ""
if type(domain) is not str:
raise ValidationError("Bad proxy(domain isn't string)")
if type(port) is not str:
raise ValidationError("Bad proxy(port isn't string)")
return protocol, auth_data, domain, port