コード例 #1
0
    async def authenticate(self, username: bytes, password: bytes) -> bool:
        """

        :param username:
        :type username: bytes
        :param password:
        :type password: bytes
        :return:
        :rtype: bool
        """
        _username = to_str(username)
        _password = to_str(password)
        if _username in self.users and _password == self.users[_username]:
            return True
        return False
コード例 #2
0
    async def authenticate(self, username: bytes, password: bytes) -> bool:
        """

        :param username:
        :type username: bytes
        :param password:
        :type password: bytes
        :return:
        :rtype: bool
        """
        _username = to_str(username)
        _password = to_str(password)

        result = self.connection.execute(f"""
            SELECT EXISTS(SELECT 1
                          FROM users
                          WHERE username = {_username}
                            AND password = {_password}
            );
            """).fetchone()
        return bool(result)
コード例 #3
0
    async def auth(self, data: bytes) -> None:
        """

        :param data:
        :type data:
        :return:
        :rtype: None
        """
        ver: int = data[0]
        ulen: int = data[1]
        uname: bytes = data[2:2 + ulen]
        plen: int = data[2 + ulen]
        passwd: bytes = data[2 + ulen + 1:2 + ulen + 1 + plen]

        if await self.backend.authenticate(uname, passwd):
            self.protocol.transport.write(pack("!BB", ver, 0x00))
        else:
            self.protocol.transport.write(pack("!BB", ver, 0xFF))
            self.logger.debug("Authentication failed: [%s]", to_str(uname))
            raise Socks5AuthenticationFailed
コード例 #4
0
    async def data_received(self, data: bytes) -> None:
        """

        :param data:
        :type data: bytes
        :return:
        :rtype: None
        """
        if not self.validate(data):
            raise ProtocolVersionNotSupportedException

        (
            ver,  # pylint: disable=unused-variable
            cmd,
            rsv,  # pylint: disable=unused-variable
            atyp,
            dst_addr,
            dst_port,
        ) = await self.parse_host_data(data)

        if cmd not in self.supported_cmd:
            raise Socks5CMDNotSupportedException

        self.logger.debug(
            "[%s] [HOST] [%s:%s] [%s:%s] received: %s",
            hex(id(self.protocol))[-4:],
            *self.protocol.info_peername,
            to_str(dst_addr),
            dst_port,
            repr(data),
        )

        cls_client = load_object(self.protocol.config["CLIENT_PROTOCOL"])

        try:
            (
                client_transport,
                client_protocol,
            ) = await self.protocol.loop.create_connection(
                lambda: cls_client.from_channel(self.protocol.channel,
                                                role="client"),
                dst_addr,
                dst_port,
            )
        except OSError as exc:
            if exc.args == (101, "Network is unreachable"):
                self.logger.error(
                    "The target is unreachable: %s:%s",
                    dst_addr,
                    dst_port,
                )
                # self.stats.increase(f"Error/{self.name}/{exc.strerror}")
                self.protocol.transport.write(
                    pack("!BBBBIH", VERSION, 0x03, 0x00, atyp, 0xFF,
                         0xFF)  # Network unreachable
                )
                raise Socks5NetworkUnreachableException
            raise exc

        client_protocol.server_transport = self.protocol.transport
        self.protocol.client_transport = client_transport

        bnd_addr_: str
        bnd_port: int
        bnd_addr_, bnd_port = client_transport.get_extra_info("sockname")

        bnd_addr: bytes = socket.inet_pton(self.protocol.socket.family,
                                           bnd_addr_)

        atyp_: int
        if self.protocol.socket.family == socket.AF_INET:
            atyp_ = 0x01
        elif self.protocol.socket.family == socket.AF_INET6:
            atyp_ = 0x03

        self.protocol.transport.write(
            pack(
                f"!BBBB{len(bnd_addr)}sH",
                VERSION,
                0x00,
                0x00,
                atyp_,
                bnd_addr,
                bnd_port,
            ))
コード例 #5
0
ファイル: misc.py プロジェクト: grammy-jiang/Bifrost
 def test_to_str(self):
     self.assertEqual(to_str(b"a"), "a")
     self.assertEqual(to_str("a"), "a")
     with self.assertRaises(TypeError):
         to_str(0)