mirror of
https://github.com/LowderPlay/cheburcheck.git
synced 2026-10-10 08:08:25 +03:00
@@ -78,6 +78,7 @@ struct Args {
|
||||
probe_token: String,
|
||||
max_concurrent_tasks: usize,
|
||||
traceroute_retries: u8,
|
||||
bundle_type: &'static str,
|
||||
}
|
||||
|
||||
impl Cli {
|
||||
@@ -94,6 +95,7 @@ impl Cli {
|
||||
.context("--probe-token or PROBE_TOKEN is required when running the probe")?,
|
||||
max_concurrent_tasks: self.max_concurrent_tasks,
|
||||
traceroute_retries: self.traceroute_retries,
|
||||
bundle_type: update::bundle_type().context("failed to detect probe bundle type")?,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -127,6 +129,7 @@ async fn main() -> Result<()> {
|
||||
online: false,
|
||||
probe_id: &args.probe_id,
|
||||
version: env!("CARGO_PKG_VERSION"),
|
||||
bundle_type: Some(args.bundle_type),
|
||||
dpi_hop_v4: None,
|
||||
dpi_hop_v6: None,
|
||||
})?;
|
||||
@@ -456,6 +459,7 @@ async fn publish_status(
|
||||
online,
|
||||
probe_id: &args.probe_id,
|
||||
version: env!("CARGO_PKG_VERSION"),
|
||||
bundle_type: Some(args.bundle_type),
|
||||
dpi_hop_v4: dpi_hops.v4,
|
||||
dpi_hop_v6: dpi_hops.v6,
|
||||
})?;
|
||||
@@ -625,6 +629,7 @@ mod tests {
|
||||
online: true,
|
||||
probe_id: "probe-1",
|
||||
version: "1.0.0",
|
||||
bundle_type: Some("debian"),
|
||||
dpi_hop_v4: Some(4),
|
||||
dpi_hop_v6: Some(6),
|
||||
};
|
||||
@@ -632,6 +637,7 @@ mod tests {
|
||||
|
||||
assert_eq!(value["dpi_hop_v4"], 4);
|
||||
assert_eq!(value["dpi_hop_v6"], 6);
|
||||
assert_eq!(value["bundle_type"], "debian");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+49
-6
@@ -33,6 +33,22 @@ enum PackageKind {
|
||||
Windows,
|
||||
}
|
||||
|
||||
impl PackageKind {
|
||||
const fn bundle_type(self) -> &'static str {
|
||||
match self {
|
||||
Self::Debian => "debian",
|
||||
Self::Apk => "openwrt-apk",
|
||||
Self::Opkg => "openwrt-opkg",
|
||||
Self::Linux => "linux",
|
||||
Self::Windows => "windows",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bundle_type() -> Result<&'static str> {
|
||||
detect_platform().map(|(kind, _, _)| kind.bundle_type())
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
struct UpdateLock {
|
||||
_file: File,
|
||||
@@ -191,14 +207,10 @@ fn detect_platform() -> Result<(PackageKind, String, bool)> {
|
||||
} else if command_exists("apk")
|
||||
&& command_succeeds("apk", &["info", "--exists", "cheburprobe"])?
|
||||
{
|
||||
let architecture = output_text("apk", &["--print-arch"])?;
|
||||
let architecture = openwrt_apk_arch()?;
|
||||
let luci_installed =
|
||||
command_succeeds("apk", &["info", "--exists", "luci-app-cheburprobe"])?;
|
||||
Ok((
|
||||
PackageKind::Apk,
|
||||
architecture.trim().to_owned(),
|
||||
luci_installed,
|
||||
))
|
||||
Ok((PackageKind::Apk, architecture, luci_installed))
|
||||
} else if command_exists("opkg")
|
||||
&& output_text("opkg", &["list-installed", "cheburprobe"])?
|
||||
.lines()
|
||||
@@ -227,6 +239,28 @@ fn detect_platform() -> Result<(PackageKind, String, bool)> {
|
||||
bail!("updates are not supported on this operating system")
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn openwrt_apk_arch() -> Result<String> {
|
||||
if let Ok(architecture) = std::fs::read_to_string("/etc/apk/arch") {
|
||||
let architecture = architecture.trim();
|
||||
if !architecture.is_empty() {
|
||||
return Ok(architecture.to_owned());
|
||||
}
|
||||
}
|
||||
let release = std::fs::read_to_string("/etc/openwrt_release")
|
||||
.context("failed to read /etc/openwrt_release")?;
|
||||
parse_openwrt_release_arch(&release).context("OpenWrt did not report DISTRIB_ARCH")
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn parse_openwrt_release_arch(release: &str) -> Option<String> {
|
||||
release.lines().find_map(|line| {
|
||||
line.strip_prefix("DISTRIB_ARCH=")
|
||||
.map(|value| value.trim_matches(['\'', '"']).to_owned())
|
||||
.filter(|value| !value.is_empty())
|
||||
})
|
||||
}
|
||||
|
||||
fn select_luci_asset<'a>(
|
||||
assets: &'a [Asset],
|
||||
kind: PackageKind,
|
||||
@@ -463,6 +497,15 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
#[test]
|
||||
fn parses_full_openwrt_architecture() {
|
||||
assert_eq!(
|
||||
parse_openwrt_release_arch("DISTRIB_ID='OpenWrt'\nDISTRIB_ARCH='aarch64_cortex-a53'\n"),
|
||||
Some("aarch64_cortex-a53".to_owned())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selects_each_package_format() {
|
||||
let assets = vec![
|
||||
|
||||
Reference in New Issue
Block a user