Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def init(self):
info = await socket.getaddrinfo(
*self.target_addr, socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP
)
addr = random.choice(info)[-1]
socks4_client_parser = socks4.client.parser(addr)
await run_parser_curio(socks4_client_parser, self.sock)
redundant = socks4_client_parser.readall()
set_disposable_recv(self.sock, redundant)
async def sendfrom(data, from_addr):
if from_addr not in self.bind_socks:
sender = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sender.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sender.setsockopt(socket.SOL_IP, IP_TRANSPARENT, 1)
sender.bind(from_addr)
self.bind_socks[from_addr] = sender
sender = self.bind_socks[from_addr]
await sender.sendto(data, addr)
def __init__(self, address, port, handler, backlog=100):
self.address = address
self.port = port
self.handler = handler
self.backlog = backlog
self.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.__socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
self.handlers = []
async def handle_send(self, data):
try:
await self.connection.sendall(data)
except socket.error:
return await self.handle_disconnect()
async def connect(self):
# ssl_context = ssl.create_default_context()
# ssl_context.verify_mode = ssl.CERT_REQUIRED
# ssl_context.check_hostname = True # redundant with match_hostname
# self._socket = await open_connection(self.host, self.port, ssl=ssl)
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
await self._socket.connect((self.host, self.port))
self._socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
try:
self._handshake.reset()
response = None
while True:
request = self._handshake.next_message(response)
if request is None:
break
# This may happen in the `V1_0` protocol where we send two requests as
# an optimization, then need to read each separately
if request:
await self.sendall(request)
# The response from the server is a null-terminated string
response = (await self.read_until(b'\0'))[:-1]
except (ReqlAuthError, ReqlTimeoutError):
await self.close()
raise
async def __call__(self, client, addr):
try:
buf = client._socket.getsockopt(socket.SOL_IP, SO_ORIGINAL_DST, 16)
port, host = struct.unpack('!2xH4s8x', buf)
self.taddr = (socket.inet_ntoa(host), port)
except Exception as e:
if verbose > 0:
print(f'{self} error: {e}')
print('--> It is not a redirect proxy connection')
await client.close()
return
return (await super().__call__(client, addr))
async def open_tcp_stream(hostname, port, delay=0.3):
# Get all of the possible targets for a given host/port
targets = await socket.getaddrinfo(hostname, port, type=socket.SOCK_STREAM)
if not targets:
raise OSError(f'nothing known about {hostname}:{port}')
# Cluster the targets into unique address families (e.g., AF_INET, AF_INET6, etc.)
# and make sure the first entries are from a different family.
families = [ list(g) for _, g in itertools.groupby(targets, key=lambda t: t[0]) ]
targets = [ fam.pop(0) for fam in families ]
targets.extend(itertools.chain(*families))
# List of accumulated errors to report in case of total failure
errors = []
# Task group to manage a collection concurrent tasks.
# Cancels all remaining once an interesting result is returned.
async with TaskGroup(wait=object) as group:
def udp_server_socket(host, port, *, family=socket.AF_INET, reuse_address=True):
sock = socket.socket(family, socket.SOCK_DGRAM)
try:
if reuse_address:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
sock.bind((host, port))
return sock
except Exception:
sock._socket.close()
raise