def do_forward(self, client): LOGGER.info('[%s] http connect %s:%s' % (repr(client), self.proxy_ip, self.proxy_port)) try: upstream_sock = client.create_tcp_socket(self.proxy_ip, self.proxy_port, 3) if self.is_secured: upstream_sock = ssl.wrap_socket(upstream_sock) client.add_resource(upstream_sock) except: if LOGGER.isEnabledFor(logging.DEBUG): LOGGER.debug('[%s] http-connect upstream socket connect timed out' % (repr(client)), exc_info=1) self.report_failure(client, 'http-connect upstream socket connect timed out') return upstream_sock.settimeout(3) upstream_sock.sendall('CONNECT %s:%s HTTP/1.0\r\n' % (client.dst_ip, client.dst_port)) if self.username and self.password: auth = base64.b64encode('%s:%s' % (self.username, self.password)).strip() upstream_sock.sendall('Proxy-Authorization: Basic %s\r\n' % auth) upstream_sock.sendall('\r\n') try: response, _ = recv_till_double_newline('', upstream_sock) except socket.timeout: self.died = True self.report_failure(client, 'http-connect upstream connect command timed out') except: if LOGGER.isEnabledFor(logging.DEBUG): LOGGER.debug('[%s] http-connect upstream connect command failed' % (repr(client)), exc_info=1) self.report_failure( client, 'http-connect upstream connect command failed: %s,%s' % (sys.exc_info()[0], sys.exc_info()[1])) match = RE_STATUS.search(response) if match and '200' == match.group(1): if LOGGER.isEnabledFor(logging.DEBUG): LOGGER.debug('[%s] upstream connected' % repr(client)) upstream_sock.sendall(client.peeked_data) client.forward(upstream_sock) else: if LOGGER.isEnabledFor(logging.DEBUG): LOGGER.debug('[%s] http connect response: %s' % (repr(client), response.strip())) LOGGER.error('[%s] http connect rejected: %s' % (repr(client), response.splitlines()[0] if response.splitlines() else 'unknown')) self.died = True client.fall_back(response.splitlines()[0] if response.splitlines() else 'unknown')
def do_forward(self, client): upstream_sock = client.create_upstream_sock() upstream_sock.settimeout(5) # upstream_sock = ssl.wrap_socket(upstream_sock) # client.add_resource(upstream_sock) LOGGER.info('[%s] http connect %s:%s' % (repr(client), self.proxy_ip, self.proxy_port)) try: upstream_sock.connect((self.proxy_ip, self.proxy_port)) except: if LOGGER.isEnabledFor(logging.DEBUG): LOGGER.debug('[%s] http-connect upstream socket connect timed out' % (repr(client)), exc_info=1) self.died = True client.fall_back(reason='http-connect upstream socket connect timed out') if 443 == client.dst_port: upstream_sock.sendall('CONNECT %s:%s HTTP/1.0\r\n\r\n' % (client.dst_ip, client.dst_port)) try: response, _ = recv_till_double_newline('', upstream_sock) except: if LOGGER.isEnabledFor(logging.DEBUG): LOGGER.debug('[%s] http-connect upstream connect command failed' % (repr(client)), exc_info=1) client.fall_back(reason='http-connect upstream connect command failed: %s' % sys.exc_info()[1]) match = RE_STATUS.search(response) if match and '200' == match.group(1): if LOGGER.isEnabledFor(logging.DEBUG): LOGGER.debug('[%s] upstream connected' % repr(client)) upstream_sock.sendall(client.peeked_data) client.forward(upstream_sock) else: if LOGGER.isEnabledFor(logging.DEBUG): LOGGER.debug('[%s] http connect response: %s' % (repr(client), response.strip())) LOGGER.error('[%s] http connect rejected: %s' % (repr(client), response.splitlines()[0] if response.splitlines() else 'unknown')) self.died = True client.fall_back(response.splitlines()[0] if response.splitlines() else 'unknown') else: response = send_first_request_and_get_response(client, upstream_sock) client.forward_started = True client.downstream_sock.sendall(response) client.forward(upstream_sock)