Example #1
0
    def get_location(self):
        if self.country_code:
            return self.country_code
        if time.time() - self.last_ckeck < 300:
            return self.country_code
        try:
            self.last_ckeck = time.time()
            ip = ip_address(socket.getaddrinfo(self.parse.hostname, 0)[0][4][0])

            if ip.is_loopback or ip.is_private:
                from connection import create_connection
                from httputil import read_response_line, read_headers
                try:
                    soc = create_connection(('bot.whatismyipaddress.com', 80), ctimeout=None, parentproxy=self)
                    soc.sendall(b'GET / HTTP/1.1\r\nConnection: keep_alive\r\nHost: bot.whatismyipaddress.com\r\nAccept-Encoding: identity\r\nUser-Agent: Python-urllib/2.7\r\n\r\n')
                    f = soc.makefile()
                    line, version, status, reason = read_response_line(f)
                    _, headers = read_headers(f)
                    assert status == 200
                    ip = soc.recv(int(headers['Content-Length']))
                    if not ip:
                        soc.close()
                        raise ValueError('%s: ip address is empty' % self.name)
                    self.country_code = ip_to_country_code(ip)
                    soc.close()
                except Exception:
                    sys.stderr.write(traceback.format_exc())
                    sys.stderr.flush()
                    self.country_code = None
            else:
                self.country_code = ip_to_country_code(ip)
        finally:
            return self.country_code
Example #2
0
 def recv(self, size):
     if not self.connected:
         self.sendall(b'')
     if self._http_obfs and not self._header_received:
         # TODO: verify
         self._header_received = True
         line, version, status, reason = read_response_line(self._rfile)
         header_data, headers = read_headers(self._rfile)
     buf = self._rbuffer
     buf.seek(0, 2)  # seek end
     buf_len = buf.tell()
     self._rbuffer = io.BytesIO()  # reset _rbuf.  we consume it via buf.
     if buf_len == 0:
         # Nothing in buffer? Try to read.
         data = self._sock.recv(self.bufsize)
         if not data:
             return b''
         data = self.crypto.decrypt(data)
         if len(data) <= size:
             return data
         buf_len = len(data)
         buf.write(data)
         del data  # explicit free
     buf.seek(0)
     rv = buf.read(size)
     if buf_len > size:
         self._rbuffer.write(buf.read())
     return rv
Example #3
0
 def recv(self, size):
     if not self.connected:
         self.sendall(b'')
     if self._http_obfs and not self._header_received:
         # TODO: verify
         self._header_received = True
         line, version, status, reason = read_response_line(self._rfile)
         header_data, headers = read_headers(self._rfile)
     buf = self._rbuffer
     buf.seek(0, 2)  # seek end
     buf_len = buf.tell()
     self._rbuffer = io.BytesIO()  # reset _rbuf.  we consume it via buf.
     if buf_len == 0:
         # Nothing in buffer? Try to read.
         data = self._sock.recv(self.bufsize)
         if not data:
             return b''
         data = self.crypto.decrypt(data)
         if len(data) <= size:
             return data
         buf_len = len(data)
         buf.write(data)
         del data  # explicit free
     buf.seek(0)
     rv = buf.read(size)
     if buf_len > size:
         self._rbuffer.write(buf.read())
     return rv
Example #4
0
def do_tunnel(soc, netloc, pp):
    s = ['CONNECT %s:%s HTTP/1.1\r\n' % (netloc[0], netloc[1]), ]
    if pp.username:
        a = '%s:%s' % (pp.username, pp.password)
        s.append('Proxy-Authorization: Basic %s\r\n' % base64.b64encode(a.encode()))
    s.append('Host: %s:%s\r\n\r\n' % (netloc[0], netloc[1]))
    soc.sendall(''.join(s).encode())
    remoterfile = soc.makefile('rb', 0)
    line, version, status, reason = read_response_line(remoterfile)
    if status != 200:
        raise IOError(0, 'create tunnel via %s failed!' % pp.name)
    read_header_data(remoterfile)
Example #5
0
def do_tunnel(soc, netloc, pp):
    s = [
        'CONNECT %s:%s HTTP/1.1\r\n' % (netloc[0], netloc[1]),
    ]
    if pp.username:
        a = '%s:%s' % (pp.username, pp.password)
        s.append('Proxy-Authorization: Basic %s\r\n' %
                 base64.b64encode(a.encode()))
    s.append('Host: %s:%s\r\n\r\n' % (netloc[0], netloc[1]))
    soc.sendall(''.join(s).encode())
    remoterfile = soc.makefile('rb', 0)
    line, version, status, reason = read_response_line(remoterfile)
    if status != 200:
        raise IOError(0, 'create tunnel via %s failed!' % pp.name)
    read_header_data(remoterfile)
Example #6
0
    def get_location(self):
        if self.country_code:
            return self.country_code
        if time.time() - self.last_ckeck < 300:
            return self.country_code
        try:
            self.last_ckeck = time.time()
            ip = ip_address(
                socket.getaddrinfo(self.parse.hostname, 0)[0][4][0])

            if ip.is_loopback or ip.is_private:
                from connection import create_connection
                from httputil import read_response_line, read_headers
                try:
                    soc = create_connection(('bot.whatismyipaddress.com', 80),
                                            ctimeout=None,
                                            parentproxy=self)
                    soc.sendall(
                        b'GET / HTTP/1.1\r\nConnection: keep_alive\r\nHost: bot.whatismyipaddress.com\r\nAccept-Encoding: identity\r\nUser-Agent: Python-urllib/2.7\r\n\r\n'
                    )
                    f = soc.makefile()
                    line, version, status, reason = read_response_line(f)
                    _, headers = read_headers(f)
                    assert status == 200
                    ip = soc.recv(int(headers['Content-Length']))
                    if not ip:
                        soc.close()
                        raise ValueError('%s: ip address is empty' % self.name)
                    self.country_code = ip_to_country_code(ip)
                    soc.close()
                except Exception:
                    sys.stderr.write(traceback.format_exc())
                    sys.stderr.flush()
                    self.country_code = None
            else:
                self.country_code = ip_to_country_code(ip)
        finally:
            return self.country_code
Example #7
0
 def _rfile_read(self, size):
     if self._http_obfs and not self._header_received:
         self._header_received = True
         line, version, status, reason = read_response_line(self._rfile)
         header_data, headers = read_headers(self._rfile)
     return self._rfile.read(size)
Example #8
0
 def _rfile_read(self, size):
     if self._http_obfs and not self._header_received:
         self._header_received = True
         line, version, status, reason = read_response_line(self._rfile)
         header_data, headers = read_headers(self._rfile)
     return self._rfile.read(size)