summaryrefslogtreecommitdiff
path: root/tools/feeder.py
blob: 224ec1ba891eaa7ce8ec21a3715a5856e5a5b64e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import requests
import random
import socket
import ipaddress
import datetime
import itertools
import json
import threading
import time

data = {
    "ip": "",
    "port": 0,
    "whois_description": "",
    "asn": "",
    "asn_country_code": "",
    "ptr": "",
    "abuse_mail": "",
    "domain": "",
    "timestamp_in_utc": "",
    "producer_unique_keys": {
        "subject_cn": "unknown",
        "subject_o": "unknown",
        "full_name": "",
        "end_of_general_support": False,
    }
}


def random_string(iters=1):
    full_string = ''
    random_string = ''

    for i in range(iters):
        for _ in range(10):
            random_integer = random.randint(97, 97 + 26 - 1)
            flip_bit = random.randint(0, 1)
            random_integer = random_integer - 32 if flip_bit == 1 else random_integer
            random_string += (chr(random_integer))
        full_string += random_string

    return full_string.lower()


def random_ipv4():
    randint = random.randint(1, 0xffffffff)

    return str(ipaddress.IPv4Address(randint))


def random_ipv6():
    randint = random.randint(0, 2**128-1)

    return str(ipaddress.IPv6Address(randint))


def random_port():
    return random.randint(1, 65536)


def random_asn():
    randint = random.randint(1, 65536)

    return f'AS{randint}'


def random_country():
    countries = [
        'AF', 'AL', 'DZ', 'AD', 'AO', 'AI', 'AQ', 'AR', 'AM', 'AW',
        'AU', 'AT', 'AZ', 'BS', 'BH', 'BD', 'BB', 'BY', 'BE', 'BZ', 'BJ', 'BM',
        'BT', 'BO', 'BQ', 'BW', 'BR', 'BG', 'BI', 'KH', 'CM', 'CA', 'TD', 'CL',
        'CN', 'CO', 'HR', 'CU', 'CW', 'CY', 'CZ', 'DK', 'DJ', 'DM', 'EC', 'EG',
        'GU', 'ER', 'EE', 'SZ', 'ET', 'FJ', 'FI', 'FR', 'GA', 'GH', 'GE', 'DE',
        'GH', 'GI', 'GR', 'GL', 'GD', 'GP', 'GU', 'GT', 'GG', 'GN', 'GW', 'GY',
        'HT', 'HN', 'HU', 'IS', 'IN', 'ID', 'IQ', 'IE', 'IL', 'IT', 'JM', 'JP',
        'JE', 'JO', 'KZ', 'KE', 'KI', 'KW', 'KG', 'LV', 'LB', 'LS', 'LR', 'LY',
        'LI', 'LT', 'LU', 'MO', 'MG', 'MW', 'MY', 'MV', 'ML', 'MT', 'MQ', 'MR',
        'MU', 'YT', 'MX', 'MC', 'MN', 'ME', 'MS', 'MA', 'MZ', 'MM', 'NA', 'NR',
        'NP', 'NI', 'NG', 'NU', 'NO', 'OM', 'PK', 'PW', 'PA', 'PY', 'PE', 'PN',
        'PL', 'PT', 'QA', 'RO', 'RW', 'RE', 'WS', 'SN', 'RS', 'SC', 'SG', 'SK',
        'SI', 'SO', 'ES', 'SR', 'SE', 'CH', 'TJ', 'TH', 'TL', 'TG', 'TK', 'TO',
        'TN', 'TR', 'TM', 'TV', 'UG', 'UA', 'UY', 'UZ', 'VU', 'YE', 'ZM', 'ZW',
        'AX'
    ]

    return random.choice(countries)


