11# examples/02_config_builder.py
22
33import time
4- import os
5- import sys
4+ from pathlib import Path
65import json
76
8- # * This ensures the script can find our local library files
9- sys .path .append (os .path .abspath (os .path .join (os .path .dirname (__file__ ), '..' )))
107
8+ from python_v2ray .downloader import BinaryDownloader
119from python_v2ray .core import XrayCore
1210from python_v2ray .config_parser import parse_uri , XrayConfigBuilder
1311
14- def run_test_with_uri (xray_path : str , uri : str ):
12+ def run_test_with_uri (vendor_dir : Path , uri : str ):
1513 """
16- * A helper function to test a single URI from start to finish.
14+ A helper function to test a single URI from start to finish.
15+ It builds a config, runs Xray, waits, and then stops.
1716 """
1817 print ("\n " + "=" * 60 )
1918 print (f"* Testing URI: { uri [:50 ]} ..." )
2019 print ("=" * 60 )
2120
21+ # Step A: Parse the URI
2222 params = parse_uri (uri )
2323 if not params :
2424 print ("! TEST FAILED: Could not parse the URI." )
2525 return
2626
2727 print (f"* PARSING SUCCESS: Protocol='{ params .protocol } ', Address='{ params .address } :{ params .port } '" )
28- # print(f"* Full Params: {params}") # note: Uncomment for deep debugging
2928
29+ # Step B: Build the configuration
3030 print ("\n * Building full Xray config..." )
3131 builder = XrayConfigBuilder ()
3232
33- # * Add a local SOCKS inbound for our apps to connect to
33+ # Add inbound with a specific tag
3434 builder .add_inbound ({
3535 "port" : 10808 , "listen" : "127.0.0.1" , "protocol" : "socks" ,
36- "settings" : {"auth" : "noauth" , "udp" : True }
36+ "settings" : {"auth" : "noauth" , "udp" : True },
37+ "tag" : "socks_in" # Add a tag to reference in routing
3738 })
3839
39- # * Build the outbound using our powerful engine
40+ # Build and add outbound
4041 outbound_dict = builder .build_outbound_from_params (params )
4142 builder .add_outbound (outbound_dict )
4243
43- # * Add default direct and block outbounds (good practice)
44- builder .add_outbound ({"protocol" : "freedom" , "tag" : "direct" })
45- builder .add_outbound ({"protocol" : "blackhole" , "tag" : "block" })
44+ # Add a routing rule to connect inbound to outbound
45+ builder .config ["routing" ]["rules" ].append ({
46+ "type" : "field" ,
47+ "inboundTag" : ["socks_in" ],
48+ "outboundTag" : outbound_dict ["tag" ]
49+ })
4650
47- print ("\n * Final JSON config generated:" )
48- print (builder .to_json ())
51+ print ("* Final JSON config generated:" )
4952
53+ print (json .dumps (builder .config , indent = 2 ))
5054
55+ # Step C: Run Xray with the generated config
5156 print ("\n * Attempting to start Xray core..." )
5257 try :
53- with XrayCore (executable_path = xray_path , config_builder = builder ) as xray :
58+ with XrayCore (vendor_dir = str ( vendor_dir ) , config_builder = builder ) as xray :
5459 if xray .is_running ():
5560 print ("\n * SUCCESS! Xray is running with this config." )
5661 print ("* Local SOCKS proxy is available on 127.0.0.1:10808" )
57- print ("* Running for 5 seconds..." )
62+ print ("* Running for 5 seconds before next test ..." )
5863 time .sleep (5 )
5964 else :
6065 print ("\n ! TEST FAILED: Xray did not start." )
@@ -66,18 +71,22 @@ def run_test_with_uri(xray_path: str, uri: str):
6671
6772def main ():
6873 """
69- * Runs a series of tests with different URI types.
74+ Runs a series of tests with a list of different URI types.
7075 """
71- project_root = os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ), '..' ))
72- xray_path = os . path . join ( project_root , "vendor" , "xray.exe" ) # ? On Linux/Mac, this would be "xray "
76+ project_root = Path ( __file__ ). parent . parent
77+ vendor_dir = project_root / "vendor "
7378
74- if not os .path .exists (xray_path ):
75- print (f"! FATAL ERROR: Xray executable not found at '{ xray_path } '" )
76- print ("! Please download it and place it in the 'vendor' folder." )
79+ print ("--- Running Python-V2Ray Batch URI Tester ---" )
80+ print (f"Vendor directory set to: { vendor_dir } " )
81+ try :
82+ downloader = BinaryDownloader (project_root )
83+ downloader .ensure_all ()
84+ except Exception as e :
85+ print (f"\n ! FATAL: { e } " )
7786 return
7887
7988 # ! =======================================================================
80- # ! === REPLACE THESE WITH YOUR OWN REAL TEST URIS ===
89+ # ! === REPLACE THESE WITH A REAL TEST URI ===
8190 # ! =======================================================================
8291 test_uris = [
8392 "vless://YOUR_UUID@your.domain.com:443?security=tls&sni=your.domain.com&fp=chrome&type=ws&path=%2F#VLESS-WS-TLS" ,
@@ -86,12 +95,11 @@ def main():
8695 "ss://YWVzLTI1Ni1nY206eW91cl9wYXNzd29yZA==@your.domain.com:8443#ShadowSocks-Test"
8796 ]
8897
98+
8999 for uri in test_uris :
90- if "YOUR_" in uri :
91- print (f"\n ! Skipping placeholder URI: { uri [:50 ]} ..." )
92- print ("! Please replace it with your own real config URI in the 'test_uris' list to test it." )
100+ if "YOUR_" in uri or "your.domain.com" in uri :
93101 continue
94- run_test_with_uri (xray_path , uri )
102+ run_test_with_uri (vendor_dir , uri )
95103
96104
97105if __name__ == "__main__" :
0 commit comments