-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenableProxy_darwin.go
263 lines (215 loc) · 8.81 KB
/
enableProxy_darwin.go
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
//go:build darwin
package main
/*
#cgo CFLAGS: -x objective-c -fmodules
#cgo LDFLAGS: -framework Foundation -framework SystemConfiguration
#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>
enum RET_ERRORS {
RET_NO_ERROR = 0,
INVALID_FORMAT = 1,
NO_PERMISSION = 2,
SYSCALL_FAILED = 3,
NO_MEMORY = 4
};
typedef Boolean (*visitor) (SCNetworkProtocolRef proxyProtocolRef, NSDictionary* oldPreferences, NSDictionary* args);
Boolean showAction(SCNetworkProtocolRef proxyProtocolRef, NSDictionary* oldPreferences, NSDictionary* args)
{
NSNumber* on = [oldPreferences valueForKey:(NSString*)kSCPropNetProxiesHTTPEnable];
NSString* nsOldProxyHost = [oldPreferences valueForKey:(NSString*)kSCPropNetProxiesHTTPProxy];
NSNumber* nsOldProxyPort = [oldPreferences valueForKey:(NSString*)kSCPropNetProxiesHTTPPort];
if ([on intValue] == 1) {
printf("%s:%d\n", [nsOldProxyHost UTF8String], [nsOldProxyPort intValue]);
}
return TRUE;
}
Boolean turnOnAction(SCNetworkProtocolRef proxyProtocolRef, NSDictionary* oldPreferences, NSDictionary* args) {
NSString* nsProxyHost = [args objectForKey:@"host"];
NSNumber* nsProxyPort = [args objectForKey:@"port"];
NSMutableDictionary *newPreferences = [NSMutableDictionary dictionaryWithDictionary: oldPreferences];
Boolean success;
[newPreferences setValue: nsProxyHost forKey:(NSString*)kSCPropNetProxiesHTTPProxy];
[newPreferences setValue: nsProxyHost forKey:(NSString*)kSCPropNetProxiesHTTPSProxy];
[newPreferences setValue: nsProxyPort forKey:(NSString*)kSCPropNetProxiesHTTPPort];
[newPreferences setValue: nsProxyPort forKey:(NSString*)kSCPropNetProxiesHTTPSPort];
[newPreferences setValue:[NSNumber numberWithInt:1] forKey:(NSString*)kSCPropNetProxiesHTTPEnable];
[newPreferences setValue:[NSNumber numberWithInt:1] forKey:(NSString*)kSCPropNetProxiesHTTPSEnable];
success = SCNetworkProtocolSetConfiguration(proxyProtocolRef, (__bridge CFDictionaryRef)newPreferences);
if(!success) {
NSLog(@"Failed to set Protocol Configuration");
}
return success;
}
Boolean turnOffAction(SCNetworkProtocolRef proxyProtocolRef, NSDictionary* oldPreferences, NSDictionary* args) {
NSMutableDictionary *newPreferences = [NSMutableDictionary dictionaryWithDictionary: oldPreferences];
Boolean success;
[newPreferences setValue:[NSNumber numberWithInt:0] forKey:(NSString*)kSCPropNetProxiesHTTPEnable];
[newPreferences setValue: @"" forKey:(NSString*)kSCPropNetProxiesHTTPProxy];
[newPreferences setValue: @"" forKey:(NSString*)kSCPropNetProxiesHTTPPort];
[newPreferences setValue:[NSNumber numberWithInt:0] forKey:(NSString*)kSCPropNetProxiesHTTPSEnable];
[newPreferences setValue: @"" forKey:(NSString*)kSCPropNetProxiesHTTPSProxy];
[newPreferences setValue: @"" forKey:(NSString*)kSCPropNetProxiesHTTPSPort];
success = SCNetworkProtocolSetConfiguration(proxyProtocolRef, (__bridge CFDictionaryRef)newPreferences);
if(!success) {
NSLog(@"Failed to set Protocol Configuration");
}
return success;
}
NSDictionary* visit(visitor v, bool persist, NSDictionary* args)
{
NSMutableDictionary *ret = [NSMutableDictionary new];
Boolean success;
SCNetworkSetRef networkSetRef;
CFArrayRef networkServicesArrayRef;
SCNetworkServiceRef networkServiceRef;
SCNetworkProtocolRef proxyProtocolRef;
NSDictionary *oldPreferences;
AuthorizationRef authRef;
AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagInteractionAllowed, &authRef);
if (authRef == nil) {
[ret setObject:@"Fail to obtain Authorization Ref" forKey:@"error"];
[ret setObject:[[NSNumber alloc] initWithInt:NO_PERMISSION] forKey:@"code"];
return ret;
}
// Get System Preferences Lock
SCPreferencesRef prefsRef = SCPreferencesCreateWithAuthorization(NULL, CFSTR("org.netmiddler.proxy"), NULL, authRef);
if (prefsRef == NULL) {
[ret setObject:@"Fail to obtain Preferences Ref" forKey:@"error"];
[ret setObject:[[NSNumber alloc] initWithInt:NO_PERMISSION] forKey:@"code"];
goto freePrefsRef;
}
success = SCPreferencesLock(prefsRef, true);
if (!success) {
[ret setObject:@"Fail to obtain PreferencesLock" forKey:@"error"];
[ret setObject:[[NSNumber alloc] initWithInt:NO_PERMISSION] forKey:@"code"];
goto freePrefsRef;
}
// Get available network services
networkSetRef = SCNetworkSetCopyCurrent(prefsRef);
if(networkSetRef == NULL) {
[ret setObject:@"Fail to get available network services" forKey:@"error"];
[ret setObject:[[NSNumber alloc] initWithInt:SYSCALL_FAILED] forKey:@"code"];
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) {
[ret setObject:@"Couldn't acquire copy of proxyProtocol" forKey:@"error"];
[ret setObject:[[NSNumber alloc] initWithInt:SYSCALL_FAILED] forKey:@"code"];
goto freeProxyProtocolRef;
}
oldPreferences = (__bridge NSDictionary*)SCNetworkProtocolGetConfiguration(proxyProtocolRef);
if (!v(proxyProtocolRef, oldPreferences, args)) {
[ret setObject:[[NSNumber alloc] initWithInt:SYSCALL_FAILED] forKey:@"code"];
}
freeProxyProtocolRef:
CFRelease(proxyProtocolRef);
}
if (persist) {
success = SCPreferencesCommitChanges(prefsRef);
if(!success) {
[ret setObject:@"Failed to Commit Changes" forKey:@"error"];
[ret setObject:[[NSNumber alloc] initWithInt:SYSCALL_FAILED] forKey:@"code"];
goto freeNetworkServicesArrayRef;
}
success = SCPreferencesApplyChanges(prefsRef);
if(!success) {
[ret setObject:@"Failed to Apply Changes" forKey:@"error"];
[ret setObject:[[NSNumber alloc] initWithInt:SYSCALL_FAILED] forKey:@"code"];
goto freeNetworkServicesArrayRef;
}
}
//Free Resources
freeNetworkServicesArrayRef:
CFRelease(networkServicesArrayRef);
freeNetworkSetRef:
CFRelease(networkSetRef);
freePrefsRef:
SCPreferencesUnlock(prefsRef);
CFRelease(prefsRef);
return ret;
}
const char* nsstring2cstring(NSString *s) {
if (s == NULL) { return NULL; }
const char *cstr = [s UTF8String];
return cstr;
}
const char* dictionaryToString(NSDictionary *dict) {
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
NSString *data;
if (! jsonData) {
data = @"{}";
} else {
data = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
return nsstring2cstring(data);
}
const char* turnOn(const char *host, const char *port)
{
NSLog(@"%s:%s", host, port);
NSString* nsProxyHost = [[NSString alloc] initWithCString: host encoding:NSUTF8StringEncoding];
NSNumber* nsProxyPort = [[NSNumber alloc] initWithLong: [[[NSString alloc] initWithCString: port encoding:NSUTF8StringEncoding] integerValue]];
NSLog(@"%@:%@", nsProxyHost, nsProxyPort);
//NSDictionary* dict = @{
// @"host": @(nsProxyHost),
// @"port": @(nsProxyPort),
//};
NSMutableDictionary *args = [NSMutableDictionary new];
[args setObject:nsProxyHost forKey:@"host"];
[args setObject:nsProxyPort forKey:@"port"];
return dictionaryToString(visit(&turnOnAction, true, args));
}
const char* turnOff() {
return dictionaryToString(visit(&turnOffAction, true, @{}));
}
*/
import "C"
import (
"encoding/json"
"errors"
"strconv"
"unsafe"
)
type RequestResponse struct {
Error string `json:"error,omitempty"`
Code uint `json:"code,omitempty"`
}
func disableProxy() error {
ret := C.turnOff()
data := C.GoString(ret)
var r RequestResponse
if err := json.Unmarshal([]byte(data), &r); err != nil {
return err
}
if r.Code != 0 {
return errors.New(r.Error)
}
return nil
}
func enableProxy(port int) error {
chost := C.CString("localhost")
cport := C.CString(strconv.Itoa(port))
ret := C.turnOn(chost, cport)
C.free(unsafe.Pointer(chost))
C.free(unsafe.Pointer(cport))
data := C.GoString(ret)
var r RequestResponse
if err := json.Unmarshal([]byte(data), &r); err != nil {
return err
}
if r.Code != 0 {
return errors.New(r.Error)
}
return nil
}