99
1010@implementation ConfigImporter
1111
12- + (NSString *)decodeBase64String : (NSString *)encoded {
12+ + (NSString * _Nonnull)decodeBase64String : (NSString *)encoded {
13+ if (!encoded || ![encoded isKindOfClass: [NSString class ]] || encoded.length == 0 ) {
14+ return @" " ;
15+ }
1316 NSMutableString * fixed = [encoded mutableCopy ];
1417 NSInteger numAdd = encoded.length % 4 ;
1518 for (int i = 0 ; i < numAdd; i += 1 ) {
1619 [fixed appendString: @" =" ];
1720 }
18- NSData * decodedData = [[NSData alloc ] initWithBase64EncodedString: fixed options: NSDataBase64DecodingIgnoreUnknownCharacters ];
19- return [[NSString alloc ] initWithData: decodedData encoding: NSUTF8StringEncoding];
21+ @try {
22+ NSData * decodedData = [[NSData alloc ] initWithBase64EncodedString: fixed options: NSDataBase64DecodingIgnoreUnknownCharacters ];
23+ NSString * decodedString = [[NSString alloc ] initWithData: decodedData encoding: NSUTF8StringEncoding];
24+ assert (decodedString != nil );
25+ return decodedString;
26+ } @catch (NSException *exception) {
27+ return @" " ;
28+ }
2029}
2130
2231+ (NSDictionary *)parseLegacySSLink : (NSString *)link {
2332 // http://shadowsocks.org/en/config/quick-guide.html
2433 @try {
2534 NSString * encoded = [[link stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet ]] substringFromIndex: 5 ];
26- NSString * encodedRemoveTag = [encoded componentsSeparatedByString: @" #" ][0 ];
35+ NSArray * hashTagSeperatedParts = [encoded componentsSeparatedByString: @" #" ];
36+ NSString * encodedRemoveTag = hashTagSeperatedParts[0 ];
2737 NSData * decodedData = [[NSData alloc ] initWithBase64EncodedString: encodedRemoveTag options: 0 ];
2838 NSString * decoded = [[NSString alloc ] initWithData: decodedData
2939 encoding: NSUTF8StringEncoding];
3040
3141 NSArray * parts = [decoded componentsSeparatedByString: @" @" ];
32- NSArray * server_port = [parts[1 ] componentsSeparatedByString: @" :" ];
33- NSMutableArray * method_password = [[parts[0 ] componentsSeparatedByString: @" :" ] mutableCopy ];
34- NSString * method = method_password[0 ];
35- [method_password removeObjectAtIndex: 0 ];
36- return @{
37- @" server" :server_port[0 ],
38- @" server_port" :server_port[1 ],
39- @" password" : [method_password componentsJoinedByString: @" :" ],
40- @" method" :method};
42+ NSArray * addressAndPort = [parts[1 ] componentsSeparatedByString: @" :" ];
43+ NSMutableArray * methodAndPassword = [[parts[0 ] componentsSeparatedByString: @" :" ] mutableCopy ];
44+ NSString * method = methodAndPassword[0 ];
45+ [methodAndPassword removeObjectAtIndex: 0 ];
46+
47+ NSNumberFormatter * f = [[NSNumberFormatter alloc ] init ];
48+ f.numberStyle = NSNumberFormatterDecimalStyle;
49+ NSNumber *port = [f numberFromString: addressAndPort[1 ]];
50+
51+ if (hashTagSeperatedParts.count == 1 ) {
52+ return @{
53+ @" server" :addressAndPort[0 ],
54+ @" server_port" :port,
55+ @" password" : [methodAndPassword componentsJoinedByString: @" :" ],
56+ @" method" :method};
57+ } else {
58+ return @{
59+ @" server" :addressAndPort[0 ],
60+ @" server_port" :port,
61+ @" password" : [methodAndPassword componentsJoinedByString: @" :" ],
62+ @" method" :method,
63+ @" tag" :hashTagSeperatedParts[1 ]
64+ };
65+ }
4166 } @catch (NSException *exception) {
4267 return nil ;
43- } @finally {
44- ;
4568 }
4669}
4770
@@ -70,7 +93,6 @@ + (NSDictionary*)parseStandardSSLink:(NSString*)link {
7093 if (!port) {
7194 return nil ;
7295 }
73-
7496 return @{
7597 @" server" :hostInfo[0 ],
7698 @" server_port" :port,
@@ -80,12 +102,10 @@ + (NSDictionary*)parseStandardSSLink:(NSString*)link {
80102 };
81103 } @catch (NSException *exception) {
82104 return nil ;
83- } @finally {
84- ;
85105 }
86106}
87107
88- + (NSMutableDictionary *)ssOutboundFromSSLink : (NSString *)link {
108+ + (NSMutableDictionary * )ssOutboundFromSSLink : (NSString *)link {
89109 NSDictionary * parsed = [ConfigImporter parseStandardSSLink: link];
90110 if (parsed) {
91111 return [ConfigImporter ssOutboundFromSSConfig: parsed];
@@ -257,7 +277,7 @@ + (NSMutableDictionary*)importFromStandardConfigFiles:(NSArray*)files {
257277 return result;
258278}
259279
260- + (NSMutableDictionary *)importFromSubscriptionOfV2RayN : (NSString *)httpLink {
280+ + (NSMutableDictionary *)importFromHTTPSubscription : (NSString *)httpLink {
261281 // https://blog.csdn.net/yi_zz32/article/details/48769487
262282 NSMutableDictionary * result = [@{@" vmess" : @[], @" other" : @[]} mutableDeepCopy];
263283 if (![@" http" isEqualToString: [httpLink substringToIndex: 4 ]]) {
@@ -286,6 +306,8 @@ + (NSMutableDictionary*)importFromSubscriptionOfV2RayN: (NSString*)httpLink {
286306 [result[@" other" ] addObject: outbound];
287307 continue ;
288308 }
309+ NSMutableDictionary * ssdResults = [ConfigImporter importFromSubscriptionOfSSD: linkStr];
310+ [result[@" other" ] addObjectsFromArray: ssdResults[@" other" ]];
289311 }
290312 }
291313 return result;
0 commit comments