-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(firewall): handle firewall rules on windows (#712)
Co-authored-by: Brian Pearce <[email protected]>
- Loading branch information
Showing
9 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,35 @@ | ||
fn main() { | ||
tauri_build::build() | ||
if cfg!(target_os = "windows") { | ||
let mut windows = tauri_build::WindowsAttributes::new(); | ||
// Require Administrator permissions to handle Firewall prompts | ||
windows = windows.app_manifest( | ||
r#" | ||
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> | ||
<dependency> | ||
<dependentAssembly> | ||
<assemblyIdentity | ||
type="win32" | ||
name="Microsoft.Windows.Common-Controls" | ||
version="6.0.0.0" | ||
processorArchitecture="*" | ||
publicKeyToken="6595b64144ccf1df" | ||
language="*" | ||
/> | ||
</dependentAssembly> | ||
</dependency> | ||
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"> | ||
<security> | ||
<requestedPrivileges> | ||
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> | ||
</requestedPrivileges> | ||
</security> | ||
</trustInfo> | ||
</assembly> | ||
"#, | ||
); | ||
let attrs = tauri_build::Attributes::new().windows_attributes(windows); | ||
tauri_build::try_build(attrs).expect("failed to run build script") | ||
} else { | ||
tauri_build::build() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ pub mod auto_rollback; | |
pub mod file_utils; | ||
pub mod logging_utils; | ||
pub mod platform_utils; | ||
pub mod setup_utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#[cfg(windows)] | ||
pub mod setup_utils { | ||
use log::{error, info}; | ||
use std::io::{self, Write}; | ||
use std::os::windows::process::CommandExt; | ||
use std::path::PathBuf; | ||
use std::process::Command; | ||
|
||
use crate::consts::PROCESS_CREATION_NO_WINDOW; | ||
|
||
const LOG_TARGET: &str = "tari::universe::setup_utils"; | ||
|
||
fn check_netsh_rule_exists(rule_name: String) -> bool { | ||
let output = Command::new("netsh") | ||
.arg("advfirewall") | ||
.arg("firewall") | ||
.arg("show") | ||
.arg("rule") | ||
.arg(format!("name={}", rule_name)) | ||
.stdout(std::process::Stdio::null()) | ||
.stderr(std::process::Stdio::piped()) | ||
.creation_flags(PROCESS_CREATION_NO_WINDOW) | ||
.output() | ||
.expect("Failed to execute netsh command"); | ||
|
||
if output.status.success() { | ||
let stdout = String::from_utf8_lossy(&output.stdout); | ||
let lines: Vec<&str> = stdout.split('\n').collect(); | ||
for line in lines { | ||
if line.contains("Action:") { | ||
let action = line.split(':').nth(1).unwrap_or("").trim(); | ||
if action == "Allow" { | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
false | ||
} | ||
|
||
pub fn add_firewall_rule(binary_name: String, binary_path: PathBuf) -> io::Result<()> { | ||
if !check_netsh_rule_exists(binary_name.clone()) { | ||
// Add a firewall rule to allow inbound connections | ||
let output = Command::new("netsh") | ||
.args(&[ | ||
"advfirewall", | ||
"firewall", | ||
"add", | ||
"rule", | ||
&format!("name={}", binary_name), | ||
"dir=in", | ||
"action=allow", | ||
&format!( | ||
"program={}.exe", | ||
&binary_path.to_str().expect("Could not get binary path") | ||
), | ||
"profile=public", | ||
]) | ||
.stdout(std::process::Stdio::null()) | ||
.stderr(std::process::Stdio::piped()) | ||
.creation_flags(PROCESS_CREATION_NO_WINDOW) | ||
.output()?; | ||
|
||
if output.status.success() { | ||
info!(target: LOG_TARGET, "Firewall rule added successfully."); | ||
} else { | ||
io::stderr().write_all(&output.stderr)?; | ||
error!(target: LOG_TARGET, "Failed to add firewall rule."); | ||
} | ||
} else { | ||
info!(target: LOG_TARGET, "Firewall rule already exists."); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters