def do_pre_handshake(self): try: # Connect to the proxy first self._sock = socket.create_connection((self._tunnelHost, self._tunnelPort), self._timeout) except socket.timeout as e: raise ProxyError(self.ERR_PROXY_OFFLINE.format(e[0])) except socket.error as e: raise ProxyError(self.ERR_PROXY_OFFLINE.format(e[1])) # Send a CONNECT request with the host we want to tunnel to self._sock.send(self.HTTP_CONNECT_REQ.format(self._host, self._port)) httpResp = parse_http_response(self._sock.recv(2048)) # Check if the proxy was able to connect to the host if httpResp.status != 200: raise ProxyError(self.ERR_CONNECT_REJECTED)
def do_pre_handshake(self): try: # Connect to the proxy first self._sock = socket.create_connection( (self._tunnelHost, self._tunnelPort), self._timeout) except socket.timeout as e: raise ProxyError(self.ERR_PROXY_OFFLINE.format(e[0])) except socket.error as e: raise ProxyError(self.ERR_PROXY_OFFLINE.format(e[1])) # Send a CONNECT request with the host we want to tunnel to self._sock.send(self.HTTP_CONNECT_REQ.format(self._host, self._port)) httpResp = parse_http_response(self._sock.recv(2048)) # Check if the proxy was able to connect to the host if httpResp.status != 200: raise ProxyError(self.ERR_CONNECT_REJECTED)
def post_handshake_check(self): result = "" try: # Send an HTTP GET to the server and store the HTTP Status Code self.write(self.HTTP_GET_REQ.format(self._host)) # Parse the response and print the Location header httpResp = parse_http_response(self.read(2048)) if httpResp.version == 9: # HTTP 0.9 => Probably not an HTTP response result = self.ERR_NOT_HTTP else: redirect = "" if httpResp.status >= 300 and httpResp.status < 400: # Add redirection URL to the result redirect = " - " + httpResp.getheader("Location", None) result = self.GET_RESULT_FORMAT.format(httpResp.status, httpResp.reason, redirect) except socket.timeout: result = self.ERR_HTTP_TIMEOUT return result
def post_handshake_check(self): try: # Send an HTTP GET to the server and store the HTTP Status Code self.write(self.HTTP_GET_REQ.format(self._host)) # Parse the response and print the Location header httpResp = parse_http_response(self.read(2048)) if httpResp.version == 9: # HTTP 0.9 => Probably not an HTTP response result = self.ERR_NOT_HTTP else: redirect = '' if 300 <= httpResp.status < 400: # Add redirection URL to the result redirect = ' - ' + httpResp.getheader('Location', None) result = self.GET_RESULT_FORMAT.format(httpResp.status, httpResp.reason, redirect) except socket.timeout: result = self.ERR_HTTP_TIMEOUT return result