@@ -5,7 +5,10 @@ def is_valid_proxy(data):
5
5
"""
6
6
check this string is within proxy format
7
7
"""
8
- if data .__contains__ (':' ):
8
+ if is_auth_proxy (data ):
9
+ host , port = extract_auth_proxy (data )
10
+ return is_ip_valid (host ) and is_port_valid (port )
11
+ elif data .__contains__ (':' ):
9
12
ip = data .split (':' )[0 ]
10
13
port = data .split (':' )[1 ]
11
14
return is_ip_valid (ip ) and is_port_valid (port )
@@ -17,6 +20,8 @@ def is_ip_valid(ip):
17
20
"""
18
21
check this string is within ip format
19
22
"""
23
+ if is_auth_proxy (ip ):
24
+ ip = ip .split ('@' )[1 ]
20
25
a = ip .split ('.' )
21
26
if len (a ) != 4 :
22
27
return False
@@ -48,9 +53,36 @@ def convert_proxy_or_proxies(data):
48
53
# skip invalid item
49
54
item = item .strip ()
50
55
if not is_valid_proxy (item ): continue
51
- host , port = item .split (':' )
56
+ if is_auth_proxy (item ):
57
+ host , port = extract_auth_proxy (item )
58
+ else :
59
+ host , port = item .split (':' )
52
60
result .append (Proxy (host = host , port = int (port )))
53
61
return result
54
62
if isinstance (data , str ) and is_valid_proxy (data ):
55
- host , port = data .split (':' )
63
+ if is_auth_proxy (data ):
64
+ host , port = extract_auth_proxy (data )
65
+ else :
66
+ host , port = data .split (':' )
56
67
return Proxy (host = host , port = int (port ))
68
+
69
+
70
+ def is_auth_proxy (data : str ) -> bool :
71
+ return '@' in data
72
+
73
+
74
+ def extract_auth_proxy (data : str ) -> (str , str ):
75
+ """
76
+ extract host and port from a proxy with authentication
77
+ """
78
+ auth = data .split ('@' )[0 ]
79
+ ip_port = data .split ('@' )[1 ]
80
+ ip = ip_port .split (':' )[0 ]
81
+ port = ip_port .split (':' )[1 ]
82
+ host = auth + '@' + ip
83
+ return host , port
84
+
85
+
86
+ if __name__ == '__main__' :
87
+ proxy = 'test1234:[email protected] :32425'
88
+ print (extract_auth_proxy (proxy ))
0 commit comments