Skip to content

Commit

Permalink
check port before turn v2ray-core on
Browse files Browse the repository at this point in the history
  • Loading branch information
yanue committed Mar 1, 2020
1 parent ca9cb03 commit 8577c41
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 8 deletions.
Binary file added Build/V2rayUPort
Binary file not shown.
6 changes: 6 additions & 0 deletions V2rayU.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
66973EB721797719001FEA1E /* ServiceManagement.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 66973EB621797719001FEA1E /* ServiceManagement.framework */; };
66ACB1A021757D5B005B5881 /* MainMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66ACB19F21757D5B005B5881 /* MainMenu.swift */; };
66BC2B89228C589E00FBB716 /* V2raySubscribe.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66BC2B88228C589E00FBB716 /* V2raySubscribe.swift */; };
66C5B199240C1EF7008A22CA /* V2rayUPort in CopyFiles */ = {isa = PBXBuildFile; fileRef = 66C5B198240C1EEE008A22CA /* V2rayUPort */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
66F07CF9236D79540088A4AE /* ping.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66F07CF8236D79540088A4AE /* ping.swift */; };
66F302A522AAA1D400FCA4E2 /* V2rayUTool in CopyFiles */ = {isa = PBXBuildFile; fileRef = 66F3029922AA9C0900FCA4E2 /* V2rayUTool */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; };
66F411B121CA49EE007AAC10 /* pac in Copy Files */ = {isa = PBXBuildFile; fileRef = 6683B1EA21C2AD1A004A1C5F /* pac */; };
Expand Down Expand Up @@ -87,6 +88,7 @@
dstPath = "";
dstSubfolderSpec = 7;
files = (
66C5B199240C1EF7008A22CA /* V2rayUPort in CopyFiles */,
66F302A522AAA1D400FCA4E2 /* V2rayUTool in CopyFiles */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -176,6 +178,8 @@
66ACB19F21757D5B005B5881 /* MainMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainMenu.swift; sourceTree = "<group>"; };
66BC2B88228C589E00FBB716 /* V2raySubscribe.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = V2raySubscribe.swift; sourceTree = "<group>"; };
66C5B194240B58B8008A22CA /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = ../en.lproj/PreferenceAdvance.strings; sourceTree = "<group>"; };
66C5B197240C17F2008A22CA /* main.go */ = {isa = PBXFileReference; lastKnownFileType = text; path = main.go; sourceTree = "<group>"; };
66C5B198240C1EEE008A22CA /* V2rayUPort */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.executable"; path = V2rayUPort; sourceTree = "<group>"; };
66F07CF8236D79540088A4AE /* ping.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ping.swift; sourceTree = "<group>"; };
66F3029922AA9C0900FCA4E2 /* V2rayUTool */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.executable"; path = V2rayUTool; sourceTree = "<group>"; };
66F3029C22AAA0A600FCA4E2 /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/MainMenu.strings"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -284,6 +288,7 @@
isa = PBXGroup;
children = (
6646669221CBC0860094F0B7 /* main.swift */,
66C5B197240C17F2008A22CA /* main.go */,
);
path = V2rayUTool;
sourceTree = "<group>";
Expand Down Expand Up @@ -352,6 +357,7 @@
66FEAD44217D75D7009DECF9 /* Build */ = {
isa = PBXGroup;
children = (
66C5B198240C1EEE008A22CA /* V2rayUPort */,
66F3029922AA9C0900FCA4E2 /* V2rayUTool */,
664FC05321A70C4300048FE3 /* build.plist */,
66FEAD45217D75FC009DECF9 /* release.sh */,
Expand Down
3 changes: 1 addition & 2 deletions V2rayU/MainMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,9 @@ class MenuController: NSObject, NSMenuDelegate {

// start v2ray core
func startV2rayCore() {
self.setStatusOff()

NSLog("start v2ray-core begin")
if !V2rayLaunch.checkPorts() {
setStatusOff()
return
}

Expand Down
37 changes: 31 additions & 6 deletions V2rayU/V2rayLaunch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,15 @@ class V2rayLaunch: NSObject {
}

static func checkPorts() -> Bool {
// stop old v2ray process
self.Stop()
// stop pac server
webServer.stop()

let localSockPort = UserDefaults.get(forKey: .localSockPort) ?? "1080"
let localSockHost = UserDefaults.get(forKey: .localSockHost) ?? "127.0.0.1"
let localHttpPort = UserDefaults.get(forKey: .localHttpPort) ?? "1087"
let localHttpHost = UserDefaults.get(forKey: .localHttpHost) ?? "127.0.0.1"
let localPacPort = UserDefaults.get(forKey: .localPacPort) ?? "11085"

// check same port
Expand All @@ -203,13 +210,31 @@ class V2rayLaunch: NSObject {
}

// check port is used
print("UInt16(localSockPort)", UInt16(localSockPort) ?? 0)
// let (res, err) = self.checkTcpPort(port: UInt16(localSockPort) ?? 0)
// if !res {
// makeToast(message: "the sock port (" + localSockPort + ") has being used: (" + err + ")", displayDuration: 3)
// return false
// }
if !self.checkPort(host: localSockHost, port: localSockPort, tip: "socks") {
return false
}

if !self.checkPort(host: localHttpHost, port: localHttpPort, tip: "http") {
return false
}

if !self.checkPort(host: "0.0.0.0", port: localPacPort, tip: "pac") {
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 res = shell(launchPath: "/bin/bash", arguments: ["-c", "cd " + AppResourcesPath + " && ./V2rayUPort -h " + host + " -p " + port])
if res != "ok" {
makeToast(message: tip + " error: " + (res ?? ""), displayDuration: 5)
return false
}
return true
}

Expand Down
39 changes: 39 additions & 0 deletions V2rayUTool/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Created by GoLand.
* User: yanue
* Date: 2019/2/28
* Time: 17:34
*/

package main

import (
"flag"
"fmt"
"net"
)

var host = ""
var port = ""

func init() {
flag.StringVar(&host, "h", "", "-h host")
flag.StringVar(&port, "p", "", "-p port")
flag.Parse()
}

const help = "V2rayUPort - check port is free \nUsage: \n V2rayUPort -h host -p port"

func main() {
if len(host) == 0 || len(port) == 0 {
fmt.Println(help)
return
}

_, err := net.Listen("tcp", host+":"+port)
if err != nil {
fmt.Println("err:", err.Error())
return
}
fmt.Println("ok")
}

0 comments on commit 8577c41

Please sign in to comment.