Debian Codegraph @

docs/socks.rst

raw
1 .. currentmodule:: aiorpcx
2
3 SOCKS Proxy
4 ===========
5
6 The :mod:`aiorpcx` package includes a `SOCKS
7 <https://en.wikipedia.org/wiki/SOCKS>`_ proxy client. It understands
8 the ``SOCKS4``, ``SOCKS4a`` and ``SOCKS5`` protocols.
9
10 Exceptions
11 ----------
12
13 .. exception:: SOCKSError
14
15 The base class of SOCKS exceptions. Each raised exception will be
16 an instance of a derived class.
17
18 .. exception:: SOCKSProtocolError
19
20 A subclass of :class:`SOCKSError`. Raised when the proxy does not
21 follow the ``SOCKS`` protocol.
22
23 .. exception:: SOCKSFailure
24
25 A subclass of :class:`SOCKSError`. Raised when the proxy refuses
26 or fails to make a connection.
27
28
29 Authentication
30 --------------
31
32 Currently the only supported authentication method is with a username
33 and password. Usernames can be used by all SOCKS protocols, but only
34 ``SOCKS5`` uses the password.
35
36 .. class:: SOCKSUserAuth
37
38 A :class:`namedtuple` for authentication with a SOCKS server. It
39 has two members:
40
41 .. attribute:: username
42
43 A string.
44
45 .. attribute:: password
46
47 A string. Ignored by the :class:`SOCKS4` and :class:`SOCKS4a`
48 protocols.
49
50
51 Protocols
52 ---------
53
54 When creating a :class:`SocksProxy` object, a protocol must be
55 specified and be one of the following.
56
57 .. class:: SOCKS4
58
59 An abstract class representing the ``SOCKS4`` protocol.
60
61 .. class:: SOCKS4a
62
63 An abstract class representing the ``SOCKS4a`` protocol.
64
65 .. class:: SOCKS5
66
67 An abstract class representing the ``SOCKS5`` protocol.
68
69
70 Proxy
71 -----
72
73 You can create a :class:`SOCKSProxy` object directly, but using one
74 of its auto-detection class methods is likely more useful.
75
76 .. class:: SOCKSProxy(address, protocol, auth)
77
78 An object representing a SOCKS proxy. The address is a Python
79 socket `address
80 <https://docs.python.org/3/library/socket.html#socket-families>`_
81 typically a (host, port) pair for IPv4, and a (host, port, flowinfo,
82 scopeid) tuple for IPv6.
83
84 The *protocol* is one of :class:`SOCKS4`, :class:`SOCKS4a` and
85 :class:`SOCKS5`.
86
87 *auth* is a :class:`SOCKSUserAuth` object or :const:`None`.
88
89 After construction, :attr:`host`, :attr:`port` and :attr:`peername`
90 are set to :const:`None`.
91
92 .. classmethod:: auto_detect_address(address, auth, \*, \
93 loop=None, timeout=5.0)
94
95 Try to detect a SOCKS proxy at *address*.
96
97 Protocols :class:`SOCKS5`, :class:`SOCKS4a` and :class:`SOCKS4`
98 are tried in order. If a SOCKS proxy is detected return a
99 :class:`SOCKSProxy` object, otherwise :const:`None`. Returning a
100 proxy object only means one was detected, not that it is
101 functioning - for example, it may not have full network
102 connectivity.
103
104 *auth* is a :class:`SOCKSUserAuth` object or :const:`None`.
105
106 If testing any protocol takes more than *timeout* seconds, it is
107 timed out and taken as not detected.
108
109 This class method is a `coroutine`_.
110
111 .. classmethod:: auto_detect_host(host, ports, auth, \*, \
112 loop=None, timeout=5.0)
113
114 Try to detect a SOCKS proxy on *host* on one of the *ports*.
115
116 Call :meth:`auto_detect_address` for each ``(host, port)`` pair
117 until a proxy is detected, and return it, otherwise
118 :const:`None`.
119
120 *auth* is a :class:`SOCKSUserAuth` object or :const:`None`.
121
122 If testing any protocol on any port takes more than *timeout*
123 seconds, it is timed out and taken as not detected.
124
125 This class method is a `coroutine`_.
126
127 .. method:: create_connection(protocol_factory, host, port, \*, \
128 resolve=False, loop=None, ssl=None, family=0, proto=0, \
129 flags=0, timeout=30.0)
130
131 Connect to (host, port) through the proxy in the background.
132 When successful, the coroutine returns a ``(transport, protocol,
133 address)`` triple, and sets the proxy attribute :attr:`peername`.
134
135 * If *resolve* is :const:`True`, *host* is resolved locally
136 rather than by the proxy. *family*, *proto*, *flags* are the
137 optional address family, protocol and flags passed to
138 `loop.getaddrinfo()`_ to get a list of remote addresses. If
139 given, these should all be integers from the corresponding
140 :mod:`socket` module constants.
141
142 * *ssl* is as documented for `loop.create_connection()`_.
143
144 If successfully connected the :attr:`_address` member of the
145 protocol is set. If *resolve* is :const:`True` it is set to the
146 successful address, otherwise ``(host, port)``.
147
148 If connecting takes more than *timeout* seconds an
149 :exc:`asyncio.TimeoutError` exception is raised.
150
151 This method is a `coroutine`_.
152
153 .. attribute:: host
154
155 Set on a successful :meth:`create_connection` to the host passed
156 to the proxy server. This will be the resolved address if its
157 *resolve* argument was :const:`True`.
158
159 .. attribute:: port
160
161 Set on a successful :meth:`create_connection` to the host passed
162 to the proxy server.
163
164 .. attribute:: peername
165
166 Set on a successful :meth:`create_connection` to the result of
167 :meth:`socket.getpeername` on the socket connected to the proxy.
168
169 .. _loop.create_connection():
170 https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.AbstractEventLoop.create_connection
171 .. _loop.getaddrinfo():
172 https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.AbstractEventLoop.getaddrinfo
173 .. _coroutine:
174 https://docs.python.org/3/library/asyncio-task.html#coroutine