Skip to content

Commit

Permalink
fix pac file
Browse files Browse the repository at this point in the history
  • Loading branch information
yanue committed May 27, 2020
1 parent dd8532d commit ad8bd36
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 100 deletions.
16 changes: 10 additions & 6 deletions V2rayU/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
// default settings
self.checkDefault()

// auto Clear Logs
if UserDefaults.getBool(forKey: .autoClearLog) {
print("ClearLogs")
V2rayLaunch.ClearLogs()
}

// auto launch
if UserDefaults.getBool(forKey: .autoLaunch) {
// Insert code here to initialize your application
Expand All @@ -54,6 +48,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
V2rayUpdater.checkForUpdatesInBackground()
}

_ = GeneratePACFile(rewrite: true)
// start http server for pac
V2rayLaunch.startHttpServer()

Expand Down Expand Up @@ -111,6 +106,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
if UserDefaults.get(forKey: .runMode) == nil {
UserDefaults.set(forKey: .runMode, value: RunMode.pac.rawValue)
}
if UserDefaults.get(forKey: .gfwPacFileContent) == nil {
let gfwlist = try? String(contentsOfFile: GFWListFilePath, encoding: String.Encoding.utf8)
UserDefaults.set(forKey: .gfwPacFileContent, value: gfwlist ?? "")
}
if V2rayServer.count() == 0 {
// add default
V2rayServer.add(remark: "default", json: "", isValid: false)
Expand All @@ -132,6 +131,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {

@objc func onWakeNote(note: NSNotification) {
print("onWakeNote")
// reconnect
if UserDefaults.getBool(forKey: .v2rayTurnOn) {
V2rayLaunch.Stop()
V2rayLaunch.Start()
}
// check v2ray core
V2rayCore().check()
// auto check updates
Expand Down
82 changes: 41 additions & 41 deletions V2rayU/Base.lproj/ConfigWindow.xib

Large diffs are not rendered by default.

6 changes: 0 additions & 6 deletions V2rayU/MainMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,6 @@ class MenuController: NSObject, NSMenuDelegate {
return
}

// pac mode
if runMode == .pac {
// generate pac file
_ = GeneratePACFile(rewrite: false)
}

V2rayLaunch.setSystemProxy(mode: runMode)
}

Expand Down
21 changes: 6 additions & 15 deletions V2rayU/Preference/PreferencePac.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ final class PreferencePacViewController: NSViewController, PreferencePane {
do {
// save user rules into UserDefaults
UserDefaults.set(forKey: .userRules, value: str)

try str.data(using: String.Encoding.utf8)?.write(to: URL(fileURLWithPath: PACUserRuleFilePath), options: .atomic)

UpdatePACFromGFWList(gfwPacListUrl: self.gfwPacListUrl.stringValue)

if GeneratePACFile(rewrite: true) {
Expand Down Expand Up @@ -119,6 +116,7 @@ final class PreferencePacViewController: NSViewController, PreferencePane {

// save to UserDefaults
UserDefaults.set(forKey: .gfwPacListUrl, value: gfwPacListUrl)
UserDefaults.set(forKey: .gfwPacFileContent, value: v)

if GeneratePACFile(rewrite: true) {
// Popup a user notification
Expand Down Expand Up @@ -147,28 +145,21 @@ func GeneratePACFile(rewrite: Bool) -> Bool {
_ = shell(launchPath: "/bin/bash", arguments: ["-c", "cd " + AppResourcesPath + " && /bin/chmod -R 755 ./pac"])

// if PACFilePath exist and not need rewrite
if (!rewrite && FileManager.default.fileExists(atPath: PACFilePath)) {
if (!(rewrite || !FileManager.default.fileExists(atPath: PACFilePath))) {
return true
}

print("GeneratePACFile rewrite", sockPort)

do {
let gfwlist = try String(contentsOfFile: GFWListFilePath, encoding: String.Encoding.utf8)
let gfwlist = UserDefaults.get(forKey: .gfwPacFileContent) ?? ""
if let data = Data(base64Encoded: gfwlist, options: .ignoreUnknownCharacters) {
let str = String(data: data, encoding: String.Encoding.utf8)
var lines = str!.components(separatedBy: CharacterSet.newlines)

// read userRules from UserDefaults
let userRules = UserDefaults.get(forKey: .userRules)
if userRules != nil {
try userRules!.data(using: String.Encoding.utf8)?.write(to: URL(fileURLWithPath: PACUserRuleFilePath), options: .atomic)
}

do {
let userRuleStr = try String(contentsOfFile: PACUserRuleFilePath, encoding: String.Encoding.utf8)
let userRuleLines = userRuleStr.components(separatedBy: CharacterSet.newlines)

// read userRules from UserDefaults
let userRules = UserDefaults.get(forKey: .userRules) ?? ""
let userRuleLines = userRules.components(separatedBy: CharacterSet.newlines)
lines = userRuleLines + lines
} catch {
NSLog("Not found user-rule.txt")
Expand Down
2 changes: 2 additions & 0 deletions V2rayU/Util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ extension UserDefaults {
case runMode
// use rules
case userRules
// gfw pac file content
case gfwPacFileContent
// gfw pac list url
case gfwPacListUrl

Expand Down
18 changes: 1 addition & 17 deletions V2rayU/V2rayLaunch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ let LAUNCH_HTTP_PLIST = "yanue.v2rayu.http.plist" // simple http server
let logFilePath = NSHomeDirectory() + "/Library/Logs/v2ray-core.log"
let launchAgentDirPath = NSHomeDirectory() + LAUNCH_AGENT_DIR
let launchAgentPlistFile = launchAgentDirPath + LAUNCH_AGENT_PLIST
let launchHttpPlistFile = launchAgentDirPath + LAUNCH_HTTP_PLIST
let AppResourcesPath = Bundle.main.bundlePath + "/Contents/Resources"
let v2rayCorePath = AppResourcesPath + "/v2ray-core"
let v2rayCoreFile = v2rayCorePath + "/v2ray"
Expand Down Expand Up @@ -59,14 +58,6 @@ class V2rayLaunch: NSObject {

dictAgent.write(toFile: launchAgentPlistFile, atomically: true)

// if old launchHttpPlistFile exist
if fileMgr.fileExists(atPath: launchHttpPlistFile) {
print("launchHttpPlistFile exist", launchHttpPlistFile)
_ = shell(launchPath: "/bin/launchctl", arguments: ["unload", launchHttpPlistFile])
_ = shell(launchPath: "/bin/launchctl", arguments: ["remove", "yanue.v2rayu.http.plist"])
try! fileMgr.removeItem(atPath: launchHttpPlistFile)
}

// permission
_ = shell(launchPath: "/bin/bash", arguments: ["-c", "cd " + AppResourcesPath + " && /bin/chmod -R 755 ."])
}
Expand All @@ -76,11 +67,8 @@ class V2rayLaunch: NSObject {
// ~/LaunchAgents/yanue.v2rayu.v2ray-core.plist
_ = shell(launchPath: "/bin/bash", arguments: ["-c", "cd " + AppResourcesPath + " && /bin/chmod -R 755 ./v2ray-core"])

self.startHttpServer()

// unload first
_ = shell(launchPath: "/bin/launchctl", arguments: ["remove", "yanue.v2rayu.v2ray-core"])
_ = shell(launchPath: "/bin/launchctl", arguments: ["remove", "yanue.v2rayu.http.plist"])
_ = shell(launchPath: "/bin/launchctl", arguments: ["unload", launchAgentPlistFile])

let task = Process.launchedProcess(launchPath: "/bin/launchctl", arguments: ["load", "-wF", launchAgentPlistFile])
Expand All @@ -93,7 +81,6 @@ class V2rayLaunch: NSObject {
}

static func Stop() {
_ = shell(launchPath: "/bin/launchctl", arguments: ["unload", launchHttpPlistFile])
_ = shell(launchPath: "/bin/launchctl", arguments: ["remove", "yanue.v2rayu.v2ray-core"])
_ = shell(launchPath: "/bin/launchctl", arguments: ["remove", "yanue.v2rayu.http.plist"])

Expand Down Expand Up @@ -222,17 +209,14 @@ class V2rayLaunch: NSObject {
return false
}

// restart pac http server
startHttpServer()

return true
}

static func checkPort(host: String, port: String, tip: String) -> Bool {
// shell("/bin/bash",["-c","cd ~ && ls -la"])
let cmd = "cd " + AppResourcesPath + " && chmod +x ./V2rayUHelper && ./V2rayUHelper -cmd port -h " + host + " -p " + port
let res = shell(launchPath: "/bin/bash", arguments: ["-c", cmd])

NSLog("checkPort: res=(\(String(describing: res))) cmd=(\(cmd))")

if res != "ok" {
Expand Down
33 changes: 18 additions & 15 deletions V2rayU/ping.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ class PingSpeed: NSObject {
}
}
menuController.statusMenu.item(withTag: 1)?.title = pingTip


// Ping.ping("www.baidu.com", withTimeout: 5.0, completionBlock: { (timeElapsed:Double?, error:Error?) in
// print("Ping.ping completionBlock")
// if let latency = timeElapsed {
Expand All @@ -67,7 +67,7 @@ class PingSpeed: NSObject {
//
// let p = Pinger.init(host: "www.baidu.com", callback: self.call(_:_:_:))
// p.start()

// let queue = DispatchQueue.global()
let queue = DispatchQueue(label: "pinger")

Expand All @@ -76,19 +76,19 @@ class PingSpeed: NSObject {
// self.writeServerToFile()
// self.pingInCmd()
// self.parsePingResult()

let itemList = V2rayServer.list()
if itemList.count == 0 {
return
}

for item in itemList {
if !item.isValid {
continue
}
self.pingEachServer(item:item)
self.pingEachServer(item: item)
}

DispatchQueue.main.async {
menuController.statusMenu.item(withTag: 1)?.title = "\(normalTitle)"

Expand All @@ -101,10 +101,13 @@ class PingSpeed: NSObject {
if V2rayServer.getIndex(name: fastV2rayName) > -1 {
// set current
UserDefaults.set(forKey: .v2rayCurrentServerName, value: fastV2rayName)
// stop first
V2rayLaunch.Stop()
// start
menuController.startV2rayCore()
// if not stop status
if UserDefaults.getBool(forKey: .v2rayTurnOn) {
// stop first
V2rayLaunch.Stop()
// start
menuController.startV2rayCore()
}
}
}

Expand All @@ -122,20 +125,20 @@ class PingSpeed: NSObject {
func pingEachServer(item: V2rayItem) {
let host = self.parseHost(item: item)
guard let _ = NSURL(string: host) else {
print("not host",host)
print("not host", host)
return
}

// Ping once
let once = SwiftyPing(host: host, configuration: PingConfiguration(interval: 0.5, with: 5), queue: DispatchQueue.global())
once?.observer = { (_, response) in
let duration = response.duration
if response.error != nil {
print("ping error",host,response.error as Any)
print("ping error", host, response.error as Any)
} else {

}
item.speed = String(format: "%.2f", duration*1000)+"ms"
item.speed = String(format: "%.2f", duration * 1000) + "ms"
item.store()
// refresh server
menuController.showServers()
Expand Down

0 comments on commit ad8bd36

Please sign in to comment.