-
Notifications
You must be signed in to change notification settings - Fork 78
/
route.m
86 lines (65 loc) · 2.43 KB
/
route.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
//
// route.m
// v2rayx_sysconf
//
// Created by tzmax on 2023/1/23.
// Copyright © 2023 Project V2Ray. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "route.h"
@implementation SYSRouteHelper : NSObject
-(void) upInterface:(NSString*) interfaceName {
if (interfaceName == NULL) {
return;
}
NSString* cmd = [[NSString alloc] initWithFormat: @"/sbin/ifconfig %@ up", interfaceName];
runCommandScript(cmd);
}
-(void) routeAdd:(NSString*) rule gateway:(NSString*) gateway {
if (rule == NULL || gateway == NULL) {
return;
}
NSString* cmd = [[NSString alloc] initWithFormat: @"/sbin/route add -net %@ %@", rule, gateway];
runCommandScript(cmd);
}
-(void) routeDelete:(NSString*) rule gateway:(NSString*) gateway {
if (rule == NULL || gateway == NULL) {
return;
}
NSString* cmd = [[NSString alloc] initWithFormat: @"/sbin/route delete -net %@ %@", rule, gateway];
runCommandScript(cmd);
}
-(NSString*) getRouteGateway:(NSString*) rule {
if (rule == NULL) {
rule = @"default";
}
NSString* cmd = [[NSString alloc] initWithFormat: @"/sbin/route -n get %@ | /usr/bin/grep 'gateway' | /usr/bin/awk '{print $2}'", rule];
NSString* outStr = [runCommandScript(cmd) stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
return outStr;
}
-(NSString*) getDefaultRouteGateway {
return [self getRouteGateway:@"default"];
}
NSString* runCommandScript(NSString* script)
{
NSString *scriptSource = [NSString stringWithFormat:@"do shell script \"%@\" with administrator privileges", script];
NSAppleScript *appleScript = [[NSAppleScript new] initWithSource:scriptSource];
NSAppleEventDescriptor *eventDescriptor = nil;
NSBundle *bunlde = [NSBundle mainBundle];
eventDescriptor = [appleScript executeAndReturnError:nil];
if (eventDescriptor)
{
return [eventDescriptor stringValue];
}
return @"";
// NSLog(@"runCommandScript: %@", script);
// NSPipe* pipe = [NSPipe pipe];
// NSTask* task = [[NSTask alloc] init];
// [task setLaunchPath: @"/bin/sh"];
// [task setArguments:@[@"-c",[NSString stringWithFormat:@"%@", script]]];
// [task setStandardOutput:pipe];
// NSFileHandle* file = [pipe fileHandleForReading];
// [task launch];
// return [[NSString alloc] initWithData:[file readDataToEndOfFile] encoding:NSUTF8StringEncoding];
}
@end