refactor: remove port_scanner dependency and simplify port checking logic

This commit is contained in:
Tunglies
2025-12-21 16:51:55 +08:00
parent a5752f7b00
commit af094bfcd7
4 changed files with 3 additions and 14 deletions

7
Cargo.lock generated
View File

@@ -1220,7 +1220,6 @@ dependencies = [
"open",
"parking_lot",
"percent-encoding",
"port_scanner",
"regex",
"reqwest",
"reqwest_dav",
@@ -5656,12 +5655,6 @@ dependencies = [
"universal-hash",
]
[[package]]
name = "port_scanner"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "325a6d2ac5dee293c3b2612d4993b98aec1dff096b0a2dae70ed7d95784a05da"
[[package]]
name = "portable-atomic"
version = "1.11.1"

View File

@@ -61,7 +61,6 @@ nanoid = "0.4"
chrono = "0.4.42"
boa_engine = "0.21.0"
once_cell = { version = "1.21.3", features = ["parking_lot"] }
port_scanner = "0.1.5"
delay_timer = "0.11.6"
percent-encoding = "2.3.2"
reqwest = { version = "0.12.24", features = ["json", "cookies", "rustls-tls"] }

View File

@@ -99,8 +99,5 @@ pub fn get_network_interfaces_info() -> CmdResult<Vec<NetworkInterface>> {
#[tauri::command]
pub fn is_port_in_use(port: u16) -> bool {
match TcpListener::bind(("127.0.0.1", port)) {
Ok(_listener) => false,
Err(_) => true,
}
TcpListener::bind(("127.0.0.1", port)).is_err()
}

View File

@@ -1,5 +1,6 @@
use super::resolve;
use crate::{
cmd::is_port_in_use,
config::{Config, DEFAULT_PAC, IVerge},
module::lightweight,
process::AsyncHandler,
@@ -9,7 +10,6 @@ use anyhow::{Result, bail};
use clash_verge_logging::{Type, logging, logging_error};
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
use port_scanner::local_port_available;
use reqwest::ClientBuilder;
use smartstring::alias::String;
use std::time::Duration;
@@ -27,7 +27,7 @@ static SHUTDOWN_SENDER: OnceCell<Mutex<Option<oneshot::Sender<()>>>> = OnceCell:
/// check whether there is already exists
pub async fn check_singleton() -> Result<()> {
let port = IVerge::get_singleton_port();
if !local_port_available(port) {
if is_port_in_use(port) {
let client = ClientBuilder::new().timeout(Duration::from_millis(500)).build()?;
// 需要确保 Send
#[allow(clippy::needless_collect)]