2626CONFIGDIR = f"{ SCRIPTDIR } /.xray-configs"
2727RESULTDIR = f"{ SCRIPTDIR } /result"
2828START_DT_STR = datetime .now ().strftime (r"%Y%m%d_%H%M%S" )
29- INTERIM_RESULTS_PATH = os .path .join (RESULTDIR , f' { START_DT_STR } _result.csv' )
29+ INTERIM_RESULTS_PATH = os .path .join (RESULTDIR , f" { START_DT_STR } _result.csv" )
3030
3131
3232def _prescan_sigint_handler (sig , frame ):
3333 console .log (
34- f"[yellow]KeyboardInterrupt detected (pre-scan phase) - { START_DT_STR } [/yellow]" )
34+ f"[yellow]KeyboardInterrupt detected (pre-scan phase) - { START_DT_STR } [/yellow]"
35+ )
3536 exit (1 )
3637
3738
@@ -52,83 +53,80 @@ def main():
5253
5354 try :
5455 console .print (
55- f"[bold green1]v{ pkg_resources .get_distribution ('cfscanner' ).version } [bold green1]\n \n " )
56+ f"[bold green1]v{ pkg_resources .get_distribution ('cfscanner' ).version } [bold green1]\n \n "
57+ )
5658 except pkg_resources .DistributionNotFound :
5759 console .print (f"[bold green1]v0.0.0[bold green1]\n \n " )
5860
5961 log_dir = os .path .join (SCRIPTDIR , "log" )
6062 os .makedirs (log_dir , exist_ok = True )
61- logging .basicConfig (
62- filename = os .path .join (log_dir , f"{ START_DT_STR } .log" )
63- )
63+ logging .basicConfig (filename = os .path .join (log_dir , f"{ START_DT_STR } .log" ))
6464
6565 console .log (f"[green]Scan started - { START_DT_STR } [/green]" )
6666
67- original_sigint_handler = signal .signal (
68- signal .SIGINT , _prescan_sigint_handler
69- )
67+ original_sigint_handler = signal .signal (signal .SIGINT , _prescan_sigint_handler )
7068
7169 args = parse_args ()
7270
7371 if not args .no_vpn :
74- with console .status (f" [green]Creating config dir \ "{ CONFIGDIR } \ " [/green]" ):
72+ with console .status (f' [green]Creating config dir "{ CONFIGDIR } "[/green]' ):
7573 try :
7674 create_dir (CONFIGDIR )
7775 except Exception as e :
7876 console .log ("[red1]Could not create config directory[/red1]" )
7977 logger .exception ("Could not create config directory" )
8078 exit (1 )
8179 console .log (
82- f"[bright_blue]Config directory created \" { CONFIGDIR } \" [/bright_blue]" )
80+ f'[bright_blue]Config directory created "{ CONFIGDIR } "[/bright_blue]'
81+ )
8382
84- with console .status (f" [green]Creating results directory \ "{ RESULTDIR } \ " [/green]" ):
83+ with console .status (f' [green]Creating results directory "{ RESULTDIR } "[/green]' ):
8584 try :
8685 create_dir (RESULTDIR )
8786 except Exception as e :
8887 console .log ("[red1]Could not create results directory[/red1]" )
8988 logger .exception ("Could not create results directory" )
9089 exit (1 )
91- console .log (
92- f"[bright_blue]Results directory created \" { RESULTDIR } \" [/bright_blue]" )
90+ console .log (f'[bright_blue]Results directory created "{ RESULTDIR } "[/bright_blue]' )
9391
9492 # create empty result file
95- with console .status (f"[green]Creating empty result file { INTERIM_RESULTS_PATH } [/green]" ):
93+ with console .status (
94+ f"[green]Creating empty result file { INTERIM_RESULTS_PATH } [/green]"
95+ ):
9696 try :
9797 with open (INTERIM_RESULTS_PATH , "w" ) as empty_file :
9898 titles = [
99- "ip" , "avg_download_speed" , "avg_upload_speed" ,
100- "avg_download_latency" , "avg_upload_latency" ,
101- "avg_download_jitter" , "avg_upload_jitter"
102- ]
103- titles += [
104- f"download_speed_{ i + 1 } " for i in range (args .n_tries )
99+ "ip" ,
100+ "avg_download_speed" ,
101+ "avg_upload_speed" ,
102+ "avg_download_latency" ,
103+ "avg_upload_latency" ,
104+ "avg_download_jitter" ,
105+ "avg_upload_jitter" ,
105106 ]
107+ titles += [f"download_speed_{ i + 1 } " for i in range (args .n_tries )]
106108 titles += [f"upload_speed_{ i + 1 } " for i in range (args .n_tries )]
107- titles += [
108- f"download_latency_{ i + 1 } " for i in range (args .n_tries )
109- ]
110- titles += [
111- f"upload_latency_{ i + 1 } " for i in range (args .n_tries )
112- ]
109+ titles += [f"download_latency_{ i + 1 } " for i in range (args .n_tries )]
110+ titles += [f"upload_latency_{ i + 1 } " for i in range (args .n_tries )]
113111 empty_file .write ("," .join (titles ) + "\n " )
114112 except Exception as e :
115113 console .log (
116- f" [red1]Could not create empty result file:\n \ "{ INTERIM_RESULTS_PATH } \ " [/red1]"
114+ f' [red1]Could not create empty result file:\n "{ INTERIM_RESULTS_PATH } "[/red1]'
117115 )
118116 logger .exception ("Could not create empty result file" )
119117 exit (1 )
120-
118+
121119 try :
122120 test_config = TestConfig .from_args (args )
123121 except TemplateReadError as e :
124122 console .log (
125- f" [red1]Could not read template from file \ "{ args .template_path } \ " [/red1]"
123+ f' [red1]Could not read template from file "{ args .template_path } "[/red1]'
126124 )
127125 logger .exception (e )
128126 exit (1 )
129127 except BinaryNotFoundError :
130128 console .log (
131- f" [red1]Could not find xray/v2ray binary from path \ "{ args .binpath } \ " [/red1]"
129+ f' [red1]Could not find xray/v2ray binary from path "{ args .binpath } "[/red1]'
132130 )
133131 logger .exception (e )
134132 exit (1 )
@@ -140,7 +138,7 @@ def main():
140138 threadsCount = args .threads
141139
142140 if args .subnets :
143- with console .status (" [green]Reading subnets from \ " {args.subnets}\ " [/green]" ):
141+ with console .status (' [green]Reading subnets from "{args.subnets}"[/green]' ):
144142 try :
145143 cidr_generator , n_cidrs = read_cidrs (
146144 args .subnets ,
@@ -155,13 +153,16 @@ def main():
155153 logger .exception (f"Unknown error in reading subnets: { e } " )
156154 exit (1 )
157155 console .log (
158- f"[bright_blue]Subnets successfully read from \" { args .subnets } \" [/bright_blue]" )
156+ f'[bright_blue]Subnets successfully read from "{ args .subnets } "[/bright_blue]'
157+ )
159158 else :
160159 subnets_default_address = "https://raw.githubusercontent.com/MortezaBashsiz/CFScanner/main/config/cf.local.iplist"
161160 console .log (
162- f" [bright_blue]Subnets not provided. Default address will be used:\n \ "{ subnets_default_address } \ " [/bright_blue]"
161+ f' [bright_blue]Subnets not provided. Default address will be used:\n "{ subnets_default_address } "[/bright_blue]'
163162 )
164- with console .status (f"[green]Retrieving subnets from \" { subnets_default_address } \" [/green]" ):
163+ with console .status (
164+ f'[green]Retrieving subnets from "{ subnets_default_address } "[/green]'
165+ ):
165166 try :
166167 cidr_generator , n_cidrs = read_cidrs (
167168 "https://raw.githubusercontent.com/MortezaBashsiz/CFScanner/main/config/cf.local.iplist" ,
@@ -172,8 +173,7 @@ def main():
172173 logger .exception (e )
173174 exit (1 )
174175 except Exception as e :
175- console .log (
176- f"[red1]Unknown error in reading subnets: { e } [/red1]" )
176+ console .log (f"[red1]Unknown error in reading subnets: { e } [/red1]" )
177177 logger .exception (e )
178178 exit (1 )
179179
@@ -189,16 +189,18 @@ def ip_generator():
189189 cidr_scanned_ips = dict ()
190190 cidr_prog_tasks = dict ()
191191
192- with TitledProgress (
193- title = f"start: [green]{ START_DT_STR } [/green]"
194- ) as progress :
192+ with TitledProgress (title = f"start: [green]{ START_DT_STR } [/green]" ) as progress :
195193 console = progress .console
196194 all_subnets_task = progress .add_task (
197- f"all subnets - { n_cidrs } subnets" , total = n_cidrs )
198- with multiprocessing .Pool (processes = threadsCount , initializer = _init_pool ) as pool :
195+ f"all subnets - { n_cidrs } subnets" , total = n_cidrs
196+ )
197+ with multiprocessing .Pool (
198+ processes = threadsCount , initializer = _init_pool
199+ ) as pool :
199200 signal .signal (signal .SIGINT , original_sigint_handler )
200201 iterator = pool .imap (
201- partial (test_ip , test_config = test_config , config_dir = CONFIGDIR ), ip_generator ()
202+ partial (test_ip , test_config = test_config , config_dir = CONFIGDIR ),
203+ ip_generator (),
202204 )
203205 while True :
204206 try :
@@ -217,25 +219,41 @@ def ip_generator():
217219 if res .is_ok :
218220 progress .ok_ips += 1
219221 down_mean_jitter = mean_jitter (
220- res .result ["download" ]["latency" ])
221- up_mean_jitter = mean_jitter (
222- res .result ["upload" ]["latency" ]) if test_config .do_upload_test else - 1
222+ res .result ["download" ]["latency" ]
223+ )
224+ up_mean_jitter = (
225+ mean_jitter (res .result ["upload" ]["latency" ])
226+ if test_config .do_upload_test
227+ else - 1
228+ )
223229 mean_down_speed = statistics .mean (
224- res .result ["download" ]["speed" ])
225- mean_up_speed = statistics .mean (
226- res .result ["upload" ]["speed" ]) if test_config .do_upload_test else - 1
230+ res .result ["download" ]["speed" ]
231+ )
232+ mean_up_speed = (
233+ statistics .mean (res .result ["upload" ]["speed" ])
234+ if test_config .do_upload_test
235+ else - 1
236+ )
227237 mean_down_latency = statistics .mean (
228- res .result ["download" ]["latency" ])
229- mean_up_latency = statistics .mean (
230- res .result ["upload" ]["latency" ]) if test_config .do_upload_test else - 1
238+ res .result ["download" ]["latency" ]
239+ )
240+ mean_up_latency = (
241+ statistics .mean (res .result ["upload" ]["latency" ])
242+ if test_config .do_upload_test
243+ else - 1
244+ )
231245
232246 console .print (res .message )
233247
234248 with open (INTERIM_RESULTS_PATH , "a" ) as outfile :
235249 res_parts = [
236- res .ip , mean_down_speed , mean_up_speed ,
237- mean_down_latency , mean_up_latency ,
238- down_mean_jitter , up_mean_jitter
250+ res .ip ,
251+ mean_down_speed ,
252+ mean_up_speed ,
253+ mean_down_latency ,
254+ mean_up_latency ,
255+ down_mean_jitter ,
256+ up_mean_jitter ,
239257 ]
240258 res_parts += res .result ["download" ]["speed" ]
241259 res_parts += res .result ["upload" ]["speed" ]
@@ -247,7 +265,9 @@ def ip_generator():
247265 console .print (res .message )
248266
249267 cidr_scanned_ips [res .cidr ] += 1
250- if cidr_scanned_ips [res .cidr ] == get_num_ips_in_cidr (res .cidr , sample_size = test_config .sample_size ):
268+ if cidr_scanned_ips [res .cidr ] == get_num_ips_in_cidr (
269+ res .cidr , sample_size = test_config .sample_size
270+ ):
251271 progress .update (all_subnets_task , advance = 1 )
252272 progress .remove_task (cidr_prog_tasks [res .cidr ])
253273 cidr_scanned_ips .pop (res .cidr )
@@ -263,15 +283,17 @@ def ip_generator():
263283 progress .remove_task (task .id )
264284 progress .stop ()
265285 progress .log (
266- f"Finished scanning ips. Start: [green]{ START_DT_STR } [/green]" )
286+ f"Finished scanning ips. Start: [green]{ START_DT_STR } [/green]"
287+ )
267288 break
268289 except KeyboardInterrupt as e :
269290 for task_id in progress .task_ids :
270291 progress .stop_task (task_id )
271292 progress .remove_task (task_id )
272293 progress .stop ()
273294 progress .log (
274- f"[yellow]KeyboardInterrupt detected (scan phase) - start: { START_DT_STR } [/yellow]" )
295+ f"[yellow]KeyboardInterrupt detected (scan phase) - start: { START_DT_STR } [/yellow]"
296+ )
275297 pool .terminate ()
276298 break
277299 except Exception as e :
0 commit comments