1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
import socket
from ._stream_async import AsyncSocketStream
from ._proto_socks4 import (
ConnectRequest,
ConnectResponse
)
class Socks4Proto:
def __init__(self, stream: AsyncSocketStream,
dest_host, dest_port, user_id=None,
rdns=None):
if rdns is None:
rdns = False
self._dest_host = dest_host
self._dest_port = dest_port
self._user_id = user_id
self._rdns = rdns
self._stream = stream
async def negotiate(self):
await self._socks_connect()
async def _socks_connect(self):
req = ConnectRequest(
host=self._dest_host,
port=self._dest_port,
user_id=self._user_id,
rdns=self._rdns
)
if req.need_resolve:
_, addr = await self._stream.resolver.resolve(
req.host,
family=socket.AF_INET
)
req.set_resolved_host(addr)
await self._stream.write_all(bytes(req))
data = await self._stream.read_exact(8)
res = ConnectResponse(data[:2])
res.validate()
|