Kaynağa Gözat

fix(windows) properly run the LoopbackExempt command on dev/build/msi, closes #788 (#794)

Lucas Fernandes Nogueira 5 yıl önce
ebeveyn
işleme
0967b4291e

+ 6 - 0
.changes/loopback.md

@@ -0,0 +1,6 @@
+---
+"tauri": patch
+"tauri-bundler": patch
+---
+
+Properly run the loopback command on Windows.

+ 1 - 1
cli/tauri-bundler/src/bundle.rs

@@ -11,7 +11,7 @@ mod path_utils;
 mod platform;
 mod rpm_bundle;
 mod settings;
-mod tauri_config;
+pub mod tauri_config;
 #[cfg(target_os = "windows")]
 mod wix;
 

+ 8 - 0
cli/tauri-bundler/src/bundle/tauri_config.rs

@@ -43,11 +43,19 @@ pub struct BundleConfig {
   pub external_bin: Option<Vec<String>>,
 }
 
+#[derive(PartialEq, Deserialize, Clone, Debug, Default)]
+#[serde(tag = "embeddedServer", rename_all = "camelCase")]
+pub struct EmbeddedServerConfig {
+  pub active: bool,
+}
+
 #[derive(PartialEq, Deserialize, Clone, Debug, Default)]
 #[serde(tag = "tauri", rename_all = "camelCase")]
 pub struct TauriConfig {
   #[serde(default)]
   pub bundle: BundleConfig,
+  #[serde(default)]
+  pub embedded_server: EmbeddedServerConfig,
 }
 
 #[derive(PartialEq, Deserialize, Clone, Debug)]

+ 4 - 0
cli/tauri-bundler/src/bundle/templates/main.wxs

@@ -148,13 +148,17 @@
 
         <SetProperty Id="ARPINSTALLLOCATION" Value="[INSTALLDIR]" After="CostFinalize"/>
 
