Generates SIP002 QR code

This commit is contained in:
Timothy Qiu
2017-08-01 21:40:09 +08:00
parent 72c52047f4
commit e7cc26f81d
4 changed files with 44 additions and 1 deletions

View File

@ -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)

View File

@ -11,6 +11,7 @@
@interface SWBQRCodeWindowController : NSWindowController
@property (nonatomic, copy) NSString *legacyQRCode;
@property (nonatomic, copy) NSString *qrCode;
@property (nonatomic, copy) NSString *title;

View File

@ -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

View File

@ -246,7 +246,7 @@ class ServerProfile: NSObject, NSCopying {
return true
}
func URL() -> Foundation.URL? {
private func makeLegacyURL() -> URL? {
var url = URLComponents()
url.host = serverHost
@ -276,6 +276,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 {
return "\(serverHost):\(serverPort)"