Skip to content

Commit

Permalink
Merge pull request #592 from MortezaBashsiz/config-change-domain
Browse files Browse the repository at this point in the history
Config change domain
  • Loading branch information
tempookian authored Jun 26, 2023
2 parents fa05575 + 0830162 commit 832184d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 61 deletions.
6 changes: 3 additions & 3 deletions config/ClientConfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"id": "201b62c5-0f84-5e61-a220-4911c0c221b4",
"host": "scherewelove.latex.cf",
"host": "schereapi.goodbyeparty.tk",
"port": "443",
"path": "api01",
"serverName": "815a2e4b-f639-56f5-b498-2c149402ee80.latex.cf",
"serverName": "815a2e4b-f639-56f5-b498-2c149402ee80.goodbyeparty.tk",
"subnetsList": "https://raw.githubusercontent.com/MortezaBashsiz/CFScanner/main/config/cf.local.iplist"
}
}
138 changes: 80 additions & 58 deletions python/cfscanner/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
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')
INTERIM_RESULTS_PATH = os.path.join(RESULTDIR, f"{START_DT_STR}_result.csv")


def _prescan_sigint_handler(sig, frame):
console.log(
f"[yellow]KeyboardInterrupt detected (pre-scan phase) - {START_DT_STR}[/yellow]")
f"[yellow]KeyboardInterrupt detected (pre-scan phase) - {START_DT_STR}[/yellow]"
)
exit(1)


Expand All @@ -52,83 +53,80 @@ def main():

try:
console.print(
f"[bold green1]v{pkg_resources.get_distribution('cfscanner').version}[bold green1]\n\n")
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")
)
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
)
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]"):
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]")
f'[bright_blue]Config directory created "{CONFIGDIR}"[/bright_blue]'
)

with console.status(f"[green]Creating results directory \"{RESULTDIR}\"[/green]"):
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]")
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]"):
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)
"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)
]
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]"
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]"
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]"
f'[red1]Could not find xray/v2ray binary from path "{args.binpath}"[/red1]'
)
logger.exception(e)
exit(1)
Expand All @@ -140,7 +138,7 @@ def main():
threadsCount = args.threads

if args.subnets:
with console.status("[green]Reading subnets from \"{args.subnets}\"[/green]"):
with console.status('[green]Reading subnets from "{args.subnets}"[/green]'):
try:
cidr_generator, n_cidrs = read_cidrs(
args.subnets,
Expand All @@ -155,13 +153,16 @@ def main():
logger.exception(f"Unknown error in reading subnets: {e}")
exit(1)
console.log(
f"[bright_blue]Subnets successfully read from \"{args.subnets}\"[/bright_blue]")
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]"
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]"):
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",
Expand All @@ -172,8 +173,7 @@ def main():
logger.exception(e)
exit(1)
except Exception as e:
console.log(
f"[red1]Unknown error in reading subnets: {e}[/red1]")
console.log(f"[red1]Unknown error in reading subnets: {e}[/red1]")
logger.exception(e)
exit(1)

Expand All @@ -189,16 +189,18 @@ def ip_generator():
cidr_scanned_ips = dict()
cidr_prog_tasks = dict()

with TitledProgress(
title=f"start: [green]{START_DT_STR}[/green]"
) as progress:
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:
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()
partial(test_ip, test_config=test_config, config_dir=CONFIGDIR),
ip_generator(),
)
while True:
try:
Expand All @@ -217,25 +219,41 @@ def ip_generator():
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
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
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
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.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"]
Expand All @@ -247,7 +265,9 @@ def ip_generator():
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):
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)
Expand All @@ -263,15 +283,17 @@ def ip_generator():
progress.remove_task(task.id)
progress.stop()
progress.log(
f"Finished scanning ips. Start: [green]{START_DT_STR}[/green]")
f"Finished scanning ips. Start: [green]{START_DT_STR}[/green]"
)
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]")
f"[yellow]KeyboardInterrupt detected (scan phase) - start: {START_DT_STR}[/yellow]"
)
pool.terminate()
break
except Exception as e:
Expand Down

0 comments on commit 832184d

Please sign in to comment.