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

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)]