-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathmain.m
296 lines (246 loc) · 13.4 KB
/
main.m
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
//
// main.m
// v2rayx_sysconf
//
// Copyright © 2016年 Cenmrev. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <sys/signal.h>
#import <Tun2socks/Tun2socks.h>
#import "sysconf_version.h"
#import "tun.h"
#import "route.h"
#define INFO "v2rayx_sysconf\n the helper tool for V2RayX, modified from clowwindy's shadowsocks_sysconf.\nusage: v2rayx_sysconf [options]\noff\t turn off proxy\nauto\t auto proxy change\nglobal port \t global proxy at the specified port number\n"
//@interface AppDelegate : NSObject<NSXPCListenerDelegate> {}
//@end
//
//@implementation AppDelegate
//-(BOOL) listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection {
// if (newConnection != NULL) {
// [newConnection resume];
// }
//
// return true;
//}
//
//@end
BOOL runLoopMark = YES;
NSString* defaultRouteGateway;
SYSRouteHelper* routeHelper;
NSString* tunAddr = @"10.0.0.0";
NSString* tunWg = @"10.0.0.1";
NSString* tunMask = @"255.255.255.0";
NSString* tunDns = @"8.8.8.8,8.8.4.4,1.1.1.1";
NSString* proxyServer;
NSString* exampleServer = @"93.184.216.34"; // example.com ip use to identify records
NSString *runtimeMode = @"";
// helper perform the cleanup task.
void cleanupHandle(int signal_ns) {
// printf("app kill signal %d\n", signal_ns);
if ([runtimeMode isEqualToString: @"tun"]) {
// Restore the default route
if (![defaultRouteGateway isEqualToString:@""] && routeHelper != NULL) {
[routeHelper routeDelete:@"default" gateway:tunWg];
[routeHelper routeAdd:@"default" gateway:defaultRouteGateway];
[routeHelper routeDelete:proxyServer gateway:defaultRouteGateway];
printf("reset DefaultRouteGateway %s\n", [defaultRouteGateway UTF8String]);
}
}
runLoopMark = NO;
}
int main(int argc, const char * argv[])
{
// prepare for XPC communication
// AppDelegate* appDelegate = [AppDelegate init];
// NSXPCListener* xpcListener = [NSXPCListener serviceListener];
// xpcListener.delegate = appDelegate;
// Initialize the routing controller
routeHelper = [[SYSRouteHelper alloc] init];
// app kill signal
signal(SIGKILL, cleanupHandle);
signal(SIGABRT, cleanupHandle);
signal(SIGINT, cleanupHandle);
if (argc < 2 || argc >4) {
printf(INFO);
return 1;
}
@autoreleasepool {
NSString *mode = [NSString stringWithUTF8String:argv[1]];
NSSet *support_args = [NSSet setWithObjects:@"off", @"auto", @"global", @"save", @"restore", @"tun", @"-v", nil];
if (![support_args containsObject:mode]) {
printf(INFO);
return 1;
}
runtimeMode = mode;
if ([mode isEqualToString:@"-v"]) {
printf("%s", [VERSION UTF8String]);
return 0;
}
if ([mode isEqualToString:@"tun"]) {
proxyServer = exampleServer; // Just avoid empty abnormalities
if (argv[2] != NULL) {
NSString* server = [NSString stringWithUTF8String:argv[2]];
if (server != NULL) {
proxyServer = server;
}
}
int localProxyPort = 0;
if (sscanf (argv[3], "%i", &localProxyPort) != 1 || localProxyPort > 65535 || localProxyPort < 0) {
printf("error - not a valid port number\n");
return 0;
}
NSString* socks5ProxyLink = [[NSString alloc] initWithFormat: @"socks5://127.0.0.1:%i", localProxyPort];
// The native way of creating TUN is deprecated
// int fd = createTUN();
// printf("tun fd is %d\n", fd);
NSError* err;
Tun2socksTun2socksCtl* ctl = Tun2socksCreateTunConnect(tunAddr, tunWg, tunMask, tunDns, socks5ProxyLink, true, &err);
if (err != NULL) {
NSLog(@"Tun2socksConnect error: %@\n", err);
return 0;
}
if (ctl != NULL && ctl.tunName != NULL) {
// NSLog(@"tun fd is %@\n", ctl.tunName);
// Process route
NSString* systemRouteBackupFilePath = [NSString stringWithFormat:@"%@/Library/Application Support/V2RayXS/system_route_backup.plist", NSHomeDirectory()];
NSMutableDictionary* systemRouteBackup = [NSMutableDictionary dictionaryWithContentsOfURL:[NSURL fileURLWithPath: systemRouteBackupFilePath]];
NSString* DEFAULT_ROUTE_GATEWAY = @"DefaultRouteGateway";
if (systemRouteBackup == NULL) {
systemRouteBackup = [[NSMutableDictionary alloc] init];
}
if (![systemRouteBackup objectForKey:DEFAULT_ROUTE_GATEWAY]) {
[systemRouteBackup setValue:@"" forKey:DEFAULT_ROUTE_GATEWAY];
}
NSString* defGateway = [routeHelper getDefaultRouteGateway];
NSString* fixGateway = systemRouteBackup[DEFAULT_ROUTE_GATEWAY];
// NSString* fixGateway = [routeHelper getRouteGateway: exampleServer];
// printf("defGateway %s\n", [defGateway UTF8String]);
// printf("fixGateway %s\n", [fixGateway UTF8String]);
if ([defGateway isEqualToString:@""] && ![fixGateway isEqualToString:@""]) {
defGateway = fixGateway;
}
if(![defGateway isEqualToString:@""] && ([fixGateway isEqualToString:@""] || ![defGateway isEqualToString:fixGateway])) {
fixGateway = defGateway;
[systemRouteBackup setValue:fixGateway forKey:DEFAULT_ROUTE_GATEWAY];
[systemRouteBackup writeToURL:[NSURL fileURLWithPath: systemRouteBackupFilePath] atomically:NO];
}
// if(![defGateway isEqualToString:@""] && [fixGateway isEqualToString:@""]) {
// printf("add fix defGateway %s\n", [defGateway UTF8String]);
// [routeHelper routeAdd:exampleServer gateway:defGateway];
// }
// if (![defGateway isEqualToString:@""] && ![defGateway isEqualToString:fixGateway]) {
// [routeHelper routeDelete:exampleServer gateway:defGateway];
// [routeHelper routeAdd:exampleServer gateway:defGateway];
// }
defaultRouteGateway = defGateway;
[routeHelper upInterface: ctl.tunName]; // up tun Interface
if (![defaultRouteGateway isEqualToString:@""]) {
[routeHelper routeDelete:@"default" gateway:defaultRouteGateway];
[routeHelper routeAdd:proxyServer gateway:defaultRouteGateway];
}
[routeHelper routeAdd:@"default" gateway:tunWg];
}
// run loop
CFRunLoopSourceContext context = {0};
CFRunLoopSourceRef source = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context);
CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
CFRelease(source);
while (runLoopMark) {
SInt32 result = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0e10, true);
if (result == kCFRunLoopRunFinished) {
runLoopMark = NO;
}
}
cleanupHandle(SIGABRT);
return 0;
}
static AuthorizationRef authRef;
static AuthorizationFlags authFlags;
authFlags = kAuthorizationFlagDefaults
| kAuthorizationFlagExtendRights
| kAuthorizationFlagInteractionAllowed
| kAuthorizationFlagPreAuthorize;
OSStatus authErr = AuthorizationCreate(nil, kAuthorizationEmptyEnvironment, authFlags, &authRef);
if (authErr != noErr) {
authRef = nil;
} else {
if (authRef == NULL) {
NSLog(@"No authorization has been granted to modify network configuration");
return 1;
}
SCPreferencesRef prefRef = SCPreferencesCreateWithAuthorization(nil, CFSTR("V2RayXS"), nil, authRef);
NSDictionary *sets = (__bridge NSDictionary *)SCPreferencesGetValue(prefRef, kSCPrefNetworkServices);
NSDictionary* originalSets;
if ([mode isEqualToString:@"save"]) {
[sets writeToURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Library/Application Support/V2RayXS/system_proxy_backup.plist",NSHomeDirectory()]] atomically:NO];
return 0;
}
// 遍历系统中的网络设备列表,设置 AirPort 和 Ethernet 的代理
if([mode isEqualToString:@"restore"]) {
originalSets = [NSDictionary dictionaryWithContentsOfURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/Library/Application Support/V2RayXS/system_proxy_backup.plist",NSHomeDirectory()]]];
}
for (NSString *key in [sets allKeys]) {
NSMutableDictionary *dict = [sets objectForKey:key];
NSString *hardware = [dict valueForKeyPath:@"Interface.Hardware"];
// NSLog(@"%@", hardware);
if ([hardware isEqualToString:@"AirPort"] || [hardware isEqualToString:@"Wi-Fi"] || [hardware isEqualToString:@"Ethernet"]) {
NSMutableDictionary *proxies = [sets[key][@"Proxies"] mutableCopy];
[proxies setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCFNetworkProxiesHTTPEnable];
[proxies setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCFNetworkProxiesHTTPSEnable];
[proxies setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCFNetworkProxiesProxyAutoConfigEnable];
[proxies setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCFNetworkProxiesSOCKSEnable];
if ([mode isEqualToString:@"restore"]) {
if ([originalSets objectForKey:key]){
proxies = originalSets[key][@"Proxies"];
}
}
if ([mode isEqualToString:@"auto"]) {
[proxies setObject:@"http://127.0.0.1:8070/proxy.pac" forKey:(NSString *)kCFNetworkProxiesProxyAutoConfigURLString];
[proxies setObject:[NSNumber numberWithInt:1] forKey:(NSString *)kCFNetworkProxiesProxyAutoConfigEnable];
} else if ([mode isEqualToString:@"global"]) {
int localPort = 0;
int httpPort = 0;
if (sscanf (argv[2], "%i", &localPort)!=1 || localPort > 65535 || localPort < 0) {
printf ("error - not a valid port number");
return 1;
}
if (sscanf (argv[3], "%i", &httpPort)!=1 || httpPort > 65535 || httpPort < 0) {
printf ("error - not a valid port number");
return 1;
}
NSLog(@"in helper %d %d", localPort, httpPort);
if (localPort > 0) {
[proxies setObject:@"127.0.0.1" forKey:(NSString *)
kCFNetworkProxiesSOCKSProxy];
[proxies setObject:[NSNumber numberWithInt:localPort] forKey:(NSString*)
kCFNetworkProxiesSOCKSPort];
[proxies setObject:[NSNumber numberWithInt:1] forKey:(NSString*)
kCFNetworkProxiesSOCKSEnable];
}
if (httpPort > 0) {
[proxies setObject:@"127.0.0.1" forKey:(NSString *)
kCFNetworkProxiesHTTPProxy];
[proxies setObject:@"127.0.0.1" forKey:(NSString *)
kCFNetworkProxiesHTTPSProxy];
[proxies setObject:[NSNumber numberWithInt:httpPort] forKey:(NSString*)
kCFNetworkProxiesHTTPPort];
[proxies setObject:[NSNumber numberWithInt:httpPort] forKey:(NSString*)
kCFNetworkProxiesHTTPSPort];
[proxies setObject:[NSNumber numberWithInt:1] forKey:(NSString*)
kCFNetworkProxiesHTTPEnable];
[proxies setObject:[NSNumber numberWithInt:1] forKey:(NSString*)
kCFNetworkProxiesHTTPSEnable];
}
}
SCPreferencesPathSetValue(prefRef, (__bridge CFStringRef)[NSString stringWithFormat:@"/%@/%@/%@", kSCPrefNetworkServices, key, kSCEntNetProxies], (__bridge CFDictionaryRef)proxies);
}
}
SCPreferencesCommitChanges(prefRef);
SCPreferencesApplyChanges(prefRef);
SCPreferencesSynchronize(prefRef);
}
printf("proxy set to %s\n", [mode UTF8String]);
}
return 0;
}