zzmez commited on
Commit
ba1dd32
·
1 Parent(s): 236f43c

Create utils/gradio_utils.py

Browse files
Files changed (1) hide show
  1. utils/gradio_utils.py +229 -0
utils/gradio_utils.py ADDED
@@ -0,0 +1,229 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ import ipaddress
3
+
4
+
5
+ ### SWAKS ###
6
+
7
+ def get_subclasses(ip_classes: str):
8
+ ip_classes = ip_classes.split('\n')
9
+ result = []
10
+ for ip_class in ip_classes:
11
+ try:
12
+ network = ipaddress.IPv4Network(ip_class, strict=False)
13
+ for ip in network.subnets(new_prefix=24):
14
+ result.append(str(ip))
15
+ except ValueError:
16
+ result.append(f"Invalid IP class: {ip_class}")
17
+ return result
18
+
19
+
20
+ def update_header(max_ips):
21
+ return f"found=0; max_ips={int(max_ips)};\n"
22
+
23
+
24
+ def str_to_list(ips_string) -> list:
25
+ """
26
+ Converts a string to a list.
27
+ """
28
+ return ips_string.split('\n')
29
+
30
+
31
+ def generate_cmds_from_bulk(big_subclasses_raw: str,
32
+ domain_raw: str,
33
+ max_ips: int = 2,
34
+ sample_size: int = 5):
35
+ """
36
+ big_subclasses_raw: The list of classes
37
+ domain_raw: The list of domains
38
+ max_ips: The maximum number of IPs to pick
39
+ sample_size: The number of sample size to generate IPs from /24 class
40
+ """
41
+ # Gradio fixes
42
+ # big_subclasses_raw = big_subclasses_raw.split('\n')
43
+ domain_raw = domain_raw.split('\n')
44
+ sample_size = int(sample_size)
45
+ max_ips = int(max_ips)
46
+
47
+ commands = []
48
+ header = f"found=0; max_ips={max_ips};"
49
+
50
+ for ip_subclass_24 in get_subclasses(big_subclasses_raw):
51
+ ip_addresses_1_20, ip_addresses_201_255 = generate_ip_addresses(ip_subclass_24)
52
+ commands.append(header)
53
+
54
+ for ip in random.sample(ip_addresses_1_20, sample_size):
55
+ commands.append(generate_randomized_output_max_ips(ip, domain_raw))
56
+
57
+ commands.append(header)
58
+ for ip in random.sample(ip_addresses_201_255, sample_size):
59
+ commands.append(generate_randomized_output_max_ips(ip, domain_raw))
60
+ return '\n'.join(commands)
61
+
62
+
63
+ def generate_ips_per_subclass(ip_subclasses: str, num_of_ips: int) -> str:
64
+ """
65
+ Generates a list of IP addresses for a given list of IP subclasses and the number of IPs.
66
+
67
+ :param ip_subclasses: List of non /24 IP subclasses in CIDR notation.
68
+ :param num_of_ips: Number of IP addresses to generate per IP subclass.
69
+ :return: List of generated IP addresses.
70
+ """
71
+ ip_addresses = []
72
+
73
+ for ip_subclass_24 in get_subclasses(ip_subclasses):
74
+ ip_addresses.extend(generate_ips_per_slash24(ip_subclass_24, num_of_ips))
75
+ return ip_addresses
76
+
77
+
78
+ def generate_ips_per_slash24(ip_class: str, num_ips: int) -> list:
79
+ """
80
+ Generates a list of IP addresses within a specified class C network (/24).
81
+
82
+ :param ip_class: The IP class in CIDR notation, e.g., "192.168.1.1/24".
83
+ :param num_ips: The number of IP addresses to generate.
84
+ :return: A list of generated IP addresses.
85
+ """
86
+ ip_addresses = []
87
+
88
+ # The IP address ranges for two disjoint domains.
89
+ domain1 = range(1, 20)
90
+ domain2 = range(201, 254)
91
+ num_ips = int(num_ips)
92
+
93
+ # The base of the IP address (e.g., "192.168.1." for "192.168.1.1/24").
94
+ base_ip = ip_class.split('/')[0].rsplit('.', 1)[0] + '.'
95
+
96
+ for i in range(1, num_ips + 1):
97
+ # If the index is in the first half of the requested IPs,
98
+ # generate an IP from the first domain.
99
+ if i <= num_ips / 2:
100
+ ip_addresses.append(base_ip +random.choice(domain1).__str__())
101
+ # If the index is in the second half of the requested IPs,
102
+ # generate an IP from the second domain.
103
+ else:
104
+ ip_addresses.append(base_ip +random.choice(domain2).__str__())
105
+
106
+ return ip_addresses
107
+
108
+
109
+ def generate_ip_addresses(slash_24: str) -> list:
110
+ ip_addresses_1_20 = []
111
+ ip_addresses_201_255 = []
112
+
113
+ # The IP address ranges for two disjoint domains.
114
+ domain_1_20 = range(1, 20)
115
+ domain_201_255 = range(201, 255)
116
+ # domain = chain(domain_1_20, domain_201_255)
117
+
118
+ # The base of the IP address (e.g., "192.168.1." for "192.168.1.1/24").
119
+ base_ip = slash_24.split('/')[0].rsplit('.', 1)[0] + '.'
120
+ for ip in domain_1_20:
121
+ ip_addresses_1_20.append(base_ip + ip.__str__())
122
+
123
+ for ip in domain_201_255:
124
+ ip_addresses_201_255.append(base_ip + ip.__str__())
125
+
126
+ return [ip_addresses_1_20, ip_addresses_201_255]
127
+
128
+
129
+ def generate_random_string(min_length=2, max_length=7):
130
+ letters = "abcdefghijklmnopqrstuvwxyz"
131
+ length = random.randint(min_length, max_length)
132
+ return ''.join(random.choice(letters) for _ in range(length))
133
+
134
+
135
+ def generate_randomized_output_max_ips(ip_address: str, domain_list: list, sleep_sec: int = 5) -> str:
136
+ part1 = generate_random_string()
137
+ part2 = generate_random_string()
138
+ part3 = generate_random_string()
139
+ part4 = generate_random_string()
140
+ # domain_list = domain_list.split('\n')
141
+ domain = random.choice(domain_list)
142
+ ip_class = ip_address.rsplit('.', 1)[0]
143
+
144
+ template = f"""if [ $found -lt $max_ips ]; then swaks -t [email protected] -h {part1}-{part2}.{part3}-{part4}.com -f from@{domain} -q from --li {ip_address} |\
145
+ tee -a swaks_full.log | \
146
+ grep -q 'sender ok'; \
147
+ if [ $? -eq 0 ]; \
148
+ then \
149
+ found=$((found+1)); \
150
+ echo {ip_address} >>bune_{ip_class}.txt; \
151
+ else echo {ip_address} >>blocate_{ip_class}.txt; \
152
+ fi; \
153
+ sleep {sleep_sec}; \
154
+ fi;"""
155
+ return template
156
+
157
+
158
+ ### MIX ###
159
+
160
+ def mix(domains: str, ip_addresses: str, num_of_ips: int) -> str:
161
+ """
162
+ Mixes the IP addresses with the domains.
163
+
164
+ :param ip_addresses: List of IP addresses.
165
+ :param domains: List of domains.
166
+ :return: List of mixed IP addresses and domains.
167
+ """
168
+ domains = domains.split('\n')
169
+ ip_addresses = ip_addresses.split('\n')
170
+
171
+ mixed = []
172
+
173
+ # Check if the number of IP addresses is the same than the number of domains.
174
+ if len(ip_addresses) == len(domains):
175
+ for i in range(len(ip_addresses)):
176
+ if i % num_of_ips == 0:
177
+ mixed.append('')
178
+ line = domains[i] + ': ' + ip_addresses[i]
179
+ mixed.append(line)
180
+ else:
181
+ raise ValueError('The number of IP addresses and domains must be the same.')
182
+
183
+ return "\n".join(mixed)
184
+
185
+
186
+ # def generate_ips_per_subclass(ip_subclasses: str, num_of_ips: int) -> str:
187
+ # """
188
+ # Generates a list of IP addresses for a given list of IP subclasses and the number of IPs.
189
+
190
+ # :param ip_subclasses: List of non /24 IP subclasses in CIDR notation.
191
+ # :param num_of_ips: Number of IP addresses to generate per IP subclass.
192
+ # :return: List of generated IP addresses.
193
+ # """
194
+ # ip_addresses = []
195
+
196
+ # for ip_subclass_24 in get_subclasses(ip_subclasses):
197
+ # ip_addresses.extend(generate_ips_per_slash24(ip_subclass_24, num_of_ips))
198
+ # return ip_addresses
199
+
200
+ def generate_ips_per_subclass(ip_subclasses: str, num_of_ips: int) -> str:
201
+ """
202
+ Generates a list of IP addresses for a given list of IP subclasses and the number of IPs.
203
+
204
+ :param ip_subclasses: List of non /24 IP subclasses in CIDR notation.
205
+ :param num_of_ips: Number of IP addresses to generate per IP subclass.
206
+ :return: List of generated IP addresses.
207
+ """
208
+ ip_subclasses = ip_subclasses.split('\n')
209
+ ip_addresses = []
210
+ mask_split_threshold = 24
211
+
212
+ for ip_subclass in ip_subclasses:
213
+ ip_base, mask = ip_subclass.rsplit('/', 1)
214
+ mask = int(mask)
215
+ ip_base = ip_base.rsplit('.', 1)[0]
216
+
217
+ if mask == mask_split_threshold:
218
+ ip_addresses.extend(generate_ips_per_slash24(ip_subclass, num_of_ips))
219
+ else:
220
+ for i in range((mask_split_threshold - mask) ** 2):
221
+ split_ip_base = ip_base.split('.')
222
+ third_octet = int(split_ip_base[2]) + i
223
+
224
+ # Construct the /24 subnet for the IP subclass
225
+ ip_subclass_24 = split_ip_base[0] + '.' + split_ip_base[1] + '.' + str(third_octet) + '.0/24'
226
+
227
+ # Assuming generate_ips_per_slash24 is the same as the previously discussed generate_ips function
228
+ ip_addresses.extend(generate_ips_per_slash24(ip_subclass_24, num_of_ips))
229
+ return "\n".join(ip_addresses)