-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbbr.py
916 lines (807 loc) · 35.6 KB
/
bbr.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
import os
import shutil
import platform
import subprocess
#Colors
RED = "\033[91m"
GREEN = "\033[92m"
YELLOW = "\033[93m"
BLUE = "\033[94m"
MAGENTA = "\033[95m"
CYAN = "\033[96m"
RESET = "\033[0m"
def show_image():
ascii_art = f"""
{MAGENTA}%%%%%%%%%%%%%%%%$%%$%$$$$%%%%%%%%%%%%%
%%%%%%%%%%%%%$!!****:!*!*$%$%%%%%%%%%%
%%%%%%%%%%%$:%**:*%:!*:::!!%%%%%%%%%%%
%%%%%%%%%%*!$*&%$%%@:!!:::!:%$%%%%%%%%
%%%%%%%%%!%$*%****$*$$@:::*::!%%%%%%%%
%%%%%%%%**%%$::*$:$:*:::::%!::*%%%%%%%
%%%%%%%%*:$!%%**%*$:**!*::%*:%:$%%%%%%
%%%%%%%%!*$*%!::::!%$$!!::%%:$*$!%%%%%
%%%%%%%%:%*%$@:::::$@$@:::$%:$%$!%%%%%
%%%%%%%*!*!%::!:::::::*::!*%**$$*%%%%%
%%%%%%**!!!*:::::::::%!!!*$$$%%$$$%%%%
%%%%%%!*!!:$%*:::::*$!!!%$@%%$%%%$%%%%
%%%%%%:!!:%%%%$*%%*%$:::*@@SSSSSSSS%%%
%%%%%::!::$$#B%!SS#S::!*S#SSSSSSSSS%%%
%%%%%!!!!!**&.&@$@#&!!:!SSBSS$@#SSS%%%
%%%%%!!!!!!@#S@&#SS$!!!:S:::#SSSSSS%%%
%%%%%!!!*&###$@&SS#S*!!%!!:#SSSSSSS%%%
%%%%%!%####@$$SS##S*!!*!%&SSSSSSSSS%%%{RESET}
"""
print(ascii_art)
def show_main_menu():
show_image()
print(f"{CYAN}V 1.5{RESET}")
print("Special thanks to the Queen")
print(f"View the project on GitHub: {BLUE}https://github.com/kalilovers{RESET}")
print(f"{BLUE}---------------------------------------------------------{RESET}")
print(f"{CYAN}LightKnight{RESET} Script for Qdisc and Congestion Control Setup and Speed Testing ")
print("")
print(f"0_ {YELLOW}Status{RESET}")
print("1_ BBR Base (Recommended)")
print("2_ Hybla Base")
print("3_ Cubic Base")
print("4_ Restore Default Settings")
print("5_ Speed Test")
print(f"q_ {RED}Exit{RESET}")
print("")
def show_status_menu():
congestion_control, qdisc_algorithm = get_current_settings()
print(f"\n{GREEN}Current Setting:{RESET}")
print("")
print(f"Current Congestion Control : {GREEN}{congestion_control}{RESET}")
print(f"Current Qdisc Algorithm : {GREEN}{qdisc_algorithm}{RESET}")
print("\n0: Back")
print("")
def show_bbr_menu():
print(f"\n{GREEN}BBR Base{RESET}")
print("")
print("1_BBR + Fq")
print("2_BBR + Fq_Codel (Recommended)")
print("3_BBR + Cake")
print("0_Back")
print("")
def show_fq_menu():
print(f"\n{GREEN}Apply With Fq{RESET}")
print("")
print(f"{RED}Warning , Choose carefully :{RESET}")
print(f"1_Delete Old File Then Setup+Backup ({GREEN}Recommended for proper optimization{RESET}) ")
print(f"2_Setup Without Delete ({YELLOW}If you have special settings in sysctl.conf{RESET})")
print("0_Back")
print("")
def show_fq_codel_menu():
print(f"\n{GREEN}Apply With Fq_Codel{RESET}")
print("")
print(f"{RED}Warning , Choose carefully :{RESET}")
print(f"1_Delete Old File Then Setup+Backup ({GREEN}Recommended for proper optimization{RESET}) ")
print(f"2_Setup Without Delete ({YELLOW}If you have special settings in sysctl.conf{RESET})")
print("0_Back")
print("")
def show_cake_menu():
print(f"\n{GREEN}Apply With Cake{RESET}")
print("")
print(f"{RED}Warning , Choose carefully :{RESET}")
print(f"1_Delete Old File Then Setup+Backup ({GREEN}Recommended for proper optimization{RESET}) ")
print(f"2_Setup Without Delete ({YELLOW}If you have special settings in sysctl.conf{RESET})")
print("0_Back")
print("")
def show_hybla_menu():
print(f"\n{GREEN}Hybla Base{RESET}")
print("")
print("1_Hybla + Fq")
print("2_Hybla + Fq_Codel")
print("3_Hybla + Cake")
print("0_Back")
print("")
def show_cubic_menu():
print(f"\n{GREEN}Cubic Base{RESET}")
print("")
print("1_Cubic + Fq")
print("2_Cubic + Fq_Codel")
print("3_Cubic + Cake")
print("0_Back")
print("")
def show_speed_test_menu():
print(f"\n{GREEN}Speed Test{RESET}")
print("")
print("1_Bench Method1")
print("2_Bench Method2")
print("3_Iperf3 (Between 2 Server)")
print("4_Speedtest Ookla")
print("0_Back")
print("")
def show_iperf_menu():
print(f"\n{GREEN}Choose which one you are:{RESET}")
print("")
print("1_Client (iran or...)")
print("2_Server (Kharej/Target or...)")
print("0_Back")
print("")
def install_required_packages():
print("Checking and installing required packages...")
required_packages = ['iproute2', 'iptables']
missing_packages = []
for package in required_packages:
result = subprocess.run(['dpkg', '-s', package], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
if result.returncode != 0:
missing_packages.append(package)
if missing_packages:
print(f"The following packages are not installed: {', '.join(missing_packages)}")
install_confirm = input("Do you want to install these packages? (y/n): ").strip().lower()
if install_confirm in ['y', 'yes']:
print(f"Installing: {', '.join(missing_packages)}")
subprocess.run(['apt', 'update'])
subprocess.run(['apt', 'install', '-y'] + missing_packages)
print("Required packages have been installed.")
else:
print("Installation of required packages was skipped. Exiting the script.")
exit(1)
else:
print("All required packages are installed.")
return True
def activate_parameters_persistent():
print("Activating parameters persistently...")
try:
with open("/etc/sysctl.conf", "a") as f:
f.write("\n# Enable parameters\n")
f.write("net.ipv4.tcp_ecn = 1\n")
f.write("net.ipv4.tcp_keepalive_time = 2700\n")
f.write("net.ipv4.tcp_keepalive_intvl = 900\n")
f.write("net.ipv4.tcp_keepalive_probes = 2\n")
f.write("net.ipv4.tcp_sack = 1\n")
subprocess.run(['sysctl', '-p'])
print("parameters activated and will remain active after reboot.")
return True
except Exception as e:
print(f"parameters activation error: {e}")
return False
def get_main_interface():
"""Find the main interface based on 'UP' status and highest MTU."""
try:
interfaces = os.listdir("/sys/class/net/")
main_interface = None
highest_mtu = -1
virtual_interfaces = ['tun', 'docker', 'veth', 'lo', 'br', 'gre', 'sit', 'vxlan', 'tap', 'gretap', 'ipip']
for interface in interfaces:
if any(interface.startswith(v) for v in virtual_interfaces):
continue
result = subprocess.run(['ip', 'addr', 'show', interface], capture_output=True, text=True)
if "UP" in result.stdout:
try:
with open(f"/sys/class/net/{interface}/mtu", "r") as mtu_file:
mtu = int(mtu_file.read().strip())
if mtu > highest_mtu:
highest_mtu = mtu
main_interface = interface
except Exception as e:
print(f"Error reading MTU for {interface}: {e}")
if main_interface:
print(f"Main interface detected: {main_interface} with MTU {highest_mtu}")
return main_interface
else:
print("No main interface found.")
return None
except Exception as e:
print(f"Error detecting main interface: {e}")
return None
def get_current_settings():
main_interface = get_main_interface()
if not main_interface:
return "Unknown", "Unknown"
try:
qdisc_result = subprocess.check_output(f"tc qdisc show dev {main_interface}", shell=True).decode().strip()
except subprocess.CalledProcessError:
print(f"Error retrieving qdisc for {main_interface}")
return "Unknown", "Unknown"
qdisc_lines = qdisc_result.splitlines()
root_qdisc = None
for line in qdisc_lines:
if "root" in line:
root_qdisc = line.split()[1]
break
try:
congestion_control = subprocess.check_output("sysctl net.ipv4.tcp_congestion_control", shell=True).decode().split('=')[1].strip()
except subprocess.CalledProcessError:
print("Error retrieving TCP congestion control")
congestion_control = "Unknown"
return congestion_control, root_qdisc if root_qdisc else "Unknown"
def setup_qdisc(algorithm):
"""Apply the Qdisc to the main interface."""
print("Setting the queuing algorithm on the main network interface...")
interface = get_main_interface()
if not interface:
print("No main network interface found. Exiting script.")
return False
try:
subprocess.run(['tc', 'qdisc', 'replace', 'dev', interface, 'root', algorithm], check=True)
print(f"The {algorithm} queuing algorithm was set on the {interface} interface.")
return True
except subprocess.CalledProcessError as e:
print(f"Error in setting the queuing algorithm on {interface}: {e}. Exiting script.")
return False
def make_qdisc_persistent(algorithm):
"""Create a systemd service to persist the Qdisc settings after reboot."""
print("Creating systemd service to preserve qdisc settings after reboot on the main interface...")
interface = get_main_interface()
if not interface:
print("No main network interface found. Exiting script.")
return False
exec_command = f"/sbin/tc qdisc replace dev {interface} root {algorithm}"
service_content = f"""[Unit]
Description=Set qdisc {algorithm} on the main network interface
After=network.target
[Service]
Type=oneshot
ExecStart=/bin/sh -c '{exec_command}'
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
"""
service_path = "/etc/systemd/system/qdisc-setup.service"
try:
with open(service_path, "w") as f:
f.write(service_content)
subprocess.run(['systemctl', 'daemon-reload'])
subprocess.run(['systemctl', 'enable', 'qdisc-setup.service'])
subprocess.run(['systemctl', 'start', 'qdisc-setup.service'])
print("The systemd service was successfully created and activated for the main interface.")
return True
except Exception as e:
print(f"Error creating systemd service: {e}. Exiting script.")
return False
def prompt_restart():
while True:
try:
choice = input(f"\n{GREEN}The mission was successfully completed. Do you want to restart? (Required) Yes or No: {RESET}").strip().lower()
if choice in ['n', 'no']:
break
elif choice in ['y', 'yes']:
os.system("reboot")
break
else:
print("Invalid input. Please enter Yes or No.")
except UnicodeDecodeError:
print("Invalid input. Please use only English characters.")
############################################## For BBR Base
def check_kernel_and_os():
print("Checking the compatibility of operating system and kernel...")
try:
with open("/etc/os-release", "r") as f:
lines = f.readlines()
distro = ""
for line in lines:
if line.startswith("ID="):
distro = line.strip().split("=")[1].strip('"').lower()
break
except FileNotFoundError:
distro = ""
if distro not in ['ubuntu', 'debian']:
print("The operating system is not supported. Only Ubuntu and Debian are supported.")
return False
kernel_version = platform.release()
version_numbers = kernel_version.split(".")
try:
kernel_major = int(version_numbers[0])
kernel_minor = int(version_numbers[1])
except (IndexError, ValueError):
print(f"Kernel version {kernel_version} could not be detected.")
return False
if kernel_major > 4 or (kernel_major == 4 and kernel_minor >= 9):
print(f"The kernel version is compatible with BBR.")
return True
else:
print(f"Kernel version {kernel_version} is not supported. Requires version 4.9 or higher.")
return False
def check_kernel_and_os_cake():
print("Checking the compatibility of operating system and kernel for Cake...")
try:
with open("/etc/os-release", "r") as f:
lines = f.readlines()
distro = ""
for line in lines:
if line.startswith("ID="):
distro = line.strip().split("=")[1].strip('"').lower()
break
except FileNotFoundError:
distro = ""
if distro not in ['ubuntu', 'debian']:
print("The operating system is not supported. Only Ubuntu and Debian are supported.")
return False
kernel_version = platform.release()
version_numbers = kernel_version.split(".")
try:
kernel_major = int(version_numbers[0])
kernel_minor = int(version_numbers[1])
except (IndexError, ValueError):
print(f"Kernel version {kernel_version} could not be detected.")
return False
if kernel_major > 4 or (kernel_major == 4 and kernel_minor >= 19):
print(f"The kernel version is compatible with Cake and BBR.")
return True
else:
print(f"Kernel version {kernel_version} is not supported. Requires version 4.19 or higher.")
return False
def configure_bbr(algorithm):
print("Setting up BBR...")
if activate_parameters_persistent():
print("parameters has been activated with BBR.")
try:
with open("/etc/sysctl.conf", "a") as f:
f.write(f"\n# {algorithm} Light Knight\n")
f.write(f"net.core.default_qdisc = {algorithm}\n")
f.write("net.ipv4.tcp_congestion_control = bbr\n")
subprocess.run(['sysctl', '-p'])
print("BBR settings applied.")
except Exception as e:
print(f"Error in setting BBR: {e}")
return False
if not setup_qdisc(algorithm):
return False
if not make_qdisc_persistent(algorithm):
return False
return True
def delete_old_file_then_setup(algorithm):
print("Deleting old files and setting new (+Backup)...")
backup_path = "/etc/sysctl.confback"
original_path = "/etc/sysctl.conf"
try:
if os.path.exists(original_path):
shutil.copy(original_path, backup_path)
os.remove(original_path)
print("The old sysctl.conf file was deleted and backed up.")
else:
print("No original sysctl.conf file to delete.")
except Exception as e:
print(f"Error deleting old files: {e}")
return False
if configure_bbr(algorithm):
prompt_restart()
else:
print("BBR settings encountered a problem.")
return True
def setup_without_delete(algorithm):
print("Setting up without deleting old files...")
backup_path = "/etc/sysctl.confback"
original_path = "/etc/sysctl.conf"
try:
if not os.path.exists(backup_path):
shutil.copy(original_path, backup_path)
print("sysctl.conf file backed up successfully.")
except Exception as e:
print(f"Error backing up sysctl.conf: {e}")
return False
if configure_bbr(algorithm):
prompt_restart()
else:
print("BBR settings encountered a problem.")
return True
###################### End of BBr based
############################################## For Hybla Base
def configure_bbr_hybla(algorithm):
print("Setting up hybla...")
if activate_parameters_persistent():
print("parameters has been activated with hybla.")
try:
with open("/etc/sysctl.conf", "a") as f:
f.write(f"\n# {algorithm} Light Knight\n")
f.write(f"net.core.default_qdisc = {algorithm}\n")
f.write("net.ipv4.tcp_congestion_control = hybla\n")
subprocess.run(['sysctl', '-p'])
print("hybla settings applied.")
except Exception as e:
print(f"Error in setting hybla: {e}")
return False
if not setup_qdisc(algorithm):
return False
if not make_qdisc_persistent(algorithm):
return False
return True
def delete_old_file_then_setup_hybla(algorithm):
print("Deleting old files and setting new (+Backup)...")
backup_path = "/etc/sysctl.confback"
original_path = "/etc/sysctl.conf"
try:
if os.path.exists(original_path):
shutil.copy(original_path, backup_path)
os.remove(original_path)
print("The old sysctl.conf file was deleted and backed up.")
else:
print("No original sysctl.conf file to delete.")
except Exception as e:
print(f"Error deleting old files: {e}")
return False
if configure_bbr_hybla(algorithm):
prompt_restart()
else:
print("hybla settings encountered a problem.")
return True
def setup_without_delete_hybla(algorithm):
print("Setting up without deleting old files...")
backup_path = "/etc/sysctl.confback"
original_path = "/etc/sysctl.conf"
try:
if not os.path.exists(backup_path):
shutil.copy(original_path, backup_path)
print("sysctl.conf file backed up successfully.")
except Exception as e:
print(f"Error backing up sysctl.conf: {e}")
return False
if configure_bbr_hybla(algorithm):
prompt_restart()
else:
print("hybla settings encountered a problem.")
return True
###################### End of Hybla based
############################################# For Cubic Base
def configure_bbr_cubic(algorithm):
print("Setting up Cubic...")
if activate_parameters_persistent():
print("parameters has been activated with Cubic.")
try:
with open("/etc/sysctl.conf", "a") as f:
f.write(f"\n# {algorithm} Light Knight\n")
f.write(f"net.core.default_qdisc = {algorithm}\n")
f.write("net.ipv4.tcp_congestion_control = cubic\n")
subprocess.run(['sysctl', '-p'])
print("Cubic settings applied.")
except Exception as e:
print(f"Error in setting Cubic: {e}")
return False
if not setup_qdisc(algorithm):
return False
if not make_qdisc_persistent(algorithm):
return False
return True
def delete_old_file_then_setup_cubic(algorithm):
print("Deleting old files and setting new (+Backup)...")
backup_path = "/etc/sysctl.confback"
original_path = "/etc/sysctl.conf"
try:
if os.path.exists(original_path):
shutil.copy(original_path, backup_path)
os.remove(original_path)
print("The old sysctl.conf file was deleted and backed up.")
else:
print("No original sysctl.conf file to delete.")
except Exception as e:
print(f"Error deleting old files: {e}")
return False
if configure_bbr_cubic(algorithm):
prompt_restart()
else:
print("Cubic settings encountered a problem.")
return True
def setup_without_delete_cubic(algorithm):
print("Setting up without deleting old files...")
backup_path = "/etc/sysctl.confback"
original_path = "/etc/sysctl.conf"
try:
if not os.path.exists(backup_path):
shutil.copy(original_path, backup_path)
print("sysctl.conf file backed up successfully.")
except Exception as e:
print(f"Error backing up sysctl.conf: {e}")
return False
if configure_bbr_cubic(algorithm):
prompt_restart()
else:
print("Cubic settings encountered a problem.")
return True
###################### End of Cubic based
def restore():
print("Restoring default settings...")
backup_path = "/etc/sysctl.confback"
original_path = "/etc/sysctl.conf"
try:
if os.path.exists(backup_path):
if os.path.exists(original_path):
os.remove(original_path)
print("Removed the original sysctl.conf file.")
shutil.copy(backup_path, original_path)
print("Restored sysctl.conf backup.")
os.remove(backup_path)
print("Backup sysctl.conf file removed.")
else:
print("No backup found for sysctl.conf.")
except Exception as e:
print(f"Error restoring sysctl.conf backup: {e}")
service_path = "/etc/systemd/system/qdisc-setup.service"
try:
if os.path.exists(service_path):
subprocess.run(['systemctl', 'disable', 'qdisc-setup.service'])
os.remove(service_path)
subprocess.run(['systemctl', 'daemon-reload'])
print("The systemd service related to qdisc was removed.")
if os.path.exists("/etc/systemd/system/multi-user.target.wants/qdisc-setup.service"):
os.remove("/etc/systemd/system/multi-user.target.wants/qdisc-setup.service")
print("Removed /etc/systemd/system/multi-user.target.wants/qdisc-setup.service.")
except Exception as e:
print(f"Error deleting systemd service: {e}")
interface = get_main_interface()
if interface:
try:
subprocess.run(['tc', 'qdisc', 'delete', 'dev', interface, 'root'], check=True)
print(f"Default qdisc settings on {interface} were restored.")
except subprocess.CalledProcessError:
print(f"Default qdisc settings could not be reset on {interface}. Applying fq instead.")
subprocess.run(['tc', 'qdisc', 'replace', 'dev', interface, 'root', 'fq'], check=True)
print(f"The fq queuing algorithm was set on the {interface} interface.")
else:
print("No main network interface found to restore settings.")
try:
subprocess.run(['sysctl', '-p'])
print("sysctl settings applied.")
except Exception as e:
print(f"Error applying sysctl settings: {e}")
prompt_restart()
return True
def run_bench_method1():
print("Running Bench Method1...")
os.system("wget -qO- bench.sh | bash")
def run_bench_method2():
print("Running Bench Method2...")
os.system("curl -Lso- bench.sh | bash")
def run_iperf_client():
print("Starting Iperf3 as client...")
os.system("apt update")
os.system("apt install iperf3 -y")
os.system("ufw allow 5201")
server_ip = input("What is the IP address of your target server? Enter Your Target IP: ")
os.system(f"iperf3 -c {server_ip} -i 1 -t 10 -P 20")
input("The download speed test was done. To test the upload speed, press Enter...")
os.system(f"iperf3 -c {server_ip} -R -i 1 -t 10 -P 20")
input("The upload speed test was done. To Back to the menu, press Enter...")
def run_iperf_server():
print("Starting Iperf3 as server...")
os.system("apt update")
os.system("apt install iperf3 -y")
os.system("ufw allow 5201")
os.system("iperf3 -s")
input("You are active as a server, please do not log out. If it is finished, to Back to the menu, press Enter...")
def run_speedtest_ookla():
print("Downloading and running Ookla Speedtest (linux-x86_64) ...")
os.system("wget https://install.speedtest.net/app/cli/ookla-speedtest-1.2.0-linux-x86_64.tgz")
os.system("tar zxvf ookla-speedtest-1.2.0-linux-x86_64.tgz")
server_num = input(f"{GREEN}Enter your{RESET} {YELLOW}ookla-server number{RESET}.{GREEN} Or press Enter to let Speedtest automatically select the closest server: {RESET}")
print(f"{CYAN}Speed test starting, please wait...{RESET}")
if not server_num:
os.system("./speedtest")
else:
os.system(f"./speedtest -s {server_num}")
try:
os.remove("ookla-speedtest-1.2.0-linux-x86_64.tgz")
if os.path.exists("ookla-speedtest-1.2.0-linux-x86_64"):
shutil.rmtree("ookla-speedtest-1.2.0-linux-x86_64")
if os.path.exists("./speedtest"):
os.remove("./speedtest")
for filename in os.listdir("."):
if "ookla" in filename or filename.startswith("speedtest"):
if os.path.isfile(filename):
os.remove(filename)
elif os.path.isdir(filename):
shutil.rmtree(filename)
except Exception as e:
print(f"Error cleaning up files: {e}")
input(f"{GREEN}SpeedTest Done and files removed, to Back to the menu, press Enter{RESET}")
def main():
try:
while True:
show_main_menu()
choice = input("Please enter your choice: ")
if choice == '0':
while True:
show_status_menu()
status_choice = input("Please enter your choice (0 to go back): ")
if status_choice == '0':
break
else:
print("Invalid choice, please try again.")
elif choice == '1':
while True:
show_bbr_menu()
bbr_choice = input("Please enter your choice: ")
if bbr_choice == '1':
while True:
show_fq_menu()
fq_choice = input("Please enter your choice: ")
if fq_choice == '1':
if check_kernel_and_os():
install_required_packages()
delete_old_file_then_setup("fq")
elif fq_choice == '2':
if check_kernel_and_os():
install_required_packages()
setup_without_delete("fq")
elif fq_choice == '0':
break
else:
print("Invalid choice, please try again.")
elif bbr_choice == '2':
while True:
show_fq_codel_menu()
fq_codel_choice = input("Please enter your choice: ")
if fq_codel_choice == '1':
if check_kernel_and_os():
install_required_packages()
delete_old_file_then_setup("fq_codel")
elif fq_codel_choice == '2':
if check_kernel_and_os():
install_required_packages()
setup_without_delete("fq_codel")
elif fq_codel_choice == '0':
break
else:
print("Invalid choice, please try again.")
elif bbr_choice == '3':
while True:
show_cake_menu()
cake_choice = input("Please enter your choice: ")
if cake_choice == '1':
if check_kernel_and_os_cake():
install_required_packages()
delete_old_file_then_setup("cake")
elif cake_choice == '2':
if check_kernel_and_os_cake():
install_required_packages()
setup_without_delete("cake")
elif cake_choice == '0':
break
else:
print("Invalid choice, please try again.")
elif bbr_choice == '0':
break
else:
print("Invalid choice, please try again.")
elif choice == '2':
while True:
show_hybla_menu()
hybla_choice = input("Please enter your choice: ")
if hybla_choice == '1':
while True:
show_fq_menu()
fq_choice = input("Please enter your choice: ")
if fq_choice == '1':
if check_kernel_and_os():
install_required_packages()
delete_old_file_then_setup_hybla("fq")
elif fq_choice == '2':
if check_kernel_and_os():
install_required_packages()
setup_without_delete_hybla("fq")
elif fq_choice == '0':
break
else:
print("Invalid choice, please try again.")
elif hybla_choice == '2':
while True:
show_fq_codel_menu()
fq_codel_choice = input("Please enter your choice: ")
if fq_codel_choice == '1':
if check_kernel_and_os():
install_required_packages()
delete_old_file_then_setup_hybla("fq_codel")
elif fq_codel_choice == '2':
if check_kernel_and_os():
install_required_packages()
setup_without_delete_hybla("fq_codel")
elif fq_codel_choice == '0':
break
else:
print("Invalid choice, please try again.")
elif hybla_choice == '3':
while True:
show_cake_menu()
cake_choice = input("Please enter your choice: ")
if cake_choice == '1':
if check_kernel_and_os_cake():
install_required_packages()
delete_old_file_then_setup_hybla("cake")
elif cake_choice == '2':
if check_kernel_and_os_cake():
install_required_packages()
setup_without_delete_hybla("cake")
elif cake_choice == '0':
break
else:
print("Invalid choice, please try again.")
elif hybla_choice == '0':
break
else:
print("Invalid choice, please try again.")
elif choice == '3':
while True:
show_cubic_menu()
cubic_choice = input("Please enter your choice: ")
if cubic_choice == '1':
while True:
show_fq_menu()
fq_choice = input("Please enter your choice: ")
if fq_choice == '1':
if check_kernel_and_os():
install_required_packages()
delete_old_file_then_setup_cubic("fq")
elif fq_choice == '2':
if check_kernel_and_os():
install_required_packages()
setup_without_delete_cubic("fq")
elif fq_choice == '0':
break
else:
print("Invalid choice, please try again.")
elif cubic_choice == '2':
while True:
show_fq_codel_menu()
fq_codel_choice = input("Please enter your choice: ")
if fq_codel_choice == '1':
if check_kernel_and_os():
install_required_packages()
delete_old_file_then_setup_cubic("fq_codel")
elif fq_codel_choice == '2':
if check_kernel_and_os():
install_required_packages()
setup_without_delete_cubic("fq_codel")
elif fq_codel_choice == '0':
break
else:
print("Invalid choice, please try again.")
elif cubic_choice == '3':
while True:
show_cake_menu()
cake_choice = input("Please enter your choice: ")
if cake_choice == '1':
if check_kernel_and_os_cake():
install_required_packages()
delete_old_file_then_setup_cubic("cake")
elif cake_choice == '2':
if check_kernel_and_os_cake():
install_required_packages()
setup_without_delete_cubic("cake")
elif cake_choice == '0':
break
else:
print("Invalid choice, please try again.")
elif cubic_choice == '0':
break
else:
print("Invalid choice, please try again.")
elif choice == '4':
restore()
elif choice == '5':
while True:
show_speed_test_menu()
speed_test_choice = input("Please enter your choice: ")
if speed_test_choice == '1':
run_bench_method1()
elif speed_test_choice == '2':
run_bench_method2()
elif speed_test_choice == '3':
while True:
show_iperf_menu()
iperf_choice = input("Please enter your choice: ")
if iperf_choice == '1':
run_iperf_client()
elif iperf_choice == '2':
run_iperf_server()
elif iperf_choice == '0':
break
else:
print("Invalid choice, please try again.")
elif speed_test_choice == '4':
run_speedtest_ookla()
elif speed_test_choice == '0':
break
else:
print("Invalid choice, please try again.")
elif choice == 'q':
print("Goodbye!")
break
else:
print("Invalid choice, please try again.")
except KeyboardInterrupt:
print("\nExiting...")
exit()
if __name__ == "__main__":
main()