def random_tld():
    tlds = [
        '.ac', '.ad', '.ae', '.af', '.ag', '.ai', '.al', '.am',
        '.ao', '.aq', '.ar', '.as', '.at', '.au', '.aw', '.ax',
        '.az', '.ba', '.bb', '.bd', '.be', '.bf', '.bg', '.bh',
        '.bi', '.bj', '.bm', '.bn', '.bo', '.bq', '.br', '.bs',
        '.bt', '.bw', '.by', '.bz', '.ca', '.cc', '.cd', '.cf',
        '.cg', '.ch', '.ci', '.ck', '.cl', '.cm', '.cn', 'Als',
        '.co', '.cr', '.cu', '.cv', '.cw', '.cx', '.cy', '.cz',
        '.de', '.dj', '.dk', '.dm', '.do', '.dz', '.ec', '.ee',
        '.eg', '.eh', '.er', '.es', '.et', '.eu', '.fi', '.fj',
        '.fk', '.fm', '.fo', '.fr', '.ga', '.gd', '.ge', '.gf',
        '.gg', '.gh', '.gi', '.gl', '.gm', '.gn', '.gp', '.gq',
        '.gr', '.gs', '.gt', '.gu', '.gw', '.gy', '.hk', '.hm',
        '.hn', '.hr', '.ht', '.hu', '.id', '.ie', '.il', '.im',
        '.in', '.io', '.iq', '.ir', '.is', '.it', '.je', '.jm',
        '.jo', '.jp', '.ke', '.kg', '.kh', '.ki', '.km', '.kn',
        '.kp', '.kr', '.kw', '.ky', '.kz', '.la', '.lb', '.lc',
        '.li', '.lk', '.lr', '.ls', '.lt', '.lu', '.lv', '.ly',
        '.ma', '.mc', '.md', '.me', '.mg', '.mh', '.mk', '.ml',
        '.mm', '.mn', '.mo', '.mp', '.mq', '.mr', '.ms', '.mt',
        '.mu', '.mv', '.mw', '.mx', '.my', '.mz', '.na', '.nc',
        '.ne', '.nf', '.ng', '.ni', '.nl', '.no', 'Als', '.np',
        '.nr', '.nu', '.nz', '.om', '.pa', '.pe', '.pf', '.pg',
        '.ph', '.pk', '.pl', '.pm', '.pn', '.pr', '.ps', '.pt',
        '.pw', '.py', '.qa', '.re', '.ro', '.rs', '.ru', '.rw',
        '.sb', '.sc', '.sd', '.se', '.sg', '.sh', '.si', '.sk',
        '.sl', '.sm', '.sn', '.so', '.sr', '.ss', '.st', '.su',
        '.sv', '.sx', '.sy', '.sz', '.tc', '.td', '.tf', '.tg',
        '.th', '.tj', '.tk', '.tl', '.tm', '.tn', '.to', '.tr',
        '.tt', '.tv', '.tw', '.tz', '.ua', '.ug', '.uk', '.us',
        '.uy', '.uz', '.va', '.vc', '.ve', '.vg', '.vi', '.vn',
        '.vu', '.wf', '.ws', '.ye', '.yt', '.za', '.zm', '.zw'
    ]

    return random.choice(tlds)


def get_timestamp():
    utc_datetime = datetime.datetime.utcnow()

    return utc_datetime.strftime("%Y-%m-%dT%H:%M:%SUTC")


def random_email():
    return random_string() + '@' + random_string(2) + random_tld()


def generate_blobs(nr_blobs=1):
    blobs = []

    for _ in range(nr_blobs):
        blobs.append({
            'ip': random_ipv4(),
            'port': random_port(),
            'whois_description': random_string(2),
            'asn': random_asn(),
            'ptr': random_string(2),
            'abuse_mail': random_email(),
            'domain': "sunet.se",
            'timestamp_in_utc': get_timestamp(),
            'producer_unique_keys': {
                'subject_cn': 'unknown',
                'subject_o': 'unknown',
                'full_name': random_string(3),
                'end_of_general_support': False,
            }
        })

    return blobs


def send_blob():
    url = 'http://localhost:8000/sc/v0/add'

    blobs = generate_blobs(10000)

    start = time.process_time()
    requests.post(url, json=blobs)
    print(time.process_time() - start)


if __name__ == '__main__':
    for _ in range(100):
        send_blob()
        print('Saved 10000 docs')