log("Getting endpoint for host '%s' on port %d ssl=%s, socks_config=%s, use_http_proxy=%s"%(target_host,target_port,target_ssl,str(socks_config),use_http_proxy),id=debugid,verbosity_level=3)
ifsession.config.http_proxyanduse_http_proxy:
target_host=session.config.http_proxy['host']
target_port=session.config.http_proxy['port']
target_ssl=False# We turn on ssl after CONNECT request if needed
log("Connecting to http proxy at %s:%d"%(target_host,target_port),id=debugid,verbosity_level=3)
ifsocks_configisnotNone:
sock_host=socks_config['host']
@ -183,7 +192,7 @@ class UpstreamHTTPProxyClient(ProxyClient):
sendreq.proxy_creds=self.creds
lines=sendreq.full_request.splitlines()
forlinlines:
self.log(l,symbol='>r',verbosity_level=3)
self.log(l,symbol='>rp',verbosity_level=3)
self.transport.write(sendreq.full_message)
defconnectionMade(self):
@ -194,10 +203,16 @@ class UpstreamHTTPProxyClient(ProxyClient):
self.connect_response=True
ifself.credsisnotNone:
connreq.proxy_creds=self.creds
lines=connreq.full_message.splitlines()
forlinlines:
self.log(l,symbol='>p',verbosity_level=3)
self.transport.write(connreq.full_message)
else:
self.proxy_connected=True
self.stream_response=True
lines=self.request.full_message.splitlines()
forlinlines:
self.log(l,symbol='>p',verbosity_level=3)
self.write_proxied_request(self.request)
defhandle_response_end(self,*args,**kwargs):
@ -211,6 +226,10 @@ class UpstreamHTTPProxyClient(ProxyClient):
self.transport.loseConnection()
assertself._response_obj.full_response
self.data_defer.callback(self.request)
elifself._response_obj.response_code!=200:
print"Error establishing connection to proxy"
self.transport.loseConnection()
return
elifself.connect_response:
self.log("Response to CONNECT request recieved from http proxy",verbosity_level=3)
self.proxy_connected=True
@ -220,10 +239,12 @@ class UpstreamHTTPProxyClient(ProxyClient):
self.completed=False
self._sent=False
self.log("Starting TLS",verbosity_level=3)
self.transport.startTLS(ClientTLSContext())
self.log("TLS started",verbosity_level=3)
lines=self.request.full_message.splitlines()
forlinlines:
self.log(l,symbol='>r',verbosity_level=3)
self.log(l,symbol='>rpr',verbosity_level=3)
self.transport.write(self.request.full_message)
classProxyClientFactory(ClientFactory):
@ -240,6 +261,7 @@ class ProxyClientFactory(ClientFactory):