store import url

This commit is contained in:
yuanxue.yang 2018-11-20 18:04:16 +08:00
parent 9603241b8d
commit caf6fefa7c
5 changed files with 80 additions and 33 deletions

View File

@ -244,7 +244,7 @@ Gw
<autoresizingMask key="autoresizingMask"/>
<clipView key="contentView" ambiguous="YES" id="0rS-HW-TeI">
<rect key="frame" x="1" y="1" width="375" height="426"/>
<autoresizingMask key="autoresizingMask"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textView ambiguous="YES" importsGraphics="NO" richText="NO" verticallyResizable="YES" fieldEditor="YES" allowsUndo="YES" usesRuler="YES" smartInsertDelete="YES" id="dNK-7u-GUm">
<rect key="frame" x="0.0" y="0.0" width="379" height="426"/>
@ -592,12 +592,13 @@ Gw
<popUpButton verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Pj0-ZW-RX6">
<rect key="frame" x="73" y="9" width="182" height="25"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<popUpButtonCell key="cell" type="push" title="auto (suggested)" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="borderAndBezel" imageScaling="proportionallyDown" inset="2" selectedItem="hCD-ZD-xad" id="Ikf-53-LUn">
<popUpButtonCell key="cell" type="push" title="auto" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="borderAndBezel" imageScaling="proportionallyDown" inset="2" selectedItem="hCD-ZD-xad" id="Ikf-53-LUn">
<behavior key="behavior" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="menu"/>
<menu key="menu" id="WhF-NB-ypf">
<items>
<menuItem title="auto (suggested)" state="on" id="hCD-ZD-xad">
<menuItem title="auto" state="on" id="hCD-ZD-xad">
<attributedString key="attributedTitle"/>
<modifierMask key="keyEquivalentModifierMask"/>
</menuItem>
<menuItem title="aes-128-cfb" id="KlG-Se-6jg">

View File

