-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathformatter.py
36 lines (28 loc) · 1.12 KB
/
formatter.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
import re
proxy_strings = [
'127.0.0.1:8000:username:password',
'username:password@127.0.0.1:8000',
'https://username:password@127.0.0.1:8000',
'socks5://username:password@127.0.0.1:8000',
'socks4://username:password@127.0.0.1:8000',
'example.com:8080',
'192.168.1.1:9000',
'username:password@example.com:8080'
]
def reformat_proxy(proxy_str):
prefixes = ("http://", "socks4://", "socks5://")
proxy_str = proxy_str.replace("https://", "http://") # in case always using http://
if proxy_str.startswith(prefixes):
return proxy_str
if "@" in proxy_str:
return f"http://{proxy_str}"
match = re.search(r'([\d.]+|[a-zA-Z0-9.-]+):(\d+)', proxy_str)
if match:
hostname, port = match.groups()
hostname = f"{hostname}:{port}"
if proxy_str.index(hostname) == 0:
proxy_str = proxy_str.replace(f"{hostname}:", "").replace(f"{hostname}", "")
return f"http://{proxy_str + '@' if proxy_str else ''}{hostname}"
raise Exception("Invalid proxy format")
for proxy_str in proxy_strings:
print(reformat_proxy(proxy_str))