• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

romis2012 / python-socks / 88

pending completion
88

push

travis-ci-com

romis2012
Read socks5 server bound address

26 of 26 new or added lines in 3 files covered. (100.0%)

1588 of 1596 relevant lines covered (99.5%)

3.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/python_socks/async_/anyio/_proxy.py
1
import ssl
4✔
2
from typing import Optional
4✔
3

4
import anyio
4✔
5

6
from ..._errors import ProxyConnectionError, ProxyTimeoutError
4✔
7
from ..._proto.http_async import HttpProto
4✔
8
from ..._proto.socks4_async import Socks4Proto
4✔
9
from ..._proto.socks5_async import Socks5Proto
4✔
10

11
from ._resolver import Resolver
4✔
12
from ._stream import AnyioSocketStream
4✔
13
from ._connect import connect_tcp
4✔
14

15
DEFAULT_TIMEOUT = 60
4✔
16

17

18
class AnyioProxy:
4✔
19
    _stream: Optional[AnyioSocketStream]
4✔
20

21
    def __init__(
4✔
22
        self,
23
        proxy_host: str,
24
        proxy_port: int,
25
        proxy_ssl: ssl.SSLContext = None,
26
    ):
27
        self._proxy_host = proxy_host
4✔
28
        self._proxy_port = proxy_port
4✔
29
        self._proxy_ssl = proxy_ssl
4✔
30

31
        self._dest_host = None
4✔
32
        self._dest_port = None
4✔
33
        self._dest_ssl = None
4✔
34
        self._timeout = None
4✔
35

36
        self._stream = None
4✔
37
        self._resolver = Resolver()
4✔
38

39
    async def connect(
4✔
40
        self,
41
        dest_host: str,
42
        dest_port: int,
43
        dest_ssl: ssl.SSLContext = None,
44
        timeout: float = None,
45
        _stream: AnyioSocketStream = None,
46
    ) -> AnyioSocketStream:
47

48
        if timeout is None:
4✔
49
            timeout = DEFAULT_TIMEOUT
4✔
50

51
        self._dest_host = dest_host
4✔
52
        self._dest_port = dest_port
4✔
53
        self._dest_ssl = dest_ssl
4✔
54
        self._timeout = timeout
4✔
55

56
        try:
4✔
57
            with anyio.fail_after(self._timeout):
4✔
58
                if _stream is None:
4✔
59
                    self._stream = AnyioSocketStream(
4✔
60
                        await connect_tcp(
61
                            host=self._proxy_host,
62
                            port=self._proxy_port,
63
                        )
64
                    )
65
                else:
66
                    self._stream = _stream
4✔
67

68
                if self._proxy_ssl is not None:
4✔
69
                    self._stream = await self._stream.start_tls(
4✔
70
                        hostname=self._proxy_host,
71
                        ssl_context=self._proxy_ssl,
72
                    )
73

74
                await self._negotiate()
4✔
75

76
                if self._dest_ssl is not None:
4✔
77
                    self._stream = await self._stream.start_tls(
4✔
78
                        hostname=self._dest_host,
79
                        ssl_context=self._dest_ssl,
80
                    )
81

82
                # return self._stream.anyio_stream
83
                return self._stream
4✔
84

85
        except TimeoutError as e:
4✔
86
            await self._close()
4✔
87
            raise ProxyTimeoutError('Proxy connection timed out: {}'.format(self._timeout)) from e
4✔
88
        except OSError as e:
4✔
89
            await self._close()
4✔
90
            msg = 'Could not connect to proxy {}:{} [{}]'.format(
4✔
91
                self._proxy_host,
92
                self._proxy_port,
93
                e.strerror,
94
            )
95
            raise ProxyConnectionError(e.errno, msg) from e
4✔
96
        except Exception:
4✔
97
            await self._close()
4✔
98
            raise
4✔
99

100
    async def _negotiate(self):
4✔
101
        raise NotImplementedError()
102

103
    async def _close(self):
4✔
104
        if self._stream is not None:
4✔
105
            await self._stream.close()
4✔
106

107
    @property
4✔
108
    def proxy_host(self):
3✔
109
        return self._proxy_host
4✔
110

111
    @property
4✔
112
    def proxy_port(self):
3✔
113
        return self._proxy_port
4✔
114

115

116
class Socks5Proxy(AnyioProxy):
4✔
117
    def __init__(
4✔
118
        self,
119
        proxy_host,
120
        proxy_port,
121
        username=None,
122
        password=None,
123
        rdns=None,
124
        proxy_ssl=None,
125
    ):
126
        super().__init__(
4✔
127
            proxy_host=proxy_host,
128
            proxy_port=proxy_port,
129
            proxy_ssl=proxy_ssl,
130
        )
131
        self._username = username
4✔
132
        self._password = password
4✔
133
        self._rdns = rdns
4✔
134

135
    async def _negotiate(self):
4✔
136
        proto = Socks5Proto(
4✔
137
            stream=self._stream,
138
            resolver=self._resolver,
139
            dest_host=self._dest_host,
140
            dest_port=self._dest_port,
141
            username=self._username,
142
            password=self._password,
143
            rdns=self._rdns,
144
        )
145
        await proto.negotiate()
4✔
146

147

148
class Socks4Proxy(AnyioProxy):
4✔
149
    def __init__(
4✔
150
        self,
151
        proxy_host,
152
        proxy_port,
153
        user_id=None,
154
        rdns=None,
155
        proxy_ssl=None,
156
    ):
157
        super().__init__(
4✔
158
            proxy_host=proxy_host,
159
            proxy_port=proxy_port,
160
            proxy_ssl=proxy_ssl,
161
        )
162
        self._user_id = user_id
4✔
163
        self._rdns = rdns
4✔
164

165
    async def _negotiate(self):
4✔
166
        proto = Socks4Proto(
4✔
167
            stream=self._stream,
168
            resolver=self._resolver,
169
            dest_host=self._dest_host,
170
            dest_port=self._dest_port,
171
            user_id=self._user_id,
172
            rdns=self._rdns,
173
        )
174
        await proto.negotiate()
4✔
175

176

177
class HttpProxy(AnyioProxy):
4✔
178
    def __init__(
4✔
179
        self,
180
        proxy_host,
181
        proxy_port,
182
        username=None,
183
        password=None,
184
        proxy_ssl=None,
185
    ):
186
        super().__init__(
4✔
187
            proxy_host=proxy_host,
188
            proxy_port=proxy_port,
189
            proxy_ssl=proxy_ssl,
190
        )
191
        self._username = username
4✔
192
        self._password = password
4✔
193

194
    async def _negotiate(self):
4✔
195
        proto = HttpProto(
4✔
196
            stream=self._stream,
197
            dest_host=self._dest_host,
198
            dest_port=self._dest_port,
199
            username=self._username,
200
            password=self._password,
201
        )
202
        await proto.negotiate()
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc