fix port check alert

# Conflicts:
#	ClashX/goClash/main.go
This commit is contained in:
yicheng 2020-04-28 12:09:09 +08:00
parent 5349a279ce
commit 3e6c937008
4 changed files with 50 additions and 58 deletions

View File

@ -44,6 +44,7 @@
499A486522EEA3FD00F6C675 /* Array+Safe.swift in Sources */ = {isa = PBXBuildFile; fileRef = 499A486422EEA3FC00F6C675 /* Array+Safe.swift */; }; 499A486522EEA3FD00F6C675 /* Array+Safe.swift in Sources */ = {isa = PBXBuildFile; fileRef = 499A486422EEA3FC00F6C675 /* Array+Safe.swift */; };
49ABB749236B0F9E00535CD7 /* UnsafePointer+bridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49ABB748236B0F9E00535CD7 /* UnsafePointer+bridge.swift */; }; 49ABB749236B0F9E00535CD7 /* UnsafePointer+bridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49ABB748236B0F9E00535CD7 /* UnsafePointer+bridge.swift */; };
49B1086A216A356D0064FFCE /* String+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49B10869216A356D0064FFCE /* String+Extension.swift */; }; 49B1086A216A356D0064FFCE /* String+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49B10869216A356D0064FFCE /* String+Extension.swift */; };
49B445162457CDF000B27E3E /* ClashStatusTool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49B445152457CDF000B27E3E /* ClashStatusTool.swift */; };
49B4575D244F4A2A00463C39 /* PrivilegedHelperManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49B4575C244F4A2A00463C39 /* PrivilegedHelperManager.swift */; }; 49B4575D244F4A2A00463C39 /* PrivilegedHelperManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49B4575C244F4A2A00463C39 /* PrivilegedHelperManager.swift */; };
49B4575F244FD4D100463C39 /* PrivilegedHelperManager+Legacy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49B4575E244FD4D100463C39 /* PrivilegedHelperManager+Legacy.swift */; }; 49B4575F244FD4D100463C39 /* PrivilegedHelperManager+Legacy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49B4575E244FD4D100463C39 /* PrivilegedHelperManager+Legacy.swift */; };
49BC061C212931F4005A0FE7 /* AboutViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49BC061B212931F4005A0FE7 /* AboutViewController.swift */; }; 49BC061C212931F4005A0FE7 /* AboutViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49BC061B212931F4005A0FE7 /* AboutViewController.swift */; };
@ -155,6 +156,7 @@
499A486422EEA3FC00F6C675 /* Array+Safe.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Array+Safe.swift"; sourceTree = "<group>"; }; 499A486422EEA3FC00F6C675 /* Array+Safe.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Array+Safe.swift"; sourceTree = "<group>"; };
49ABB748236B0F9E00535CD7 /* UnsafePointer+bridge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UnsafePointer+bridge.swift"; sourceTree = "<group>"; }; 49ABB748236B0F9E00535CD7 /* UnsafePointer+bridge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UnsafePointer+bridge.swift"; sourceTree = "<group>"; };
49B10869216A356D0064FFCE /* String+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Extension.swift"; sourceTree = "<group>"; }; 49B10869216A356D0064FFCE /* String+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Extension.swift"; sourceTree = "<group>"; };
49B445152457CDF000B27E3E /* ClashStatusTool.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClashStatusTool.swift; sourceTree = "<group>"; };
49B4575C244F4A2A00463C39 /* PrivilegedHelperManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrivilegedHelperManager.swift; sourceTree = "<group>"; }; 49B4575C244F4A2A00463C39 /* PrivilegedHelperManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrivilegedHelperManager.swift; sourceTree = "<group>"; };
49B4575E244FD4D100463C39 /* PrivilegedHelperManager+Legacy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PrivilegedHelperManager+Legacy.swift"; sourceTree = "<group>"; }; 49B4575E244FD4D100463C39 /* PrivilegedHelperManager+Legacy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "PrivilegedHelperManager+Legacy.swift"; sourceTree = "<group>"; };
49BC061B212931F4005A0FE7 /* AboutViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutViewController.swift; sourceTree = "<group>"; }; 49BC061B212931F4005A0FE7 /* AboutViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutViewController.swift; sourceTree = "<group>"; };
@ -254,6 +256,7 @@
4960A6DA2136529200B940C9 /* JSBridgeHandler.swift */, 4960A6DA2136529200B940C9 /* JSBridgeHandler.swift */,
493AEAE2221AE3420016FE98 /* AppVersionUtil.swift */, 493AEAE2221AE3420016FE98 /* AppVersionUtil.swift */,
49D176A62355FE680093DD7B /* NetworkChangeNotifier.swift */, 49D176A62355FE680093DD7B /* NetworkChangeNotifier.swift */,
49B445152457CDF000B27E3E /* ClashStatusTool.swift */,
); );
path = Utils; path = Utils;
sourceTree = "<group>"; sourceTree = "<group>";
@ -704,6 +707,7 @@
49862FA0218418C600A1D5EC /* ClashRule.swift in Sources */, 49862FA0218418C600A1D5EC /* ClashRule.swift in Sources */,
49C9EF64223E78F5005D8B6A /* ClashProxy.swift in Sources */, 49C9EF64223E78F5005D8B6A /* ClashProxy.swift in Sources */,
F9E8F34623A12B89002DE5E8 /* String+Encode.swift in Sources */, F9E8F34623A12B89002DE5E8 /* String+Encode.swift in Sources */,
49B445162457CDF000B27E3E /* ClashStatusTool.swift in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };

View File

@ -115,7 +115,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply { func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
let group = DispatchGroup() let group = DispatchGroup()
var shouldWait = false var shouldWait = false
if ConfigManager.shared.proxyPortAutoSet && !ConfigManager.shared.isProxySetByOtherVariable.value { if ConfigManager.shared.proxyPortAutoSet && !ConfigManager.shared.isProxySetByOtherVariable.value {
Logger.log("ClashX quit need clean proxy setting") Logger.log("ClashX quit need clean proxy setting")
shouldWait = true shouldWait = true
@ -132,7 +132,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
return .terminateNow return .terminateNow
} }
statusItem.menu = nil if statusItem != nil, statusItem.menu != nil {
statusItem.menu = nil
}
disposeBag = DisposeBag()
DispatchQueue.global(qos: .default).async { DispatchQueue.global(qos: .default).async {
let res = group.wait(timeout: .now() + 5) let res = group.wait(timeout: .now() + 5)
@ -142,9 +145,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
case .timedOut: case .timedOut:
Logger.log("ClashX quit after clean up timeout") Logger.log("ClashX quit after clean up timeout")
} }
DispatchQueue.main.async { NSApp.reply(toApplicationShouldTerminate: true)
NSApp.reply(toApplicationShouldTerminate: true)
}
} }
Logger.log("ClashX quit wait for clean up") Logger.log("ClashX quit wait for clean up")
@ -232,9 +233,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
self.apiPortMenuItem.title = "Api Port: \(ConfigManager.shared.apiPort)" self.apiPortMenuItem.title = "Api Port: \(ConfigManager.shared.apiPort)"
self.ipMenuItem.title = "IP: \(NetworkChangeNotifier.getPrimaryIPAddress() ?? "")" self.ipMenuItem.title = "IP: \(NetworkChangeNotifier.getPrimaryIPAddress() ?? "")"
if config.port == 0 || config.socketPort == 0 { ClashStatusTool.checkPortConfig(cfg: config)
self.showClashPortErrorAlert()
}
}.disposed(by: disposeBag) }.disposed(by: disposeBag)
} }
@ -811,13 +810,3 @@ extension AppDelegate {
} }
} }
} }
// MARK: - Alerts
extension AppDelegate {
func showClashPortErrorAlert() {
let alert = NSAlert()
alert.messageText = NSLocalizedString("ClashX Start Error!", comment: "")
alert.informativeText = NSLocalizedString("Ports Open Fail, Please try to restart ClashX", comment: "")
}
}

View File

@ -0,0 +1,28 @@
//
// ClashStatusTool.swift
// ClashX Pro
//
// Created by yicheng on 2020/4/28.
// Copyright © 2020 west2online. All rights reserved.
//
import Cocoa
class ClashStatusTool {
static func checkPortConfig(cfg: ClashConfig?) {
guard let cfg = cfg else { return }
Logger.log("checkPortConfig: \(cfg.port) \(cfg.socketPort)", level: .debug)
if cfg.port == 0 || cfg.socketPort == 0 {
let alert = NSAlert()
alert.messageText = NSLocalizedString("ClashX Start Error!", comment: "")
alert.informativeText = NSLocalizedString("Ports Open Fail, Please try to restart ClashX", comment: "")
alert.addButton(withTitle: NSLocalizedString("Quit", comment: ""))
alert.addButton(withTitle: "Edit Config")
let ret = alert.runModal()
if ret == .alertSecondButtonReturn {
NSWorkspace.shared.openFile(Paths.configPath(for: "config"))
}
NSApp.terminate(nil)
}
}
}

View File

@ -1,7 +1,13 @@
package main package main
import ( import (
"C" "C"
"encoding/json" "encoding/json"
"net"
"path/filepath"
"strconv"
"strings"
"github.com/Dreamacro/clash/config" "github.com/Dreamacro/clash/config"
"github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/hub/executor" "github.com/Dreamacro/clash/hub/executor"
@ -9,10 +15,6 @@ import (
"github.com/Dreamacro/clash/log" "github.com/Dreamacro/clash/log"
"github.com/oschwald/geoip2-golang" "github.com/oschwald/geoip2-golang"
"github.com/phayes/freeport" "github.com/phayes/freeport"
"net"
"path/filepath"
"strconv"
"strings"
) )
func isAddrValid(addr string) bool { func isAddrValid(addr string) bool {
@ -20,7 +22,7 @@ func isAddrValid(addr string) bool {
comps := strings.Split(addr, ":") comps := strings.Split(addr, ":")
v := comps[len(comps)-1] v := comps[len(comps)-1]
if port, err := strconv.Atoi(v); err == nil { if port, err := strconv.Atoi(v); err == nil {
if port > 0 && port < 65535 && checkPortAvailable(port, false) { if port > 0 && port < 65535 {
return true return true
} }
} }
@ -28,24 +30,6 @@ func isAddrValid(addr string) bool {
return false return false
} }
func checkPortAvailable(port int, lan bool) bool {
var addr string
if port < 1 || port > 65534 {
return false
}
if lan {
addr = ":"
} else {
addr = "127.0.0.1:"
}
l, err := net.Listen("tcp", addr+strconv.Itoa(port))
if err != nil {
return false
}
_ = l.Close()
return true
}
//export initClashCore //export initClashCore
func initClashCore() { func initClashCore() {
configFile := filepath.Join(constant.Path.HomeDir(), constant.Path.Config()) configFile := filepath.Join(constant.Path.HomeDir(), constant.Path.Config())
@ -66,19 +50,6 @@ func parseConfig(checkPort bool) (*config.Config, error) {
cfg.General.ExternalController = "127.0.0.1:" + strconv.Itoa(port) cfg.General.ExternalController = "127.0.0.1:" + strconv.Itoa(port)
cfg.General.Secret = "" cfg.General.Secret = ""
} }
lan := cfg.General.AllowLan
if !checkPortAvailable(cfg.General.Port, lan) {
if port, err := freeport.GetFreePort(); err == nil {
cfg.General.Port = port
}
}
if !checkPortAvailable(cfg.General.SocksPort, lan) {
if port, err := freeport.GetFreePort(); err == nil {
cfg.General.SocksPort = port
}
}
} }
go route.Start(cfg.General.ExternalController, cfg.General.Secret) go route.Start(cfg.General.ExternalController, cfg.General.Secret)
@ -151,13 +122,13 @@ func clashGetConfigs() *C.char {
func verifyGEOIPDataBase() bool { func verifyGEOIPDataBase() bool {
mmdb, err := geoip2.Open(constant.Path.MMDB()) mmdb, err := geoip2.Open(constant.Path.MMDB())
if err != nil { if err != nil {
log.Warnln("mmdb fail:%s",err.Error()) log.Warnln("mmdb fail:%s", err.Error())
return false return false
} }
_,err = mmdb.Country(net.ParseIP("114.114.114.114")) _, err = mmdb.Country(net.ParseIP("114.114.114.114"))
if err!=nil { if err != nil {
log.Warnln("mmdb lookup fail:%s",err.Error()) log.Warnln("mmdb lookup fail:%s", err.Error())
return false return false
} }
return true return true