-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
170 lines (146 loc) · 4.69 KB
/
main.c
File metadata and controls
170 lines (146 loc) · 4.69 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#import <Foundation/NSArray.h>
#import <Foundation/Foundation.h>
#import <SystemConfiguration/SCPreferences.h>
#import <SystemConfiguration/SCNetworkConfiguration.h>
#include <sys/syslimits.h>
#include <sys/stat.h>
#include <mach-o/dyld.h>
#include "common.h"
void usage(const char* binName)
{
printf("Usage: %s <on | off> <ip> <port>\n", binName);
exit(INVALID_FORMAT);
}
int main(int argc, char* argv[]) {
if (argc < 2) {
usage(argv[0]);
}
#ifdef DARWIN
if (strcmp(argv[1], "setuid") == 0) {
return setUid();
}
#endif
if (strcmp(argv[1], "on") == 0) {
if (argc < 4) {
usage(argv[0]);
}
return togglePac(true, argv[2] ,atoi(argv[3]));
} else if (strcmp(argv[1], "off") == 0) {
return togglePac(false, "", 0);
} else {
usage(argv[0]);
}
// code never reaches here, just stops compiler from complain
return RET_NO_ERROR;
}
int setUid()
{
char exeFullPath [PATH_MAX];
uint32_t size = PATH_MAX;
if (_NSGetExecutablePath(exeFullPath, &size) != 0)
{
printf("Path longer than %d, should not occur!!!!!", size);
return SYSCALL_FAILED;
}
if (chown(exeFullPath, 0, 0) != 0) // root:wheel
{
puts("Error chown");
return NO_PERMISSION;
}
if (chmod(exeFullPath, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH | S_ISUID) != 0)
{
puts("Error chmod");
return NO_PERMISSION;
}
return RET_NO_ERROR;
}
int togglePac(bool turnOn, const char* SocksAddr,const int SocksPort)
{
NSString* nsSocksAddr = [[NSString alloc] initWithCString: SocksAddr encoding:NSUTF8StringEncoding];
int ret = RET_NO_ERROR;
Boolean success;
SCNetworkSetRef networkSetRef;
CFArrayRef networkServicesArrayRef;
SCNetworkServiceRef networkServiceRef;
SCNetworkProtocolRef proxyProtocolRef;
NSDictionary *oldPreferences;
NSMutableDictionary *newPreferences;
NSString *wantedHost;
// Get System Preferences Lock
SCPreferencesRef prefsRef = SCPreferencesCreate(NULL, CFSTR("org.getlantern.lantern"), NULL);
if(prefsRef==NULL) {
NSLog(@"Fail to obtain Preferences Ref");
ret = NO_PERMISSION;
goto freePrefsRef;
}
success = SCPreferencesLock(prefsRef, true);
if (!success) {
NSLog(@"Fail to obtain PreferencesLock");
ret = NO_PERMISSION;
goto freePrefsRef;
}
// Get available network services
networkSetRef = SCNetworkSetCopyCurrent(prefsRef);
if(networkSetRef == NULL) {
NSLog(@"Fail to get available network services");
ret = SYSCALL_FAILED;
goto freeNetworkSetRef;
}
//Look up interface entry
networkServicesArrayRef = SCNetworkSetCopyServices(networkSetRef);
networkServiceRef = NULL;
for (long i = 0; i < CFArrayGetCount(networkServicesArrayRef); i++) {
networkServiceRef = CFArrayGetValueAtIndex(networkServicesArrayRef, i);
// Get proxy protocol
proxyProtocolRef = SCNetworkServiceCopyProtocol(networkServiceRef, kSCNetworkProtocolTypeProxies);
if(proxyProtocolRef == NULL) {
NSLog(@"Couldn't acquire copy of proxyProtocol");
ret = SYSCALL_FAILED;
goto freeProxyProtocolRef;
}
oldPreferences = (__bridge NSDictionary*)SCNetworkProtocolGetConfiguration(proxyProtocolRef);
newPreferences = [NSMutableDictionary dictionaryWithDictionary: oldPreferences];
wantedHost = @"localhost";
if(turnOn == true) {
//[newPreferences setValue: wantedHost forKey:(NSString*)kSCPropNetProxiesHTTPProxy];
[newPreferences setValue:[NSNumber numberWithInt:1] forKey:(NSString*)kSCPropNetProxiesSOCKSEnable];
[newPreferences setValue:nsSocksAddr forKey:(NSString*)kSCPropNetProxiesSOCKSProxy];
[newPreferences setValue:[NSNumber numberWithInt:SocksPort] forKey:(NSString*)kSCPropNetProxiesSOCKSPort];
} else {
[newPreferences setValue:[NSNumber numberWithInt:0] forKey:(NSString*)kSCPropNetProxiesSOCKSEnable];
}
success = SCNetworkProtocolSetConfiguration(proxyProtocolRef, (__bridge CFDictionaryRef)newPreferences);
if(!success) {
NSLog(@"Failed to set Protocol Configuration");
ret = SYSCALL_FAILED;
goto freeProxyProtocolRef;
}
freeProxyProtocolRef:
CFRelease(proxyProtocolRef);
}
success = SCPreferencesCommitChanges(prefsRef);
if(!success) {
NSLog(@"Failed to Commit Changes");
ret = SYSCALL_FAILED;
goto freeNetworkServicesArrayRef;
}
success = SCPreferencesApplyChanges(prefsRef);
if(!success) {
NSLog(@"Failed to Apply Changes");
ret = SYSCALL_FAILED;
goto freeNetworkServicesArrayRef;
}
success = true;
//Free Resources
freeNetworkServicesArrayRef:
CFRelease(networkServicesArrayRef);
freeNetworkSetRef:
CFRelease(networkSetRef);
freePrefsRef:
SCPreferencesUnlock(prefsRef);
CFRelease(prefsRef);
return ret;
}