use std::process::Command;
use crate::feature::FeatureControl;
use anyhow::Result;
use async_trait::async_trait;
pub struct WslpfUsecaseImpl {}
impl WslpfUsecaseImpl {
pub fn new() -> Self {
Self {}
}
}
#[async_trait]
impl FeatureControl for WslpfUsecaseImpl {
fn name(&self) -> &str {
"wslpf"
}
async fn enable(&self, setting: Option<serde_json::Value>) -> Result<()> {
Ok(())
}
async fn disable(&self) -> Result<()> {
Ok(())
}
async fn update(
&self,
old_setting: Option<serde_json::Value>,
setting: Option<serde_json::Value>,
) -> Result<()> {
Ok(())
}
async fn lid_change(&self, open: bool) {}
}
fn setup_windows_firewall(local_port: u16, wsl2_port: u16) -> Result<()> {
let ps_script = format!(
r#"
$wsl_ip = bash -c "ifconfig eth0 | grep 'inet '"
$found = $wsl_ip -match '\d{{1,3}}\.\d{{1,3}}\.\d{{1,3}}\.\d{{1,3}}';
$wsl_ip = $matches[0];
Write-Host "WSL2 IP address: " $wsl_ip;
iex "netsh interface portproxy delete v4tov4 listenaddress=0.0.0.0 listenport={local_port}"
iex "netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport={local_port} connectaddress=$wsl_ip connectport={wsl2_port}"
iex "netsh advfirewall firewall add rule name='open port {local_port} for wsl2 port forwarding' dir=in action=allow protocol=TCP localport={local_port}"
"#,
local_port = local_port,
wsl2_port = wsl2_port,
);
let output = Command::new("powershell")
.arg("-Command")
.arg(&ps_script)
.output()?;
let output_str = String::from_utf8_lossy(&output.stdout);
println!("Output: {}", output_str);
Ok(())
}