1+ # examples/02_config_builder.py
2+
3+ import time
4+ import os
5+ import sys
6+ import json
7+
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__ ), '..' )))
10+
11+ from python_v2ray .core import XrayCore
12+ from python_v2ray .config_parser import parse_uri , XrayConfigBuilder
13+
14+ def run_test_with_uri (xray_path : str , uri : str ):
15+ """
16+ * A helper function to test a single URI from start to finish.
17+ """
18+ print ("\n " + "=" * 60 )
19+ print (f"* Testing URI: { uri [:50 ]} ..." )
20+ print ("=" * 60 )
21+
22+ params = parse_uri (uri )
23+ if not params :
24+ print ("! TEST FAILED: Could not parse the URI." )
25+ return
26+
27+ print (f"* PARSING SUCCESS: Protocol='{ params .protocol } ', Address='{ params .address } :{ params .port } '" )
28+ # print(f"* Full Params: {params}") # note: Uncomment for deep debugging
29+
30+ print ("\n * Building full Xray config..." )
31+ builder = XrayConfigBuilder ()
32+
33+ # * Add a local SOCKS inbound for our apps to connect to
34+ builder .add_inbound ({
35+ "port" : 10808 , "listen" : "127.0.0.1" , "protocol" : "socks" ,
36+ "settings" : {"auth" : "noauth" , "udp" : True }
37+ })
38+
39+ # * Build the outbound using our powerful engine
40+ outbound_dict = builder .build_outbound_from_params (params )
41+ builder .add_outbound (outbound_dict )
42+
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" })
46+
47+ print ("\n * Final JSON config generated:" )
48+ print (builder .to_json ())
49+
50+
51+ print ("\n * Attempting to start Xray core..." )
52+ try :
53+ with XrayCore (executable_path = xray_path , config_builder = builder ) as xray :
54+ if xray .is_running ():
55+ print ("\n * SUCCESS! Xray is running with this config." )
56+ print ("* Local SOCKS proxy is available on 127.0.0.1:10808" )
57+ print ("* Running for 5 seconds..." )
58+ time .sleep (5 )
59+ else :
60+ print ("\n ! TEST FAILED: Xray did not start." )
61+ except Exception as e :
62+ print (f"\n ! TEST FAILED: An error occurred during Xray execution: { e } " )
63+
64+ print (f"* Test finished for URI: { uri [:50 ]} ..." )
65+
66+
67+ def main ():
68+ """
69+ * Runs a series of tests with different URI types.
70+ """
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"
73+
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." )
77+ return
78+
79+ # ! =======================================================================
80+ # ! === REPLACE THESE WITH YOUR OWN REAL TEST URIS ===
81+ # ! =======================================================================
82+ test_uris = [
83+ "vless://YOUR_UUID@your.domain.com:443?security=tls&sni=your.domain.com&fp=chrome&type=ws&path=%2F#VLESS-WS-TLS" ,
84+ "vmess://ewogICJ2IjogIjIiLAogICJwcyI6ICJWbWVzcy1URU5UIEtleSIsCiAgImFkZCI6ICJzb21lLmRvbWFpbi5jb20iLAogICJwb3J0IjogIjgwODAiLAogICJpZCI6ICJZV1JzTFRrM1pHRXRaV1UwTnkxa05EVm1MVGhsWm1NdFkyVTVNRFJsWWpkaE5XRmpZdyIsCiAgImFpZCI6ICIwIiwKICAic2N5IjogImF1dG8iLAogICJuZXQiOiAidGNwIiwKICAidHlwZSI6ICJub25lIiwKICAiaG9zdCI6ICIiLAogICJwYXRoIjogIi8iLAogICJ0bHMiOiAiIiwKICAic25pIjogIiIKfQ==" ,
85+ "trojan://YOUR_PASSWORD@your.domain.com:443?sni=your.domain.com#Trojan-Test" ,
86+ "ss://YWVzLTI1Ni1nY206eW91cl9wYXNzd29yZA==@your.domain.com:8443#ShadowSocks-Test"
87+ ]
88+
89+ 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." )
93+ continue
94+ run_test_with_uri (xray_path , uri )
95+
96+
97+ if __name__ == "__main__" :
98+ main ()
0 commit comments