added --take-dns for take over dns
This commit is contained in:
parent
9d0a5289ea
commit
520ab964f3
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@ -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"]
|
// "rust-analyzer.cargo.features": ["tun"]
|
||||||
}
|
}
|
||||||
@ -5,7 +5,7 @@ use sdlan_sn_rs::utils::{Mac, Result, SDLanError};
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use md5::Md5;
|
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";
|
const DIGEST_KEY: &'static str = "H6p*2RfEu4ITcL";
|
||||||
type HmacMd5 = Hmac<Md5>;
|
type HmacMd5 = Hmac<Md5>;
|
||||||
@ -125,19 +125,25 @@ where T: Serialize + HMacCalculator,
|
|||||||
return Err(SDLanError::IOError("failed to calculate hmac".to_owned()));
|
return Err(SDLanError::IOError("failed to calculate hmac".to_owned()));
|
||||||
};
|
};
|
||||||
|
|
||||||
let Ok(response) = client
|
let response = match client
|
||||||
.post(url)
|
.post(url)
|
||||||
.header("X-sign", hmac)
|
.header("X-sign", hmac)
|
||||||
.json(&data)
|
.json(&data)
|
||||||
.send()
|
.send()
|
||||||
.await else {
|
.await {
|
||||||
return Err(SDLanError::IOError("failed to do request".to_owned()));
|
|
||||||
|
Ok(response) => {
|
||||||
|
response
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
return Err(SDLanError::IOError(format!("failed to do request: {}", e)));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// println!("status: {}", response.status());
|
// println!("status: {}", response.status());
|
||||||
let text = response.text().await.unwrap();
|
let text = response.text().await.unwrap();
|
||||||
|
|
||||||
println!("got test: {}", text);
|
// println!("got test: {}", text);
|
||||||
let data = serde_json::from_str(&text).unwrap();
|
let data = serde_json::from_str(&text).unwrap();
|
||||||
|
|
||||||
// println!("response: {}", response.text().await.unwrap());
|
// println!("response: {}", response.text().await.unwrap());
|
||||||
|
|||||||
@ -64,7 +64,7 @@ const APP_TOKEN_ENV_NAME: &str = "PUNCH_TOKEN";
|
|||||||
fn parse_connect_result(res: Result<ConnectResponse>) -> ConnectData {
|
fn parse_connect_result(res: Result<ConnectResponse>) -> ConnectData {
|
||||||
match res {
|
match res {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("failed to connect");
|
eprintln!("failed to connect: {}", e.as_str());
|
||||||
process::exit(-3);
|
process::exit(-3);
|
||||||
}
|
}
|
||||||
Ok(data) => {
|
Ok(data) => {
|
||||||
@ -123,6 +123,7 @@ async fn daemonize_me(
|
|||||||
route_file: String,
|
route_file: String,
|
||||||
route_str: String,
|
route_str: String,
|
||||||
mac: Mac,
|
mac: Mac,
|
||||||
|
take_over_dns: bool,
|
||||||
) {
|
) {
|
||||||
let _guard = log::init_log(&format!("{}/.output", get_base_dir()));
|
let _guard = log::init_log(&format!("{}/.output", get_base_dir()));
|
||||||
|
|
||||||
@ -184,6 +185,7 @@ async fn daemonize_me(
|
|||||||
allow_p2p: true,
|
allow_p2p: true,
|
||||||
route_file,
|
route_file,
|
||||||
route_str,
|
route_str,
|
||||||
|
take_dns: take_over_dns,
|
||||||
},
|
},
|
||||||
tx,
|
tx,
|
||||||
&punchnet::get_install_channel(),
|
&punchnet::get_install_channel(),
|
||||||
@ -262,7 +264,7 @@ async fn daemonize_me(
|
|||||||
edge.quic_endpoint.close(0u32.into(), "bye".as_bytes());
|
edge.quic_endpoint.close(0u32.into(), "bye".as_bytes());
|
||||||
println!("quic is quitting");
|
println!("quic is quitting");
|
||||||
delete_pid_file();
|
delete_pid_file();
|
||||||
let _ = restore_dns();
|
let _ = restore_dns(edge.take_over_dns);
|
||||||
debug!("restored dns");
|
debug!("restored dns");
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@ -330,6 +332,7 @@ fn main() {
|
|||||||
)
|
)
|
||||||
.await,
|
.await,
|
||||||
);
|
);
|
||||||
|
println!("login ok");
|
||||||
});
|
});
|
||||||
process::exit(0);
|
process::exit(0);
|
||||||
// TODO: do login with user
|
// TODO: do login with user
|
||||||
@ -341,6 +344,7 @@ fn main() {
|
|||||||
login_with_token(TEST_PREFIX, &client_id, &tk.token, mac, system, version)
|
login_with_token(TEST_PREFIX, &client_id, &tk.token, mac, system, version)
|
||||||
.await,
|
.await,
|
||||||
);
|
);
|
||||||
|
println!("login ok");
|
||||||
});
|
});
|
||||||
process::exit(0);
|
process::exit(0);
|
||||||
}
|
}
|
||||||
@ -493,6 +497,7 @@ fn run_it(
|
|||||||
rtinfo.route_file.clone(),
|
rtinfo.route_file.clone(),
|
||||||
rtinfo.route.clone(),
|
rtinfo.route.clone(),
|
||||||
mac,
|
mac,
|
||||||
|
rtinfo.take_dns,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
}),
|
}),
|
||||||
@ -529,6 +534,7 @@ fn run_it(
|
|||||||
tk.route_file.clone(),
|
tk.route_file.clone(),
|
||||||
tk.route.clone(),
|
tk.route.clone(),
|
||||||
mac,
|
mac,
|
||||||
|
tk.take_dns,
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
}),
|
}),
|
||||||
|
|||||||
@ -127,6 +127,7 @@ pub async fn init_edge(
|
|||||||
udp_sock_for_global_dns,
|
udp_sock_for_global_dns,
|
||||||
server_ip,
|
server_ip,
|
||||||
install_channel,
|
install_channel,
|
||||||
|
args.take_dns,
|
||||||
);
|
);
|
||||||
|
|
||||||
edge.route_table
|
edge.route_table
|
||||||
@ -258,6 +259,8 @@ pub struct Node {
|
|||||||
// store pending, and known peers
|
// store pending, and known peers
|
||||||
pub pending_peers: PeerMap,
|
pub pending_peers: PeerMap,
|
||||||
|
|
||||||
|
pub take_over_dns: bool,
|
||||||
|
|
||||||
#[cfg(any(feature = "tun", target_os = "windows"))]
|
#[cfg(any(feature = "tun", target_os = "windows"))]
|
||||||
pub arp_table: ArpTable,
|
pub arp_table: ArpTable,
|
||||||
|
|
||||||
@ -438,6 +441,7 @@ impl Node {
|
|||||||
udp_sock_for_global_dns: Arc<UdpSocket>,
|
udp_sock_for_global_dns: Arc<UdpSocket>,
|
||||||
server_ip: String,
|
server_ip: String,
|
||||||
install_channel: String,
|
install_channel: String,
|
||||||
|
take_over_dns: bool,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let mode = if cfg!(not(feature = "tun")) {
|
let mode = if cfg!(not(feature = "tun")) {
|
||||||
Mode::Tap
|
Mode::Tap
|
||||||
@ -517,6 +521,7 @@ impl Node {
|
|||||||
cookie_match: Queryer::new(),
|
cookie_match: Queryer::new(),
|
||||||
server_ip,
|
server_ip,
|
||||||
install_channel,
|
install_channel,
|
||||||
|
take_over_dns,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -196,7 +196,7 @@ impl Iface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: set dns should be opened
|
// 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());
|
error!("failed to set dns: {}", e.as_str());
|
||||||
}
|
}
|
||||||
} else {
|
} 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());
|
error!("failed to set dns: {}", e.as_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -922,21 +922,23 @@ fn add_resolvectl(name: &str, network_domain: &str) -> Result<()> {
|
|||||||
Ok(())
|
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);
|
error!("network_domain = {}", network_domain);
|
||||||
if iface.has_resolvectl {
|
if iface.has_resolvectl {
|
||||||
add_resolvectl(name, network_domain)?;
|
add_resolvectl(name, network_domain)?;
|
||||||
} else {
|
} else {
|
||||||
|
if take_over_dns {
|
||||||
backup_resolv_conf()?;
|
backup_resolv_conf()?;
|
||||||
modify_resolv_conf(&vec!["100.100.100.100".to_owned()], network_domain, true)?;
|
modify_resolv_conf(&vec!["100.100.100.100".to_owned()], network_domain, true)?;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
add_dns_route(name)?;
|
add_dns_route(name)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn restore_dns() -> Result<()> {
|
pub fn restore_dns(take_over_dns: bool) -> Result<()> {
|
||||||
let eee = get_edge();
|
let eee = get_edge();
|
||||||
if !eee.device.has_resolvectl {
|
if !eee.device.has_resolvectl && take_over_dns{
|
||||||
// should restore /etc/resolv.conf
|
// should restore /etc/resolv.conf
|
||||||
restore_resolv_conf()?;
|
restore_resolv_conf()?;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -796,7 +796,7 @@ pub fn set_dns(name: &str, _network_domain: &str, gw: &str, ifidx: u32) -> Resul
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn restore_dns() {}
|
pub fn restore_dns(take_over_dns: bool) {}
|
||||||
|
|
||||||
pub fn del_route(net: &Ipv4Net, gw: &Ipv4Addr) -> Result<()> {
|
pub fn del_route(net: &Ipv4Net, gw: &Ipv4Addr) -> Result<()> {
|
||||||
error!("deleting route: {} gw {}", net, gw);
|
error!("deleting route: {} gw {}", net, gw);
|
||||||
|
|||||||
@ -63,6 +63,10 @@ pub struct StartArguments {
|
|||||||
|
|
||||||
#[arg(short, long, default_value_t=false)]
|
#[arg(short, long, default_value_t=false)]
|
||||||
pub allow_routing: bool,
|
pub allow_routing: bool,
|
||||||
|
|
||||||
|
// take over dns or not if change /etc/resolv.conf
|
||||||
|
#[arg(long)]
|
||||||
|
pub take_dns: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Args, Debug)]
|
#[derive(Args, Debug)]
|
||||||
@ -102,6 +106,10 @@ pub struct AutoRunArgument {
|
|||||||
|
|
||||||
#[arg(short, long, default_value="")]
|
#[arg(short, long, default_value="")]
|
||||||
pub route_file: String,
|
pub route_file: String,
|
||||||
|
|
||||||
|
// take over dns or not if change /etc/resolv.conf
|
||||||
|
#[arg(long)]
|
||||||
|
pub take_dns: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Args, Debug)]
|
#[derive(Args, Debug)]
|
||||||
@ -186,6 +194,11 @@ pub struct CommandLine {
|
|||||||
|
|
||||||
#[structopt(long, default_value="")]
|
#[structopt(long, default_value="")]
|
||||||
pub route_file: String,
|
pub route_file: String,
|
||||||
|
|
||||||
|
// take over dns or not
|
||||||
|
#[structopt(long)]
|
||||||
|
pub take_dns: bool,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for CommandLine {
|
impl Clone for CommandLine {
|
||||||
@ -208,6 +221,7 @@ impl Clone for CommandLine {
|
|||||||
nat_server2: self.nat_server2.clone(),
|
nat_server2: self.nat_server2.clone(),
|
||||||
route_str: self.route_str.clone(),
|
route_str: self.route_str.clone(),
|
||||||
route_file: self.route_file.clone(),
|
route_file: self.route_file.clone(),
|
||||||
|
take_dns: self.take_dns,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user