From 5c1db71ab71bae1da3054099372c5b4eae3b38fa Mon Sep 17 00:00:00 2001 From: Charlie Qiu Date: Thu, 12 Jan 2017 11:35:05 +0800 Subject: [PATCH] Encode kcptun infomations to ss url. --- ShadowsocksX-NG/KcptunProfile.swift | 50 +++++++++++++++++++++++++++++ ShadowsocksX-NG/ServerProfile.swift | 16 +++++++++ 2 files changed, 66 insertions(+) diff --git a/ShadowsocksX-NG/KcptunProfile.swift b/ShadowsocksX-NG/KcptunProfile.swift index 844b9fb..5e40996 100644 --- a/ShadowsocksX-NG/KcptunProfile.swift +++ b/ShadowsocksX-NG/KcptunProfile.swift @@ -71,4 +71,54 @@ class KcptunProfile: NSObject { ] return conf } + + func urlQueryItems() -> [URLQueryItem] { + return [ + URLQueryItem(name: "mode", value: mode), + URLQueryItem(name: "key", value: key), + URLQueryItem(name: "crypt", value: crypt), + URLQueryItem(name: "datashard", value: "\(datashard)"), + URLQueryItem(name: "parityshard", value: "\(parityshard)"), + URLQueryItem(name: "nocomp", value: nocomp.description), + ] + } + + func loadUrlQueryItems(items: [URLQueryItem]) { + for item in items { + switch item.name { + case "mode": + if let v = item.value { + mode = v + } + case "key": + if let v = item.value { + key = v + } + case "crypt": + if let v = item.value { + crypt = v + } + case "datashard": + if let v = item.value { + if let vv = uint(v) { + datashard = vv + } + } + case "parityshard": + if let v = item.value { + if let vv = uint(v) { + parityshard = vv + } + } + case "nocomp": + if let v = item.value { + if let vv = Bool(v) { + nocomp = vv + } + } + default: + continue + } + } + } } diff --git a/ShadowsocksX-NG/ServerProfile.swift b/ShadowsocksX-NG/ServerProfile.swift index df89a47..752583c 100644 --- a/ShadowsocksX-NG/ServerProfile.swift +++ b/ShadowsocksX-NG/ServerProfile.swift @@ -81,6 +81,16 @@ class ServerProfile: NSObject, NSCopying { .filter({ $0.name == "OTA" }).first?.value { ota = NSString(string: otaStr).boolValue } + if let enabledKcptunStr = parsedUrl.queryItems? + .filter({ $0.name == "Kcptun" }).first?.value { + enabledKcptun = NSString(string: enabledKcptunStr).boolValue + } + + if enabledKcptun { + if let items = parsedUrl.queryItems { + self.kcptunProfile.loadUrlQueryItems(items: items) + } + } } public func copy(with zone: NSZone? = nil) -> Any { @@ -219,6 +229,12 @@ class ServerProfile: NSObject, NSCopying { url.queryItems = [URLQueryItem(name: "Remark", value: remark), URLQueryItem(name: "OTA", value: ota.description)] + if enabledKcptun { + url.queryItems?.append(contentsOf: [ + URLQueryItem(name: "Kcptun", value: enabledKcptun.description), + ]) + url.queryItems?.append(contentsOf: kcptunProfile.urlQueryItems()) + } let parts = url.string?.replacingOccurrences( of: "//", with: "",