From 5e24ea86b22c9258f231e1984d5f861d3ab89d39 Mon Sep 17 00:00:00 2001 From: Chunchi Che Date: Sun, 30 Jun 2024 14:18:44 +0800 Subject: [PATCH] optimize some codes with global static variables (#476) --- clash_lib/src/app/dns/dummy_keys.rs | 51 ++-------------------------- clash_lib/src/app/dns/test/test.cert | 19 +++++++++++ clash_lib/src/app/dns/test/test.key | 28 +++++++++++++++ clash_lib/src/lib.rs | 17 ++++------ 4 files changed, 55 insertions(+), 60 deletions(-) create mode 100644 clash_lib/src/app/dns/test/test.cert create mode 100644 clash_lib/src/app/dns/test/test.key diff --git a/clash_lib/src/app/dns/dummy_keys.rs b/clash_lib/src/app/dns/dummy_keys.rs index c49d964fa..ee755e290 100644 --- a/clash_lib/src/app/dns/dummy_keys.rs +++ b/clash_lib/src/app/dns/dummy_keys.rs @@ -2,53 +2,6 @@ /// host: dns.example.com /// TODO(#51): use real certificate and key -pub static TEST_CERT: &str = "-----BEGIN CERTIFICATE----- -MIIDITCCAgmgAwIBAgIQOp++4lLpVGLWkaGuRrrP2zANBgkqhkiG9w0BAQsFADAS -MRAwDgYDVQQKEwdBY21lIENvMB4XDTIzMDkwMjA4MzIyM1oXDTI0MDkwMTA4MzIy -M1owEjEQMA4GA1UEChMHQWNtZSBDbzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC -AQoCggEBAPS+pfYexKHq50myjnY6kAZ1uLDmgR/Vy9y7rPcrkqci25ClCnuUiUgY -ABhrdq16oW2wOymc9A56YH9X99UNX6E+VmckCP8v9xcPV4/Zoc+KIeuJHRUBHFMK -ieiWe560XcRCTP7OudUZ+iSHbjhaxZJCEfFaE8aSZXmiLKUETZ7z2lAm75fRCBaR -Vy+jewy04F1BKg4VlnrZnVHzQCcBHZEpsAz4YpbNHMYf9sYRrh+T2j9Zya5tUklI -LzEobWXUzjhrkcBXSVC5dLa9sxz7eSeg0dY0SkP7O5pelr8LaXnBudAk5B0ByqFD -s83Z1AZMkv7vL2L8AGA/X1PP2UV3BrECAwEAAaNzMHEwDgYDVR0PAQH/BAQDAgKk -MBMGA1UdJQQMMAoGCCsGAQUFBwMBMA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYE -FIiHKw+Z9rUjV6pbqcDl+teSTLxoMBoGA1UdEQQTMBGCD2Rucy5leGFtcGxlLmNv -bTANBgkqhkiG9w0BAQsFAAOCAQEACTYIij1QqPhke3aI6HVLuXy75YHjxic0cIcX -7u41WSnZWOrnsBYAFPfjwH7sW6dX+5twUludSCSQ9Xyhl/tdQ0AKWJJNZ7irArS9 -kz2rU4u7YjtR4tuuRB2t+8UEcGA/m0EPhQfFbZedAy/Y2oc+RodwlqibVB/WCMOQ -BL4HS1wFaYZw9WhXk3nzb+wjBhvyEQkI9oeMqVYZLN/9kIY9QuGtDg5onFrVSgjZ -qiR7EfdICe8ogM6IiemQJfZ5SeWkoLpuRlaeVhqFFaFFeJ6cMTJ+Jluh6a3DGP91 -aRaPVO7r8gPq4mACua0HQcBfmH4VKS3hsQHdWDivRUT7xkZ/6g== ------END CERTIFICATE----- -"; +pub static TEST_CERT: &str = include_str!("test/test.cert"); -pub static TEST_KEY: &str = "-----BEGIN PRIVATE KEY----- -MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQD0vqX2HsSh6udJ -so52OpAGdbiw5oEf1cvcu6z3K5KnItuQpQp7lIlIGAAYa3ateqFtsDspnPQOemB/ -V/fVDV+hPlZnJAj/L/cXD1eP2aHPiiHriR0VARxTConolnuetF3EQkz+zrnVGfok -h244WsWSQhHxWhPGkmV5oiylBE2e89pQJu+X0QgWkVcvo3sMtOBdQSoOFZZ62Z1R -80AnAR2RKbAM+GKWzRzGH/bGEa4fk9o/WcmubVJJSC8xKG1l1M44a5HAV0lQuXS2 -vbMc+3knoNHWNEpD+zuaXpa/C2l5wbnQJOQdAcqhQ7PN2dQGTJL+7y9i/ABgP19T -z9lFdwaxAgMBAAECggEAaMFNYcoLmc5kjsvJZFtumAU9NyKCND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MWqeO7LQi6A8uWkfRFPWn/h3gECqItrWFogJixRyL+G -TWQ7SWmJctHD8YjU3E6tlPSWQhD2U75pZWtjpGAIYSdvS5V9Q+95jaKKhsYneThB -SNEnosgWOR5eg9c0FznaDn6nRw== ------END PRIVATE KEY----- -"; +pub static TEST_KEY: &str = include_str!("test/test.key"); diff --git a/clash_lib/src/app/dns/test/test.cert b/clash_lib/src/app/dns/test/test.cert new file mode 100644 index 000000000..4d9a87294 --- /dev/null +++ b/clash_lib/src/app/dns/test/test.cert @@ -0,0 +1,19 @@ +-----BEGIN CERTIFICATE----- +MIIDITCCAgmgAwIBAgIQOp++4lLpVGLWkaGuRrrP2zANBgkqhkiG9w0BAQsFADAS +MRAwDgYDVQQKEwdBY21lIENvMB4XDTIzMDkwMjA4MzIyM1oXDTI0MDkwMTA4MzIy +M1owEjEQMA4GA1UEChMHQWNtZSBDbzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC +AQoCggEBAPS+pfYexKHq50myjnY6kAZ1uLDmgR/Vy9y7rPcrkqci25ClCnuUiUgY +ABhrdq16oW2wOymc9A56YH9X99UNX6E+VmckCP8v9xcPV4/Zoc+KIeuJHRUBHFMK +ieiWe560XcRCTP7OudUZ+iSHbjhaxZJCEfFaE8aSZXmiLKUETZ7z2lAm75fRCBaR +Vy+jewy04F1BKg4VlnrZnVHzQCcBHZEpsAz4YpbNHMYf9sYRrh+T2j9Zya5tUklI +LzEobWXUzjhrkcBXSVC5dLa9sxz7eSeg0dY0SkP7O5pelr8LaXnBudAk5B0ByqFD +s83Z1AZMkv7vL2L8AGA/X1PP2UV3BrECAwEAAaNzMHEwDgYDVR0PAQH/BAQDAgKk +MBMGA1UdJQQMMAoGCCsGAQUFBwMBMA8GA1UdEwEB/wQFMAMBAf8wHQYDVR0OBBYE +FIiHKw+Z9rUjV6pbqcDl+teSTLxoMBoGA1UdEQQTMBGCD2Rucy5leGFtcGxlLmNv +bTANBgkqhkiG9w0BAQsFAAOCAQEACTYIij1QqPhke3aI6HVLuXy75YHjxic0cIcX +7u41WSnZWOrnsBYAFPfjwH7sW6dX+5twUludSCSQ9Xyhl/tdQ0AKWJJNZ7irArS9 +kz2rU4u7YjtR4tuuRB2t+8UEcGA/m0EPhQfFbZedAy/Y2oc+RodwlqibVB/WCMOQ +BL4HS1wFaYZw9WhXk3nzb+wjBhvyEQkI9oeMqVYZLN/9kIY9QuGtDg5onFrVSgjZ +qiR7EfdICe8ogM6IiemQJfZ5SeWkoLpuRlaeVhqFFaFFeJ6cMTJ+Jluh6a3DGP91 +aRaPVO7r8gPq4mACua0HQcBfmH4VKS3hsQHdWDivRUT7xkZ/6g== +-----END CERTIFICATE----- diff --git a/clash_lib/src/app/dns/test/test.key b/clash_lib/src/app/dns/test/test.key new file mode 100644 index 000000000..eb2e6342e --- /dev/null +++ b/clash_lib/src/app/dns/test/test.key @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQD0vqX2HsSh6udJ +so52OpAGdbiw5oEf1cvcu6z3K5KnItuQpQp7lIlIGAAYa3ateqFtsDspnPQOemB/ +V/fVDV+hPlZnJAj/L/cXD1eP2aHPiiHriR0VARxTConolnuetF3EQkz+zrnVGfok +h244WsWSQhHxWhPGkmV5oiylBE2e89pQJu+X0QgWkVcvo3sMtOBdQSoOFZZ62Z1R +80AnAR2RKbAM+GKWzRzGH/bGEa4fk9o/WcmubVJJSC8xKG1l1M44a5HAV0lQuXS2 +vbMc+3knoNHWNEpD+zuaXpa/C2l5wbnQJOQdAcqhQ7PN2dQGTJL+7y9i/ABgP19T +z9lFdwaxAgMBAAECggEAaMFNYcoLmc5kjsvJZFtumAU9NyKCNDEbX/BIeUcCL12h +IwkxMnICTIRRTiJ5Gom5nKxotkgCwkupD/iEEIH345k9/EmVPDy4gvtDHEQnmSBj +ol/+vaXLDNQe8Rmv8d77n2xNbmbnbYn/4jDBgYeAtzhmW6qVelHg8y3x8/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RIVATE KEY----- diff --git a/clash_lib/src/lib.rs b/clash_lib/src/lib.rs index 5fe662bec..12355e0a5 100644 --- a/clash_lib/src/lib.rs +++ b/clash_lib/src/lib.rs @@ -16,13 +16,10 @@ use crate::{ use app::{dispatcher::StatisticsManager, dns::SystemResolver, profile}; use common::{auth, http::new_http_client, mmdb}; use config::def::LogLevel; +use once_cell::sync::OnceCell; use proxy::tun::get_tun_runner; -use std::{ - io, - path::PathBuf, - sync::{Arc, OnceLock}, -}; +use std::{io, path::PathBuf, sync::Arc}; use thiserror::Error; use tokio::{ sync::{broadcast, mpsc, oneshot, Mutex}, @@ -108,8 +105,7 @@ pub struct RuntimeController { shutdown_tx: mpsc::Sender<()>, } -static RUNTIME_CONTROLLER: OnceLock> = - OnceLock::new(); +static RUNTIME_CONTROLLER: OnceCell = OnceCell::new(); pub fn start(opts: Options) -> Result<(), Error> { let rt = match opts.rt.as_ref().unwrap_or(&TokioRuntime::MultiThread) { @@ -133,8 +129,8 @@ pub fn start(opts: Options) -> Result<(), Error> { } pub fn shutdown() -> bool { - match RUNTIME_CONTROLLER.get().unwrap().write() { - Ok(rt) => rt.shutdown_tx.blocking_send(()).is_ok(), + match RUNTIME_CONTROLLER.get() { + Some(controller) => controller.shutdown_tx.blocking_send(()).is_ok(), _ => false, } } @@ -142,8 +138,7 @@ pub fn shutdown() -> bool { async fn start_async(opts: Options) -> Result<(), Error> { let (shutdown_tx, mut shutdown_rx) = mpsc::channel(1); - let _ = RUNTIME_CONTROLLER - .set(std::sync::RwLock::new(RuntimeController { shutdown_tx })); + let _ = RUNTIME_CONTROLLER.get_or_init(|| RuntimeController { shutdown_tx }); let config: InternalConfig = opts.config.try_parse()?;