-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerProfile.m
177 lines (168 loc) · 7.59 KB
/
ServerProfile.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
//
// ServerProfile.m
// V2RayX
//
// Copyright © 2016年 Cenmrev. All rights reserved.
//
#import "ServerProfile.h"
@implementation ServerProfile
- (ServerProfile*)init {
self = [super init];
if (self) {
[self setAddress:@"server.cc"];
[self setPort:10086];
[self setUserId:@"00000000-0000-0000-0000-000000000000"];
[self setAlterId:64];
[self setLevel:0];
[self setOutboundTag:@"test server"];
[self setSecurity:auto_];
[self setNetwork:tcp];
[self setSendThrough:@"0.0.0.0"];
[self setStreamSettings:@{
@"security": @"none",
@"tlsSettings": @{
@"serverName": @"server.cc",
@"alpn": @[@"http/1.1"],
@"allowInsecure": [NSNumber numberWithBool:NO],
@"allowInsecureCiphers": [NSNumber numberWithBool:NO]
},
@"tcpSettings": @{
@"header": @{
@"type": @"none"
}
},
@"kcpSettings": @{
@"mtu": @1350,
@"tti": @20,
@"uplinkCapacity": @5,
@"downlinkCapacity": @20,
@"congestion": [NSNumber numberWithBool:NO],
@"readBufferSize": @1,
@"writeBufferSize": @1,
@"header": @{
@"type": @"none"
}
},
@"wsSettings": @{
@"path": @"",
@"headers": @{}
},
@"httpSettings": @{
@"host": @[@""],
@"path": @""
},
@"quicSettings": @{
@"security": @"none",
@"key": @"",
@"header": @{ @"type": @"none" }
},
@"sockopt": @{}
}];
[self setMuxSettings:@{
@"enabled": [NSNumber numberWithBool:NO],
@"concurrency": @8
}];
}
return self;
}
- (NSString*)description {
return [[self outboundProfile] description];
}
+ (NSArray*)profilesFromJson:(NSDictionary*)outboundJson {
if (![outboundJson[@"protocol"] isKindOfClass:[NSString class]]
|| ![outboundJson[@"protocol"] isEqualToString:@"vmess"] ) {
return @[];
}
NSMutableArray* profiles = [[NSMutableArray alloc] init];
NSString* sendThrough = nilCoalescing(outboundJson[@"sendThrough"], @"0.0.0.0");
if (![[outboundJson valueForKeyPath:@"settings.vnext"] isKindOfClass:[NSArray class]]) {
return @[];
}
for (NSDictionary* vnext in [outboundJson valueForKeyPath:@"settings.vnext"]) {
ServerProfile* profile = [[ServerProfile alloc] init];
profile.address = nilCoalescing(vnext[@"address"], @"127.0.0.1");
profile.outboundTag = nilCoalescing(outboundJson[@"tag"], @"");
profile.port = [vnext[@"port"] unsignedIntegerValue];
if (![vnext[@"users"] isKindOfClass:[NSArray class]] || [vnext[@"users"] count] == 0) {
continue;
}
profile.userId = nilCoalescing(vnext[@"users"][0][@"id"], @"23ad6b10-8d1a-40f7-8ad0-e3e35cd38287");
profile.alterId = [vnext[@"users"][0][@"alterId"] unsignedIntegerValue];
profile.level = [vnext[@"users"][0][@"level"] unsignedIntegerValue];
profile.security = searchInArray(vnext[@"users"][0][@"security"], VMESS_SECURITY_LIST);
if (outboundJson[@"streamSettings"] != nil) {
profile.streamSettings = outboundJson[@"streamSettings"];
profile.network = searchInArray(outboundJson[@"streamSettings"][@"network"], NETWORK_LIST);
}
if (outboundJson[@"mux"] != nil) {
profile.muxSettings = outboundJson[@"mux"];
}
profile.sendThrough = sendThrough;
[profiles addObject:profile];
}
return profiles;
}
+ (ServerProfile* _Nullable )readFromAnOutboundDic:(NSDictionary*)outDict {
NSArray *allProfiles = [self profilesFromJson:outDict];
if ([allProfiles count] > 0) {
return allProfiles[0];
} else {
return NULL;
}
}
-(ServerProfile*)deepCopy {
ServerProfile* aCopy = [[ServerProfile alloc] init];
aCopy.address = [NSString stringWithString:nilCoalescing(self.address, @"")];
aCopy.port = self.port;
aCopy.userId = [NSString stringWithString:nilCoalescing(self.userId, @"")];
aCopy.alterId = self.alterId;
aCopy.level = self.level;
aCopy.outboundTag = [NSString stringWithString:nilCoalescing(self.outboundTag, @"")];
aCopy.security = self.security;
aCopy.network = self.network;
aCopy.sendThrough = [NSString stringWithString:nilCoalescing(self.sendThrough, @"")];
aCopy.streamSettings = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:self.streamSettings]];
aCopy.muxSettings = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:self.muxSettings]];
return aCopy;
}
- (NSMutableDictionary*)outboundProfile {
NSMutableDictionary* fullStreamSettings = [NSMutableDictionary dictionaryWithDictionary:streamSettings];
fullStreamSettings[@"network"] = NETWORK_LIST[network];
NSDictionary* result =
@{
@"sendThrough": sendThrough,
@"tag": nilCoalescing(outboundTag, @""),
@"protocol": @"vmess",
@"settings": [@{
@"vnext": @[
@{
@"address": nilCoalescing([address stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] , @""),
@"port": [NSNumber numberWithUnsignedInteger:port],
@"users": @[
@{
@"id": userId != nil ? [userId stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]: @"",
@"alterId": [NSNumber numberWithUnsignedInteger:alterId],
@"security": VMESS_SECURITY_LIST[security],
@"level": [NSNumber numberWithUnsignedInteger:level]
}
]
}
]
} mutableCopy],
@"streamSettings": fullStreamSettings,
@"mux": muxSettings,
};
return [result mutableCopy];
}
@synthesize address;
@synthesize port;
@synthesize userId;
@synthesize alterId;
@synthesize level;
@synthesize outboundTag;
@synthesize security;
@synthesize network;
@synthesize sendThrough;
@synthesize muxSettings;
@synthesize streamSettings;
@end