Esempio n. 1
0
def verify(local_skt, auth_method):
    """
    Verify client
    """
    try:
        plt_head = local_skt.recv(2)
        ver, nmethods = struct.unpack("!BB", plt_head)
    except struct.error as e:
        logger.error(e)
        return False

    all_methods = [ord(local_skt.recv(1)) for _ in range(nmethods)]

    ensure_socks_version(SOCKS_VERSION, ver)

    if auth_method not in all_methods:
        raise ex.ProtocolException("no matched authentication method")

    local_skt.sendall(struct.pack("!BB", ver, auth_method))
    if auth_method == USERNAME_PASSWORD:
        if verify_credentials(local_skt, USERNAME, PASSWORD):
            # verify success
            pass
        else:
            raise ex.ProtocolException("username/password verify failed")

    logger.info("verify success...")
    return True
Esempio n. 2
0
def check_version(version):
    """ check protocol version

    :param version: protocol version

    """
    if version != VERSION:
        raise ex.ProtocolException('mismatched protocol version')
Esempio n. 3
0
    def handle_protocol(self):
        head = self.proxy_skt.recv(8)
        ver, conn_type, ip, port = struct.unpack("!BBIH", head)
        raw.check_version(ver)

        ip = socket.inet_ntoa(struct.pack("!I", ip))
        glogger.debug("ver: %s, type: %s, addr: %s:%s", ver, conn_type, ip,
                      port)

        if conn_type == raw.NEW_CONN:
            target_skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            try:
                target_skt.settimeout(3)
                target_skt.connect((ip, port))
            except (ConnectionRefusedError, socket.timeout):
                raise ex.ProtocolException("connect to target server failed")
            self.target_skt = target_skt
            return target_skt
        raise ex.ProtocolException("proxy protocol resolution failed")
Esempio n. 4
0
 def handle_protocol(self, proxy_addr, target_addr):
     """
     :param target_addr: (ip, port)
     :returns: bool, connect success|failed
     :rtype:
     """
     ip, port = target_addr
     ip = ip2int(ip)
     head = struct.pack("!BBIH", 1, 1, ip, port)
     try:
         self.proxy_skt.connect(proxy_addr)
     except ConnectionRefusedError:
         raise ex.ProtocolException("proxy socket connect failed")
     self.proxy_skt.sendall(head)
     return True
Esempio n. 5
0
 def handle_protocol(self):
     ok, target_addr = socks5.serve(self.local_skt)
     if ok:
         self.target_addr = target_addr
     else:
         raise ex.ProtocolException("socks5 resolution falied")
Esempio n. 6
0
def ensure_socks_version(expected, actual):
    if expected != actual:
        raise ex.ProtocolException("SOCKS version is not 5")