-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathProxySettingsTableViewController.m
366 lines (331 loc) · 17.1 KB
/
ProxySettingsTableViewController.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//
// ProxySettingsTableViewController.m
// shadowsocks-iOS
//
// Created by clowwindy on 12-12-31.
// Copyright (c) 2012年 clowwindy. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "ProxySettingsTableViewController.h"
#import "SimpleTableViewSource.h"
#import "local.h"
#import "SWBAppDelegate.h"
// rows
#define kIPRow 0
#define kPortRow 1
#define kPasswordRow 2
// config keys
#define kIPKey @"proxy ip"
#define kPortKey @"proxy port"
#define kPasswordKey @"proxy password"
#define kEncryptionKey @"proxy encryption"
#define kProxyModeKey @"proxy mode"
#define kUsePublicServer @"public server"
@interface ProxySettingsTableViewController () {
SimpleTableViewSource *encryptionSource;
SimpleTableViewSource *apnSource;
SimpleTableViewSource *modeSource;
}
@end
@implementation ProxySettingsTableViewController
+ (BOOL)settingsAreNotComplete {
if ((![ProxySettingsTableViewController isUsingPublicServer]) && ([[NSUserDefaults standardUserDefaults] stringForKey:kIPKey] == nil ||
[[NSUserDefaults standardUserDefaults] stringForKey:kPortKey] == nil ||
[[NSUserDefaults standardUserDefaults] stringForKey:kPasswordKey] == nil)) {
return YES;
} else {
return NO;
}
}
+ (BOOL)runProxy {
if (![ProxySettingsTableViewController settingsAreNotComplete]) {
local_main();
return YES;
} else {
#ifdef DEBUG
NSLog(@"warning: settings are not complete");
#endif
return NO;
}
}
+ (void)reloadConfig {
if (![ProxySettingsTableViewController settingsAreNotComplete]) {
if ([ProxySettingsTableViewController isUsingPublicServer]) {
set_config("106.186.124.182", "8910", "Shadowsocks", "aes-128-cfb");
memcpy(shadowsocks_key, "\x45\xd1\xd9\x9e\xbd\xf5\x8c\x85\x34\x55\xdd\x65\x46\xcd\x06\xd3", 16);
} else {
NSString *v = [[NSUserDefaults standardUserDefaults] objectForKey:kEncryptionKey];
if (!v) {
v = @"aes-256-cfb";
}
set_config([[[NSUserDefaults standardUserDefaults] stringForKey:kIPKey] cStringUsingEncoding:NSUTF8StringEncoding], [[[NSUserDefaults standardUserDefaults] stringForKey:kPortKey] cStringUsingEncoding:NSUTF8StringEncoding], [[[NSUserDefaults standardUserDefaults] stringForKey:kPasswordKey] cStringUsingEncoding:NSUTF8StringEncoding], [v cStringUsingEncoding:NSUTF8StringEncoding]);
}
}
}
- (void)saveConfigForKey:(NSString *)key value:(NSString *)value {
[[NSUserDefaults standardUserDefaults] setObject:value forKey:key];
}
+ (BOOL)isUsingPublicServer {
NSNumber *usePublicServer = [[NSUserDefaults standardUserDefaults] objectForKey:kUsePublicServer];
if (usePublicServer != nil) {
return [usePublicServer boolValue];
} else {
return YES;
}
}
- (void)changePublicServer:(UISegmentedControl *)segmentedControl {
BOOL result = NO;
if (segmentedControl.selectedSegmentIndex == 0) {
result = YES;
}
[[NSUserDefaults standardUserDefaults] setBool:result forKey:kUsePublicServer];
if ((self.tableView.numberOfSections == 1) && !result) {
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
} else if ((self.tableView.numberOfSections == 2) && result) {
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (id)initWithStyle:(UITableViewStyle)style {
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)];
self.navigationItem.rightBarButtonItem = done;
UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
self.navigationItem.leftBarButtonItem = cancel;
self.navigationItem.title = _L(Proxy
Settings);
self.contentSizeForViewInPopover = CGSizeMake(320, 400);
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
#pragma mark - navigation
- (void)cancel {
[self dismissModalViewControllerAnimated:YES];
if (self->_myPopoverController) {
[_myPopoverController dismissPopoverAnimated:YES];
}
}
- (void)done {
if (ipField.text == nil) {
ipField.text = @"";
}
if (portField.text == nil) {
portField.text = @"";
}
if (passwordField.text == nil) {
passwordField.text = @"";
}
[self saveConfigForKey:kIPKey value:ipField.text];
[self saveConfigForKey:kPortKey value:portField.text];
[self saveConfigForKey:kPasswordKey value:passwordField.text];
[ProxySettingsTableViewController reloadConfig];
[self dismissModalViewControllerAnimated:YES];
if (self->_myPopoverController) {
[_myPopoverController dismissPopoverAnimated:YES];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
if ([ProxySettingsTableViewController isUsingPublicServer]) {
return 1;
}
return 2;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0) {
return _L(Server);
} else {
return nil;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
if (section == 0) {
return 1;
}
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == 0) {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"c"];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[_L(Public), _L(Custom)]];
[cell.contentView addSubview:segmentedControl];
segmentedControl.center = CGPointMake(320 * 0.5f, cell.bounds.size.height * 0.5f);
if ([ProxySettingsTableViewController isUsingPublicServer]) {
segmentedControl.selectedSegmentIndex = 0;
} else {
segmentedControl.selectedSegmentIndex = 1;
}
[segmentedControl addTarget:self action:@selector(changePublicServer:) forControlEvents:UIControlEventValueChanged];
return cell;
} else if (indexPath.section == 1) {
if (indexPath.row == 3) {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"bb"];
cell.textLabel.text = _L(Method);
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
if (indexPath.row == 4) {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"bb"];
cell.textLabel.text = _L(Proxy
Mode);
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
if (indexPath.row == 5) {
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"bb"];
cell.textLabel.text = _L(Enable / Disable
APN);
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"aaaaa"];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.adjustsFontSizeToFitWidth = YES;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.returnKeyType = UIReturnKeyDone;
switch (indexPath.row) {
case kIPRow:
cell.textLabel.text = _L(IP);
textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
textField.secureTextEntry = NO;
textField.text = [[NSUserDefaults standardUserDefaults] stringForKey:kIPKey];
ipField = textField;
break;
case kPortRow:
cell.textLabel.text = _L(Port);
textField.keyboardType = UIKeyboardTypeNumberPad;
textField.secureTextEntry = NO;
textField.text = [[NSUserDefaults standardUserDefaults] stringForKey:kPortKey];
portField = textField;
break;
case kPasswordRow:
cell.textLabel.text = _L(Password);
textField.keyboardType = UIKeyboardTypeDefault;
textField.secureTextEntry = YES;
textField.text = [[NSUserDefaults standardUserDefaults] stringForKey:kPasswordKey];
passwordField = textField;
break;
default:
break;
}
[cell addSubview:textField];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
return nil;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
if (indexPath.row == 3) {
NSString *v = [[NSUserDefaults standardUserDefaults] objectForKey:kEncryptionKey];
if (!v) {
v = @"aes-256-cfb";
}
encryptionSource = [[SimpleTableViewSource alloc] initWithLabels:[NSArray arrayWithObjects:@"table",
@"aes-256-cfb",
@"aes-192-cfb",
@"aes-128-cfb",
@"bf-cfb",
@"camellia-128-cfb",
@"camellia-192-cfb",
@"camellia-256-cfb",
@"cast5-cfb",
@"des-cfb",
@"idea-cfb",
@"rc2-cfb",
@"rc4",
@"seed-cfb",
nil]
values:[NSArray arrayWithObjects:@"table",
@"aes-256-cfb",
@"aes-192-cfb",
@"aes-128-cfb",
@"bf-cfb",
@"camellia-128-cfb",
@"camellia-192-cfb",
@"camellia-256-cfb",
@"cast5-cfb",
@"des-cfb",
@"idea-cfb",
@"rc2-cfb",
@"rc4",
@"seed-cfb",
nil]
initialValue:v selectionBlock:^(NSObject *value) {
[[NSUserDefaults standardUserDefaults] setObject:value forKey:kEncryptionKey];
}];
UIViewController *controller = [[UIViewController alloc] init];
controller.contentSizeForViewInPopover = self.contentSizeForViewInPopover;
controller.navigationItem.title = _L(Method);
UITableView *tableView1 = [[UITableView alloc] initWithFrame:controller.view.frame style:UITableViewStyleGrouped];
tableView1.dataSource = encryptionSource;
tableView1.delegate = encryptionSource;
controller.view = tableView1;
[self.navigationController pushViewController:controller animated:YES];
} else if (indexPath.row == 4) {
NSString *v = [[NSUserDefaults standardUserDefaults] objectForKey:kProxyModeKey];
if (!v) {
v = @"pac";
}
modeSource = [[SimpleTableViewSource alloc] initWithLabels:[NSArray arrayWithObjects:_L(PAC), _L(Global), _L(Off), nil]
values:[NSArray arrayWithObjects:@"pac", @"global", @"off", nil]
initialValue:v selectionBlock:^(NSObject *value) {
[[NSUserDefaults standardUserDefaults] setObject:value forKey:kProxyModeKey];
SWBAppDelegate *appDelegate = (SWBAppDelegate *) [UIApplication sharedApplication].delegate;
[appDelegate updateProxyMode];
}];
UIViewController *controller = [[UIViewController alloc] init];
controller.contentSizeForViewInPopover = CGSizeMake(320, 480);
controller.navigationItem.title = _L(Proxy
Mode);
UITableView *tableView1 = [[UITableView alloc] initWithFrame:controller.view.frame style:UITableViewStyleGrouped];
tableView1.dataSource = modeSource;
tableView1.delegate = modeSource;
controller.view = tableView1;
[self.navigationController pushViewController:controller animated:YES];
} else if (indexPath.row == 5) {
apnSource = [[SimpleTableViewSource alloc] initWithLabels:[NSArray arrayWithObjects:_L(Enable
Unicom), _L(Disable
Unicom), nil]
values:[NSArray arrayWithObjects:@"3gnet_enable", @"3gnet_disable", nil]
initialValue:nil selectionBlock:^(NSObject *value) {
SWBAppDelegate *appDelegate = (SWBAppDelegate *) [UIApplication sharedApplication].delegate;
NSString *v = (NSString *) value;
[appDelegate setPolipo:[v rangeOfString:@"enable"].length > 0];
// TODO: open after 1s, using a timer
[[UIApplication sharedApplication] openURL:
[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8080/apn?id=%@", (NSString *) value]]];
}];
UIViewController *controller = [[UIViewController alloc] init];
controller.contentSizeForViewInPopover = CGSizeMake(320, 480);
UITableView *tableView1 = [[UITableView alloc] initWithFrame:controller.view.frame style:UITableViewStyleGrouped];
tableView1.dataSource = apnSource;
tableView1.delegate = apnSource;
controller.view = tableView1;
[self.navigationController pushViewController:controller animated:YES];
}
}
@end