D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
proc
/
thread-self
/
root
/
home
/
vblioqus
/
karachi777.vip
/
images
/
494334
/
65412
/
Filename :
python3.6.tar
back
Copy
site-packages/socks.py 0000644 00000077031 15152656744 0011007 0 ustar 00 """SocksiPy - Python SOCKS module. Copyright 2006 Dan-Haim. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of Dan Haim nor the names of his contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY DAN HAIM "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAN HAIM OR HIS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. This module provides a standard socket-like interface for Python for tunneling connections through SOCKS proxies. =============================================================================== Minor modifications made by Christopher Gilbert (http://motomastyle.com/) for use in PyLoris (http://pyloris.sourceforge.net/) Minor modifications made by Mario Vilas (http://breakingcode.wordpress.com/) mainly to merge bug fixes found in Sourceforge Modifications made by Anorov (https://github.com/Anorov) -Forked and renamed to PySocks -Fixed issue with HTTP proxy failure checking (same bug that was in the old ___recvall() method) -Included SocksiPyHandler (sockshandler.py), to be used as a urllib2 handler, courtesy of e000 (https://github.com/e000): https://gist.github.com/869791#file_socksipyhandler.py -Re-styled code to make it readable -Aliased PROXY_TYPE_SOCKS5 -> SOCKS5 etc. -Improved exception handling and output -Removed irritating use of sequence indexes, replaced with tuple unpacked variables -Fixed up Python 3 bytestring handling - chr(0x03).encode() -> b"\x03" -Other general fixes -Added clarification that the HTTP proxy connection method only supports CONNECT-style tunneling HTTP proxies -Various small bug fixes """ from base64 import b64encode from collections import Callable from errno import EOPNOTSUPP, EINVAL, EAGAIN import functools from io import BytesIO import logging import os from os import SEEK_CUR import socket import struct import sys __version__ = "1.6.7" if os.name == "nt" and sys.version_info < (3, 0): try: import win_inet_pton except ImportError: raise ImportError( "To run PySocks on Windows you must install win_inet_pton") log = logging.getLogger(__name__) PROXY_TYPE_SOCKS4 = SOCKS4 = 1 PROXY_TYPE_SOCKS5 = SOCKS5 = 2 PROXY_TYPE_HTTP = HTTP = 3 PROXY_TYPES = {"SOCKS4": SOCKS4, "SOCKS5": SOCKS5, "HTTP": HTTP} PRINTABLE_PROXY_TYPES = dict(zip(PROXY_TYPES.values(), PROXY_TYPES.keys())) _orgsocket = _orig_socket = socket.socket def set_self_blocking(function): @functools.wraps(function) def wrapper(*args, **kwargs): self = args[0] try: _is_blocking = self.gettimeout() if _is_blocking == 0: self.setblocking(True) return function(*args, **kwargs) except Exception as e: raise finally: # set orgin blocking if _is_blocking == 0: self.setblocking(False) return wrapper class ProxyError(IOError): """Socket_err contains original socket.error exception.""" def __init__(self, msg, socket_err=None): self.msg = msg self.socket_err = socket_err if socket_err: self.msg += ": {0}".format(socket_err) def __str__(self): return self.msg class GeneralProxyError(ProxyError): pass class ProxyConnectionError(ProxyError): pass class SOCKS5AuthError(ProxyError): pass class SOCKS5Error(ProxyError): pass class SOCKS4Error(ProxyError): pass class HTTPError(ProxyError): pass SOCKS4_ERRORS = { 0x5B: "Request rejected or failed", 0x5C: ("Request rejected because SOCKS server cannot connect to identd on" " the client"), 0x5D: ("Request rejected because the client program and identd report" " different user-ids") } SOCKS5_ERRORS = { 0x01: "General SOCKS server failure", 0x02: "Connection not allowed by ruleset", 0x03: "Network unreachable", 0x04: "Host unreachable", 0x05: "Connection refused", 0x06: "TTL expired", 0x07: "Command not supported, or protocol error", 0x08: "Address type not supported" } DEFAULT_PORTS = {SOCKS4: 1080, SOCKS5: 1080, HTTP: 8080} def set_default_proxy(proxy_type=None, addr=None, port=None, rdns=True, username=None, password=None): """Sets a default proxy. All further socksocket objects will use the default unless explicitly changed. All parameters are as for socket.set_proxy().""" socksocket.default_proxy = (proxy_type, addr, port, rdns, username.encode() if username else None, password.encode() if password else None) def setdefaultproxy(*args, **kwargs): if "proxytype" in kwargs: kwargs["proxy_type"] = kwargs.pop("proxytype") return set_default_proxy(*args, **kwargs) def get_default_proxy(): """Returns the default proxy, set by set_default_proxy.""" return socksocket.default_proxy getdefaultproxy = get_default_proxy def wrap_module(module): """Attempts to replace a module's socket library with a SOCKS socket. Must set a default proxy using set_default_proxy(...) first. This will only work on modules that import socket directly into the namespace; most of the Python Standard Library falls into this category.""" if socksocket.default_proxy: module.socket.socket = socksocket else: raise GeneralProxyError("No default proxy specified") wrapmodule = wrap_module def create_connection(dest_pair, timeout=None, source_address=None, proxy_type=None, proxy_addr=None, proxy_port=None, proxy_rdns=True, proxy_username=None, proxy_password=None, socket_options=None): """create_connection(dest_pair, *[, timeout], **proxy_args) -> socket object Like socket.create_connection(), but connects to proxy before returning the socket object. dest_pair - 2-tuple of (IP/hostname, port). **proxy_args - Same args passed to socksocket.set_proxy() if present. timeout - Optional socket timeout value, in seconds. source_address - tuple (host, port) for the socket to bind to as its source address before connecting (only for compatibility) """ # Remove IPv6 brackets on the remote address and proxy address. remote_host, remote_port = dest_pair if remote_host.startswith("["): remote_host = remote_host.strip("[]") if proxy_addr and proxy_addr.startswith("["): proxy_addr = proxy_addr.strip("[]") err = None # Allow the SOCKS proxy to be on IPv4 or IPv6 addresses. for r in socket.getaddrinfo(proxy_addr, proxy_port, 0, socket.SOCK_STREAM): family, socket_type, proto, canonname, sa = r sock = None try: sock = socksocket(family, socket_type, proto) if socket_options: for opt in socket_options: sock.setsockopt(*opt) if isinstance(timeout, (int, float)): sock.settimeout(timeout) if proxy_type: sock.set_proxy(proxy_type, proxy_addr, proxy_port, proxy_rdns, proxy_username, proxy_password) if source_address: sock.bind(source_address) sock.connect((remote_host, remote_port)) return sock except (socket.error, ProxyConnectionError) as e: err = e if sock: sock.close() sock = None if err: raise err raise socket.error("gai returned empty list.") class _BaseSocket(socket.socket): """Allows Python 2 delegated methods such as send() to be overridden.""" def __init__(self, *pos, **kw): _orig_socket.__init__(self, *pos, **kw) self._savedmethods = dict() for name in self._savenames: self._savedmethods[name] = getattr(self, name) delattr(self, name) # Allows normal overriding mechanism to work _savenames = list() def _makemethod(name): return lambda self, *pos, **kw: self._savedmethods[name](*pos, **kw) for name in ("sendto", "send", "recvfrom", "recv"): method = getattr(_BaseSocket, name, None) # Determine if the method is not defined the usual way # as a function in the class. # Python 2 uses __slots__, so there are descriptors for each method, # but they are not functions. if not isinstance(method, Callable): _BaseSocket._savenames.append(name) setattr(_BaseSocket, name, _makemethod(name)) class socksocket(_BaseSocket): """socksocket([family[, type[, proto]]]) -> socket object Open a SOCKS enabled socket. The parameters are the same as those of the standard socket init. In order for SOCKS to work, you must specify family=AF_INET and proto=0. The "type" argument must be either SOCK_STREAM or SOCK_DGRAM. """ default_proxy = None def __init__(self, family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0, *args, **kwargs): if type not in (socket.SOCK_STREAM, socket.SOCK_DGRAM): msg = "Socket type must be stream or datagram, not {!r}" raise ValueError(msg.format(type)) super(socksocket, self).__init__(family, type, proto, *args, **kwargs) self._proxyconn = None # TCP connection to keep UDP relay alive if self.default_proxy: self.proxy = self.default_proxy else: self.proxy = (None, None, None, None, None, None) self.proxy_sockname = None self.proxy_peername = None self._timeout = None def _readall(self, file, count): """Receive EXACTLY the number of bytes requested from the file object. Blocks until the required number of bytes have been received.""" data = b"" while len(data) < count: d = file.read(count - len(data)) if not d: raise GeneralProxyError("Connection closed unexpectedly") data += d return data def settimeout(self, timeout): self._timeout = timeout try: # test if we're connected, if so apply timeout peer = self.get_proxy_peername() super(socksocket, self).settimeout(self._timeout) except socket.error: pass def gettimeout(self): return self._timeout def setblocking(self, v): if v: self.settimeout(None) else: self.settimeout(0.0) def set_proxy(self, proxy_type=None, addr=None, port=None, rdns=True, username=None, password=None): """ Sets the proxy to be used. proxy_type - The type of the proxy to be used. Three types are supported: PROXY_TYPE_SOCKS4 (including socks4a), PROXY_TYPE_SOCKS5 and PROXY_TYPE_HTTP addr - The address of the server (IP or DNS). port - The port of the server. Defaults to 1080 for SOCKS servers and 8080 for HTTP proxy servers. rdns - Should DNS queries be performed on the remote side (rather than the local side). The default is True. Note: This has no effect with SOCKS4 servers. username - Username to authenticate with to the server. The default is no authentication. password - Password to authenticate with to the server. Only relevant when username is also provided.""" self.proxy = (proxy_type, addr, port, rdns, username.encode() if username else None, password.encode() if password else None) def setproxy(self, *args, **kwargs): if "proxytype" in kwargs: kwargs["proxy_type"] = kwargs.pop("proxytype") return self.set_proxy(*args, **kwargs) def bind(self, *pos, **kw): """Implements proxy connection for UDP sockets. Happens during the bind() phase.""" (proxy_type, proxy_addr, proxy_port, rdns, username, password) = self.proxy if not proxy_type or self.type != socket.SOCK_DGRAM: return _orig_socket.bind(self, *pos, **kw) if self._proxyconn: raise socket.error(EINVAL, "Socket already bound to an address") if proxy_type != SOCKS5: msg = "UDP only supported by SOCKS5 proxy type" raise socket.error(EOPNOTSUPP, msg) super(socksocket, self).bind(*pos, **kw) # Need to specify actual local port because # some relays drop packets if a port of zero is specified. # Avoid specifying host address in case of NAT though. _, port = self.getsockname() dst = ("0", port) self._proxyconn = _orig_socket() proxy = self._proxy_addr() self._proxyconn.connect(proxy) UDP_ASSOCIATE = b"\x03" _, relay = self._SOCKS5_request(self._proxyconn, UDP_ASSOCIATE, dst) # The relay is most likely on the same host as the SOCKS proxy, # but some proxies return a private IP address (10.x.y.z) host, _ = proxy _, port = relay super(socksocket, self).connect((host, port)) super(socksocket, self).settimeout(self._timeout) self.proxy_sockname = ("0.0.0.0", 0) # Unknown def sendto(self, bytes, *args, **kwargs): if self.type != socket.SOCK_DGRAM: return super(socksocket, self).sendto(bytes, *args, **kwargs) if not self._proxyconn: self.bind(("", 0)) address = args[-1] flags = args[:-1] header = BytesIO() RSV = b"\x00\x00" header.write(RSV) STANDALONE = b"\x00" header.write(STANDALONE) self._write_SOCKS5_address(address, header) sent = super(socksocket, self).send(header.getvalue() + bytes, *flags, **kwargs) return sent - header.tell() def send(self, bytes, flags=0, **kwargs): if self.type == socket.SOCK_DGRAM: return self.sendto(bytes, flags, self.proxy_peername, **kwargs) else: return super(socksocket, self).send(bytes, flags, **kwargs) def recvfrom(self, bufsize, flags=0): if self.type != socket.SOCK_DGRAM: return super(socksocket, self).recvfrom(bufsize, flags) if not self._proxyconn: self.bind(("", 0)) buf = BytesIO(super(socksocket, self).recv(bufsize + 1024, flags)) buf.seek(2, SEEK_CUR) frag = buf.read(1) if ord(frag): raise NotImplementedError("Received UDP packet fragment") fromhost, fromport = self._read_SOCKS5_address(buf) if self.proxy_peername: peerhost, peerport = self.proxy_peername if fromhost != peerhost or peerport not in (0, fromport): raise socket.error(EAGAIN, "Packet filtered") return (buf.read(bufsize), (fromhost, fromport)) def recv(self, *pos, **kw): bytes, _ = self.recvfrom(*pos, **kw) return bytes def close(self): if self._proxyconn: self._proxyconn.close() return super(socksocket, self).close() def get_proxy_sockname(self): """Returns the bound IP address and port number at the proxy.""" return self.proxy_sockname getproxysockname = get_proxy_sockname def get_proxy_peername(self): """ Returns the IP and port number of the proxy. """ return self.getpeername() getproxypeername = get_proxy_peername def get_peername(self): """Returns the IP address and port number of the destination machine. Note: get_proxy_peername returns the proxy.""" return self.proxy_peername getpeername = get_peername def _negotiate_SOCKS5(self, *dest_addr): """Negotiates a stream connection through a SOCKS5 server.""" CONNECT = b"\x01" self.proxy_peername, self.proxy_sockname = self._SOCKS5_request( self, CONNECT, dest_addr) def _SOCKS5_request(self, conn, cmd, dst): """ Send SOCKS5 request with given command (CMD field) and address (DST field). Returns resolved DST address that was used. """ proxy_type, addr, port, rdns, username, password = self.proxy writer = conn.makefile("wb") reader = conn.makefile("rb", 0) # buffering=0 renamed in Python 3 try: # First we'll send the authentication packages we support. if username and password: # The username/password details were supplied to the # set_proxy method so we support the USERNAME/PASSWORD # authentication (in addition to the standard none). writer.write(b"\x05\x02\x00\x02") else: # No username/password were entered, therefore we # only support connections with no authentication. writer.write(b"\x05\x01\x00") # We'll receive the server's response to determine which # method was selected writer.flush() chosen_auth = self._readall(reader, 2) if chosen_auth[0:1] != b"\x05": # Note: string[i:i+1] is used because indexing of a bytestring # via bytestring[i] yields an integer in Python 3 raise GeneralProxyError( "SOCKS5 proxy server sent invalid data") # Check the chosen authentication method if chosen_auth[1:2] == b"\x02": # Okay, we need to perform a basic username/password # authentication. writer.write(b"\x01" + chr(len(username)).encode() + username + chr(len(password)).encode() + password) writer.flush() auth_status = self._readall(reader, 2) if auth_status[0:1] != b"\x01": # Bad response raise GeneralProxyError( "SOCKS5 proxy server sent invalid data") if auth_status[1:2] != b"\x00": # Authentication failed raise SOCKS5AuthError("SOCKS5 authentication failed") # Otherwise, authentication succeeded # No authentication is required if 0x00 elif chosen_auth[1:2] != b"\x00": # Reaching here is always bad if chosen_auth[1:2] == b"\xFF": raise SOCKS5AuthError( "All offered SOCKS5 authentication methods were" " rejected") else: raise GeneralProxyError( "SOCKS5 proxy server sent invalid data") # Now we can request the actual connection writer.write(b"\x05" + cmd + b"\x00") resolved = self._write_SOCKS5_address(dst, writer) writer.flush() # Get the response resp = self._readall(reader, 3) if resp[0:1] != b"\x05": raise GeneralProxyError( "SOCKS5 proxy server sent invalid data") status = ord(resp[1:2]) if status != 0x00: # Connection failed: server returned an error error = SOCKS5_ERRORS.get(status, "Unknown error") raise SOCKS5Error("{0:#04x}: {1}".format(status, error)) # Get the bound address/port bnd = self._read_SOCKS5_address(reader) super(socksocket, self).settimeout(self._timeout) return (resolved, bnd) finally: reader.close() writer.close() def _write_SOCKS5_address(self, addr, file): """ Return the host and port packed for the SOCKS5 protocol, and the resolved address as a tuple object. """ host, port = addr proxy_type, _, _, rdns, username, password = self.proxy family_to_byte = {socket.AF_INET: b"\x01", socket.AF_INET6: b"\x04"} # If the given destination address is an IP address, we'll # use the IP address request even if remote resolving was specified. # Detect whether the address is IPv4/6 directly. for family in (socket.AF_INET, socket.AF_INET6): try: addr_bytes = socket.inet_pton(family, host) file.write(family_to_byte[family] + addr_bytes) host = socket.inet_ntop(family, addr_bytes) file.write(struct.pack(">H", port)) return host, port except socket.error: continue # Well it's not an IP number, so it's probably a DNS name. if rdns: # Resolve remotely host_bytes = host.encode("idna") file.write(b"\x03" + chr(len(host_bytes)).encode() + host_bytes) else: # Resolve locally addresses = socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM, socket.IPPROTO_TCP, socket.AI_ADDRCONFIG) # We can't really work out what IP is reachable, so just pick the # first. target_addr = addresses[0] family = target_addr[0] host = target_addr[4][0] addr_bytes = socket.inet_pton(family, host) file.write(family_to_byte[family] + addr_bytes) host = socket.inet_ntop(family, addr_bytes) file.write(struct.pack(">H", port)) return host, port def _read_SOCKS5_address(self, file): atyp = self._readall(file, 1) if atyp == b"\x01": addr = socket.inet_ntoa(self._readall(file, 4)) elif atyp == b"\x03": length = self._readall(file, 1) addr = self._readall(file, ord(length)) elif atyp == b"\x04": addr = socket.inet_ntop(socket.AF_INET6, self._readall(file, 16)) else: raise GeneralProxyError("SOCKS5 proxy server sent invalid data") port = struct.unpack(">H", self._readall(file, 2))[0] return addr, port def _negotiate_SOCKS4(self, dest_addr, dest_port): """Negotiates a connection through a SOCKS4 server.""" proxy_type, addr, port, rdns, username, password = self.proxy writer = self.makefile("wb") reader = self.makefile("rb", 0) # buffering=0 renamed in Python 3 try: # Check if the destination address provided is an IP address remote_resolve = False try: addr_bytes = socket.inet_aton(dest_addr) except socket.error: # It's a DNS name. Check where it should be resolved. if rdns: addr_bytes = b"\x00\x00\x00\x01" remote_resolve = True else: addr_bytes = socket.inet_aton( socket.gethostbyname(dest_addr)) # Construct the request packet writer.write(struct.pack(">BBH", 0x04, 0x01, dest_port)) writer.write(addr_bytes) # The username parameter is considered userid for SOCKS4 if username: writer.write(username) writer.write(b"\x00") # DNS name if remote resolving is required # NOTE: This is actually an extension to the SOCKS4 protocol # called SOCKS4A and may not be supported in all cases. if remote_resolve: writer.write(dest_addr.encode("idna") + b"\x00") writer.flush() # Get the response from the server resp = self._readall(reader, 8) if resp[0:1] != b"\x00": # Bad data raise GeneralProxyError( "SOCKS4 proxy server sent invalid data") status = ord(resp[1:2]) if status != 0x5A: # Connection failed: server returned an error error = SOCKS4_ERRORS.get(status, "Unknown error") raise SOCKS4Error("{0:#04x}: {1}".format(status, error)) # Get the bound address/port self.proxy_sockname = (socket.inet_ntoa(resp[4:]), struct.unpack(">H", resp[2:4])[0]) if remote_resolve: self.proxy_peername = socket.inet_ntoa(addr_bytes), dest_port else: self.proxy_peername = dest_addr, dest_port finally: reader.close() writer.close() def _negotiate_HTTP(self, dest_addr, dest_port): """Negotiates a connection through an HTTP server. NOTE: This currently only supports HTTP CONNECT-style proxies.""" proxy_type, addr, port, rdns, username, password = self.proxy # If we need to resolve locally, we do this now addr = dest_addr if rdns else socket.gethostbyname(dest_addr) http_headers = [ (b"CONNECT " + addr.encode("idna") + b":" + str(dest_port).encode() + b" HTTP/1.1"), b"Host: " + dest_addr.encode("idna") ] if username and password: http_headers.append(b"Proxy-Authorization: basic " + b64encode(username + b":" + password)) http_headers.append(b"\r\n") self.sendall(b"\r\n".join(http_headers)) # We just need the first line to check if the connection was successful fobj = self.makefile() status_line = fobj.readline() fobj.close() if not status_line: raise GeneralProxyError("Connection closed unexpectedly") try: proto, status_code, status_msg = status_line.split(" ", 2) except ValueError: raise GeneralProxyError("HTTP proxy server sent invalid response") if not proto.startswith("HTTP/"): raise GeneralProxyError( "Proxy server does not appear to be an HTTP proxy") try: status_code = int(status_code) except ValueError: raise HTTPError( "HTTP proxy server did not return a valid HTTP status") if status_code != 200: error = "{0}: {1}".format(status_code, status_msg) if status_code in (400, 403, 405): # It's likely that the HTTP proxy server does not support the # CONNECT tunneling method error += ("\n[*] Note: The HTTP proxy server may not be" " supported by PySocks (must be a CONNECT tunnel" " proxy)") raise HTTPError(error) self.proxy_sockname = (b"0.0.0.0", 0) self.proxy_peername = addr, dest_port _proxy_negotiators = { SOCKS4: _negotiate_SOCKS4, SOCKS5: _negotiate_SOCKS5, HTTP: _negotiate_HTTP } @set_self_blocking def connect(self, dest_pair): """ Connects to the specified destination through a proxy. Uses the same API as socket's connect(). To select the proxy server, use set_proxy(). dest_pair - 2-tuple of (IP/hostname, port). """ if len(dest_pair) != 2 or dest_pair[0].startswith("["): # Probably IPv6, not supported -- raise an error, and hope # Happy Eyeballs (RFC6555) makes sure at least the IPv4 # connection works... raise socket.error("PySocks doesn't support IPv6: %s" % str(dest_pair)) dest_addr, dest_port = dest_pair if self.type == socket.SOCK_DGRAM: if not self._proxyconn: self.bind(("", 0)) dest_addr = socket.gethostbyname(dest_addr) # If the host address is INADDR_ANY or similar, reset the peer # address so that packets are received from any peer if dest_addr == "0.0.0.0" and not dest_port: self.proxy_peername = None else: self.proxy_peername = (dest_addr, dest_port) return (proxy_type, proxy_addr, proxy_port, rdns, username, password) = self.proxy # Do a minimal input check first if (not isinstance(dest_pair, (list, tuple)) or len(dest_pair) != 2 or not dest_addr or not isinstance(dest_port, int)): # Inputs failed, raise an error raise GeneralProxyError( "Invalid destination-connection (host, port) pair") # We set the timeout here so that we don't hang in connection or during # negotiation. super(socksocket, self).settimeout(self._timeout) if proxy_type is None: # Treat like regular socket object self.proxy_peername = dest_pair super(socksocket, self).settimeout(self._timeout) super(socksocket, self).connect((dest_addr, dest_port)) return proxy_addr = self._proxy_addr() try: # Initial connection to proxy server. super(socksocket, self).connect(proxy_addr) except socket.error as error: # Error while connecting to proxy self.close() proxy_addr, proxy_port = proxy_addr proxy_server = "{0}:{1}".format(proxy_addr, proxy_port) printable_type = PRINTABLE_PROXY_TYPES[proxy_type] msg = "Error connecting to {0} proxy {1}".format(printable_type, proxy_server) log.debug("%s due to: %s", msg, error) raise ProxyConnectionError(msg, error) else: # Connected to proxy server, now negotiate try: # Calls negotiate_{SOCKS4, SOCKS5, HTTP} negotiate = self._proxy_negotiators[proxy_type] negotiate(self, dest_addr, dest_port) except socket.error as error: # Wrap socket errors self.close() raise GeneralProxyError("Socket error", error) except ProxyError: # Protocol error while negotiating with proxy self.close() raise def _proxy_addr(self): """ Return proxy address to connect to as tuple object """ (proxy_type, proxy_addr, proxy_port, rdns, username, password) = self.proxy proxy_port = proxy_port or DEFAULT_PORTS.get(proxy_type) if not proxy_port: raise GeneralProxyError("Invalid proxy type") return proxy_addr, proxy_port site-packages/iotop/__init__.py 0000644 00000000000 15152656744 0012534 0 ustar 00 site-packages/iotop/data.py 0000644 00000036132 15152656744 0011725 0 ustar 00 # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Library General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # # See the COPYING file for license information. # # Copyright (c) 2007 Guillaume Chazarain <guichaz@gmail.com> # Allow printing with same syntax in Python 2/3 from __future__ import print_function import errno import os import pprint import pwd import stat import struct import sys import time from procfs import cmdline # # Check for requirements: # o Linux >= 2.6.20 with I/O accounting and VM event counters # ioaccounting = os.path.exists('/proc/self/io') try: from iotop.vmstat import VmStat vmstat_f = VmStat() except: vm_event_counters = False else: vm_event_counters = True if not ioaccounting or not vm_event_counters: print('Could not run iotop as some of the requirements are not met:') print('- Linux >= 2.6.20 with') if not ioaccounting: print(' - I/O accounting support ' \ '(CONFIG_TASKSTATS, CONFIG_TASK_DELAY_ACCT, ' \ 'CONFIG_TASK_IO_ACCOUNTING, kernel.task_delayacct sysctl)') if not vm_event_counters: print(' - VM event counters (CONFIG_VM_EVENT_COUNTERS)') sys.exit(1) from iotop import ioprio, vmstat from iotop.netlink import Connection, NETLINK_GENERIC, U32Attr, NLM_F_REQUEST from iotop.genetlink import Controller, GeNlMessage class DumpableObject(object): """Base class for all objects that allows easy introspection when printed""" def __repr__(self): return '%s: %s>' % (str(type(self))[:-1], pprint.pformat(self.__dict__)) # # Interesting fields in a taskstats output # class Stats(DumpableObject): members_offsets = [ ('blkio_delay_total', 40), ('swapin_delay_total', 56), ('read_bytes', 248), ('write_bytes', 256), ('cancelled_write_bytes', 264) ] has_blkio_delay_total = None def __init__(self, task_stats_buffer): sd = self.__dict__ for name, offset in Stats.members_offsets: data = task_stats_buffer[offset:offset + 8] sd[name] = struct.unpack('Q', data)[0] # This is a heuristic to detect if CONFIG_TASK_DELAY_ACCT is enabled in # the kernel. if not Stats.has_blkio_delay_total: Stats.has_blkio_delay_total = sysctl_task_delayacct() def accumulate(self, other_stats, destination, coeff=1): """Update destination from operator(self, other_stats)""" dd = destination.__dict__ sd = self.__dict__ od = other_stats.__dict__ for member, offset in Stats.members_offsets: dd[member] = sd[member] + coeff * od[member] def delta(self, other_stats, destination): """Update destination with self - other_stats""" return self.accumulate(other_stats, destination, coeff=-1) def is_all_zero(self): sd = self.__dict__ for name, offset in Stats.members_offsets: if sd[name] != 0: return False return True @staticmethod def build_all_zero(): stats = Stats.__new__(Stats) std = stats.__dict__ for name, offset in Stats.members_offsets: std[name] = 0 return stats # # Netlink usage for taskstats # TASKSTATS_CMD_GET = 1 TASKSTATS_CMD_ATTR_PID = 1 TASKSTATS_TYPE_AGGR_PID = 4 TASKSTATS_TYPE_PID = 1 TASKSTATS_TYPE_STATS = 3 class TaskStatsNetlink(object): # Keep in sync with format_stats() and pinfo.did_some_io() def __init__(self, options): self.options = options self.connection = Connection(NETLINK_GENERIC) controller = Controller(self.connection) self.family_id = controller.get_family_id('TASKSTATS') def build_request(self, tid): return GeNlMessage(self.family_id, cmd=TASKSTATS_CMD_GET, attrs=[U32Attr(TASKSTATS_CMD_ATTR_PID, tid)], flags=NLM_F_REQUEST) def get_single_task_stats(self, thread): thread.task_stats_request.send(self.connection) try: reply = GeNlMessage.recv(self.connection) except OSError as e: if e.errno == errno.ESRCH: # OSError: Netlink error: No such process (3) return raise for attr_type, attr_value in reply.attrs.items(): if attr_type == TASKSTATS_TYPE_AGGR_PID: reply = attr_value.nested() break else: return taskstats_data = reply[TASKSTATS_TYPE_STATS].data if len(taskstats_data) < 272: # Short reply return taskstats_version = struct.unpack('H', taskstats_data[:2])[0] assert taskstats_version >= 4 return Stats(taskstats_data) # # PIDs manipulations # def find_uids(options): """Build options.uids from options.users by resolving usernames to UIDs""" options.uids = [] error = False for u in options.users or []: try: uid = int(u) except ValueError: try: passwd = pwd.getpwnam(u) except KeyError: print('Unknown user:', u, file=sys.stderr) error = True else: uid = passwd.pw_uid if not error: options.uids.append(uid) if error: sys.exit(1) def parse_proc_pid_status(pid): result_dict = {} try: for line in open('/proc/%d/status' % pid): key, value = line.split(':\t', 1) result_dict[key] = value.strip() except IOError: pass # No such process return result_dict def safe_utf8_decode(s): try: return s.decode('utf-8') except UnicodeDecodeError: return s.encode('string_escape') except AttributeError: return s class ThreadInfo(DumpableObject): """Stats for a single thread""" def __init__(self, tid, taskstats_connection): self.tid = tid self.mark = True self.stats_total = None self.stats_delta = Stats.__new__(Stats) self.task_stats_request = taskstats_connection.build_request(tid) def get_ioprio(self): return ioprio.get(self.tid) def set_ioprio(self, ioprio_class, ioprio_data): return ioprio.set_ioprio(ioprio.IOPRIO_WHO_PROCESS, self.tid, ioprio_class, ioprio_data) def update_stats(self, stats): if not self.stats_total: self.stats_total = stats stats.delta(self.stats_total, self.stats_delta) self.stats_total = stats class ProcessInfo(DumpableObject): """Stats for a single process (a single line in the output): if options.processes is set, it is a collection of threads, otherwise a single thread.""" def __init__(self, pid): self.pid = pid self.uid = None self.user = None self.threads = {} # {tid: ThreadInfo} self.stats_delta = Stats.build_all_zero() self.stats_accum = Stats.build_all_zero() self.stats_accum_timestamp = time.time() def is_monitored(self, options): if (options.pids and not options.processes and self.pid not in options.pids): # We only monitor some threads, not this one return False if options.uids and self.get_uid() not in options.uids: # We only monitor some users, not this one return False return True def get_uid(self): if self.uid: return self.uid # uid in (None, 0) means either we don't know the UID yet or the process # runs as root so it can change its UID. In both cases it means we have # to find out its current UID. try: uid = os.stat('/proc/%d' % self.pid)[stat.ST_UID] except OSError: # The process disappeared uid = None if uid != self.uid: # Maybe the process called setuid() self.user = None self.uid = uid return uid def get_user(self): uid = self.get_uid() if uid is not None and not self.user: try: self.user = safe_utf8_decode(pwd.getpwuid(uid).pw_name) except (KeyError, AttributeError): self.user = str(uid) return self.user or '{none}' def get_cmdline(self): # A process may exec, so we must always reread its cmdline try: proc_cmdline = open('/proc/%d/cmdline' % self.pid) cmdline = proc_cmdline.read(4096) except IOError: return '{no such process}' proc_status = parse_proc_pid_status(self.pid) if not cmdline: # Probably a kernel thread, get its name from /proc/PID/status proc_status_name = proc_status.get('Name', '') if proc_status_name: proc_status_name = '[%s]' % proc_status_name else: proc_status_name = '{no name}' return proc_status_name suffix = '' tgid = int(proc_status.get('Tgid', self.pid)) if tgid != self.pid: # Not the main thread, maybe it has a custom name tgid_name = parse_proc_pid_status(tgid).get('Name', '') thread_name = proc_status.get('Name', '') if thread_name != tgid_name: suffix += ' [%s]' % thread_name parts = cmdline.split('\0') if parts[0].startswith('/'): first_command_char = parts[0].rfind('/') + 1 parts[0] = parts[0][first_command_char:] cmdline = ' '.join(parts).strip() return safe_utf8_decode(cmdline + suffix) def did_some_io(self, accumulated): if accumulated: return not self.stats_accum.is_all_zero() for t in self.threads.values(): if not t.stats_delta.is_all_zero(): return True return False def get_ioprio(self): priorities = set(t.get_ioprio() for t in self.threads.values()) if len(priorities) == 1: return priorities.pop() return '?dif' def set_ioprio(self, ioprio_class, ioprio_data): for thread in self.threads.values(): thread.set_ioprio(ioprio_class, ioprio_data) def ioprio_sort_key(self): return ioprio.sort_key(self.get_ioprio()) def get_thread(self, tid, taskstats_connection): thread = self.threads.get(tid, None) if not thread: thread = ThreadInfo(tid, taskstats_connection) self.threads[tid] = thread return thread def update_stats(self): stats_delta = Stats.build_all_zero() for tid, thread in self.threads.items(): if not thread.mark: stats_delta.accumulate(thread.stats_delta, stats_delta) self.threads = dict([(tid, thread) for tid, thread in self.threads.items() if not thread.mark]) nr_threads = len(self.threads) if not nr_threads: return False stats_delta.blkio_delay_total /= nr_threads stats_delta.swapin_delay_total /= nr_threads self.stats_delta = stats_delta self.stats_accum.accumulate(self.stats_delta, self.stats_accum) return True class ProcessList(DumpableObject): def __init__(self, taskstats_connection, options): # {pid: ProcessInfo} self.processes = {} self.taskstats_connection = taskstats_connection self.options = options self.timestamp = time.time() self.vmstat = vmstat.VmStat() # A first time as we are interested in the delta self.update_process_counts() def get_process(self, pid): """Either get the specified PID from self.processes or build a new ProcessInfo if we see this PID for the first time""" process = self.processes.get(pid, None) if not process: process = ProcessInfo(pid) self.processes[pid] = process if process.is_monitored(self.options): return process def list_tgids(self): if self.options.pids: return self.options.pids tgids = os.listdir('/proc') if self.options.processes: return [int(tgid) for tgid in tgids if '0' <= tgid[0] <= '9'] tids = [] for tgid in tgids: if '0' <= tgid[0] <= '9': try: tids.extend(map(int, os.listdir('/proc/' + tgid + '/task'))) except OSError: # The PID went away pass return tids def list_tids(self, tgid): if not self.options.processes: return [tgid] try: tids = list(map(int, os.listdir('/proc/%d/task' % tgid))) except OSError: return [] if self.options.pids: tids = list(set(self.options.pids).intersection(set(tids))) return tids def update_process_counts(self): new_timestamp = time.time() self.duration = new_timestamp - self.timestamp self.timestamp = new_timestamp total_read = total_write = 0 for tgid in self.list_tgids(): process = self.get_process(tgid) if not process: continue for tid in self.list_tids(tgid): thread = process.get_thread(tid, self.taskstats_connection) stats = self.taskstats_connection.get_single_task_stats(thread) if stats: thread.update_stats(stats) delta = thread.stats_delta total_read += delta.read_bytes total_write += delta.write_bytes thread.mark = False return (total_read, total_write), self.vmstat.delta() def refresh_processes(self): for process in self.processes.values(): for thread in process.threads.values(): thread.mark = True total_read_and_write = self.update_process_counts() self.processes = dict([(pid, process) for pid, process in self.processes.items() if process.update_stats()]) return total_read_and_write def clear(self): self.processes = {} def sysctl_task_delayacct(): """ WAS: try: with open('/proc/sys/kernel/task_delayacct') as f: return bool(int(f.read().strip())) except FileNotFoundError: return None Because /proc/sys/kernel/task_delayacct doesn't exist on RHEL8, it always returns None, which is equivalent to False in the end. On RHEL8, delayacct_on kernel variable is enabled by default """ return 'nodelayacct' not in cmdline().keys() site-packages/iotop/genetlink.py 0000644 00000003774 15152656744 0013002 0 ustar 00 ''' Netlink message generation/parsing Copyright 2007 Johannes Berg <johannes@sipsolutions.net> GPLv2+; See copying for details. ''' import struct from iotop.netlink import NLM_F_REQUEST, NLMSG_MIN_TYPE, Message, parse_attributes from iotop.netlink import NulStrAttr, Connection, NETLINK_GENERIC CTRL_CMD_UNSPEC = 0 CTRL_CMD_NEWFAMILY = 1 CTRL_CMD_DELFAMILY = 2 CTRL_CMD_GETFAMILY = 3 CTRL_CMD_NEWOPS = 4 CTRL_CMD_DELOPS = 5 CTRL_CMD_GETOPS = 6 CTRL_ATTR_UNSPEC = 0 CTRL_ATTR_FAMILY_ID = 1 CTRL_ATTR_FAMILY_NAME = 2 CTRL_ATTR_VERSION = 3 CTRL_ATTR_HDRSIZE = 4 CTRL_ATTR_MAXATTR = 5 CTRL_ATTR_OPS = 6 class GenlHdr: def __init__(self, cmd, version = 0): self.cmd = cmd self.version = version def _dump(self): return struct.pack("BBxx", self.cmd, self.version) def _genl_hdr_parse(data): return GenlHdr(*struct.unpack("BBxx", data)) GENL_ID_CTRL = NLMSG_MIN_TYPE class GeNlMessage(Message): def __init__(self, family, cmd, attrs=[], flags=0): self.cmd = cmd self.attrs = attrs self.family = family Message.__init__(self, family, flags=flags, payload=[GenlHdr(self.cmd)]+attrs) @staticmethod def recv(conn): msg = conn.recv() packet = msg.payload hdr = _genl_hdr_parse(packet[:4]) genlmsg = GeNlMessage(msg.type, hdr.cmd, [], msg.flags) genlmsg.attrs = parse_attributes(packet[4:]) genlmsg.version = hdr.version return genlmsg class Controller: def __init__(self, conn): self.conn = conn def get_family_id(self, family): a = NulStrAttr(CTRL_ATTR_FAMILY_NAME, family) m = GeNlMessage(GENL_ID_CTRL, CTRL_CMD_GETFAMILY, flags=NLM_F_REQUEST, attrs=[a]) m.send(self.conn) m = GeNlMessage.recv(self.conn) return m.attrs[CTRL_ATTR_FAMILY_ID].u16() connection = Connection(NETLINK_GENERIC) controller = Controller(connection) site-packages/iotop/__pycache__/genetlink.cpython-36.opt-1.pyc 0000644 00000005263 15152656744 0020220 0 ustar 00 3 B��Q� � @ s� d Z ddlZddlmZmZmZmZ ddlmZmZm Z dZ dZdZdZ dZd Zd ZdZdZdZdZdZd Zd ZG dd� d�Zd d� ZeZG dd� de�ZG dd� d�Zee �Zee�ZdS )z� Netlink message generation/parsing Copyright 2007 Johannes Berg <johannes@sipsolutions.net> GPLv2+; See copying for details. � N)� NLM_F_REQUEST�NLMSG_MIN_TYPE�Message�parse_attributes)� NulStrAttr� Connection�NETLINK_GENERIC� � � � � � c @ s e Zd Zddd�Zdd� ZdS )�GenlHdrr c C s || _ || _d S )N)�cmd�version)�selfr r � r �/usr/lib/python3.6/genetlink.py�__init__ s zGenlHdr.__init__c C s t jd| j| j�S )N�BBxx)�struct�packr r )r r r r �_dump! s z GenlHdr._dumpN)r )�__name__� __module__�__qualname__r r r r r r r s r c C s t tjd| �� S )Nr )r r �unpack)�datar r r �_genl_hdr_parse$ s r c @ s&