|
|
|
@ -26,7 +26,13 @@ use reqwest::{
|
|
|
|
|
};
|
|
|
|
|
use semver::Version;
|
|
|
|
|
use serde::{de::Error as DeError, Deserialize, Deserializer, Serialize};
|
|
|
|
|
use tauri::{utils::platform::current_exe, AppHandle, Resource, Runtime};
|
|
|
|
|
use tauri::{
|
|
|
|
|
utils::{
|
|
|
|
|
config::BundleType,
|
|
|
|
|
platform::{bundle_type, current_exe},
|
|
|
|
|
},
|
|
|
|
|
AppHandle, Resource, Runtime,
|
|
|
|
|
};
|
|
|
|
|
use time::OffsetDateTime;
|
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
|
@ -37,6 +43,31 @@ use crate::{
|
|
|
|
|
|
|
|
|
|
const UPDATER_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);
|
|
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
|
pub enum Installer {
|
|
|
|
|
AppImage,
|
|
|
|
|
Deb,
|
|
|
|
|
Rpm,
|
|
|
|
|
|
|
|
|
|
App,
|
|
|
|
|
|
|
|
|
|
Msi,
|
|
|
|
|
Nsis,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Installer {
|
|
|
|
|
fn suffix(self) -> &'static str {
|
|
|
|
|
match self {
|
|
|
|
|
Self::AppImage => "appimage",
|
|
|
|
|
Self::Deb => "deb",
|
|
|
|
|
Self::Rpm => "rpm",
|
|
|
|
|
Self::App => "app",
|
|
|
|
|
Self::Msi => "msi",
|
|
|
|
|
Self::Nsis => "nsis",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
|
|
|
|
pub struct ReleaseManifestPlatform {
|
|
|
|
|
/// Download URL for the platform
|
|
|
|
@ -270,7 +301,8 @@ impl UpdaterBuilder {
|
|
|
|
|
(target.clone(), target)
|
|
|
|
|
} else {
|
|
|
|
|
let target = get_updater_target().ok_or(Error::UnsupportedOs)?;
|
|
|
|
|
(target.to_string(), format!("{target}-{arch}"))
|
|
|
|
|
let json_target = format!("{target}-{arch}");
|
|
|
|
|
(target.to_owned(), json_target)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let executable_path = self.executable_path.clone().unwrap_or(current_exe()?);
|
|
|
|
@ -327,7 +359,7 @@ pub struct Updater {
|
|
|
|
|
proxy: Option<Url>,
|
|
|
|
|
endpoints: Vec<Url>,
|
|
|
|
|
arch: &'static str,
|
|
|
|
|
// The `{{target}}` variable we replace in the endpoint
|
|
|
|
|
// The `{{target}}` variable we replace in the endpoint and serach for in the JSON
|
|
|
|
|
target: String,
|
|
|
|
|
// The value we search if the updater server returns a JSON with the `platforms` object
|
|
|
|
|
json_target: String,
|
|
|
|
@ -342,6 +374,18 @@ pub struct Updater {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Updater {
|
|
|
|
|
fn get_updater_installer(&self) -> Option<Installer> {
|
|
|
|
|
bundle_type().and_then(|t| match t {
|
|
|
|
|
BundleType::Deb => Some(Installer::Deb),
|
|
|
|
|
BundleType::Rpm => Some(Installer::Rpm),
|
|
|
|
|
BundleType::AppImage => Some(Installer::AppImage),
|
|
|
|
|
BundleType::Msi => Some(Installer::Msi),
|
|
|
|
|
BundleType::Nsis => Some(Installer::Nsis),
|
|
|
|
|
BundleType::App => Some(Installer::App),
|
|
|
|
|
BundleType::Dmg => None,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn check(&self) -> Result<Option<Update>> {
|
|
|
|
|
// we want JSON only
|
|
|
|
|
let mut headers = self.headers.clone();
|
|
|
|
@ -466,6 +510,31 @@ impl Updater {
|
|
|
|
|
None => release.version > self.current_version,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut download_url = release.download_url(&self.json_target);
|
|
|
|
|
let mut signature = release.signature(&self.json_target);
|
|
|
|
|
|
|
|
|
|
let installer = self.get_updater_installer();
|
|
|
|
|
if installer.is_none() && (download_url.is_err() || signature.is_err()) {
|
|
|
|
|
return Err(Error::TargetNotFound(self.json_target.clone()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(installer) = installer {
|
|
|
|
|
let target = &format!("{}-{}", &self.json_target, installer.suffix());
|
|
|
|
|
download_url =
|
|
|
|
|
release
|
|
|
|
|
.download_url(target)
|
|
|
|
|
.or(download_url.or(Err(Error::TargetsNotFound(
|
|
|
|
|
self.json_target.clone(),
|
|
|
|
|
target.clone(),
|
|
|
|
|
))));
|
|
|
|
|
signature = release
|
|
|
|
|
.signature(target)
|
|
|
|
|
.or(signature.or(Err(Error::TargetsNotFound(
|
|
|
|
|
self.json_target.clone(),
|
|
|
|
|
target.clone(),
|
|
|
|
|
))));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let update = if should_update {
|
|
|
|
|
Some(Update {
|
|
|
|
|
run_on_main_thread: self.run_on_main_thread.clone(),
|
|
|
|
@ -477,9 +546,10 @@ impl Updater {
|
|
|
|
|
extract_path: self.extract_path.clone(),
|
|
|
|
|
version: release.version.to_string(),
|
|
|
|
|
date: release.pub_date,
|
|
|
|
|
download_url: release.download_url(&self.json_target)?.to_owned(),
|
|
|
|
|
signature: release.signature(&self.json_target)?.to_owned(),
|
|
|
|
|
body: release.notes,
|
|
|
|
|
download_url: download_url?.to_owned(),
|
|
|
|
|
body: release.notes.clone(),
|
|
|
|
|
signature: signature?.to_owned(),
|
|
|
|
|
installer,
|
|
|
|
|
raw_json: raw_json.unwrap(),
|
|
|
|
|
timeout: None,
|
|
|
|
|
proxy: self.proxy.clone(),
|
|
|
|
@ -513,6 +583,8 @@ pub struct Update {
|
|
|
|
|
pub date: Option<OffsetDateTime>,
|
|
|
|
|
/// Target
|
|
|
|
|
pub target: String,
|
|
|
|
|
/// Current installer
|
|
|
|
|
pub installer: Option<Installer>,
|
|
|
|
|
/// Download URL announced
|
|
|
|
|
pub download_url: Url,
|
|
|
|
|
/// Signature announced
|
|
|
|
@ -852,11 +924,10 @@ impl Update {
|
|
|
|
|
/// └── ...
|
|
|
|
|
///
|
|
|
|
|
fn install_inner(&self, bytes: &[u8]) -> Result<()> {
|
|
|
|
|
if self.is_deb_package() {
|
|
|
|
|
self.install_deb(bytes)
|
|
|
|
|
} else {
|
|
|
|
|
// Handle AppImage or other formats
|
|
|
|
|
self.install_appimage(bytes)
|
|
|
|
|
match self.installer {
|
|
|
|
|
Some(Installer::Deb) => self.install_deb(bytes),
|
|
|
|
|
Some(Installer::Rpm) => self.install_rpm(bytes),
|
|
|
|
|
_ => self.install_appimage(bytes),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -933,39 +1004,6 @@ impl Update {
|
|
|
|
|
Err(Error::TempDirNotOnSameMountPoint)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn is_deb_package(&self) -> bool {
|
|
|
|
|
// First check if we're in a typical Debian installation path
|
|
|
|
|
let in_system_path = self
|
|
|
|
|
.extract_path
|
|
|
|
|
.to_str()
|
|
|
|
|
.map(|p| p.starts_with("/usr"))
|
|
|
|
|
.unwrap_or(false);
|
|
|
|
|
|
|
|
|
|
if !in_system_path {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Then verify it's actually a Debian-based system by checking for dpkg
|
|
|
|
|
let dpkg_exists = std::path::Path::new("/var/lib/dpkg").exists();
|
|
|
|
|
let apt_exists = std::path::Path::new("/etc/apt").exists();
|
|
|
|
|
|
|
|
|
|
// Additional check for the package in dpkg database
|
|
|
|
|
let package_in_dpkg = if let Ok(output) = std::process::Command::new("dpkg")
|
|
|
|
|
.args(["-S", &self.extract_path.to_string_lossy()])
|
|
|
|
|
.output()
|
|
|
|
|
{
|
|
|
|
|
output.status.success()
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Consider it a deb package only if:
|
|
|
|
|
// 1. We're in a system path AND
|
|
|
|
|
// 2. We have Debian package management tools AND
|
|
|
|
|
// 3. The binary is tracked by dpkg
|
|
|
|
|
dpkg_exists && apt_exists && package_in_dpkg
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn install_deb(&self, bytes: &[u8]) -> Result<()> {
|
|
|
|
|
// First verify the bytes are actually a .deb package
|
|
|
|
|
if !infer::archive::is_deb(bytes) {
|
|
|
|
@ -973,6 +1011,18 @@ impl Update {
|
|
|
|
|
return Err(Error::InvalidUpdaterFormat);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.try_tmp_locations(bytes, "dpkg", "-i")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn install_rpm(&self, bytes: &[u8]) -> Result<()> {
|
|
|
|
|
// First verify the bytes are actually a .rpm package
|
|
|
|
|
if !infer::archive::is_rpm(bytes) {
|
|
|
|
|
return Err(Error::InvalidUpdaterFormat);
|
|
|
|
|
}
|
|
|
|
|
self.try_tmp_locations(bytes, "rpm", "-U")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn try_tmp_locations(&self, bytes: &[u8], install_cmd: &str, install_arg: &str) -> Result<()> {
|
|
|
|
|
// Try different temp directories
|
|
|
|
|
let tmp_dir_locations = vec![
|
|
|
|
|
Box::new(|| Some(std::env::temp_dir())) as Box<dyn FnOnce() -> Option<PathBuf>>,
|
|
|
|
@ -984,15 +1034,19 @@ impl Update {
|
|
|
|
|
for tmp_dir_location in tmp_dir_locations {
|
|
|
|
|
if let Some(path) = tmp_dir_location() {
|
|
|
|
|
if let Ok(tmp_dir) = tempfile::Builder::new()
|
|
|
|
|
.prefix("tauri_deb_update")
|
|
|
|
|
.prefix("tauri_rpm_update")
|
|
|
|
|
.tempdir_in(path)
|
|
|
|
|
{
|
|
|
|
|
let deb_path = tmp_dir.path().join("package.deb");
|
|
|
|
|
let pkg_path = tmp_dir.path().join("package.rpm");
|
|
|
|
|
|
|
|
|
|
// Try writing the .deb file
|
|
|
|
|
if std::fs::write(&deb_path, bytes).is_ok() {
|
|
|
|
|
if std::fs::write(&pkg_path, bytes).is_ok() {
|
|
|
|
|
// If write succeeds, proceed with installation
|
|
|
|
|
return self.try_install_with_privileges(&deb_path);
|
|
|
|
|
return self.try_install_with_privileges(
|
|
|
|
|
&pkg_path,
|
|
|
|
|
install_cmd,
|
|
|
|
|
install_arg,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
// If write fails, continue to next temp location
|
|
|
|
|
}
|
|
|
|
@ -1003,12 +1057,17 @@ impl Update {
|
|
|
|
|
Err(Error::TempDirNotFound)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn try_install_with_privileges(&self, deb_path: &Path) -> Result<()> {
|
|
|
|
|
fn try_install_with_privileges(
|
|
|
|
|
&self,
|
|
|
|
|
pkg_path: &Path,
|
|
|
|
|
install_cmd: &str,
|
|
|
|
|
install_arg: &str,
|
|
|
|
|
) -> Result<()> {
|
|
|
|
|
// 1. First try using pkexec (graphical sudo prompt)
|
|
|
|
|
if let Ok(status) = std::process::Command::new("pkexec")
|
|
|
|
|
.arg("dpkg")
|
|
|
|
|
.arg("-i")
|
|
|
|
|
.arg(deb_path)
|
|
|
|
|
.arg(install_cmd)
|
|
|
|
|
.arg(install_arg)
|
|
|
|
|
.arg(pkg_path)
|
|
|
|
|
.status()
|
|
|
|
|
{
|
|
|
|
|
if status.success() {
|
|
|
|
@ -1019,7 +1078,7 @@ impl Update {
|
|
|
|
|
|
|
|
|
|
// 2. Try zenity or kdialog for a graphical sudo experience
|
|
|
|
|
if let Ok(password) = self.get_password_graphically() {
|
|
|
|
|
if self.install_with_sudo(deb_path, &password)? {
|
|
|
|
|
if self.install_with_sudo(pkg_path, &password, install_cmd, install_arg)? {
|
|
|
|
|
log::debug!("installed deb with GUI sudo");
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
@ -1027,16 +1086,16 @@ impl Update {
|
|
|
|
|
|
|
|
|
|
// 3. Final fallback: terminal sudo
|
|
|
|
|
let status = std::process::Command::new("sudo")
|
|
|
|
|
.arg("dpkg")
|
|
|
|
|
.arg("-i")
|
|
|
|
|
.arg(deb_path)
|
|
|
|
|
.arg(install_cmd)
|
|
|
|
|
.arg(install_arg)
|
|
|
|
|
.arg(pkg_path)
|
|
|
|
|
.status()?;
|
|
|
|
|
|
|
|
|
|
if status.success() {
|
|
|
|
|
log::debug!("installed deb with sudo");
|
|
|
|
|
Ok(())
|
|
|
|
|
} else {
|
|
|
|
|
Err(Error::DebInstallFailed)
|
|
|
|
|
Err(Error::PackageInstallFailed)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -1070,15 +1129,21 @@ impl Update {
|
|
|
|
|
Err(Error::AuthenticationFailed)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn install_with_sudo(&self, deb_path: &Path, password: &str) -> Result<bool> {
|
|
|
|
|
fn install_with_sudo(
|
|
|
|
|
&self,
|
|
|
|
|
pkg_path: &Path,
|
|
|
|
|
password: &str,
|
|
|
|
|
install_cmd: &str,
|
|
|
|
|
install_arg: &str,
|
|
|
|
|
) -> Result<bool> {
|
|
|
|
|
use std::io::Write;
|
|
|
|
|
use std::process::{Command, Stdio};
|
|
|
|
|
|
|
|
|
|
let mut child = Command::new("sudo")
|
|
|
|
|
.arg("-S") // read password from stdin
|
|
|
|
|
.arg("dpkg")
|
|
|
|
|
.arg("-i")
|
|
|
|
|
.arg(deb_path)
|
|
|
|
|
.arg(install_cmd)
|
|
|
|
|
.arg(install_arg)
|
|
|
|
|
.arg(pkg_path)
|
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
|
.stderr(Stdio::piped())
|
|
|
|
@ -1086,7 +1151,7 @@ impl Update {
|
|
|
|
|
|
|
|
|
|
if let Some(mut stdin) = child.stdin.take() {
|
|
|
|
|
// Write password to stdin
|
|
|
|
|
writeln!(stdin, "{}", password)?;
|
|
|
|
|
writeln!(stdin, "{password}")?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let status = child.wait()?;
|
|
|
|
|