- Drop support for Python 3.7. - Use offline geolocation database instead of ip-api.com. - Add support for proxy authentication. - Add pre-compiled binaries. - Refactor the code by a lot. - Simplify config. - Add automatic text encoding detection. - Use TOML instead of INI for configuration. - Add support for using httpbin-compatible services and services which return plain IP address for getting proxy's exit node. - Add support for reading proxy lists from local files. - Add support for saving to json. - Remove support for saving geolocation to txt, use json instead. - Improve parser. - Improve config validation. - Use poetry instead of requirements.txt. - Use venv in start scripts.
20 lines
840 B
Python
20 lines
840 B
Python
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
PROXY_REGEX = re.compile(
|
|
r"(?:^|[^\.\/\d:@A-Za-z])(?:(?P<protocol>https?|socks[45]):\/\/)?(?:(?P<username>[\dA-Za-z]*):(?P<password>[\dA-Za-z]*)@)?(?P<host>(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(?:\.(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){3}):(?P<port>\d|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])(?=[^\.\/\d:@A-Za-z]|$)",
|
|
flags=re.MULTILINE | re.IGNORECASE,
|
|
)
|
|
IPV4_REGEX = re.compile(
|
|
r"^\s*(?P<host>(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(?:\.(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){3})(?::(?:\d|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5]))?\s*$"
|
|
)
|
|
|
|
|
|
def parse_ipv4(s: str, /) -> str:
|
|
"""Parse ip from ip:port."""
|
|
match = IPV4_REGEX.match(s)
|
|
if not match:
|
|
raise ValueError
|
|
return match.group("host")
|