From e7cc26f81dd2b82aa2e5898d357b785e8d9938bf Mon Sep 17 00:00:00 2001 From: Timothy Qiu Date: Tue, 1 Aug 2017 21:40:09 +0800 Subject: [PATCH] Generates SIP002 QR code --- ShadowsocksX-NG/AppDelegate.swift | 1 + ShadowsocksX-NG/SWBQRCodeWindowController.h | 1 + ShadowsocksX-NG/SWBQRCodeWindowController.m | 9 ++++++ ShadowsocksX-NG/ServerProfile.swift | 34 ++++++++++++++++++++- 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/ShadowsocksX-NG/AppDelegate.swift b/ShadowsocksX-NG/AppDelegate.swift index 841c235..97883e6 100755 --- a/ShadowsocksX-NG/AppDelegate.swift +++ b/ShadowsocksX-NG/AppDelegate.swift @@ -241,6 +241,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele } qrcodeWinCtrl = SWBQRCodeWindowController(windowNibName: "SWBQRCodeWindowController") qrcodeWinCtrl.qrCode = profile.URL()!.absoluteString + qrcodeWinCtrl.legacyQRCode = profile.URL(legacy: true)!.absoluteString qrcodeWinCtrl.title = profile.title() qrcodeWinCtrl.showWindow(self) NSApp.activate(ignoringOtherApps: true) diff --git a/ShadowsocksX-NG/SWBQRCodeWindowController.h b/ShadowsocksX-NG/SWBQRCodeWindowController.h index 85a3e9a..ff9b194 100644 --- a/ShadowsocksX-NG/SWBQRCodeWindowController.h +++ b/ShadowsocksX-NG/SWBQRCodeWindowController.h @@ -11,6 +11,7 @@ @interface SWBQRCodeWindowController : NSWindowController +@property (nonatomic, copy) NSString *legacyQRCode; @property (nonatomic, copy) NSString *qrCode; @property (nonatomic, copy) NSString *title; diff --git a/ShadowsocksX-NG/SWBQRCodeWindowController.m b/ShadowsocksX-NG/SWBQRCodeWindowController.m index a7e97e9..e123be0 100644 --- a/ShadowsocksX-NG/SWBQRCodeWindowController.m +++ b/ShadowsocksX-NG/SWBQRCodeWindowController.m @@ -78,4 +78,13 @@ [pasteboard writeObjects:copiedObjects]; } +- (void)flagsChanged:(NSEvent *)event { + NSUInteger modifiers = event.modifierFlags & NSDeviceIndependentModifierFlagsMask; + if (modifiers & NSAlternateKeyMask) { + [self setQRCode:self.legacyQRCode]; + } else { + [self setQRCode:self.qrCode]; + } +} + @end diff --git a/ShadowsocksX-NG/ServerProfile.swift b/ShadowsocksX-NG/ServerProfile.swift index 2fde458..af86a89 100644 --- a/ShadowsocksX-NG/ServerProfile.swift +++ b/ShadowsocksX-NG/ServerProfile.swift @@ -246,7 +246,7 @@ class ServerProfile: NSObject, NSCopying { return true } - func URL() -> Foundation.URL? { + private func makeLegacyURL() -> URL? { var url = URLComponents() url.host = serverHost @@ -275,6 +275,38 @@ class ServerProfile: NSObject, NSCopying { } return nil } + + func URL(legacy: Bool = false) -> URL? { + // If you want the URL from <= 1.5.1 + if (legacy) { + return self.makeLegacyURL() + } + + guard let rawUserInfo = "\(method):\(password)".data(using: .utf8) else { + return nil + } + let paddings = CharacterSet(charactersIn: "=") + let userInfo = rawUserInfo.base64EncodedString().trimmingCharacters(in: paddings) + + var items = [URLQueryItem(name: "OTA", value: ota.description)] + if enabledKcptun { + items.append(URLQueryItem(name: "Kcptun", value: enabledKcptun.description)) + items.append(contentsOf: kcptunProfile.urlQueryItems()) + } + + var comps = URLComponents() + + comps.scheme = "ss" + comps.host = serverHost + comps.port = Int(serverPort) + comps.user = userInfo + comps.fragment = remark + comps.queryItems = items + + let url = try? comps.asURL() + + return url + } func title() -> String { if remark.isEmpty {