+        {{#if embedded_server}}
         <InstallExecuteSequence>
             <Custom Action='LoopBackCmd' After='InstallFiles'/>
         </InstallExecuteSequence>
+        {{/if}}
         
     </Product>
 
+    {{#if embedded_server}}
     <Fragment>
         <CustomAction Id="LoopBackCmd" Directory="ApplicationProgramsFolder" Execute="commit" Impersonate="no" ExeCommand="cmd.exe /c &quot;CheckNetIsolation.exe LoopbackExempt -a -n=&quot;Microsoft.Win32WebViewHost_cw5n1h2txyewy&quot;&quot;" Return="ignore" />
     </Fragment>
+    {{/if}}
 </Wix>

+ 9 - 2
cli/tauri-bundler/src/bundle/wix.rs

@@ -12,7 +12,7 @@ use zip::ZipArchive;
 
 use std::collections::BTreeMap;
 use std::fs::{create_dir_all, remove_dir_all, write, File};
-use std::io::{BufRead, BufReader, Cursor, Read, Write};
+use std::io::{Cursor, Read, Write};
 use std::path::{Path, PathBuf};
 use std::process::{Command, Stdio};
 
@@ -348,7 +348,7 @@ fn run_candle(
     .stdout(Stdio::piped())
     .current_dir(build_path);
 
-  common::print_info("running candle.exe");
+  common::print_info("running candle.exe")?;
   common::execute_with_output(&mut cmd).map_err(|_| crate::Error::CandleError)
 }
 
@@ -411,6 +411,13 @@ pub fn build_wix_app_installer(
 
   let mut data = BTreeMap::new();
 
+  if let Ok(tauri_config) = crate::bundle::tauri_config::get() {
+    data.insert(
+      "embedded_server",
+      to_json(tauri_config.tauri.embedded_server.active),
+    );
+  }
+
   data.insert("product_name", to_json(settings.bundle_name()));
   data.insert("version", to_json(settings.version_string()));
   let manufacturer = settings.bundle_identifier().to_string();

+ 26 - 9
cli/tauri-bundler/src/main.rs

@@ -2,8 +2,6 @@ mod bundle;
 mod error;
 pub use error::{Error, Result};
 
-#[cfg(windows)]
-use crate::bundle::print_info;
 use crate::bundle::{bundle_project, check_icons, PackageType, Settings};
 
 use clap::{crate_version, App, AppSettings, Arg, SubCommand};
@@ -109,13 +107,32 @@ fn run() -> crate::Result<()> {
 
   #[cfg(windows)]
   {
-    print_info("Running Loopback command")?;
-    Command::new("cmd")
-      .args(&vec![
-        "CheckNetIsolation.exe",
-        r#"LoopbackExempt -a -n="Microsoft.Win32WebViewHost_cw5n1h2txyewy""#,
-      ])
-      .force_prompt(true);
+    if let Ok(tauri_config) = crate::bundle::tauri_config::get() {
+      if tauri_config.tauri.embedded_server.active {
+        let exempt_output = std::process::Command::new("CheckNetIsolation")
+          .args(&vec!["LoopbackExempt", "-s"])
+          .output()
+          .expect("failed to read LoopbackExempt -s");
+
+        if !exempt_output.status.success() {
+          panic!("Failed to execute CheckNetIsolation LoopbackExempt -s");
+        }
+
+        let output_str = String::from_utf8(exempt_output.stdout)
+          .map_err(|_| anyhow::anyhow!("failed to convert LoopbackExempt output to String"))?
+          .to_lowercase();
+        if !output_str.contains("win32webviewhost_cw5n1h2txyewy") {
+          println!("Running Loopback command");
+          Command::new("powershell")
+            .args(&vec![
+              "CheckNetIsolation LoopbackExempt -a -n=\"Microsoft.Win32WebViewHost_cw5n1h2txyewy\"",
+            ])
+            .force_prompt(true)
+            .status()
+            .expect("failed to run Loopback command");
+        }
+      }
+    }
   }
 
   if let Some(m) = m.subcommand_matches("tauri-bundler") {

+ 4 - 0
tauri/Cargo.toml

@@ -35,6 +35,10 @@ envmnt = "0.8.3"
 once_cell = "1.4.0"
 tauri-api = { version = "0.7", path = "../tauri-api" }
 
+# this is actually only needed on dev, but we can't specify it yet
+[target."cfg(target_os = \"windows\")".dependencies]
+runas = "0.2"
+
 [build-dependencies]
 tauri_includedir_codegen = "0.6.2"
 cfg_aliases = "0.1.0"

+ 23 - 0
tauri/src/app/runner.rs

@@ -64,6 +64,29 @@ pub(crate) fn run(application: &mut App) -> crate::Result<()> {
 fn setup_content() -> crate::Result<Content<String>> {
   let config = get()?;
   if config.build.dev_path.starts_with("http") {
+    #[cfg(windows)]
+    {
+      let exempt_output = std::process::Command::new("CheckNetIsolation")
+        .args(&vec!["LoopbackExempt", "-s"])
+        .output()
+        .expect("failed to read LoopbackExempt -s");
+
+      if !exempt_output.status.success() {
+        panic!("Failed to execute CheckNetIsolation LoopbackExempt -s");
+      }
+
+      let output_str = String::from_utf8(exempt_output.stdout)?.to_lowercase();
+      if !output_str.contains("win32webviewhost_cw5n1h2txyewy") {
+        println!("Running Loopback command");
+        runas::Command::new("powershell")
+          .args(&vec![
+            "CheckNetIsolation LoopbackExempt -a -n=\"Microsoft.Win32WebViewHost_cw5n1h2txyewy\"",
+          ])
+          .force_prompt(true)
+          .status()
+          .expect("failed to run Loopback command");
+      }
+    }
     Ok(Content::Url(config.build.dev_path.clone()))
   } else {
     let dev_dir = &config.build.dev_path;