added --take-dns for take over dns

This commit is contained in:
alex 2026-04-27 14:51:13 +08:00
parent 9d0a5289ea
commit 520ab964f3
7 changed files with 49 additions and 16 deletions

View File

@ -1,4 +1,4 @@
{
// "rust-analyzer.cargo.target": "x86_64-pc-windows-gnu",
"rust-analyzer.cargo.target": "x86_64-pc-windows-gnu",
// "rust-analyzer.cargo.features": ["tun"]
}

View File

@ -5,7 +5,7 @@ use sdlan_sn_rs::utils::{Mac, Result, SDLanError};
use serde::{Deserialize, Serialize};
use md5::Md5;
pub const TEST_PREFIX: &'static str = "https://punchnet.s5s8.com/api";
pub const TEST_PREFIX: &'static str = "https://root.punchsky.com/api";
const DIGEST_KEY: &'static str = "H6p*2RfEu4ITcL";
type HmacMd5 = Hmac<Md5>;
@ -125,19 +125,25 @@ where T: Serialize + HMacCalculator,
return Err(SDLanError::IOError("failed to calculate hmac".to_owned()));
};
let Ok(response) = client
let response = match client
.post(url)
.header("X-sign", hmac)
.json(&data)
.send()
.await else {
return Err(SDLanError::IOError("failed to do request".to_owned()));
.await {
Ok(response) => {
response
}
Err(e) => {
return Err(SDLanError::IOError(format!("failed to do request: {}", e)));
}
};
// println!("status: {}", response.status());
let text = response.text().await.unwrap();
println!("got test: {}", text);
// println!("got test: {}", text);
let data = serde_json::from_str(&text).unwrap();
// println!("response: {}", response.text().await.unwrap());

View File

@ -64,7 +64,7 @@ const APP_TOKEN_ENV_NAME: &str = "PUNCH_TOKEN";
fn parse_connect_result(res: Result<ConnectResponse>) -> ConnectData {
match res {
Err(e) => {
eprintln!("failed to connect");
eprintln!("failed to connect: {}", e.as_str());
process::exit(-3);
}
Ok(data) => {
@ -123,6 +123,7 @@ async fn daemonize_me(
route_file: String,
route_str: String,
mac: Mac,
take_over_dns: bool,
) {
let _guard = log::init_log(&format!("{}/.output", get_base_dir()));
@ -184,6 +185,7 @@ async fn daemonize_me(
allow_p2p: true,
route_file,
route_str,
take_dns: take_over_dns,
},
tx,
&punchnet::get_install_channel(),
@ -262,7 +264,7 @@ async fn daemonize_me(
edge.quic_endpoint.close(0u32.into(), "bye".as_bytes());
println!("quic is quitting");
delete_pid_file();
let _ = restore_dns();
let _ = restore_dns(edge.take_over_dns);
debug!("restored dns");
}
Err(err) => {
@ -330,6 +332,7 @@ fn main() {
)
.await,
);
println!("login ok");
});
process::exit(0);
// TODO: do login with user
@ -341,6 +344,7 @@ fn main() {
login_with_token(TEST_PREFIX, &client_id, &tk.token, mac, system, version)
.await,
);
println!("login ok");
});
process::exit(0);
}
@ -493,6 +497,7 @@ fn run_it(
rtinfo.route_file.clone(),
rtinfo.route.clone(),
mac,
rtinfo.take_dns,
)
.await;
}),
@ -529,6 +534,7 @@ fn run_it(
tk.route_file.clone(),
tk.route.clone(),
mac,
tk.take_dns,
)
.await;
}),

View File

