Skip to content

Commit

Permalink
improve linux dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrtgs committed Feb 2, 2025
1 parent 845ac79 commit ff50ee7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,4 @@ system-configuration = "0.6.1"

[build-dependencies]
tokio = { version = "1.43.0", features = ["rt", "macros", "fs", "io-util", "process"] }
anyhow = "1.0.95"
23 changes: 12 additions & 11 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,26 @@ macro_rules! json_sources {
};
}

async fn make_default_sources_toml() -> io::Result<()> {
async fn make_default_sources_toml() -> anyhow::Result<()> {
let mut data = String::new();

let plain_sources = plaintext_sources!();
for source in plain_sources {
writeln!(data, r#"["{source}"]"#).unwrap();
writeln!(data, "steps = [\"Plaintext\"]\n").unwrap();
writeln!(data, r#"["{source}"]"#)?;
writeln!(data, "steps = [\"Plaintext\"]\n")?;
}

let plain_sources = json_sources!();
for (source, key) in plain_sources {
writeln!(data, r#"["{source}"]"#).unwrap();
writeln!(data, r#"steps = [{{ Json = {{ key = "{key}" }} }}]"#).unwrap();
writeln!(data, r#"["{source}"]"#)?;
writeln!(data, r#"steps = [{{ Json = {{ key = "{key}" }} }}]"#)?;
}

tokio::fs::write("includes/sources.toml", data.trim()).await
tokio::fs::write("includes/sources.toml", data.trim()).await?;
Ok(())
}

async fn make_default_sources_rs() -> io::Result<()> {
async fn make_default_sources_rs() -> anyhow::Result<()> {
let mut file = BufWriter::new(File::create("includes/sources.array").await?);

#[derive(Clone)]
Expand Down Expand Up @@ -85,10 +86,12 @@ async fn make_default_sources_rs() -> io::Result<()> {

file.write_all(format!("{sources:?}").0.as_bytes()).await?;

file.flush().await
file.flush().await?;

Ok(())
}

async fn generate_dispatcher() -> io::Result<()> {
async fn generate_dispatcher() -> anyhow::Result<()> {
macro_rules! get_var {
($lit: literal) => {{
println!("cargo::rerun-if-env-changed={}", $lit);
Expand All @@ -103,8 +106,6 @@ async fn generate_dispatcher() -> io::Result<()> {
let target = get_var!("TARGET")?;
let target = target.trim();

tokio::fs::remove_dir_all(format!("./target/{target}/linux-dispatcher")).await?;

Command::new("cargo")
.stdout(Stdio::from(io::stderr()))
.stderr(Stdio::inherit())
Expand Down
39 changes: 35 additions & 4 deletions src/network_listener/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use anyhow::Result;
use dbus::nonblock::{Proxy, SyncConnection};
use futures::{StreamExt, TryStreamExt};
use std::fs::OpenOptions;
use std::io::Write;
use std::io::{Read, Write};
use std::num::NonZero;
use std::os::unix::fs::OpenOptionsExt;
use std::path::Path;
use std::sync::{Arc, LazyLock};
use std::thread;
use std::time::Duration;
use std::{io, thread};
use tempfile::TempPath;
use tokio::fs;
use tokio::net::UnixListener;
Expand Down Expand Up @@ -92,21 +92,52 @@ pub async fn has_internet() -> bool {
}

async fn place_dispatcher() -> Result<()> {
const DISPATCHER: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/dispatcher-bin"));

let locations = include!("./dispatcher-locations")
.map(Path::new)
.map(|loc| loc.join(include_str!("./dispatcher-name")));

let futures = locations.map(|location| async move {
tokio::task::spawn_blocking(move || {
if let Some(parent) = location.parent() {
if !location.try_exists()? && parent.try_exists()? {
let eq_contents = |loc: &Path| {
let file = io::BufReader::new(std::fs::File::open(loc)?);

enum CmpErr {
Io(io::Error),
Cmp,
}

let mut dispatcher_bytes = DISPATCHER.iter();
let res = file.bytes().try_fold((), |(), byte| {
byte.map_err(CmpErr::Io).and_then(|byte| {
match Some(byte) == dispatcher_bytes.next().copied() {
true => Ok(()),
false => Err(CmpErr::Cmp),
}
})
});

let eq = match res {
Ok(()) => true,
Err(CmpErr::Cmp) => false,
Err(CmpErr::Io(io)) => return Err(io),
};

Ok(eq && dispatcher_bytes.as_slice().is_empty())
};
let invalid =
|loc: &Path| Ok::<_, io::Error>(!loc.try_exists()? || eq_contents(loc)?);

if invalid(&location)? && parent.try_exists()? {
OpenOptions::new()
.read(true)
.write(true)
.create_new(true)
.mode(0o777)
.open(location)?
.write_all(include_bytes!(concat!(env!("OUT_DIR"), "/dispatcher-bin")))?;
.write_all(DISPATCHER)?;
}
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/network_listener/linux/socket-path
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/tmp/cloudflare-ddns-sock-1717935207
/run/cloudflare-ddns.sock

0 comments on commit ff50ee7

Please sign in to comment.