|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +V2Root Configuration Manager Example |
| 4 | +
|
| 5 | +This example demonstrates how to: |
| 6 | +1. Save and load V2Ray configurations |
| 7 | +2. Test multiple configurations |
| 8 | +3. Select the best performing configuration |
| 9 | +4. Apply and start a configuration |
| 10 | +""" |
| 11 | + |
| 12 | +import os |
| 13 | +import sys |
| 14 | +import time |
| 15 | +from colorama import init, Fore, Style |
| 16 | + |
| 17 | +# Add the parent directory to sys.path to import v2root |
| 18 | +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 19 | +from v2root import V2ROOT |
| 20 | + |
| 21 | +init(autoreset=True) |
| 22 | + |
| 23 | +def main(): |
| 24 | + # Initialize V2ROOT with custom ports |
| 25 | + v2root = V2ROOT(http_port=2300, socks_port=2301) |
| 26 | + |
| 27 | + print(f"{Fore.CYAN}V2Root Configuration Manager Example{Style.RESET_ALL}") |
| 28 | + print(f"{Fore.CYAN}===================================={Style.RESET_ALL}") |
| 29 | + |
| 30 | + while True: |
| 31 | + print("\nOptions:") |
| 32 | + print("1. Save a new configuration") |
| 33 | + print("2. List saved configurations") |
| 34 | + print("3. Load and test a configuration") |
| 35 | + print("4. Batch test multiple configurations") |
| 36 | + print("5. Reset network proxy") |
| 37 | + print("6. Exit") |
| 38 | + |
| 39 | + choice = input("\nEnter your choice (1-6): ") |
| 40 | + |
| 41 | + if choice == '1': |
| 42 | + # Save a new configuration |
| 43 | + config_str = input("Enter the V2Ray configuration string: ") |
| 44 | + name = input("Enter a name for this configuration (or press Enter for auto-name): ") |
| 45 | + name = name if name.strip() else None |
| 46 | + |
| 47 | + try: |
| 48 | + file_path = v2root.save_config(config_str, name) |
| 49 | + print(f"{Fore.GREEN}Configuration saved to {file_path}{Style.RESET_ALL}") |
| 50 | + except Exception as e: |
| 51 | + print(f"{Fore.RED}Error: {str(e)}{Style.RESET_ALL}") |
| 52 | + |
| 53 | + elif choice == '2': |
| 54 | + # List saved configurations |
| 55 | + configs = v2root.list_saved_configs() |
| 56 | + |
| 57 | + elif choice == '3': |
| 58 | + # Load and test a configuration |
| 59 | + configs = v2root.list_saved_configs() |
| 60 | + if not configs: |
| 61 | + continue |
| 62 | + |
| 63 | + index = input("Enter the number of the configuration to load: ") |
| 64 | + try: |
| 65 | + index = int(index) - 1 |
| 66 | + if 0 <= index < len(configs): |
| 67 | + config = v2root.load_saved_config(configs[index]) |
| 68 | + |
| 69 | + print(f"{Fore.CYAN}Testing configuration...{Style.RESET_ALL}") |
| 70 | + try: |
| 71 | + latency = v2root.test_connection(config) |
| 72 | + print(f"{Fore.GREEN}Test successful! Latency: {latency}ms{Style.RESET_ALL}") |
| 73 | + |
| 74 | + start = input("Do you want to start V2Ray with this configuration? (y/n): ") |
| 75 | + if start.lower() == 'y': |
| 76 | + v2root.set_config_string(config) |
| 77 | + v2root.start() |
| 78 | + except Exception as e: |
| 79 | + print(f"{Fore.RED}Test failed: {str(e)}{Style.RESET_ALL}") |
| 80 | + else: |
| 81 | + print(f"{Fore.RED}Invalid selection{Style.RESET_ALL}") |
| 82 | + except ValueError: |
| 83 | + print(f"{Fore.RED}Please enter a valid number{Style.RESET_ALL}") |
| 84 | + |
| 85 | + elif choice == '4': |
| 86 | + # Batch test multiple configurations |
| 87 | + configs = v2root.list_saved_configs() |
| 88 | + if not configs: |
| 89 | + continue |
| 90 | + |
| 91 | + config_list = [] |
| 92 | + for config_name in configs: |
| 93 | + try: |
| 94 | + config = v2root.load_saved_config(config_name) |
| 95 | + config_list.append(config) |
| 96 | + except Exception as e: |
| 97 | + print(f"{Fore.RED}Error loading {config_name}: {str(e)}{Style.RESET_ALL}") |
| 98 | + |
| 99 | + if not config_list: |
| 100 | + print(f"{Fore.RED}No valid configurations to test{Style.RESET_ALL}") |
| 101 | + continue |
| 102 | + |
| 103 | + print(f"{Fore.CYAN}Testing {len(config_list)} configurations...{Style.RESET_ALL}") |
| 104 | + timeout = input("Enter timeout in seconds (default: 10): ") |
| 105 | + timeout = int(timeout) if timeout.strip() and timeout.isdigit() else 10 |
| 106 | + |
| 107 | + parallel = input("Test in parallel? (slower machines should use 'n') (y/n): ") |
| 108 | + parallel = parallel.lower() == 'y' |
| 109 | + |
| 110 | + start_time = time.time() |
| 111 | + results = v2root.batch_test(config_list, timeout, parallel) |
| 112 | + elapsed = time.time() - start_time |
| 113 | + |
| 114 | + print(f"{Fore.CYAN}Testing completed in {elapsed:.2f} seconds{Style.RESET_ALL}") |
| 115 | + |
| 116 | + if results: |
| 117 | + print(f"{Fore.GREEN}Top 3 configurations by latency:{Style.RESET_ALL}") |
| 118 | + for i, (config, latency) in enumerate(results[:3], 1): |
| 119 | + print(f"{i}. Latency: {latency}ms - Config: {config[:50]}...") |
| 120 | + |
| 121 | + use_best = input("Do you want to use the best configuration? (y/n): ") |
| 122 | + if use_best.lower() == 'y': |
| 123 | + best_config = results[0][0] |
| 124 | + v2root.set_config_string(best_config) |
| 125 | + v2root.start() |
| 126 | + else: |
| 127 | + print(f"{Fore.RED}No working configurations found{Style.RESET_ALL}") |
| 128 | + |
| 129 | + elif choice == '5': |
| 130 | + # Reset network proxy |
| 131 | + try: |
| 132 | + v2root.reset_network_proxy() |
| 133 | + except Exception as e: |
| 134 | + print(f"{Fore.RED}Error: {str(e)}{Style.RESET_ALL}") |
| 135 | + |
| 136 | + elif choice == '6': |
| 137 | + # Exit |
| 138 | + try: |
| 139 | + v2root.reset_network_proxy() |
| 140 | + except: |
| 141 | + pass |
| 142 | + print(f"{Fore.CYAN}Exiting...{Style.RESET_ALL}") |
| 143 | + break |
| 144 | + |
| 145 | + else: |
| 146 | + print(f"{Fore.RED}Invalid choice. Please enter a number between 1 and 6.{Style.RESET_ALL}") |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + try: |
| 150 | + main() |
| 151 | + except KeyboardInterrupt: |
| 152 | + print(f"{Fore.CYAN}\nExiting...{Style.RESET_ALL}") |
| 153 | + try: |
| 154 | + v2root = V2ROOT() |
| 155 | + v2root.reset_network_proxy() |
| 156 | + except: |
| 157 | + pass |
| 158 | + sys.exit(0) |
0 commit comments