Skip to content

Commit 9f6388d

Browse files
committed
Optimize: valid profile when import json file
1 parent f2f3eb4 commit 9f6388d

File tree

3 files changed

+36
-29
lines changed

3 files changed

+36
-29
lines changed

ClashX/General/ConfigFileFactory.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ class ConfigFileFactory {
7474
profile.method = item["method"] as! String
7575
profile.password = item["password"] as! String
7676
profile.remark = item["remarks"] as! String
77-
78-
profiles.append(profile)
77+
if (profile.isValid()) {
78+
profiles.append(profile)
79+
}
7980
}
8081

8182
let configStr = self.configFile(proxies: profiles)

ClashX/Models/ProxyServerModel.swift

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,28 @@ class ProxyServerModel: NSObject, Codable {
1515
@objc dynamic var method:String = "RC4-MD5"
1616
@objc dynamic var remark:String = "Proxy"
1717

18+
19+
static let supportMethod = [
20+
"RC4-MD5",
21+
"AES-128-CTR",
22+
"AES-192-CTR",
23+
"AES-256-CTR",
24+
"AES-128-CFB",
25+
"AES-192-CFB",
26+
"AES-256-CFB",
27+
"CHACHA20",
28+
"CHACHA20-IETF",
29+
"XCHACHA20",
30+
"AEAD_AES_128_GCM",
31+
"AEAD_AES_192_GCM",
32+
"AEAD_AES_256_GCM",
33+
"AEAD_CHACHA20_POLY1305"
34+
]
35+
1836
func isValid() -> Bool {
37+
let whitespace = NSCharacterSet.whitespacesAndNewlines
38+
remark = remark.components(separatedBy: whitespace).joined()
39+
1940
func validateIpAddress(_ ipToValidate: String) -> Bool {
2041

2142
var sin = sockaddr_in()
@@ -34,6 +55,7 @@ class ProxyServerModel: NSObject, Codable {
3455
}
3556

3657
func validateDomainName(_ value: String) -> Bool {
58+
// this regex from ss-ng seems useless
3759
let validHostnameRegex = "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])$"
3860

3961
if (value.range(of: validHostnameRegex, options: .regularExpression) != nil) {
@@ -43,16 +65,22 @@ class ProxyServerModel: NSObject, Codable {
4365
}
4466
}
4567

46-
func validatePort(_ value: String) -> Bool {
68+
func vaildatePort(_ value: String) -> Bool {
4769
if let port = Int(value) {
4870
return port > 0 && port <= 65535
4971
}
5072
return false
5173
}
5274

53-
if !(validateIpAddress(serverHost) ||
54-
validateDomainName(serverHost) ||
55-
validatePort(serverPort)){
75+
func vaildateMethod(_ method:String) -> Bool {
76+
return type(of: self).supportMethod.contains(method.uppercased())
77+
}
78+
79+
if !(validateIpAddress(serverHost) || validateDomainName(serverHost)) {
80+
return false
81+
}
82+
83+
if !(vaildateMethod(method) && vaildatePort(serverPort)) {
5684
return false
5785
}
5886

ClashX/ViewControllers/PreferencesWindowController.swift

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,7 @@ class PreferencesWindowController: NSWindowController
5151
serverConfigs.append(ProxyServerModel())
5252
}
5353

54-
55-
methodTextField.addItems(withObjectValues: [
56-
"aes-128-gcm",
57-
"aes-192-gcm",
58-
"aes-256-gcm",
59-
"aes-128-cfb",
60-
"aes-192-cfb",
61-
"aes-256-cfb",
62-
"aes-128-ctr",
63-
"aes-192-ctr",
64-
"aes-256-ctr",
65-
"camellia-128-cfb",
66-
"camellia-192-cfb",
67-
"camellia-256-cfb",
68-
"bf-cfb",
69-
"chacha20-ietf-poly1305",
70-
"salsa20",
71-
"chacha20",
72-
"chacha20-ietf",
73-
"rc4-md5",
74-
])
75-
76-
54+
methodTextField.addItems(withObjectValues: ProxyServerModel.supportMethod)
7755

7856
profilesTableView.reloadData()
7957
updateProfileBoxVisible()

0 commit comments

Comments
 (0)