forked from shadowsocks/shadowsocks-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProfileManager.m
74 lines (66 loc) · 2.85 KB
/
ProfileManager.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
//
// Created by clowwindy on 11/3/14.
// Copyright (c) 2014 clowwindy. All rights reserved.
//
#import "ProfileManager.h"
#import "ShadowsocksRunner.h"
#define CONFIG_DATA_KEY @"config"
@implementation ProfileManager {
}
+ (Configuration *)configuration {
NSData *data = [[NSUserDefaults standardUserDefaults] dataForKey:CONFIG_DATA_KEY];
Configuration *configuration;
if (data == nil) {
// upgrade data from old version
configuration = [[Configuration alloc] init];
configuration.profiles = [[NSMutableArray alloc] initWithCapacity:16];
if ([ShadowsocksRunner isUsingPublicServer]) {
configuration.current = -1;
} else {
configuration.current = 0;
Profile *profile = [[Profile alloc] init];
profile.server = [ShadowsocksRunner configForKey:kShadowsocksIPKey];
profile.serverPort = [[ShadowsocksRunner configForKey:kShadowsocksPortKey] integerValue];
profile.password = [ShadowsocksRunner configForKey:kShadowsocksPasswordKey];
profile.method = [ShadowsocksRunner configForKey:kShadowsocksEncryptionKey];
[((NSMutableArray *)configuration.profiles) addObject:profile];
}
return configuration;
}
if (data == nil) {
// load default configuration
configuration = [[Configuration alloc] init];
// public server
configuration.current = -1;
configuration.profiles = [[NSMutableArray alloc] initWithCapacity:16];
} else {
configuration = [[Configuration alloc] initWithJSONData:data];
}
return configuration;
}
+ (void)saveConfiguration:(Configuration *)configuration {
if (configuration.profiles.count == 0) {
configuration.current = -1;
}
if (configuration.current != -1 && configuration.current >= configuration.profiles.count) {
configuration.current = 0;
}
[[NSUserDefaults standardUserDefaults] setObject:[configuration JSONData] forKey:CONFIG_DATA_KEY];
[ProfileManager reloadShadowsocksRunner];
}
+ (void)reloadShadowsocksRunner {
Configuration *configuration = [ProfileManager configuration];
if (configuration.current == -1) {
[ShadowsocksRunner setUsingPublicServer:YES];
[ShadowsocksRunner reloadConfig];
} else {
Profile *profile = configuration.profiles[configuration.current];
[ShadowsocksRunner setUsingPublicServer:NO];
[ShadowsocksRunner saveConfigForKey:kShadowsocksIPKey value:profile.server];
[ShadowsocksRunner saveConfigForKey:kShadowsocksPortKey value:[NSString stringWithFormat:@"%ld", (long)profile.serverPort]];
[ShadowsocksRunner saveConfigForKey:kShadowsocksPasswordKey value:profile.password];
[ShadowsocksRunner saveConfigForKey:kShadowsocksEncryptionKey value:profile.method];
[ShadowsocksRunner reloadConfig];
}
}
@end