forked from haxpor/Potatso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProxyManager.m
190 lines (168 loc) · 6.2 KB
/
ProxyManager.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
//
// ProxyManager.m
// Potatso
//
// Created by LEI on 2/23/16.
// Copyright © 2016 TouchingApp. All rights reserved.
//
#import "ProxyManager.h"
#import <ShadowPath/ShadowPath.h>
#import <netinet/in.h>
#import "PotatsoBase.h"
@interface ProxyManager ()
@property (nonatomic) BOOL socksProxyRunning;
@property (nonatomic) int socksProxyPort;
@property (nonatomic) BOOL httpProxyRunning;
@property (nonatomic) int httpProxyPort;
@property (nonatomic) BOOL shadowsocksProxyRunning;
@property (nonatomic) int shadowsocksProxyPort;
@property (nonatomic, copy) SocksProxyCompletion socksCompletion;
@property (nonatomic, copy) HttpProxyCompletion httpCompletion;
@property (nonatomic, copy) ShadowsocksProxyCompletion shadowsocksCompletion;
- (void)onSocksProxyCallback: (int)fd;
- (void)onHttpProxyCallback: (int)fd;
- (void)onShadowsocksCallback:(int)fd;
@end
void http_proxy_handler(int fd, void *udata) {
ProxyManager *provider = (__bridge ProxyManager *)udata;
[provider onHttpProxyCallback:fd];
}
void shadowsocks_handler(int fd, void *udata) {
ProxyManager *provider = (__bridge ProxyManager *)udata;
[provider onShadowsocksCallback:fd];
}
int sock_port (int fd) {
struct sockaddr_in sin;
socklen_t len = sizeof(sin);
if (getsockname(fd, (struct sockaddr *)&sin, &len) < 0) {
NSLog(@"getsock_port(%d) error: %s",
fd, strerror (errno));
return 0;
}else{
return ntohs(sin.sin_port);
}
}
@implementation ProxyManager
+ (ProxyManager *)sharedManager {
static dispatch_once_t onceToken;
static ProxyManager *manager;
dispatch_once(&onceToken, ^{
manager = [ProxyManager new];
});
return manager;
}
- (void)startSocksProxy:(SocksProxyCompletion)completion {
self.socksCompletion = [completion copy];
NSString *confContent = [NSString stringWithContentsOfURL:[Potatso sharedSocksConfUrl] encoding:NSUTF8StringEncoding error:nil];
confContent = [confContent stringByReplacingOccurrencesOfString:@"${ssport}" withString:[NSString stringWithFormat:@"%d", [self shadowsocksProxyPort]]];
int fd = [[AntinatServer sharedServer] startWithConfig:confContent];
[self onSocksProxyCallback:fd];
}
- (void)stopSocksProxy {
[[AntinatServer sharedServer] stop];
self.socksProxyRunning = NO;
}
- (void)onSocksProxyCallback:(int)fd {
NSError *error;
if (fd > 0) {
self.socksProxyPort = sock_port(fd);
self.socksProxyRunning = YES;
}else {
error = [NSError errorWithDomain:[[NSBundle mainBundle] bundleIdentifier] code:100 userInfo:@{NSLocalizedDescriptionKey: @"Fail to start socks proxy"}];
}
if (self.socksCompletion) {
self.socksCompletion(self.socksProxyPort, error);
}
}
# pragma mark - Shadowsocks
- (void)startShadowsocks: (ShadowsocksProxyCompletion)completion {
self.shadowsocksCompletion = [completion copy];
[NSThread detachNewThreadSelector:@selector(_startShadowsocks) toTarget:self withObject:nil];
}
- (void)_startShadowsocks {
NSString *confContent = [NSString stringWithContentsOfURL:[Potatso sharedProxyConfUrl] encoding:NSUTF8StringEncoding error:nil];
NSDictionary *json = [confContent jsonDictionary];
NSString *host = json[@"host"];
NSNumber *port = json[@"port"];
NSString *password = json[@"password"];
NSString *authscheme = json[@"authscheme"];
NSString *protocol = json[@"protocol"];
NSString *obfs = json[@"obfs"];
NSString *obfs_param = json[@"obfs_param"];
BOOL ota = [json[@"ota"] boolValue];
if (host && port && password && authscheme) {
profile_t profile;
memset(&profile, 0, sizeof(profile_t));
profile.remote_host = strdup([host UTF8String]);
profile.remote_port = [port intValue];
profile.password = strdup([password UTF8String]);
profile.method = strdup([authscheme UTF8String]);
profile.local_addr = "127.0.0.1";
profile.local_port = 0;
profile.timeout = 600;
profile.auth = ota;
if (protocol.length > 0) {
profile.protocol = strdup([protocol UTF8String]);
}
if (obfs.length > 0) {
profile.obfs = strdup([obfs UTF8String]);
}
if (obfs_param.length > 0) {
profile.obfs_param = strdup([obfs_param UTF8String]);
}
start_ss_local_server(profile, shadowsocks_handler, (__bridge void *)self);
}else {
if (self.shadowsocksCompletion) {
self.shadowsocksCompletion(0, nil);
}
return;
}
}
- (void)stopShadowsocks {
// Do nothing
}
- (void)onShadowsocksCallback:(int)fd {
NSError *error;
if (fd > 0) {
self.shadowsocksProxyPort = sock_port(fd);
self.shadowsocksProxyRunning = YES;
}else {
error = [NSError errorWithDomain:[[NSBundle mainBundle] bundleIdentifier] code:100 userInfo:@{NSLocalizedDescriptionKey: @"Fail to start http proxy"}];
}
if (self.shadowsocksCompletion) {
self.shadowsocksCompletion(self.shadowsocksProxyPort, error);
}
}
# pragma mark - Http Proxy
- (void)startHttpProxy:(HttpProxyCompletion)completion {
self.httpCompletion = [completion copy];
[NSThread detachNewThreadSelector:@selector(_startHttpProxy:) toTarget:self withObject:[Potatso sharedHttpProxyConfUrl]];
}
- (void)_startHttpProxy: (NSURL *)confURL {
struct forward_spec *proxy = NULL;
if (self.shadowsocksProxyPort > 0) {
proxy = (malloc(sizeof(struct forward_spec)));
memset(proxy, 0, sizeof(struct forward_spec));
proxy->type = SOCKS_5;
proxy->gateway_host = "127.0.0.1";
proxy->gateway_port = self.shadowsocksProxyPort;
}
shadowpath_main(strdup([[confURL path] UTF8String]), proxy, http_proxy_handler, (__bridge void *)self);
}
- (void)stopHttpProxy {
// polipoExit();
// self.httpProxyRunning = NO;
}
- (void)onHttpProxyCallback:(int)fd {
NSError *error;
if (fd > 0) {
self.httpProxyPort = sock_port(fd);
self.httpProxyRunning = YES;
}else {
error = [NSError errorWithDomain:[[NSBundle mainBundle] bundleIdentifier] code:100 userInfo:@{NSLocalizedDescriptionKey: @"Fail to start http proxy"}];
}
if (self.httpCompletion) {
self.httpCompletion(self.httpProxyPort, error);
}
}
@end