Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../lib/python3..../site-pac...
File: socks.py
"""SocksiPy - Python SOCKS module.
[0] Fix | Delete
[1] Fix | Delete
Copyright 2006 Dan-Haim. All rights reserved.
[2] Fix | Delete
[3] Fix | Delete
Redistribution and use in source and binary forms, with or without
[4] Fix | Delete
modification, are permitted provided that the following conditions are met:
[5] Fix | Delete
1. Redistributions of source code must retain the above copyright notice, this
[6] Fix | Delete
list of conditions and the following disclaimer.
[7] Fix | Delete
2. Redistributions in binary form must reproduce the above copyright notice,
[8] Fix | Delete
this list of conditions and the following disclaimer in the documentation
[9] Fix | Delete
and/or other materials provided with the distribution.
[10] Fix | Delete
3. Neither the name of Dan Haim nor the names of his contributors may be used
[11] Fix | Delete
to endorse or promote products derived from this software without specific
[12] Fix | Delete
prior written permission.
[13] Fix | Delete
[14] Fix | Delete
THIS SOFTWARE IS PROVIDED BY DAN HAIM "AS IS" AND ANY EXPRESS OR IMPLIED
[15] Fix | Delete
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
[16] Fix | Delete
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
[17] Fix | Delete
EVENT SHALL DAN HAIM OR HIS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
[18] Fix | Delete
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
[19] Fix | Delete
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA
[20] Fix | Delete
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
[21] Fix | Delete
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
[22] Fix | Delete
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
[23] Fix | Delete
[24] Fix | Delete
[25] Fix | Delete
This module provides a standard socket-like interface for Python
[26] Fix | Delete
for tunneling connections through SOCKS proxies.
[27] Fix | Delete
[28] Fix | Delete
===============================================================================
[29] Fix | Delete
[30] Fix | Delete
Minor modifications made by Christopher Gilbert (http://motomastyle.com/)
[31] Fix | Delete
for use in PyLoris (http://pyloris.sourceforge.net/)
[32] Fix | Delete
[33] Fix | Delete
Minor modifications made by Mario Vilas (http://breakingcode.wordpress.com/)
[34] Fix | Delete
mainly to merge bug fixes found in Sourceforge
[35] Fix | Delete
[36] Fix | Delete
Modifications made by Anorov (https://github.com/Anorov)
[37] Fix | Delete
-Forked and renamed to PySocks
[38] Fix | Delete
-Fixed issue with HTTP proxy failure checking (same bug that was in the
[39] Fix | Delete
old ___recvall() method)
[40] Fix | Delete
-Included SocksiPyHandler (sockshandler.py), to be used as a urllib2 handler,
[41] Fix | Delete
courtesy of e000 (https://github.com/e000):
[42] Fix | Delete
https://gist.github.com/869791#file_socksipyhandler.py
[43] Fix | Delete
-Re-styled code to make it readable
[44] Fix | Delete
-Aliased PROXY_TYPE_SOCKS5 -> SOCKS5 etc.
[45] Fix | Delete
-Improved exception handling and output
[46] Fix | Delete
-Removed irritating use of sequence indexes, replaced with tuple unpacked
[47] Fix | Delete
variables
[48] Fix | Delete
-Fixed up Python 3 bytestring handling - chr(0x03).encode() -> b"\x03"
[49] Fix | Delete
-Other general fixes
[50] Fix | Delete
-Added clarification that the HTTP proxy connection method only supports
[51] Fix | Delete
CONNECT-style tunneling HTTP proxies
[52] Fix | Delete
-Various small bug fixes
[53] Fix | Delete
"""
[54] Fix | Delete
[55] Fix | Delete
from base64 import b64encode
[56] Fix | Delete
from collections import Callable
[57] Fix | Delete
from errno import EOPNOTSUPP, EINVAL, EAGAIN
[58] Fix | Delete
import functools
[59] Fix | Delete
from io import BytesIO
[60] Fix | Delete
import logging
[61] Fix | Delete
import os
[62] Fix | Delete
from os import SEEK_CUR
[63] Fix | Delete
import socket
[64] Fix | Delete
import struct
[65] Fix | Delete
import sys
[66] Fix | Delete
[67] Fix | Delete
__version__ = "1.6.7"
[68] Fix | Delete
[69] Fix | Delete
[70] Fix | Delete
if os.name == "nt" and sys.version_info < (3, 0):
[71] Fix | Delete
try:
[72] Fix | Delete
import win_inet_pton
[73] Fix | Delete
except ImportError:
[74] Fix | Delete
raise ImportError(
[75] Fix | Delete
"To run PySocks on Windows you must install win_inet_pton")
[76] Fix | Delete
[77] Fix | Delete
log = logging.getLogger(__name__)
[78] Fix | Delete
[79] Fix | Delete
PROXY_TYPE_SOCKS4 = SOCKS4 = 1
[80] Fix | Delete
PROXY_TYPE_SOCKS5 = SOCKS5 = 2
[81] Fix | Delete
PROXY_TYPE_HTTP = HTTP = 3
[82] Fix | Delete
[83] Fix | Delete
PROXY_TYPES = {"SOCKS4": SOCKS4, "SOCKS5": SOCKS5, "HTTP": HTTP}
[84] Fix | Delete
PRINTABLE_PROXY_TYPES = dict(zip(PROXY_TYPES.values(), PROXY_TYPES.keys()))
[85] Fix | Delete
[86] Fix | Delete
_orgsocket = _orig_socket = socket.socket
[87] Fix | Delete
[88] Fix | Delete
[89] Fix | Delete
def set_self_blocking(function):
[90] Fix | Delete
[91] Fix | Delete
@functools.wraps(function)
[92] Fix | Delete
def wrapper(*args, **kwargs):
[93] Fix | Delete
self = args[0]
[94] Fix | Delete
try:
[95] Fix | Delete
_is_blocking = self.gettimeout()
[96] Fix | Delete
if _is_blocking == 0:
[97] Fix | Delete
self.setblocking(True)
[98] Fix | Delete
return function(*args, **kwargs)
[99] Fix | Delete
except Exception as e:
[100] Fix | Delete
raise
[101] Fix | Delete
finally:
[102] Fix | Delete
# set orgin blocking
[103] Fix | Delete
if _is_blocking == 0:
[104] Fix | Delete
self.setblocking(False)
[105] Fix | Delete
return wrapper
[106] Fix | Delete
[107] Fix | Delete
[108] Fix | Delete
class ProxyError(IOError):
[109] Fix | Delete
"""Socket_err contains original socket.error exception."""
[110] Fix | Delete
def __init__(self, msg, socket_err=None):
[111] Fix | Delete
self.msg = msg
[112] Fix | Delete
self.socket_err = socket_err
[113] Fix | Delete
[114] Fix | Delete
if socket_err:
[115] Fix | Delete
self.msg += ": {0}".format(socket_err)
[116] Fix | Delete
[117] Fix | Delete
def __str__(self):
[118] Fix | Delete
return self.msg
[119] Fix | Delete
[120] Fix | Delete
[121] Fix | Delete
class GeneralProxyError(ProxyError):
[122] Fix | Delete
pass
[123] Fix | Delete
[124] Fix | Delete
[125] Fix | Delete
class ProxyConnectionError(ProxyError):
[126] Fix | Delete
pass
[127] Fix | Delete
[128] Fix | Delete
[129] Fix | Delete
class SOCKS5AuthError(ProxyError):
[130] Fix | Delete
pass
[131] Fix | Delete
[132] Fix | Delete
[133] Fix | Delete
class SOCKS5Error(ProxyError):
[134] Fix | Delete
pass
[135] Fix | Delete
[136] Fix | Delete
[137] Fix | Delete
class SOCKS4Error(ProxyError):
[138] Fix | Delete
pass
[139] Fix | Delete
[140] Fix | Delete
[141] Fix | Delete
class HTTPError(ProxyError):
[142] Fix | Delete
pass
[143] Fix | Delete
[144] Fix | Delete
SOCKS4_ERRORS = {
[145] Fix | Delete
0x5B: "Request rejected or failed",
[146] Fix | Delete
0x5C: ("Request rejected because SOCKS server cannot connect to identd on"
[147] Fix | Delete
" the client"),
[148] Fix | Delete
0x5D: ("Request rejected because the client program and identd report"
[149] Fix | Delete
" different user-ids")
[150] Fix | Delete
}
[151] Fix | Delete
[152] Fix | Delete
SOCKS5_ERRORS = {
[153] Fix | Delete
0x01: "General SOCKS server failure",
[154] Fix | Delete
0x02: "Connection not allowed by ruleset",
[155] Fix | Delete
0x03: "Network unreachable",
[156] Fix | Delete
0x04: "Host unreachable",
[157] Fix | Delete
0x05: "Connection refused",
[158] Fix | Delete
0x06: "TTL expired",
[159] Fix | Delete
0x07: "Command not supported, or protocol error",
[160] Fix | Delete
0x08: "Address type not supported"
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
DEFAULT_PORTS = {SOCKS4: 1080, SOCKS5: 1080, HTTP: 8080}
[164] Fix | Delete
[165] Fix | Delete
[166] Fix | Delete
def set_default_proxy(proxy_type=None, addr=None, port=None, rdns=True,
[167] Fix | Delete
username=None, password=None):
[168] Fix | Delete
"""Sets a default proxy.
[169] Fix | Delete
[170] Fix | Delete
All further socksocket objects will use the default unless explicitly
[171] Fix | Delete
changed. All parameters are as for socket.set_proxy()."""
[172] Fix | Delete
socksocket.default_proxy = (proxy_type, addr, port, rdns,
[173] Fix | Delete
username.encode() if username else None,
[174] Fix | Delete
password.encode() if password else None)
[175] Fix | Delete
[176] Fix | Delete
[177] Fix | Delete
def setdefaultproxy(*args, **kwargs):
[178] Fix | Delete
if "proxytype" in kwargs:
[179] Fix | Delete
kwargs["proxy_type"] = kwargs.pop("proxytype")
[180] Fix | Delete
return set_default_proxy(*args, **kwargs)
[181] Fix | Delete
[182] Fix | Delete
[183] Fix | Delete
def get_default_proxy():
[184] Fix | Delete
"""Returns the default proxy, set by set_default_proxy."""
[185] Fix | Delete
return socksocket.default_proxy
[186] Fix | Delete
[187] Fix | Delete
getdefaultproxy = get_default_proxy
[188] Fix | Delete
[189] Fix | Delete
[190] Fix | Delete
def wrap_module(module):
[191] Fix | Delete
"""Attempts to replace a module's socket library with a SOCKS socket.
[192] Fix | Delete
[193] Fix | Delete
Must set a default proxy using set_default_proxy(...) first. This will
[194] Fix | Delete
only work on modules that import socket directly into the namespace;
[195] Fix | Delete
most of the Python Standard Library falls into this category."""
[196] Fix | Delete
if socksocket.default_proxy:
[197] Fix | Delete
module.socket.socket = socksocket
[198] Fix | Delete
else:
[199] Fix | Delete
raise GeneralProxyError("No default proxy specified")
[200] Fix | Delete
[201] Fix | Delete
wrapmodule = wrap_module
[202] Fix | Delete
[203] Fix | Delete
[204] Fix | Delete
def create_connection(dest_pair,
[205] Fix | Delete
timeout=None, source_address=None,
[206] Fix | Delete
proxy_type=None, proxy_addr=None,
[207] Fix | Delete
proxy_port=None, proxy_rdns=True,
[208] Fix | Delete
proxy_username=None, proxy_password=None,
[209] Fix | Delete
socket_options=None):
[210] Fix | Delete
"""create_connection(dest_pair, *[, timeout], **proxy_args) -> socket object
[211] Fix | Delete
[212] Fix | Delete
Like socket.create_connection(), but connects to proxy
[213] Fix | Delete
before returning the socket object.
[214] Fix | Delete
[215] Fix | Delete
dest_pair - 2-tuple of (IP/hostname, port).
[216] Fix | Delete
**proxy_args - Same args passed to socksocket.set_proxy() if present.
[217] Fix | Delete
timeout - Optional socket timeout value, in seconds.
[218] Fix | Delete
source_address - tuple (host, port) for the socket to bind to as its source
[219] Fix | Delete
address before connecting (only for compatibility)
[220] Fix | Delete
"""
[221] Fix | Delete
# Remove IPv6 brackets on the remote address and proxy address.
[222] Fix | Delete
remote_host, remote_port = dest_pair
[223] Fix | Delete
if remote_host.startswith("["):
[224] Fix | Delete
remote_host = remote_host.strip("[]")
[225] Fix | Delete
if proxy_addr and proxy_addr.startswith("["):
[226] Fix | Delete
proxy_addr = proxy_addr.strip("[]")
[227] Fix | Delete
[228] Fix | Delete
err = None
[229] Fix | Delete
[230] Fix | Delete
# Allow the SOCKS proxy to be on IPv4 or IPv6 addresses.
[231] Fix | Delete
for r in socket.getaddrinfo(proxy_addr, proxy_port, 0, socket.SOCK_STREAM):
[232] Fix | Delete
family, socket_type, proto, canonname, sa = r
[233] Fix | Delete
sock = None
[234] Fix | Delete
try:
[235] Fix | Delete
sock = socksocket(family, socket_type, proto)
[236] Fix | Delete
[237] Fix | Delete
if socket_options:
[238] Fix | Delete
for opt in socket_options:
[239] Fix | Delete
sock.setsockopt(*opt)
[240] Fix | Delete
[241] Fix | Delete
if isinstance(timeout, (int, float)):
[242] Fix | Delete
sock.settimeout(timeout)
[243] Fix | Delete
[244] Fix | Delete
if proxy_type:
[245] Fix | Delete
sock.set_proxy(proxy_type, proxy_addr, proxy_port, proxy_rdns,
[246] Fix | Delete
proxy_username, proxy_password)
[247] Fix | Delete
if source_address:
[248] Fix | Delete
sock.bind(source_address)
[249] Fix | Delete
[250] Fix | Delete
sock.connect((remote_host, remote_port))
[251] Fix | Delete
return sock
[252] Fix | Delete
[253] Fix | Delete
except (socket.error, ProxyConnectionError) as e:
[254] Fix | Delete
err = e
[255] Fix | Delete
if sock:
[256] Fix | Delete
sock.close()
[257] Fix | Delete
sock = None
[258] Fix | Delete
[259] Fix | Delete
if err:
[260] Fix | Delete
raise err
[261] Fix | Delete
[262] Fix | Delete
raise socket.error("gai returned empty list.")
[263] Fix | Delete
[264] Fix | Delete
[265] Fix | Delete
class _BaseSocket(socket.socket):
[266] Fix | Delete
"""Allows Python 2 delegated methods such as send() to be overridden."""
[267] Fix | Delete
def __init__(self, *pos, **kw):
[268] Fix | Delete
_orig_socket.__init__(self, *pos, **kw)
[269] Fix | Delete
[270] Fix | Delete
self._savedmethods = dict()
[271] Fix | Delete
for name in self._savenames:
[272] Fix | Delete
self._savedmethods[name] = getattr(self, name)
[273] Fix | Delete
delattr(self, name) # Allows normal overriding mechanism to work
[274] Fix | Delete
[275] Fix | Delete
_savenames = list()
[276] Fix | Delete
[277] Fix | Delete
[278] Fix | Delete
def _makemethod(name):
[279] Fix | Delete
return lambda self, *pos, **kw: self._savedmethods[name](*pos, **kw)
[280] Fix | Delete
for name in ("sendto", "send", "recvfrom", "recv"):
[281] Fix | Delete
method = getattr(_BaseSocket, name, None)
[282] Fix | Delete
[283] Fix | Delete
# Determine if the method is not defined the usual way
[284] Fix | Delete
# as a function in the class.
[285] Fix | Delete
# Python 2 uses __slots__, so there are descriptors for each method,
[286] Fix | Delete
# but they are not functions.
[287] Fix | Delete
if not isinstance(method, Callable):
[288] Fix | Delete
_BaseSocket._savenames.append(name)
[289] Fix | Delete
setattr(_BaseSocket, name, _makemethod(name))
[290] Fix | Delete
[291] Fix | Delete
[292] Fix | Delete
class socksocket(_BaseSocket):
[293] Fix | Delete
"""socksocket([family[, type[, proto]]]) -> socket object
[294] Fix | Delete
[295] Fix | Delete
Open a SOCKS enabled socket. The parameters are the same as
[296] Fix | Delete
those of the standard socket init. In order for SOCKS to work,
[297] Fix | Delete
you must specify family=AF_INET and proto=0.
[298] Fix | Delete
The "type" argument must be either SOCK_STREAM or SOCK_DGRAM.
[299] Fix | Delete
"""
[300] Fix | Delete
[301] Fix | Delete
default_proxy = None
[302] Fix | Delete
[303] Fix | Delete
def __init__(self, family=socket.AF_INET, type=socket.SOCK_STREAM,
[304] Fix | Delete
proto=0, *args, **kwargs):
[305] Fix | Delete
if type not in (socket.SOCK_STREAM, socket.SOCK_DGRAM):
[306] Fix | Delete
msg = "Socket type must be stream or datagram, not {!r}"
[307] Fix | Delete
raise ValueError(msg.format(type))
[308] Fix | Delete
[309] Fix | Delete
super(socksocket, self).__init__(family, type, proto, *args, **kwargs)
[310] Fix | Delete
self._proxyconn = None # TCP connection to keep UDP relay alive
[311] Fix | Delete
[312] Fix | Delete
if self.default_proxy:
[313] Fix | Delete
self.proxy = self.default_proxy
[314] Fix | Delete
else:
[315] Fix | Delete
self.proxy = (None, None, None, None, None, None)
[316] Fix | Delete
self.proxy_sockname = None
[317] Fix | Delete
self.proxy_peername = None
[318] Fix | Delete
[319] Fix | Delete
self._timeout = None
[320] Fix | Delete
[321] Fix | Delete
def _readall(self, file, count):
[322] Fix | Delete
"""Receive EXACTLY the number of bytes requested from the file object.
[323] Fix | Delete
[324] Fix | Delete
Blocks until the required number of bytes have been received."""
[325] Fix | Delete
data = b""
[326] Fix | Delete
while len(data) < count:
[327] Fix | Delete
d = file.read(count - len(data))
[328] Fix | Delete
if not d:
[329] Fix | Delete
raise GeneralProxyError("Connection closed unexpectedly")
[330] Fix | Delete
data += d
[331] Fix | Delete
return data
[332] Fix | Delete
[333] Fix | Delete
def settimeout(self, timeout):
[334] Fix | Delete
self._timeout = timeout
[335] Fix | Delete
try:
[336] Fix | Delete
# test if we're connected, if so apply timeout
[337] Fix | Delete
peer = self.get_proxy_peername()
[338] Fix | Delete
super(socksocket, self).settimeout(self._timeout)
[339] Fix | Delete
except socket.error:
[340] Fix | Delete
pass
[341] Fix | Delete
[342] Fix | Delete
def gettimeout(self):
[343] Fix | Delete
return self._timeout
[344] Fix | Delete
[345] Fix | Delete
def setblocking(self, v):
[346] Fix | Delete
if v:
[347] Fix | Delete
self.settimeout(None)
[348] Fix | Delete
else:
[349] Fix | Delete
self.settimeout(0.0)
[350] Fix | Delete
[351] Fix | Delete
def set_proxy(self, proxy_type=None, addr=None, port=None, rdns=True,
[352] Fix | Delete
username=None, password=None):
[353] Fix | Delete
""" Sets the proxy to be used.
[354] Fix | Delete
[355] Fix | Delete
proxy_type - The type of the proxy to be used. Three types
[356] Fix | Delete
are supported: PROXY_TYPE_SOCKS4 (including socks4a),
[357] Fix | Delete
PROXY_TYPE_SOCKS5 and PROXY_TYPE_HTTP
[358] Fix | Delete
addr - The address of the server (IP or DNS).
[359] Fix | Delete
port - The port of the server. Defaults to 1080 for SOCKS
[360] Fix | Delete
servers and 8080 for HTTP proxy servers.
[361] Fix | Delete
rdns - Should DNS queries be performed on the remote side
[362] Fix | Delete
(rather than the local side). The default is True.
[363] Fix | Delete
Note: This has no effect with SOCKS4 servers.
[364] Fix | Delete
username - Username to authenticate with to the server.
[365] Fix | Delete
The default is no authentication.
[366] Fix | Delete
password - Password to authenticate with to the server.
[367] Fix | Delete
Only relevant when username is also provided."""
[368] Fix | Delete
self.proxy = (proxy_type, addr, port, rdns,
[369] Fix | Delete
username.encode() if username else None,
[370] Fix | Delete
password.encode() if password else None)
[371] Fix | Delete
[372] Fix | Delete
def setproxy(self, *args, **kwargs):
[373] Fix | Delete
if "proxytype" in kwargs:
[374] Fix | Delete
kwargs["proxy_type"] = kwargs.pop("proxytype")
[375] Fix | Delete
return self.set_proxy(*args, **kwargs)
[376] Fix | Delete
[377] Fix | Delete
def bind(self, *pos, **kw):
[378] Fix | Delete
"""Implements proxy connection for UDP sockets.
[379] Fix | Delete
[380] Fix | Delete
Happens during the bind() phase."""
[381] Fix | Delete
(proxy_type, proxy_addr, proxy_port, rdns, username,
[382] Fix | Delete
password) = self.proxy
[383] Fix | Delete
if not proxy_type or self.type != socket.SOCK_DGRAM:
[384] Fix | Delete
return _orig_socket.bind(self, *pos, **kw)
[385] Fix | Delete
[386] Fix | Delete
if self._proxyconn:
[387] Fix | Delete
raise socket.error(EINVAL, "Socket already bound to an address")
[388] Fix | Delete
if proxy_type != SOCKS5:
[389] Fix | Delete
msg = "UDP only supported by SOCKS5 proxy type"
[390] Fix | Delete
raise socket.error(EOPNOTSUPP, msg)
[391] Fix | Delete
super(socksocket, self).bind(*pos, **kw)
[392] Fix | Delete
[393] Fix | Delete
# Need to specify actual local port because
[394] Fix | Delete
# some relays drop packets if a port of zero is specified.
[395] Fix | Delete
# Avoid specifying host address in case of NAT though.
[396] Fix | Delete
_, port = self.getsockname()
[397] Fix | Delete
dst = ("0", port)
[398] Fix | Delete
[399] Fix | Delete
self._proxyconn = _orig_socket()
[400] Fix | Delete
proxy = self._proxy_addr()
[401] Fix | Delete
self._proxyconn.connect(proxy)
[402] Fix | Delete
[403] Fix | Delete
UDP_ASSOCIATE = b"\x03"
[404] Fix | Delete
_, relay = self._SOCKS5_request(self._proxyconn, UDP_ASSOCIATE, dst)
[405] Fix | Delete
[406] Fix | Delete
# The relay is most likely on the same host as the SOCKS proxy,
[407] Fix | Delete
# but some proxies return a private IP address (10.x.y.z)
[408] Fix | Delete
host, _ = proxy
[409] Fix | Delete
_, port = relay
[410] Fix | Delete
super(socksocket, self).connect((host, port))
[411] Fix | Delete
super(socksocket, self).settimeout(self._timeout)
[412] Fix | Delete
self.proxy_sockname = ("0.0.0.0", 0) # Unknown
[413] Fix | Delete
[414] Fix | Delete
def sendto(self, bytes, *args, **kwargs):
[415] Fix | Delete
if self.type != socket.SOCK_DGRAM:
[416] Fix | Delete
return super(socksocket, self).sendto(bytes, *args, **kwargs)
[417] Fix | Delete
if not self._proxyconn:
[418] Fix | Delete
self.bind(("", 0))
[419] Fix | Delete
[420] Fix | Delete
address = args[-1]
[421] Fix | Delete
flags = args[:-1]
[422] Fix | Delete
[423] Fix | Delete
header = BytesIO()
[424] Fix | Delete
RSV = b"\x00\x00"
[425] Fix | Delete
header.write(RSV)
[426] Fix | Delete
STANDALONE = b"\x00"
[427] Fix | Delete
header.write(STANDALONE)
[428] Fix | Delete
self._write_SOCKS5_address(address, header)
[429] Fix | Delete
[430] Fix | Delete
sent = super(socksocket, self).send(header.getvalue() + bytes, *flags,
[431] Fix | Delete
**kwargs)
[432] Fix | Delete
return sent - header.tell()
[433] Fix | Delete
[434] Fix | Delete
def send(self, bytes, flags=0, **kwargs):
[435] Fix | Delete
if self.type == socket.SOCK_DGRAM:
[436] Fix | Delete
return self.sendto(bytes, flags, self.proxy_peername, **kwargs)
[437] Fix | Delete
else:
[438] Fix | Delete
return super(socksocket, self).send(bytes, flags, **kwargs)
[439] Fix | Delete
[440] Fix | Delete
def recvfrom(self, bufsize, flags=0):
[441] Fix | Delete
if self.type != socket.SOCK_DGRAM:
[442] Fix | Delete
return super(socksocket, self).recvfrom(bufsize, flags)
[443] Fix | Delete
if not self._proxyconn:
[444] Fix | Delete
self.bind(("", 0))
[445] Fix | Delete
[446] Fix | Delete
buf = BytesIO(super(socksocket, self).recv(bufsize + 1024, flags))
[447] Fix | Delete
buf.seek(2, SEEK_CUR)
[448] Fix | Delete
frag = buf.read(1)
[449] Fix | Delete
if ord(frag):
[450] Fix | Delete
raise NotImplementedError("Received UDP packet fragment")
[451] Fix | Delete
fromhost, fromport = self._read_SOCKS5_address(buf)
[452] Fix | Delete
[453] Fix | Delete
if self.proxy_peername:
[454] Fix | Delete
peerhost, peerport = self.proxy_peername
[455] Fix | Delete
if fromhost != peerhost or peerport not in (0, fromport):
[456] Fix | Delete
raise socket.error(EAGAIN, "Packet filtered")
[457] Fix | Delete
[458] Fix | Delete
return (buf.read(bufsize), (fromhost, fromport))
[459] Fix | Delete
[460] Fix | Delete
def recv(self, *pos, **kw):
[461] Fix | Delete
bytes, _ = self.recvfrom(*pos, **kw)
[462] Fix | Delete
return bytes
[463] Fix | Delete
[464] Fix | Delete
def close(self):
[465] Fix | Delete
if self._proxyconn:
[466] Fix | Delete
self._proxyconn.close()
[467] Fix | Delete
return super(socksocket, self).close()
[468] Fix | Delete
[469] Fix | Delete
def get_proxy_sockname(self):
[470] Fix | Delete
"""Returns the bound IP address and port number at the proxy."""
[471] Fix | Delete
return self.proxy_sockname
[472] Fix | Delete
[473] Fix | Delete
getproxysockname = get_proxy_sockname
[474] Fix | Delete
[475] Fix | Delete
def get_proxy_peername(self):
[476] Fix | Delete
"""
[477] Fix | Delete
Returns the IP and port number of the proxy.
[478] Fix | Delete
"""
[479] Fix | Delete
return self.getpeername()
[480] Fix | Delete
[481] Fix | Delete
getproxypeername = get_proxy_peername
[482] Fix | Delete
[483] Fix | Delete
def get_peername(self):
[484] Fix | Delete
"""Returns the IP address and port number of the destination machine.
[485] Fix | Delete
[486] Fix | Delete
Note: get_proxy_peername returns the proxy."""
[487] Fix | Delete
return self.proxy_peername
[488] Fix | Delete
[489] Fix | Delete
getpeername = get_peername
[490] Fix | Delete
[491] Fix | Delete
def _negotiate_SOCKS5(self, *dest_addr):
[492] Fix | Delete
"""Negotiates a stream connection through a SOCKS5 server."""
[493] Fix | Delete
CONNECT = b"\x01"
[494] Fix | Delete
self.proxy_peername, self.proxy_sockname = self._SOCKS5_request(
[495] Fix | Delete
self, CONNECT, dest_addr)
[496] Fix | Delete
[497] Fix | Delete
def _SOCKS5_request(self, conn, cmd, dst):
[498] Fix | Delete
"""
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function