Fixed: Config json import and add notification

This commit is contained in:
yicheng 2018-08-06 14:17:04 +08:00
parent 9f6388dbb3
commit 27b6242cca
5 changed files with 85 additions and 36 deletions

View File

@ -22,6 +22,7 @@
495A44BF20D2660A00888A0A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 495A44BE20D2660A00888A0A /* AppDelegate.swift */; };
495A44CD20D266BA00888A0A /* ClashXLaunchHelper.app in CopyFiles */ = {isa = PBXBuildFile; fileRef = 495A44BC20D2660A00888A0A /* ClashXLaunchHelper.app */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
495A44D320D267D000888A0A /* LaunchAtLogin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 495A44D220D267D000888A0A /* LaunchAtLogin.swift */; };
4966E9E32118153A00A391FB /* NSUserNotificationCenter+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4966E9E22118153A00A391FB /* NSUserNotificationCenter+Extension.swift */; };
497F0DF320DE2FE50077AD41 /* Icon.icns in Resources */ = {isa = PBXBuildFile; fileRef = 497F0DF220DE2FE50077AD41 /* Icon.icns */; };
4989F98020D01C8F0001E564 /* clash.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 4989F97E20D01C8F0001E564 /* clash.a */; };
4989F98420D02D200001E564 /* Country.mmdb in Resources */ = {isa = PBXBuildFile; fileRef = 4989F98320D02D200001E564 /* Country.mmdb */; };
@ -90,6 +91,7 @@
495A44C720D2660B00888A0A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
495A44C820D2660B00888A0A /* ClashXLaunchHelper.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = ClashXLaunchHelper.entitlements; sourceTree = "<group>"; };
495A44D220D267D000888A0A /* LaunchAtLogin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LaunchAtLogin.swift; sourceTree = "<group>"; };
4966E9E22118153A00A391FB /* NSUserNotificationCenter+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSUserNotificationCenter+Extension.swift"; sourceTree = "<group>"; };
497F0DF220DE2FE50077AD41 /* Icon.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; path = Icon.icns; sourceTree = "<group>"; };
4989F97E20D01C8F0001E564 /* clash.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = clash.a; sourceTree = "<group>"; };
4989F97F20D01C8F0001E564 /* clash.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = clash.h; sourceTree = "<group>"; };
@ -195,6 +197,7 @@
4997732320D251A60009B136 /* SWBApplication.m */,
4997732420D251A60009B136 /* SWBApplication.h */,
495A44D220D267D000888A0A /* LaunchAtLogin.swift */,
4966E9E22118153A00A391FB /* NSUserNotificationCenter+Extension.swift */,
);
path = Basic;
sourceTree = "<group>";
@ -545,6 +548,7 @@
492C4869210EE6B9004554A0 /* ApiRequest.swift in Sources */,
49CF3B6520CEE06C001EBF94 /* ConfigManager.swift in Sources */,
4952C3BF2115C7CA004A4FA8 /* ProxyMenuItemFactory.swift in Sources */,
4966E9E32118153A00A391FB /* NSUserNotificationCenter+Extension.swift in Sources */,
4952C3CE2116EA2E004A4FA8 /* ProxyServerModel.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;

View File

@ -214,9 +214,17 @@ class AppDelegate: NSObject, NSApplicationDelegate {
@IBAction func actionUpdateConfig(_ sender: Any) {
ApiRequest.requestConfigUpdate() { [unowned self] success in
self.syncConfig(){
self.resetTrafficMonitor()
if (success) {
self.syncConfig(){self.resetTrafficMonitor()}
NSUserNotificationCenter
.default
.post(title: "Reload Config Succeed", info: "succees")
} else {
NSUserNotificationCenter
.default
.post(title: "Reload Config Fail", info: "Please Check Config Fils")
}
}
}

View File

@ -0,0 +1,18 @@
//
// NSUserNotificationCenter+Extension.swift
// ClashX
//
// Created by CYC on 2018/8/6.
// Copyright © 2018 west2online. All rights reserved.
//
import Cocoa
extension NSUserNotificationCenter {
func post(title:String,info:String) {
let notification = NSUserNotification()
notification.title = title
notification.informativeText = info
self.deliver(notification)
}
}

View File

@ -7,6 +7,7 @@
//
import Foundation
import AppKit
import SwiftyJSON
class ConfigFileFactory {
static func configFile(proxies:[ProxyServerModel]) -> String {
@ -26,6 +27,8 @@ class ConfigFileFactory {
let autoGroupStr = "ProxyAuto = url-test, \(proxyGroupStr), http://www.google.com/generate_204, 300"
sampleConfigStr = sampleConfigStr?.replacingOccurrences(of: "{{ProxyAutoGroupPlaceHolder}}", with: autoGroupStr)
proxyGroupStr.append("ProxyAuto,")
} else {
sampleConfigStr = sampleConfigStr?.replacingOccurrences(of: "{{ProxyAutoGroupPlaceHolder}}", with: "")
}
proxyGroupStr = String(proxyGroupStr.dropLast())
@ -56,45 +59,60 @@ class ConfigFileFactory {
openPanel.canChooseFiles = true
openPanel.becomeKey()
let result = openPanel.runModal()
if (result.rawValue == NSFileHandlingPanelOKButton && (openPanel.url) != nil) {
let fileManager = FileManager.default
let filePath:String = (openPanel.url?.path)!
var profiles = [ProxyServerModel]()
if (fileManager.fileExists(atPath: filePath) && filePath.hasSuffix("json")) {
let data = fileManager.contents(atPath: filePath)
let readString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)!
let readStringData = readString.data(using: String.Encoding.utf8.rawValue)
let jsonArr1 = try! JSONSerialization.jsonObject(with: readStringData!, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
for item in jsonArr1.object(forKey: "configs") as! [[String: AnyObject]]{
let profile = ProxyServerModel()
profile.serverHost = item["server"] as! String
profile.serverPort = item["server_port"] as! String
profile.method = item["method"] as! String
profile.password = item["password"] as! String
profile.remark = item["remarks"] as! String
if (profile.isValid()) {
profiles.append(profile)
guard (result.rawValue == NSFileHandlingPanelOKButton && (openPanel.url) != nil) else {
NSUserNotificationCenter.default
.post(title: "Import Server Profile failed!",
info: "Invalid config file!")
return
}
let fileManager = FileManager.default
let filePath:String = (openPanel.url?.path)!
var profiles = [ProxyServerModel]()
if fileManager.fileExists(atPath: filePath) &&
filePath.hasSuffix("json") {
if let data = fileManager.contents(atPath: filePath),
let json = try? JSON(data: data) {
let remarkSet = Set<String>()
for item in json["configs"].arrayValue{
if let host = item["server"].string,
let method = item["method"].string,
let password = item["password"].string{
let profile = ProxyServerModel()
profile.serverHost = host
profile.serverPort = String(item["server_port"].intValue)
profile.method = method
profile.password = password
profile.remark = item["remarks"].stringValue
if remarkSet.contains(profile.remark) {
profile.remark.append("Dup")
}
if (profile.isValid()) {
profiles.append(profile)
}
}
}
let configStr = self.configFile(proxies: profiles)
self.saveToClashConfigFile(str: configStr)
if (profiles.count > 0) {
let configStr = self.configFile(proxies: profiles)
self.saveToClashConfigFile(str: configStr)
NSUserNotificationCenter
.default
.post(title: "Import Server Profile succeed!",
info: "Successful import \(profiles.count) items")
} else {
NSUserNotificationCenter
.default
.post(title: "Import Server Profile Fail!",
info: "No proxies are imported")
}
let notification = NSUserNotification()
notification.title = "Import Server Profile succeed!"
notification.informativeText = "Successful import \(profiles.count) items"
NSUserNotificationCenter.default
.deliver(notification)
}else{
let notification = NSUserNotification()
notification.title = "Import Server Profile failed!"
notification.informativeText = "Invalid config file!"
NSUserNotificationCenter.default
.deliver(notification)
return
}
}
}
}

View File

@ -36,6 +36,7 @@ class ProxyServerModel: NSObject, Codable {
func isValid() -> Bool {
let whitespace = NSCharacterSet.whitespacesAndNewlines
remark = remark.components(separatedBy: whitespace).joined()
if remark == "" {remark = "NewProxy"}
func validateIpAddress(_ ipToValidate: String) -> Bool {