Feature: Add Import bunch json file from ss-ng

This commit is contained in:
yicheng 2018-08-05 23:16:58 +08:00
parent 4e0c96bc52
commit f2f3eb47e2
3 changed files with 60 additions and 1 deletions

View File

@ -220,6 +220,9 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
}
@IBAction func actionImportBunchJsonFile(_ sender: NSMenuItem) {
ConfigFileFactory.importConfigFile()
}
@IBAction func actionSwitchProxyMode(_ sender: NSMenuItem) {
let mode:ClashProxyMode
switch sender {

View File

@ -104,8 +104,11 @@
<action selector="actionUpdateConfig:" target="Voe-Tx-rLC" id="oW5-Ll-SY8"/>
</connections>
</menuItem>
<menuItem title="Import config" id="GRE-Ra-Pkx">
<menuItem title="Import bunch json file" id="GRE-Ra-Pkx">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="actionImportBunchJsonFile:" target="Voe-Tx-rLC" id="wPl-iW-WvG"/>
</connections>
</menuItem>
</items>
</menu>

View File

@ -6,6 +6,7 @@
// Copyright © 2018 west2online. All rights reserved.
//
import Foundation
import AppKit
class ConfigFileFactory {
static func configFile(proxies:[ProxyServerModel]) -> String {
@ -42,5 +43,57 @@ class ConfigFileFactory {
try? FileManager.default.removeItem(at: URL(fileURLWithPath: path))
}
try? str.write(to: URL(fileURLWithPath: path), atomically: true, encoding: .utf8)
NotificationCenter.default.post(Notification(name:kShouldUpDateConfig))
}
static func importConfigFile() {
let openPanel = NSOpenPanel()
openPanel.title = "Choose Config Json File"
openPanel.allowsMultipleSelection = false
openPanel.canChooseDirectories = false
openPanel.canCreateDirectories = false
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
profiles.append(profile)
}
let configStr = self.configFile(proxies: profiles)
self.saveToClashConfigFile(str: configStr)
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
}
}
}
}