예제 #1
0
    def testPToN(self):
        from twisted.python.compat import inet_pton

        f = lambda a: inet_pton(socket.AF_INET6, a)
        g = lambda a: inet_pton(socket.AF_INET, a)

        self.assertEqual('\x00\x00\x00\x00', g('0.0.0.0'))
        self.assertEqual('\xff\x00\xff\x00', g('255.0.255.0'))
        self.assertEqual('\xaa\xaa\xaa\xaa', g('170.170.170.170'))

        self.assertEqual('\x00' * 16, f('::'))
        self.assertEqual('\x00' * 16, f('0::0'))
        self.assertEqual('\x00\x01' + '\x00' * 14, f('1::'))
        self.assertEqual(
            '\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae',
            f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae'))

        self.assertEqual('\x00' * 14 + '\x00\x01', f('::1'))
        self.assertEqual('\x00' * 12 + '\x01\x02\x03\x04', f('::1.2.3.4'))
        self.assertEqual(
            '\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x01\x02\x03\xff',
            f('1:2:3:4:5:6:1.2.3.255'))

        for badaddr in ['1:2:3:4:5:6:7:8:', ':1:2:3:4:5:6:7:8', '1::2::3',
                        '1:::3', ':::', '1:2', '::1.2', '1.2.3.4::',
                        'abcd:1.2.3.4:abcd:abcd:abcd:abcd:abcd',
                        '1234:1.2.3.4:1234:1234:1234:1234:1234:1234',
                        '1.2.3.4']:
            self.assertRaises(ValueError, f, badaddr)
예제 #2
0
파일: socks5.py 프로젝트: Ahiknsr/obfsproxy
 def getRawBoundAddr(self):
     host = self.transport.getHost()
     port = host.port
     af = socket.getaddrinfo(host.host, port, 0, socket.SOCK_STREAM, socket.IPPROTO_TCP, socket.AI_NUMERICHOST | socket.AI_NUMERICSERV)[0][0]
     raw_addr = compat.inet_pton(af, host.host)
     if af == socket.AF_INET:
         atype = _SOCKS_ATYP_IP_V4
     elif af == socket.AF_INET6:
         atype = _SOCKS_ATYP_IP_V6
     else:
         raise ValueError("Invalid Address Family")
     return (atype, raw_addr, port)
예제 #3
0
    def testPToN(self):
        """
        L{twisted.python.compat.inet_pton} parses IPv4 and IPv6 addresses in a
        manner similar to that of L{socket.inet_pton}.
        """
        from twisted.python.compat import inet_pton

        f = lambda a: inet_pton(socket.AF_INET6, a)
        g = lambda a: inet_pton(socket.AF_INET, a)

        self.assertEqual('\x00\x00\x00\x00', g('0.0.0.0'))
        self.assertEqual('\xff\x00\xff\x00', g('255.0.255.0'))
        self.assertEqual('\xaa\xaa\xaa\xaa', g('170.170.170.170'))

        self.assertEqual('\x00' * 16, f('::'))
        self.assertEqual('\x00' * 16, f('0::0'))
        self.assertEqual('\x00\x01' + '\x00' * 14, f('1::'))
        self.assertEqual(
            '\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae',
            f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae'))
        # Scope ID doesn't affect the binary representation.
        self.assertEqual(
            '\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae',
            f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae%en0'))

        self.assertEqual('\x00' * 14 + '\x00\x01', f('::1'))
        self.assertEqual('\x00' * 12 + '\x01\x02\x03\x04', f('::1.2.3.4'))
        self.assertEqual(
            '\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x01\x02\x03\xff',
            f('1:2:3:4:5:6:1.2.3.255'))

        for badaddr in [
                '1:2:3:4:5:6:7:8:', ':1:2:3:4:5:6:7:8', '1::2::3', '1:::3',
                ':::', '1:2', '::1.2', '1.2.3.4::',
                'abcd:1.2.3.4:abcd:abcd:abcd:abcd:abcd',
                '1234:1.2.3.4:1234:1234:1234:1234:1234:1234', '1.2.3.4', '',
                '%eth0'
        ]:
            self.assertRaises(ValueError, f, badaddr)
예제 #4
0
 def getRawBoundAddr(self):
     host = self.transport.getHost()
     port = host.port
     af = socket.getaddrinfo(host.host, port, 0, socket.SOCK_STREAM, \
         socket.IPPROTO_TCP, socket.AI_NUMERICHOST | socket.AI_NUMERICSERV)[0][0]
     raw_addr = compat.inet_pton(af, host.host)
     if af == socket.AF_INET:
         atype = _SOCKS_ATYP_IP_V4
     elif af == socket.AF_INET6:
         atype = _SOCKS_ATYP_IP_V6
     else:
         raise ValueError("Invalid Address Family")
     return (atype, raw_addr, port)
예제 #5
0
    def testPToN(self):
        """
        L{twisted.python.compat.inet_pton} parses IPv4 and IPv6 addresses in a
        manner similar to that of L{socket.inet_pton}.
        """
        from twisted.python.compat import inet_pton

        f = lambda a: inet_pton(socket.AF_INET6, a)
        g = lambda a: inet_pton(socket.AF_INET, a)

        self.assertEqual('\x00\x00\x00\x00', g('0.0.0.0'))
        self.assertEqual('\xff\x00\xff\x00', g('255.0.255.0'))
        self.assertEqual('\xaa\xaa\xaa\xaa', g('170.170.170.170'))

        self.assertEqual('\x00' * 16, f('::'))
        self.assertEqual('\x00' * 16, f('0::0'))
        self.assertEqual('\x00\x01' + '\x00' * 14, f('1::'))
        self.assertEqual(
            '\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae',
            f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae'))
        # Scope ID doesn't affect the binary representation.
        self.assertEqual(
            '\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae',
            f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae%en0'))

        self.assertEqual('\x00' * 14 + '\x00\x01', f('::1'))
        self.assertEqual('\x00' * 12 + '\x01\x02\x03\x04', f('::1.2.3.4'))
        self.assertEqual(
            '\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x01\x02\x03\xff',
            f('1:2:3:4:5:6:1.2.3.255'))

        for badaddr in ['1:2:3:4:5:6:7:8:', ':1:2:3:4:5:6:7:8', '1::2::3',
                        '1:::3', ':::', '1:2', '::1.2', '1.2.3.4::',
                        'abcd:1.2.3.4:abcd:abcd:abcd:abcd:abcd',
                        '1234:1.2.3.4:1234:1234:1234:1234:1234:1234',
                        '1.2.3.4', '', '%eth0']:
            self.assertRaises(ValueError, f, badaddr)
예제 #6
0
    def testPToN(self):
        from twisted.python.compat import inet_pton

        f = lambda a: inet_pton(socket.AF_INET6, a)
        g = lambda a: inet_pton(socket.AF_INET, a)

        self.assertEqual("\x00\x00\x00\x00", g("0.0.0.0"))
        self.assertEqual("\xff\x00\xff\x00", g("255.0.255.0"))
        self.assertEqual("\xaa\xaa\xaa\xaa", g("170.170.170.170"))

        self.assertEqual("\x00" * 16, f("::"))
        self.assertEqual("\x00" * 16, f("0::0"))
        self.assertEqual("\x00\x01" + "\x00" * 14, f("1::"))
        self.assertEqual(
            "\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae",
            f("45ef:76cb:1a:56ef:afeb:bac:1924:aeae"),
        )

        self.assertEqual("\x00" * 14 + "\x00\x01", f("::1"))
        self.assertEqual("\x00" * 12 + "\x01\x02\x03\x04", f("::1.2.3.4"))
        self.assertEqual("\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x01\x02\x03\xff", f("1:2:3:4:5:6:1.2.3.255"))

        for badaddr in [
            "1:2:3:4:5:6:7:8:",
            ":1:2:3:4:5:6:7:8",
            "1::2::3",
            "1:::3",
            ":::",
            "1:2",
            "::1.2",
            "1.2.3.4::",
            "abcd:1.2.3.4:abcd:abcd:abcd:abcd:abcd",
            "1234:1.2.3.4:1234:1234:1234:1234:1234:1234",
            "1.2.3.4",
        ]:
            self.assertRaises(ValueError, f, badaddr)
예제 #7
0
 def ip_sort_key(ip):
     addr, port = ip.rsplit(":", 1)
     addr = inet_pton(socket.AF_INET6 if ":" in addr else socket.AF_INET, addr)
     return (addr, port)