Skip to content

Commit

Permalink
fix #7,#8,#9
Browse files Browse the repository at this point in the history
  • Loading branch information
yanue committed Dec 9, 2018
1 parent 3a3c783 commit 83a0323
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 61 deletions.
19 changes: 16 additions & 3 deletions V2rayU/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {


func applicationDidFinishLaunching(_ aNotification: Notification) {
NSAppleEventManager.shared().setEventHandler(self, andSelector: #selector(self.handleAppleEvent(event:replyEvent:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))

// Insert code here to initialize your application
let startedAtLogin = NSWorkspace.shared.runningApplications.contains {
$0.bundleIdentifier == launcherAppIdentifier
Expand All @@ -40,6 +38,12 @@ class AppDelegate: NSObject, NSApplicationDelegate {
// check version
V2rayUpdater.checkForUpdatesInBackground()
}

// wake and sleep
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(onWakeNote(note:)), name: NSWorkspace.willSleepNotification, object: nil)
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(onSleepNote(note:)), name: NSWorkspace.didWakeNotification, object: nil)
// url scheme
NSAppleEventManager.shared().setEventHandler(self, andSelector: #selector(self.handleAppleEvent(event:replyEvent:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
}

func checkDefault() {
Expand Down Expand Up @@ -69,8 +73,17 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

let appleEventURL = URL(string: appleEventURLString)
// todo
}

print("Received Apple Event URL: \(appleEventURL)")
@objc func onWakeNote(note: NSNotification) {
if UserDefaults.getBool(forKey: .v2rayTurnOn) {
V2rayLaunch.Start()
}
}

@objc func onSleepNote(note: NSNotification) {
V2rayLaunch.Stop()
}

func applicationWillTerminate(_ aNotification: Notification) {
Expand Down
23 changes: 11 additions & 12 deletions V2rayU/Base.lproj/ConfigWindow.xib
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<window title="V2rayU" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" animationBehavior="default" id="QvC-M9-y7g">
<windowStyleMask key="styleMask" titled="YES" closable="YES" fullSizeContentView="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="196" y="240" width="597" height="566"/>
<rect key="screenRect" x="0.0" y="0.0" width="1440" height="877"/>
<view key="contentView" wantsLayer="YES" id="EiT-Mj-1SZ">
Expand Down Expand Up @@ -175,7 +174,7 @@ Gw
<font key="font" metaFont="system"/>
<tabViewItems>
<tabViewItem label="Import" identifier="Import" id="BS9-X1-ueN">
<view key="view" id="Ab3-ML-cu8">
<view key="view" ambiguous="YES" id="Ab3-ML-cu8">
<rect key="frame" x="10" y="33" width="394" height="491"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
Expand Down Expand Up @@ -267,20 +266,11 @@ Gw
<autoresizingMask key="autoresizingMask"/>
</scroller>
</scrollView>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="gyE-Zy-64R">
<rect key="frame" x="5" y="-5" width="381" height="16"/>
<autoresizingMask key="autoresizingMask"/>
<textFieldCell key="cell" lineBreakMode="clipping" id="wIV-Kw-tAz">
<font key="font" metaFont="smallSystem"/>
<color key="textColor" name="systemRedColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
</subviews>
</view>
</tabViewItem>
<tabViewItem label="Manual" identifier="Manual" id="qq1-yV-VsK">
<view key="view" ambiguous="YES" id="YXI-Uw-7SR">
<view key="view" id="YXI-Uw-7SR">
<rect key="frame" x="10" y="33" width="394" height="491"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
Expand Down Expand Up @@ -1582,6 +1572,15 @@ Gw
<color key="backgroundColor" name="underPageBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="gyE-Zy-64R">
<rect key="frame" x="178" y="1" width="404" height="16"/>
<autoresizingMask key="autoresizingMask"/>
<textFieldCell key="cell" lineBreakMode="clipping" id="wIV-Kw-tAz">
<font key="font" metaFont="smallSystem"/>
<color key="textColor" name="systemRedColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
</subviews>
</view>
<accessibility identifier="ConfigWindow"/>
Expand Down
3 changes: 1 addition & 2 deletions V2rayU/Base.lproj/QrcodeWindow.xib
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<window title="Share QRCode" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" animationBehavior="default" id="QvC-M9-y7g">
<windowStyleMask key="styleMask" titled="YES" closable="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="196" y="240" width="415" height="363"/>
<rect key="contentRect" x="196" y="392" width="415" height="363"/>
<rect key="screenRect" x="0.0" y="0.0" width="1440" height="877"/>
<view key="contentView" wantsLayer="YES" id="EiT-Mj-1SZ">
<rect key="frame" x="0.0" y="0.0" width="415" height="363"/>
Expand Down
19 changes: 16 additions & 3 deletions V2rayU/ConfigWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ class ConfigWindowController: NSWindowController, NSWindowDelegate, NSTabViewDel
func loadJsonData(rowIndex: Int) {
defer {
self.bindDataToView()
// replace current
self.switchToImportView()
}

// reset
Expand Down Expand Up @@ -447,9 +449,18 @@ class ConfigWindowController: NSWindowController, NSWindowDelegate, NSTabViewDel

// save
let errMsg = V2rayServer.save(idx: self.serversTableView.selectedRow, isValid: self.v2rayConfig.isValid, jsonData: text)
self.errTip.stringValue = errMsg

self.refreshServerList(ok: errMsg.count == 0)
if errMsg.count == 0 {
if self.errTip.stringValue == "" {
self.errTip.stringValue = "save success"
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
// your code here
self.errTip.stringValue = ""
}
}
self.refreshServerList(ok: errMsg.count == 0)
} else {
self.errTip.stringValue = errMsg
}
}

func refreshServerList(ok: Bool = true) {
Expand Down Expand Up @@ -482,6 +493,8 @@ class ConfigWindowController: NSWindowController, NSWindowDelegate, NSTabViewDel
@IBAction func setV2rayLogLevel(_ sender: NSPopUpButton) {
if let item = logLevel.selectedItem {
UserDefaults.set(forKey: .v2rayLogLevel, value: item.title)
// replace current
self.switchToImportView()
// restart service
menuController.startV2rayCore()
}
Expand Down
74 changes: 36 additions & 38 deletions V2rayU/MainMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ let preferencesWindowController = PreferencesWindowController(
PreferenceGeneralViewController(),
]
)
var configWindow = ConfigWindowController()
var qrcodeWindow = QrcodeWindowController()

// menu controller
class MenuController: NSObject, NSMenuDelegate {
var closedByConfigWindow: Bool = false
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
var statusItemClicked: (() -> Void)?
var configWindow: ConfigWindowController!

@IBOutlet weak var v2rayRulesMode: NSMenuItem!
@IBOutlet weak var globalMode: NSMenuItem!
Expand Down Expand Up @@ -112,12 +112,12 @@ class MenuController: NSObject, NSMenuDelegate {
NSLog("start v2ray-core begin")

guard let v2ray = V2rayServer.loadSelectedItem() else {
self.notice(title: "start v2ray fail", subtitle: "", informativeText: "v2ray config not found")
noticeTip(title: "start v2ray fail", subtitle: "", informativeText: "v2ray config not found")
return
}

if !v2ray.isValid {
self.notice(title: "start v2ray fail", subtitle: "", informativeText: "invalid v2ray config")
noticeTip(title: "start v2ray fail", subtitle: "", informativeText: "invalid v2ray config")
return
}

Expand Down Expand Up @@ -153,14 +153,6 @@ class MenuController: NSObject, NSMenuDelegate {
NSApplication.shared.terminate(self)
}

@IBAction func generateQRCode(_ sender: NSMenuItem) {
NSLog("GenerateQRCode")
}

@IBAction func scanQRCode(_ sender: NSMenuItem) {
NSLog("ScanQRCode")
}

@IBAction func openPreference(_ sender: NSMenuItem) {
preferencesWindowController.showWindow()
}
Expand Down Expand Up @@ -188,14 +180,21 @@ class MenuController: NSObject, NSMenuDelegate {

// open config window
@IBAction func openConfig(_ sender: NSMenuItem) {
// close before
configWindow.close()
// renew
configWindow = ConfigWindowController()
if configWindow != nil {
// close before
if closedByConfigWindow {
configWindow.close()
// renew
configWindow = ConfigWindowController()
}
} else {
// renew
configWindow = ConfigWindowController()
}

// show window
configWindow.showWindow(self)
// center
configWindow.window?.center()
configWindow.showWindow(nil)
configWindow.window?.makeKeyAndOrderFront(self)
// show dock icon
NSApp.setActivationPolicy(.regular)
// bring to front
Expand Down Expand Up @@ -308,14 +307,14 @@ class MenuController: NSObject, NSMenuDelegate {
@IBAction func generateQrcode(_ sender: NSMenuItem) {
guard let v2ray = V2rayServer.loadSelectedItem() else {
NSLog("v2ray config not found")
self.notice(title: "generate Qrcode fail", subtitle: "", informativeText: "no available servers")
noticeTip(title: "generate Qrcode fail", subtitle: "", informativeText: "no available servers")
return
}

let share = ShareUri()
share.qrcode(item: v2ray)
if share.error.count > 0 {
self.notice(title: "generate Qrcode fail", subtitle: "", informativeText: share.error)
noticeTip(title: "generate Qrcode fail", subtitle: "", informativeText: share.error)
return
}

Expand All @@ -338,28 +337,28 @@ class MenuController: NSObject, NSMenuDelegate {
if uri.count > 0 {
self.importUri(url: uri)
} else {
self.notice(title: "import server fail", subtitle: "", informativeText: "no found qrcode")
noticeTip(title: "import server fail", subtitle: "", informativeText: "no found qrcode")
}
}

@IBAction func ImportFromPasteboard(_ sender: NSMenuItem) {
if let uri = NSPasteboard.general.string(forType: .string), uri.count > 0 {
self.importUri(url: uri)
} else {
self.notice(title: "import server fail", subtitle: "", informativeText: "no found ss:// or vmess:// from Pasteboard")
noticeTip(title: "import server fail", subtitle: "", informativeText: "no found ss:// or vmess:// from Pasteboard")
}
}

func importUri(url: String) {
let uri = url.trimmingCharacters(in: .whitespaces)

if uri.count == 0 {
self.notice(title: "import server fail", subtitle: "", informativeText: "import error: uri not found")
noticeTip(title: "import server fail", subtitle: "", informativeText: "import error: uri not found")
return
}

if URL(string: uri) == nil {
self.notice(title: "import server fail", subtitle: "", informativeText: "no found ss:// or vmess://")
noticeTip(title: "import server fail", subtitle: "", informativeText: "no found ss:// or vmess://")
return
}

Expand All @@ -377,18 +376,7 @@ class MenuController: NSObject, NSMenuDelegate {
return
}

self.notice(title: "import server fail", subtitle: "", informativeText: "no found ss:// or vmess://")
}

func notice(title: String = "", subtitle: String = "", informativeText: String = "") {
// 定义NSUserNotification
let userNotification = NSUserNotification()
userNotification.title = title
userNotification.subtitle = subtitle
userNotification.informativeText = informativeText
// 使用NSUserNotificationCenter发送NSUserNotification
let userNotificationCenter = NSUserNotificationCenter.default
userNotificationCenter.scheduleNotification(userNotification)
noticeTip(title: "import server fail", subtitle: "", informativeText: "no found ss:// or vmess://")
}

func saveServer(importUri: ImportUri) {
Expand All @@ -398,10 +386,20 @@ class MenuController: NSObject, NSMenuDelegate {
// refresh server
self.showServers()

self.notice(title: "import server success", subtitle: "", informativeText: importUri.remark)
noticeTip(title: "import server success", subtitle: "", informativeText: importUri.remark)
} else {
self.notice(title: "import server fail", subtitle: "", informativeText: importUri.error)
noticeTip(title: "import server fail", subtitle: "", informativeText: importUri.error)
}
}

func noticeTip(title: String = "", subtitle: String = "", informativeText: String = "") {
// 定义NSUserNotification
let userNotification = NSUserNotification()
userNotification.title = title
userNotification.subtitle = subtitle
userNotification.informativeText = informativeText
// 使用NSUserNotificationCenter发送NSUserNotification
let userNotificationCenter = NSUserNotificationCenter.default
userNotificationCenter.scheduleNotification(userNotification)
}
}
6 changes: 3 additions & 3 deletions V2rayU/v2ray/V2rayConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class V2rayConfig: NSObject {

func combineManualData() {
// base
self.v2ray.log.loglevel = V2rayLog.logLevel(rawValue: self.logLevel)!
self.v2ray.log.loglevel = V2rayLog.logLevel(rawValue: UserDefaults.get(forKey: .v2rayLogLevel) ?? "info")!
self.v2ray.dns.servers = self.dns.components(separatedBy: ",")
// ------------------------------------- inbound start ---------------------------------------------
var inHttp = V2rayInbound()
Expand Down Expand Up @@ -1126,8 +1126,8 @@ class V2rayConfig: NSObject {
kcpSettings.readBufferSize = steamJson["kcpSettings"]["readBufferSize"].intValue
kcpSettings.writeBufferSize = steamJson["kcpSettings"]["writeBufferSize"].intValue
// "none"
if KcpSettingsHeaderType.firstIndex(of: steamJson["kcpSettings"]["type"].stringValue) != nil {
kcpSettings.header.type = steamJson["kcpSettings"]["type"].stringValue
if KcpSettingsHeaderType.firstIndex(of: steamJson["kcpSettings"]["header"]["type"].stringValue) != nil {
kcpSettings.header.type = steamJson["kcpSettings"]["header"]["type"].stringValue
}
stream.kcpSettings = kcpSettings
}
Expand Down

0 comments on commit 83a0323

Please sign in to comment.