|
| 1 | +from contextlib import suppress |
| 2 | +from enum import IntEnum, auto |
| 3 | +from functools import partial |
| 4 | +from ipaddress import ip_address |
| 5 | +from pathlib import Path |
| 6 | +from socket import socket, SOCK_STREAM, AF_INET, gethostbyname |
| 7 | +from threading import Lock, Thread |
| 8 | +from typing import Match, AnyStr, Set, Collection |
| 9 | + |
| 10 | +from maxminddb.types import RecordDict |
| 11 | +from requests import Session |
| 12 | +from socks import socksocket, SOCKS4, SOCKS5, HTTP |
| 13 | +from yarl import URL |
| 14 | + |
| 15 | +from PyRoxy import GeoIP, Tools |
| 16 | +from PyRoxy.Exceptions import ProxyInvalidHost, ProxyInvalidPort, ProxyParseError |
| 17 | +from PyRoxy.Tools import Patterns |
| 18 | + |
| 19 | + |
| 20 | +class ProxyType(IntEnum): |
| 21 | + HTTP = auto() |
| 22 | + HTTPS = auto() |
| 23 | + SOCKS4 = auto() |
| 24 | + SOCKS5 = auto() |
| 25 | + |
| 26 | + def asPySocksType(self): |
| 27 | + return SOCKS5 if self == ProxyType.SOCKS5 else \ |
| 28 | + SOCKS4 if self == ProxyType.SOCKS4 else \ |
| 29 | + HTTP |
| 30 | + |
| 31 | + @staticmethod |
| 32 | + def stringToProxyType(n: str): |
| 33 | + return ProxyType.HTTP if not (n.isdigit() and not (int(n) == 1)) else \ |
| 34 | + int(n) if int(n) in PRINTABLE_PROXY_TYPES else \ |
| 35 | + ProxyType.SOCKS5 if int(n) == 5 else \ |
| 36 | + ProxyType.SOCKS4 if int(n) == 4 else \ |
| 37 | + ProxyType.HTTP |
| 38 | + |
| 39 | + |
| 40 | +PROXY_TYPES = {"SOCKS4": ProxyType.SOCKS4, "SOCKS5": ProxyType.SOCKS5, "HTTP": ProxyType.HTTP, "HTTPS": ProxyType.HTTPS} |
| 41 | +PRINTABLE_PROXY_TYPES = dict(zip(PROXY_TYPES.values(), PROXY_TYPES.keys())) |
| 42 | + |
| 43 | + |
| 44 | +class Proxy(object): |
| 45 | + user: AnyStr | None |
| 46 | + password: AnyStr | None |
| 47 | + country: AnyStr | RecordDict | None |
| 48 | + port: int |
| 49 | + type: ProxyType |
| 50 | + host: AnyStr |
| 51 | + |
| 52 | + def __init__(self, host: str, port: int = 0, proxy_type: ProxyType = ProxyType.HTTP, user=None, password=None): |
| 53 | + if Patterns.URL.match(host): host = gethostbyname(host) |
| 54 | + assert self.validate(host, port) |
| 55 | + self.host = host |
| 56 | + self.type = proxy_type |
| 57 | + self.port = port |
| 58 | + self.country = GeoIP.get(host) |
| 59 | + if self.country: self.country = self.country["country"]["iso_code"] |
| 60 | + self.user = user or None |
| 61 | + self.password = password or None |
| 62 | + |
| 63 | + def __str__(self): |
| 64 | + return "%s://%s:%d%s" % (self.type.name.lower(), self.host, self.port, ("%s:%s" % (self.user, self.password) |
| 65 | + if self.password and self.user else "")) |
| 66 | + |
| 67 | + def __repr__(self): |
| 68 | + return "<Proxy %s:%d>" % (self.host, self.port) |
| 69 | + |
| 70 | + @staticmethod |
| 71 | + def fromString(string: str): |
| 72 | + with suppress(KeyboardInterrupt): |
| 73 | + proxy: Match[str] | None = Patterns.Proxy.search(string) |
| 74 | + return Proxy(proxy.group(1), |
| 75 | + int(proxy.group(2)) if proxy.group(3) and proxy.group(2).isdigit() else 80, |
| 76 | + ProxyType.stringToProxyType(proxy.group(1)), |
| 77 | + proxy.group(3), |
| 78 | + proxy.group(4)) |
| 79 | + raise ProxyParseError("'%s' is an Invalid Proxy URL" % string) |
| 80 | + |
| 81 | + def ip_port(self): |
| 82 | + return "%s:%d" % (self.host, self.port) |
| 83 | + |
| 84 | + @staticmethod |
| 85 | + def validate(host: str, port: int): |
| 86 | + with suppress(ValueError): |
| 87 | + if not ip_address(host): |
| 88 | + raise ProxyInvalidHost(host) |
| 89 | + if not Tools.Patterns.Port.match(str(port)): |
| 90 | + raise ProxyInvalidPort(port) |
| 91 | + return True |
| 92 | + raise ProxyInvalidHost(host) |
| 93 | + |
| 94 | + # noinspection PyShadowingBuiltins |
| 95 | + def open_socket(self, family=AF_INET, type=SOCK_STREAM, proto=-1, fileno=None): |
| 96 | + return ProxySocket(self, family, type, proto, fileno) |
| 97 | + |
| 98 | + def wrap(self, sock: socket | Session): |
| 99 | + if isinstance(sock, socket): |
| 100 | + return self.open_socket(sock.family, sock.type, sock.proto, sock.fileno()) |
| 101 | + sock.proxies = self.asRequest() |
| 102 | + return sock |
| 103 | + |
| 104 | + def asRequest(self): |
| 105 | + return {"http": self.__str__()} |
| 106 | + |
| 107 | + # noinspection PyUnreachableCode |
| 108 | + def check(self, url: str | URL = "https://httpbin.org/get", timeout=5): |
| 109 | + if not isinstance(url, URL): url = URL(url) |
| 110 | + with suppress(KeyboardInterrupt): |
| 111 | + with self.open_socket() as sock: |
| 112 | + sock.settimeout(timeout) |
| 113 | + return sock.connect((url.host, url.port or 80)) |
| 114 | + return False |
| 115 | + |
| 116 | + |
| 117 | +# noinspection PyShadowingBuiltins |
| 118 | +class ProxySocket(socksocket): |
| 119 | + def __init__(self, proxy: Proxy, family=-1, type=-1, proto=-1, fileno=None): |
| 120 | + super().__init__(family, type, proto, fileno) |
| 121 | + if proxy.port: |
| 122 | + if proxy.user and proxy.password: |
| 123 | + self.setproxy(proxy.type.asPySocksType(), proxy.host, proxy.port, username=proxy.user, |
| 124 | + password=proxy.password) |
| 125 | + return |
| 126 | + self.setproxy(proxy.type.asPySocksType(), proxy.host, proxy.port) |
| 127 | + return |
| 128 | + if proxy.user and proxy.password: |
| 129 | + self.setproxy(proxy.type.asPySocksType(), proxy.host, username=proxy.user, |
| 130 | + password=proxy.password) |
| 131 | + return |
| 132 | + self.setproxy(proxy.type.asPySocksType(), proxy.host) |
| 133 | + |
| 134 | + |
| 135 | +class ProxyChecker: |
| 136 | + result: Set[Proxy] |
| 137 | + out_lock: Lock |
| 138 | + |
| 139 | + def __init__(self): |
| 140 | + self.out_lock = Lock() |
| 141 | + self.result = set() |
| 142 | + |
| 143 | + def check(self, proxy: Proxy, url: str | URL = "https://httpbin.org/get", timeout=5): |
| 144 | + with suppress(Exception), self.out_lock: |
| 145 | + if proxy.check(url, timeout): |
| 146 | + self.result.add(proxy) |
| 147 | + |
| 148 | + def checkAll(self, proxies: Collection[Proxy], url: str | URL = "https://httpbin.org/get", timeout=5): |
| 149 | + threads = map(partial(Thread, target=self.check, args=(url, timeout,)), proxies) |
| 150 | + for thr in threads: |
| 151 | + thr.start() |
| 152 | + for thr in threads: |
| 153 | + thr.join() |
| 154 | + |
| 155 | + |
| 156 | +class ProxyUtiles: |
| 157 | + @staticmethod |
| 158 | + def parseAll(proxies: Collection[str]) -> Set[Proxy]: |
| 159 | + return set(map(Proxy.fromString, proxies)) |
| 160 | + |
| 161 | + @staticmethod |
| 162 | + def readFromFile(path: Path | str) -> Set[Proxy]: |
| 163 | + if isinstance(path, Path): |
| 164 | + with path.open("r+") as read: |
| 165 | + lines = read.readlines() |
| 166 | + else: |
| 167 | + with open(path, "r+") as read: |
| 168 | + lines = read.readlines() |
| 169 | + |
| 170 | + return ProxyUtiles.parseAll([prox.strip() for prox in lines]) |
0 commit comments