def _create_stream(self, max_buffer_size, af, addr, source_ip=None,
source_port=None):
# Always connect in plaintext; we'll convert to ssl if necessary
# after one connection has completed.
source_port_bind = source_port if isinstance(source_port, int) else 0
source_ip_bind = source_ip
socket_obj = socket.socket(af)
set_close_exec(socket_obj.fileno())
try:
stream = IOStream(socket_obj,
io_loop=self.io_loop,
max_buffer_size=max_buffer_size)
# connect proxy
if source_port_bind or source_ip_bind:
@gen.coroutine
def _(addr):
proxy_headers = get_proxy_headers(source_ip_bind)
parsed = urlparse(source_ip_bind)
scheme, host, port = parsed.scheme, parsed.hostname, source_port_bind
if 'socks' in scheme:
r = yield self._negotiate_socks(addr, (source_ip_bind, source_port_bind))
raise gen.Return(r)
elif scheme in ('http', 'https'):
r = yield stream.connect((host, port))
if scheme == 'https':
yield self._connect_tunnel(stream, addr, proxy_headers)
raise gen.Return(r)
else:
raise AttributeError('Unknown scheme: %s' % scheme)
return _(addr)
else:
return stream.connect(addr)
except socket.error as e:
fu = Future()
fu.set_exception(e)
return fu
评论列表
文章目录