33namespace InvisibleManXRay . Core
44{
55 using Models ;
6- using Models . Proxies ;
6+ using Handlers . Proxies ;
7+ using Handlers . Tunnels ;
78 using Values ;
89 using Utilities ;
910
@@ -14,16 +15,28 @@ public class InvisibleManXRayCore
1415 private const int TEST_PORT = 10802 ;
1516
1617 private Func < Config > getConfig ;
18+ private Func < Mode > getMode ;
19+ private Func < string > getTunIp ;
20+ private Func < string > getDns ;
1721 private Func < IProxy > getProxy ;
22+ private Func < ITunnel > getTunnel ;
1823 private Action < string > onFailLoadingConfig ;
1924
2025 public void Setup (
2126 Func < Config > getConfig ,
27+ Func < Mode > getMode ,
28+ Func < string > getTunIp ,
29+ Func < string > getDns ,
2230 Func < IProxy > getProxy ,
31+ Func < ITunnel > getTunnel ,
2332 Action < string > onFailLoadingConfig )
2433 {
2534 this . getConfig = getConfig ;
35+ this . getMode = getMode ;
36+ this . getTunIp = getTunIp ;
37+ this . getDns = getDns ;
2638 this . getProxy = getProxy ;
39+ this . getTunnel = getTunnel ;
2740 this . onFailLoadingConfig = onFailLoadingConfig ;
2841 }
2942
@@ -54,31 +67,111 @@ public Status LoadConfig(string path)
5467 return new Status ( Code . SUCCESS , SubCode . SUCCESS , file ) ;
5568 }
5669
57- public void EnableProxy ( )
70+ public Status EnableMode ( )
5871 {
59- IProxy proxy = getProxy . Invoke ( ) ;
60- proxy . Enable ( LOCAL_HOST , DEFAULT_PORT ) ;
72+ Mode mode = getMode . Invoke ( ) ;
73+
74+ if ( mode == Mode . PROXY )
75+ return EnableProxy ( ) ;
76+ else
77+ return EnableTunnel ( ) ;
6178 }
6279
63- public void DisableProxy ( )
80+ public void DisableMode ( )
6481 {
65- IProxy proxy = getProxy . Invoke ( ) ;
66- proxy . Disable ( ) ;
82+ DisableProxy ( ) ;
83+ DisableTunnel ( ) ;
6784 }
6885
6986 public void Run ( string config )
7087 {
71- XRayCoreWrapper . StartServer ( config , DEFAULT_PORT ) ;
88+ Mode mode = getMode . Invoke ( ) ;
89+ XRayCoreWrapper . StartServer ( config , DEFAULT_PORT , mode == Mode . TUN ) ;
7290 }
7391
7492 public void Stop ( )
7593 {
7694 XRayCoreWrapper . StopServer ( ) ;
7795 }
7896
97+ public void Cancel ( )
98+ {
99+ CancelProxy ( ) ;
100+ CancelTunnel ( ) ;
101+ }
102+
79103 public bool Test ( string config )
80104 {
81105 return XRayCoreWrapper . TestConnection ( config , TEST_PORT ) ;
82106 }
107+
108+ private Status EnableProxy ( )
109+ {
110+ IProxy proxy = getProxy . Invoke ( ) ;
111+
112+ return proxy . Enable (
113+ ip : LOCAL_HOST ,
114+ port : DEFAULT_PORT
115+ ) ;
116+ }
117+
118+ private void DisableProxy ( )
119+ {
120+ IProxy proxy = getProxy . Invoke ( ) ;
121+ proxy . Disable ( ) ;
122+ }
123+
124+ private Status EnableTunnel ( )
125+ {
126+ Status configStatus = LoadConfigFile ( ) ;
127+ if ( configStatus . Code == Code . ERROR )
128+ return configStatus ;
129+
130+ string server = JsonUtility . Find (
131+ key : "address" ,
132+ parent : "outbounds" ,
133+ jsonString : configStatus . Content . ToString ( )
134+ ) ;
135+ string address = getTunIp . Invoke ( ) ;
136+ string dns = getDns . Invoke ( ) ;
137+
138+ ITunnel tunnel = getTunnel . Invoke ( ) ;
139+
140+ return tunnel . Enable (
141+ ip : LOCAL_HOST ,
142+ port : DEFAULT_PORT ,
143+ address : address ,
144+ server : server ,
145+ dns : dns
146+ ) ;
147+
148+ Status LoadConfigFile ( )
149+ {
150+ Config config = getConfig . Invoke ( ) ;
151+
152+ if ( config == null )
153+ return new Status ( Code . ERROR , SubCode . NO_CONFIG , Message . NO_CONFIGS_FOUND ) ;
154+
155+ return new Status ( Code . SUCCESS , SubCode . SUCCESS , System . IO . File . ReadAllText ( config . Path ) . ToLower ( ) ) ;
156+ }
157+ }
158+
159+ private void DisableTunnel ( )
160+ {
161+ ITunnel tunnel = getTunnel . Invoke ( ) ;
162+ tunnel . Disable ( ) ;
163+ }
164+
165+ private void CancelProxy ( )
166+ {
167+ IProxy proxy = getProxy . Invoke ( ) ;
168+ proxy . Cancel ( ) ;
169+ }
170+
171+ private void CancelTunnel ( )
172+ {
173+ ITunnel tunnel = getTunnel . Invoke ( ) ;
174+ tunnel . Cancel ( ) ;
175+ }
83176 }
84177}
0 commit comments