forked from alefkheh/CFPorkon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
330 lines (299 loc) · 13.9 KB
/
main.py
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import logging
import multiprocessing
import os
import signal
import statistics
from datetime import datetime
from functools import partial
import pkg_resources
from rich.console import Console
from .args.parser import parse_args
from .args.testconfig import TestConfig
from .report.print import TitledProgress
from .speedtest.conduct import test_ip
from .speedtest.tools import mean_jitter
from .subnets import cidr_to_ip_gen, get_num_ips_in_cidr, read_cidrs
from .utils.exceptions import *
from .utils.os import create_dir
from .dnsrecord.cfDNSprogram import addAllCloudflareRecords,deleteCloudflareExistingRecord,getCloudflareExistingRecords
from .dnsrecord.readcredentials import read_cf_credentials
from .dnsrecord.resultsToIP import extract_ip_addresses
import concurrent.futures
import re
console = Console()
SCRIPTDIR = os.getcwd()
CONFIGDIR = f"{SCRIPTDIR}/.xray-configs"
RESULTDIR = f"{SCRIPTDIR}/result"
START_DT_STR = datetime.now().strftime(r"%Y%m%d_%H%M%S")
INTERIM_RESULTS_PATH = os.path.join(RESULTDIR, f"{START_DT_STR}_result.csv")
def _prescan_sigint_handler(sig, frame):
console.log(
f"[yellow]KeyboardIntessrrupt detected (pre-scan phase) - {START_DT_STR}[/yellow]"
)
exit(1)
def _init_pool():
signal.signal(signal.SIGINT, signal.SIG_IGN)
def main():
logger = logging.getLogger(__name__)
console = Console()
logo = """
____ ____ ____ ____ ____ _ _ _ _ ____ ____
| |___ [__ | |__| |\ | |\ | |___ |__/
|___ | ___] |___ | | | \| | \| |___ | \
"""
console.print(f"[bold green1]{logo}[/bold green1]")
try:
console.print(
f"[bold green1]v{pkg_resources.get_distribution('cfscanner').version}[bold green1]\n\n"
)
except pkg_resources.DistributionNotFound:
console.print(f"[bold green1]v0.0.0[bold green1]\n\n")
log_dir = os.path.join(SCRIPTDIR, "log")
os.makedirs(log_dir, exist_ok=True)
logging.basicConfig(filename=os.path.join(log_dir, f"{START_DT_STR}.log"))
console.log(f"[green]Scan started - {START_DT_STR}[/green]")
original_sigint_handler = signal.signal(signal.SIGINT, _prescan_sigint_handler)
args = parse_args()
if not args.no_vpn:
with console.status(f'[green]Creating config dir "{CONFIGDIR}"[/green]'):
try:
create_dir(CONFIGDIR)
except Exception as e:
console.log("[red1]Could not create config directory[/red1]")
logger.exception("Could not create config directory")
exit(1)
console.log(
f'[bright_blue]Config directory created "{CONFIGDIR}"[/bright_blue]'
)
with console.status(f'[green]Creating results directory "{RESULTDIR}"[/green]'):
try:
create_dir(RESULTDIR)
except Exception as e:
console.log("[red1]Could not create results directory[/red1]")
logger.exception("Could not create results directory")
exit(1)
console.log(f'[bright_blue]Results directory created "{RESULTDIR}"[/bright_blue]')
# create empty result file
with console.status(
f"[green]Creating empty result file {INTERIM_RESULTS_PATH}[/green]"
):
try:
with open(INTERIM_RESULTS_PATH, "w") as empty_file:
titles = [
"ip",
"avg_download_speed",
"avg_upload_speed",
"avg_download_latency",
"avg_upload_latency",
"avg_download_jitter",
"avg_upload_jitter",
]
titles += [f"download_speed_{i+1}" for i in range(args.n_tries)]
titles += [f"upload_speed_{i+1}" for i in range(args.n_tries)]
titles += [f"download_latency_{i+1}" for i in range(args.n_tries)]
titles += [f"upload_latency_{i+1}" for i in range(args.n_tries)]
empty_file.write(",".join(titles) + "\n")
except Exception as e:
console.log(
f'[red1]Could not create empty result file:\n"{INTERIM_RESULTS_PATH}"[/red1]'
)
logger.exception("Could not create empty result file")
exit(1)
try:
test_config = TestConfig.from_args(args)
except TemplateReadError as e:
console.log(
f'[red1]Could not read template from file "{args.template_path}"[/red1]'
)
logger.exception(e)
exit(1)
except BinaryNotFoundError:
console.log(
f'[red1]Could not find xray/v2ray binary from path "{args.binpath}"[/red1]'
)
logger.exception(e)
exit(1)
except Exception as e:
console.print_exception()
logger.exception(e)
exit(1)
threadsCount = args.threads
if args.subnets:
with console.status('[green]Reading subnets from "{args.subnets}"[/green]'):
try:
cidr_generator, n_cidrs = read_cidrs(
args.subnets,
shuffle=args.shuffle_subnets,
)
except SubnetsReadError as e:
console.log("[red1]Could not read subnets.[/red1]")
logger.exception("Could not read subnets")
exit(1)
except Exception as e:
console.log(f"Unknown error in reading subnets: {e}")
logger.exception(f"Unknown error in reading subnets: {e}")
exit(1)
console.log(
f'[bright_blue]Subnets successfully read from "{args.subnets}"[/bright_blue]'
)
else:
subnets_default_address = "https://raw.githubusercontent.com/MortezaBashsiz/CFScanner/main/config/cf.local.iplist"
console.log(
f'[bright_blue]Subnets not provided. Default address will be used:\n"{subnets_default_address}"[/bright_blue]'
)
with console.status(
f'[green]Retrieving subnets from "{subnets_default_address}"[/green]'
):
try:
cidr_generator, n_cidrs = read_cidrs(
"https://raw.githubusercontent.com/MortezaBashsiz/CFScanner/main/config/cf.local.iplist",
shuffle=args.shuffle_subnets,
)
except SubnetsReadError as e:
console.log(f"[red1]Could not read subnets. {e}[/red1]")
logger.exception(e)
exit(1)
except Exception as e:
console.log(f"[red1]Unknown error in reading subnets: {e}[/red1]")
logger.exception(e)
exit(1)
def ip_generator():
for cidr in cidr_generator:
for ip in cidr_to_ip_gen(
cidr,
sample_size=test_config.sample_size,
sampling_timeout=test_config.sampling_timeout,
):
yield ip, cidr
cidr_scanned_ips = dict()
cidr_prog_tasks = dict()
with TitledProgress(title=f"start: [green]{START_DT_STR}[/green]") as progress:
console = progress.console
all_subnets_task = progress.add_task(
f"all subnets - {n_cidrs} subnets", total=n_cidrs
)
with multiprocessing.Pool(
processes=threadsCount, initializer=_init_pool
) as pool:
signal.signal(signal.SIGINT, original_sigint_handler)
iterator = pool.imap(
partial(test_ip, test_config=test_config, config_dir=CONFIGDIR),
ip_generator(),
)
while True:
try:
res = next(iterator)
if res.cidr not in cidr_scanned_ips:
cidr_scanned_ips[res.cidr] = 0
n_ips_cidr = get_num_ips_in_cidr(
res.cidr, sample_size=test_config.sample_size
)
cidr_prog_tasks[res.cidr] = progress.add_task(
f"{res.cidr:17s} - {n_ips_cidr} ips", total=n_ips_cidr
)
progress.update(cidr_prog_tasks[res.cidr], advance=1)
progress.scanned_ips += 1
if res.is_ok:
progress.ok_ips += 1
down_mean_jitter = mean_jitter(
res.result["download"]["latency"]
)
up_mean_jitter = (
mean_jitter(res.result["upload"]["latency"])
if test_config.do_upload_test
else -1
)
mean_down_speed = statistics.mean(
res.result["download"]["speed"]
)
mean_up_speed = (
statistics.mean(res.result["upload"]["speed"])
if test_config.do_upload_test
else -1
)
mean_down_latency = statistics.mean(
res.result["download"]["latency"]
)
mean_up_latency = (
statistics.mean(res.result["upload"]["latency"])
if test_config.do_upload_test
else -1
)
console.print(res.message)
with open(INTERIM_RESULTS_PATH, "a") as outfile:
res_parts = [
res.ip,
mean_down_speed,
mean_up_speed,
mean_down_latency,
mean_up_latency,
down_mean_jitter,
up_mean_jitter,
]
res_parts += res.result["download"]["speed"]
res_parts += res.result["upload"]["speed"]
res_parts += res.result["download"]["latency"]
res_parts += res.result["upload"]["latency"]
outfile.write(",".join(map(str, res_parts)) + "\n")
else:
console.print(res.message)
cidr_scanned_ips[res.cidr] += 1
if cidr_scanned_ips[res.cidr] == get_num_ips_in_cidr(
res.cidr, sample_size=test_config.sample_size
):
progress.update(all_subnets_task, advance=1)
progress.remove_task(cidr_prog_tasks[res.cidr])
cidr_scanned_ips.pop(res.cidr)
except StartProxyServiceError as e:
progress.stop()
console.log(f"[red1]{e}[/red1]")
pool.terminate()
logger.exception("Error in starting xray service.")
break
except StopIteration as e:
for task in progress.tasks:
progress.stop_task(task.id)
progress.remove_task(task.id)
progress.stop()
progress.log(
f"Finished scanning ips. Start: [green]{START_DT_STR}[/green]"
)
input_file = INTERIM_RESULTS_PATH
output_file = 'tempIPlist.txt'
progress.log(f"[green]Starting...[/green]")
extract_ip_addresses(input_file, output_file)
with open("tempIPlist.txt", "r") as file:
ips = [re.search(r"\b(?:\d{1,3}\.){3}\d{1,3}\b", line).group() for line in file]
cfcredentials = "cfcredentials.txt"
cloudflare_email,cloudflare_api_key,cloudflare_zone_id,cloudflare_subdomain = read_cf_credentials(cfcredentials)
progress.log(f"[red]Deleting records...[/red]")
existing_records = getCloudflareExistingRecords(cloudflare_email, cloudflare_api_key, cloudflare_zone_id, cloudflare_subdomain)
if not existing_records:
progress.log(f"[green]No existing records found. Proceeding to add new records.[/green]")
else:
progress.log(f"[yellow]Existing records found. Deleting...[/yellow]")
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = []
for record in existing_records:
futures.append(executor.submit(deleteCloudflareExistingRecord, cloudflare_email, cloudflare_api_key, cloudflare_zone_id, record["id"]))
concurrent.futures.wait(futures)
progress.log(f"[green]Existing records deleted.[/green]")
progress.log(f"[yellow]Adding new records...[/yellow]")
addAllCloudflareRecords(cloudflare_email, cloudflare_api_key, cloudflare_zone_id, cloudflare_subdomain, ips)
progress.log(f"[green]Adding succeeded.[/green]")
os.remove("tempIPlist.txt")
break
except KeyboardInterrupt as e:
for task_id in progress.task_ids:
progress.stop_task(task_id)
progress.remove_task(task_id)
progress.stop()
progress.log(
f"[yellow]KeyboardInterrupt detected (scan phase) - start: {START_DT_STR}[/yellow]"
)
pool.terminate()
break
except Exception as e:
progress.log("[red1]Unknown error![/red1]")
console.print_exception()
logger.exception(e)