@ -197,7 +197,6 @@ class ConfigWindowController: NSWindowController, NSWindowDelegate, NSTabViewDel
self.v2rayConfig = V2rayConfig()
defer {
print("v2rayConfig.serverProtocol", v2rayConfig.serverProtocol, self.v2rayConfig.isValid, self.v2rayConfig.errors)
if self.configText.string.count > 0 && self.v2rayConfig.isValid {
self.bindDataToView()
}
@ -205,6 +204,7 @@ class ConfigWindowController: NSWindowController, NSWindowDelegate, NSTabViewDel
// re parse json
self.v2rayConfig.parseJson(jsonText: self.configText.string)
print("parse errors:", self.v2rayConfig.errors, self.v2rayConfig.isValid)
if self.v2rayConfig.errors.count > 0 {
self.errTip.stringValue = self.v2rayConfig.errors[0]
return
@ -219,6 +219,7 @@ class ConfigWindowController: NSWindowController, NSWindowDelegate, NSTabViewDel
v2rayConfig.checkManualValid()
print("v2rayConfig.isValid", v2rayConfig.isValid)
if v2rayConfig.isValid {
let jsonText = v2rayConfig.combineManual()
self.configText.string = jsonText
@ -407,9 +408,10 @@ class ConfigWindowController: NSWindowController, NSWindowDelegate, NSTabViewDel
return
}
let v2rayItem = V2rayServer.loadV2rayItem(idx: rowIndex)
self.configText.string = v2rayItem?.json ?? ""
self.v2rayConfig.isValid = v2rayItem?.isValid ?? false
let item = V2rayServer.loadV2rayItem(idx: rowIndex)
self.configText.string = item?.json ?? ""
self.v2rayConfig.isValid = item?.isValid ?? false
self.jsonUrl.stringValue = item?.url ?? ""
self.v2rayConfig.parseJson(jsonText: self.configText.string)
if self.v2rayConfig.errors.count > 0 {
@ -479,7 +481,8 @@ class ConfigWindowController: NSWindowController, NSWindowDelegate, NSTabViewDel
func importJson() {
let text = self.configText.string
// edit item remark
V2rayServer.edit(rowIndex: self.serversTableView.selectedRow, url: jsonUrl.stringValue)
// download json file
Alamofire.request(jsonUrl.stringValue).responseString { DataResponse in
if (DataResponse.error != nil) {
@ -656,7 +659,7 @@ extension ConfigWindowController: NSTableViewDataSource {
NSLog("remark is nil")
return
}
// edit item
// edit item remark
V2rayServer.edit(rowIndex: row, remark: remark)
// reload table
tableView.reloadData()

View File

@ -141,7 +141,7 @@ class MenuController: NSObject, NSMenuDelegate {
// switch server
@IBAction func switchServer(_ sender: NSMenuItem) {
guard let obj = sender.representedObject as? v2rayItem else {
guard let obj = sender.representedObject as? V2rayItem else {
NSLog("switchServer err")
return
}

View File

@ -22,7 +22,7 @@ class V2rayServer: NSObject {
}
// v2ray server list
static private var v2rayItemList: [v2rayItem] = []
static private var v2rayItemList: [V2rayItem] = []
// (init) load v2ray server list from UserDefaults
static func loadConfig() {
@ -35,15 +35,15 @@ class V2rayServer: NSObject {
if list == nil {
list = ["default"]
// store default
let model = v2rayItem(name: self.defaultV2rayName, remark: "default", isValid: false)
let model = V2rayItem(name: self.defaultV2rayName, remark: "default", isValid: false)
model.store()
}
// load each v2rayItem
// load each V2rayItem
for item in list! {
guard let v2ray = v2rayItem.load(name: item) else {
guard let v2ray = V2rayItem.load(name: item) else {
// delete from UserDefaults
v2rayItem.remove(name: item)
V2rayItem.remove(name: item)
continue
}
// append
@ -52,7 +52,7 @@ class V2rayServer: NSObject {
}
// get list from v2ray server list
static func list() -> [v2rayItem] {
static func list() -> [V2rayItem] {
return self.v2rayItemList
}
@ -76,6 +76,22 @@ class V2rayServer: NSObject {
v2ray.store()
}
static func edit(rowIndex: Int, url: String) {
if !self.v2rayItemList.indices.contains(rowIndex) {
NSLog("index out of range", rowIndex)
return
}
// update list
self.v2rayItemList[rowIndex].url = url
// save
let v2ray = self.v2rayItemList[rowIndex]
v2ray.url = url
v2ray.store()
}
// move item to new index
static func move(oldIndex: Int, newIndex: Int) {
if !V2rayServer.v2rayItemList.indices.contains(oldIndex) {
@ -110,7 +126,7 @@ class V2rayServer: NSObject {
// name is : config. + current time str
let name = "config." + dateStr
let v2ray = v2rayItem(name: name, remark: "new server", isValid: false)
let v2ray = V2rayItem(name: name, remark: "new server", isValid: false)
// save to v2ray UserDefaults
v2ray.store()
@ -134,7 +150,7 @@ class V2rayServer: NSObject {
self.v2rayItemList.remove(at: idx)
// delete from v2ray UserDefaults
v2rayItem.remove(name: v2ray.name)
V2rayItem.remove(name: v2ray.name)
// update server list UserDefaults
self.saveItemList()
@ -162,7 +178,7 @@ class V2rayServer: NSObject {
}
// load json file data
static func loadV2rayItem(idx: Int) -> v2rayItem? {
static func loadV2rayItem(idx: Int) -> V2rayItem? {
if !V2rayServer.v2rayItemList.indices.contains(idx) {
NSLog("index out of range", idx)
return nil
@ -172,19 +188,19 @@ class V2rayServer: NSObject {
}
// load selected v2ray item
static func loadSelectedItem() -> v2rayItem? {
static func loadSelectedItem() -> V2rayItem? {
var v2ray: v2rayItem? = nil
var v2ray: V2rayItem? = nil
if let curName = UserDefaults.get(forKey: .v2rayCurrentServerName) {
v2ray = v2rayItem.load(name: curName)
v2ray = V2rayItem.load(name: curName)
}
// if default server not fould
if v2ray == nil {
for item in self.v2rayItemList {
if item.isValid {
v2ray = v2rayItem.load(name: item.name)
v2ray = V2rayItem.load(name: item.name)
break
}
}
@ -230,18 +246,20 @@ class V2rayServer: NSObject {
}
// ----- v2ray server item -----
class v2rayItem: NSObject, NSCoding {
class V2rayItem: NSObject, NSCoding {
var name: String
var remark: String
var json: String
var isValid: Bool
var url: String
// init
required init(name: String, remark: String, isValid: Bool, json: String = "") {
required init(name: String, remark: String, isValid: Bool, json: String = "", url: String = "") {
self.name = name
self.remark = remark
self.json = json
self.isValid = isValid
self.url = url
}
// decode
@ -250,6 +268,7 @@ class v2rayItem: NSObject, NSCoding {
self.remark = decoder.decodeObject(forKey: "Remark") as? String ?? ""
self.json = decoder.decodeObject(forKey: "Json") as? String ?? ""
self.isValid = decoder.decodeBool(forKey: "IsValid")
self.url = decoder.decodeObject(forKey: "Url") as? String ?? ""
}
// object encode
@ -258,6 +277,7 @@ class v2rayItem: NSObject, NSCoding {
coder.encode(remark, forKey: "Remark")
coder.encode(json, forKey: "Json")
coder.encode(isValid, forKey: "IsValid")
coder.encode(url, forKey: "Url")
}
// store into UserDefaults
@ -267,11 +287,17 @@ class v2rayItem: NSObject, NSCoding {
}
// static load from UserDefaults
static func load(name: String) -> v2rayItem? {
static func load(name: String) -> V2rayItem? {
guard let myModelData = UserDefaults.standard.data(forKey: name) else {
return nil
}
return NSKeyedUnarchiver.unarchiveObject(with: myModelData) as? v2rayItem
do {
let result = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(myModelData)
return result as? V2rayItem
} catch {
print("load userDefault error:", error)
return nil
}
}
// remove from UserDefaults

View File

@ -168,7 +168,10 @@ class V2rayConfig: NSObject {
let inbounds: [V2rayInbound] = [inSocks, inHttp]
self.v2ray.inbounds = inbounds
}
self.v2ray.inboundDetour = nil
self.v2ray.inbound = nil
} else {
self.v2ray.inbounds = nil
// inbound
var inType: V2rayProtocolInbound = V2rayProtocolInbound.socks
if self.v2ray.inbound != nil {
@ -230,7 +233,10 @@ class V2rayConfig: NSObject {
if self.isEmptyInput {
if self.isNewVersion {
self.v2ray.outbounds = [outbound, outboundFreedom, outboundBlackhole]
self.v2ray.outbound = nil
self.v2ray.outboundDetour = nil
} else {
self.v2ray.outbounds = nil
self.v2ray.outbound = outbound
self.v2ray.outboundDetour = [outboundFreedom, outboundBlackhole]
}
@ -249,7 +255,12 @@ class V2rayConfig: NSObject {
} else {
self.v2ray.outbounds = [outbound, outboundFreedom, outboundBlackhole]
}
self.v2ray.outboundDetour = nil
self.v2ray.outbound = nil
} else {
// if has outbounds
self.v2ray.outbounds = nil
// outbound
if self.v2ray.outbound != nil {
self.v2ray.outbound = self.replaceOutbound(item: self.v2ray.outbound!)
@ -543,10 +554,11 @@ class V2rayConfig: NSObject {
// check outbounds
if json["outbounds"].arrayValue.count > 0 {
// outbounds
var outbounds: [V2rayOutbound] = []
json["outbounds"].arrayValue.forEach { val in
// set into v2ray
self.v2ray.outbounds?.append(self.parseOutbound(jsonParams: val))
outbounds += [self.parseOutbound(jsonParams: val)]
}
self.v2ray.outbounds = outbounds
} else {
self.errors += ["missing outbounds"]
}
@ -560,9 +572,14 @@ class V2rayConfig: NSObject {
}
// outboundDetour
json["outboundDetour"].arrayValue.forEach { val in
// set into v2ray
self.v2ray.outboundDetour?.append(self.parseOutbound(jsonParams: val))
if json["outboundDetour"].arrayValue.count > 0 {
var outboundDetour: [V2rayOutbound] = []
json["outboundDetour"].arrayValue.forEach { val in
outboundDetour += [self.parseOutbound(jsonParams: val)]
}
self.v2ray.outboundDetour = outboundDetour
}
}
// ------------ parse outbound end -------------------------------------------
@ -1105,7 +1122,7 @@ class V2rayConfig: NSObject {
}
// create current v2ray server json file
static func createJsonFile(item: v2rayItem) {
static func createJsonFile(item: V2rayItem) {
let jsonText = item.json
// path: /Application/V2rayU.app/Contents/Resources/config.json