@ -127,6 +127,7 @@ pub async fn init_edge(
udp_sock_for_global_dns,
server_ip,
install_channel,
args.take_dns,
);
edge.route_table
@ -258,6 +259,8 @@ pub struct Node {
// store pending, and known peers
pub pending_peers: PeerMap,
pub take_over_dns: bool,
#[cfg(any(feature = "tun", target_os = "windows"))]
pub arp_table: ArpTable,
@ -438,6 +441,7 @@ impl Node {
udp_sock_for_global_dns: Arc<UdpSocket>,
server_ip: String,
install_channel: String,
take_over_dns: bool,
) -> Self {
let mode = if cfg!(not(feature = "tun")) {
Mode::Tap
@ -517,6 +521,7 @@ impl Node {
cookie_match: Queryer::new(),
server_ip,
install_channel,
take_over_dns,
}
}

View File

@ -196,7 +196,7 @@ impl Iface {
}
// TODO: set dns should be opened
if let Err(e) = set_dns(self, &self.name, network_domain, &ip_to_string(&default_gw)) {
if let Err(e) = set_dns(self, node.take_over_dns, &self.name, network_domain, &ip_to_string(&default_gw)) {
error!("failed to set dns: {}", e.as_str());
}
} else {
@ -223,7 +223,7 @@ impl Iface {
}
}
if let Err(e) = set_dns(self, &self.name, network_domain, &ip_to_string(&default_gw)) {
if let Err(e) = set_dns(self, node.take_over_dns, &self.name, network_domain, &ip_to_string(&default_gw)) {
error!("failed to set dns: {}", e.as_str());
}
}
@ -922,21 +922,23 @@ fn add_resolvectl(name: &str, network_domain: &str) -> Result<()> {
Ok(())
}
fn set_dns(iface: &Iface, name: &str, network_domain: &str, gw: &str) -> Result<()> {
fn set_dns(iface: &Iface, take_over_dns: bool, name: &str, network_domain: &str, gw: &str) -> Result<()> {
error!("network_domain = {}", network_domain);
if iface.has_resolvectl {
add_resolvectl(name, network_domain)?;
} else {
backup_resolv_conf()?;
modify_resolv_conf(&vec!["100.100.100.100".to_owned()], network_domain, true)?;
if take_over_dns {
backup_resolv_conf()?;
modify_resolv_conf(&vec!["100.100.100.100".to_owned()], network_domain, true)?;
}
}
add_dns_route(name)?;
Ok(())
}
pub fn restore_dns() -> Result<()> {
pub fn restore_dns(take_over_dns: bool) -> Result<()> {
let eee = get_edge();
if !eee.device.has_resolvectl {
if !eee.device.has_resolvectl && take_over_dns{
// should restore /etc/resolv.conf
restore_resolv_conf()?;
}

View File

@ -796,7 +796,7 @@ pub fn set_dns(name: &str, _network_domain: &str, gw: &str, ifidx: u32) -> Resul
Ok(())
}
pub fn restore_dns() {}
pub fn restore_dns(take_over_dns: bool) {}
pub fn del_route(net: &Ipv4Net, gw: &Ipv4Addr) -> Result<()> {
error!("deleting route: {} gw {}", net, gw);

View File

@ -63,6 +63,10 @@ pub struct StartArguments {
#[arg(short, long, default_value_t=false)]
pub allow_routing: bool,
// take over dns or not if change /etc/resolv.conf
#[arg(long)]
pub take_dns: bool,
}
#[derive(Args, Debug)]
@ -102,6 +106,10 @@ pub struct AutoRunArgument {
#[arg(short, long, default_value="")]
pub route_file: String,
// take over dns or not if change /etc/resolv.conf
#[arg(long)]
pub take_dns: bool,
}
#[derive(Args, Debug)]
@ -186,6 +194,11 @@ pub struct CommandLine {
#[structopt(long, default_value="")]
pub route_file: String,
// take over dns or not
#[structopt(long)]
pub take_dns: bool,
}
impl Clone for CommandLine {
@ -208,6 +221,7 @@ impl Clone for CommandLine {
nat_server2: self.nat_server2.clone(),
route_str: self.route_str.clone(),
route_file: self.route_file.clone(),
take_dns: self.take_dns,
}
}
}