mirror of
https://github.com/eycorsican/leaf.git
synced 2026-12-05 16:02:32 +00:00
outbound/tun: gateway mode
This commit is contained in:
1 parent
3a3b2e7797
commit
927f404ced
5 files changed
+205
No files matched your search
@@ -16,3 +16,4 @@ panic = "abort"
|
||||
[profile.dev]
|
||||
opt-level = 0
|
||||
debug = true
|
||||
split-debuginfo = "unpacked"
|
||||
@@ -308,3 +308,93 @@ pub fn delete_default_ipv6_rule(addr: Ipv6Addr) -> Result<()> {
|
||||
.expect("failed to execute command");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_ipv4_forwarding() -> Result<bool> {
|
||||
let out = Command::new("sysctl")
|
||||
.arg("-n")
|
||||
.arg("net.ipv4.ip_forward")
|
||||
.output()
|
||||
.expect("failed to execute command");
|
||||
let out = String::from_utf8_lossy(&out.stdout).to_string();
|
||||
let res = if out
|
||||
.trim()
|
||||
.parse::<i8>()
|
||||
.expect("unexpected ip_forward value")
|
||||
== 0
|
||||
{
|
||||
false
|
||||
} else {
|
||||
true
|
||||
};
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
pub fn get_ipv6_forwarding() -> Result<bool> {
|
||||
let out = Command::new("sysctl")
|
||||
.arg("-n")
|
||||
.arg("net.ipv6.conf.all.forwarding")
|
||||
.output()
|
||||
.expect("failed to execute command");
|
||||
let out = String::from_utf8_lossy(&out.stdout).to_string();
|
||||
let res = if out
|
||||
.trim()
|
||||
.parse::<i8>()
|
||||
.expect("unexpected ip_forward value")
|
||||
== 0
|
||||
{
|
||||
false
|
||||
} else {
|
||||
true
|
||||
};
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
pub fn set_ipv4_forwarding(val: bool) -> Result<()> {
|
||||
Command::new("sysctl")
|
||||
.arg("-w")
|
||||
.arg(format!(
|
||||
"net.ipv4.ip_forward={}",
|
||||
if val { "1" } else { "0" }
|
||||
))
|
||||
.status()
|
||||
.expect("failed to execute command");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_ipv6_forwarding(val: bool) -> Result<()> {
|
||||
Command::new("sysctl")
|
||||
.arg("-w")
|
||||
.arg(format!(
|
||||
"net.ipv6.conf.all.forwarding={}",
|
||||
if val { "1" } else { "0" }
|
||||
))
|
||||
.status()
|
||||
.expect("failed to execute command");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn add_iptable_forward(interface: &str) -> Result<()> {
|
||||
Command::new("iptables")
|
||||
.arg("-I")
|
||||
.arg("FORWARD")
|
||||
.arg("-o")
|
||||
.arg(interface)
|
||||
.arg("-j")
|
||||
.arg("ACCEPT")
|
||||
.status()
|
||||
.expect("failed to execute command");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn delete_iptable_forward(interface: &str) -> Result<()> {
|
||||
Command::new("iptables")
|
||||
.arg("-D")
|
||||
.arg("FORWARD")
|
||||
.arg("-o")
|
||||
.arg(interface)
|
||||
.arg("-j")
|
||||
.arg("ACCEPT")
|
||||
.status()
|
||||
.expect("failed to execute command");
|
||||
Ok(())
|
||||
}
|
||||
@@ -195,3 +195,67 @@ pub fn delete_default_ipv6_route(ifscope: Option<String>) -> Result<()> {
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_ipv4_forwarding() -> Result<bool> {
|
||||
let out = Command::new("sysctl")
|
||||
.arg("-n")
|
||||
.arg("net.inet.ip.forwarding")
|
||||
.output()
|
||||
.expect("failed to execute command");
|
||||
let out = String::from_utf8_lossy(&out.stdout).to_string();
|
||||
let res = if out
|
||||
.trim()
|
||||
.parse::<i8>()
|
||||
.expect("unexpected ip_forward value")
|
||||
== 0
|
||||
{
|
||||
false
|
||||
} else {
|
||||
true
|
||||
};
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
pub fn get_ipv6_forwarding() -> Result<bool> {
|
||||
let out = Command::new("sysctl")
|
||||
.arg("-n")
|
||||
.arg("net.inet6.ip6.forwarding")
|
||||
.output()
|
||||
.expect("failed to execute command");
|
||||
let out = String::from_utf8_lossy(&out.stdout).to_string();
|
||||
let res = if out
|
||||
.trim()
|
||||
.parse::<i8>()
|
||||
.expect("unexpected ip_forward value")
|
||||
== 0
|
||||
{
|
||||
false
|
||||
} else {
|
||||
true
|
||||
};
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
pub fn set_ipv4_forwarding(val: bool) -> Result<()> {
|
||||
Command::new("sysctl")
|
||||
.arg("-w")
|
||||
.arg(format!(
|
||||
"net.inet.ip.forwarding={}",
|
||||
if val { "1" } else { "0" }
|
||||
))
|
||||
.status()
|
||||
.expect("failed to execute command");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_ipv6_forwarding(val: bool) -> Result<()> {
|
||||
Command::new("sysctl")
|
||||
.arg("-w")
|
||||
.arg(format!(
|
||||
"net.inet6.ip6.forwarding={}",
|
||||
if val { "1" } else { "0" }
|
||||
))
|
||||
.status()
|
||||
.expect("failed to execute command");
|
||||
Ok(())
|
||||
}
|
||||
@@ -93,6 +93,10 @@ lazy_static! {
|
||||
pub static ref OUTBOUND_INTERFACE: String = {
|
||||
get_env_var("OUTBOUND_INTERFACE", "".to_string())
|
||||
};
|
||||
|
||||
pub static ref GATEWAY_MODE: bool = {
|
||||
get_env_var("GATEWAY_MODE", false)
|
||||
};
|
||||
}
|
||||
|
||||
/// UDP session timeout. A UDP session shall be terminated if there are no
|
||||
|
||||
@@ -6,6 +6,8 @@ pub struct NetInfo {
|
||||
pub default_ipv6_gateway: Option<String>,
|
||||
pub default_ipv4_address: Option<String>,
|
||||
pub default_ipv6_address: Option<String>,
|
||||
pub ipv4_forwarding: bool,
|
||||
pub ipv6_forwarding: bool,
|
||||
pub default_interface: Option<String>,
|
||||
}
|
||||
|
||||
@@ -16,6 +18,8 @@ impl Default for NetInfo {
|
||||
default_ipv6_gateway: None,
|
||||
default_ipv4_address: None,
|
||||
default_ipv6_address: None,
|
||||
ipv4_forwarding: false,
|
||||
ipv6_forwarding: false,
|
||||
default_interface: None,
|
||||
}
|
||||
}
|
||||
@@ -61,12 +65,20 @@ pub fn get_net_info() -> NetInfo {
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let ipv4_forwarding = common::cmd::get_ipv4_forwarding().unwrap();
|
||||
let ipv6_forwarding = if *option::ENABLE_IPV6 {
|
||||
common::cmd::get_ipv6_forwarding().unwrap()
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
NetInfo {
|
||||
default_ipv4_gateway: Some(ipv4_gw),
|
||||
default_ipv6_gateway: ipv6_gw,
|
||||
default_ipv4_address: ipv4_addr,
|
||||
default_ipv6_address: ipv6_addr,
|
||||
ipv4_forwarding,
|
||||
ipv6_forwarding,
|
||||
default_interface: Some(iface),
|
||||
}
|
||||
}
|
||||
@@ -77,6 +89,8 @@ pub fn post_tun_creation_setup(net_info: &NetInfo) {
|
||||
default_ipv6_gateway: ipv6_gw,
|
||||
default_ipv4_address: ipv4_addr,
|
||||
default_ipv6_address: ipv6_addr,
|
||||
ipv4_forwarding,
|
||||
ipv6_forwarding,
|
||||
default_interface: Some(iface),
|
||||
} = net_info
|
||||
{
|
||||
@@ -110,6 +124,10 @@ pub fn post_tun_creation_setup(net_info: &NetInfo) {
|
||||
}
|
||||
}
|
||||
|
||||
if *option::GATEWAY_MODE && !ipv4_forwarding {
|
||||
common::cmd::set_ipv4_forwarding(true).unwrap();
|
||||
}
|
||||
|
||||
if *option::ENABLE_IPV6 {
|
||||
common::cmd::add_interface_ipv6_address(
|
||||
option::DEFAULT_TUN_NAME,
|
||||
@@ -140,6 +158,17 @@ pub fn post_tun_creation_setup(net_info: &NetInfo) {
|
||||
common::cmd::add_default_ipv6_rule(a.parse::<Ipv6Addr>().unwrap()).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
if *option::GATEWAY_MODE && !ipv6_forwarding {
|
||||
common::cmd::set_ipv6_forwarding(true).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
if *option::GATEWAY_MODE {
|
||||
common::cmd::add_iptable_forward(option::DEFAULT_TUN_NAME).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -150,6 +179,8 @@ pub fn post_tun_completion_setup(net_info: &NetInfo) {
|
||||
default_ipv6_gateway: ipv6_gw,
|
||||
default_ipv4_address: ipv4_addr,
|
||||
default_ipv6_address: ipv6_addr,
|
||||
ipv4_forwarding,
|
||||
ipv6_forwarding,
|
||||
default_interface: Some(iface),
|
||||
} = &net_info
|
||||
{
|
||||
@@ -171,6 +202,10 @@ pub fn post_tun_completion_setup(net_info: &NetInfo) {
|
||||
}
|
||||
}
|
||||
|
||||
if *option::GATEWAY_MODE && !ipv4_forwarding {
|
||||
common::cmd::set_ipv4_forwarding(false).unwrap();
|
||||
}
|
||||
|
||||
if *option::ENABLE_IPV6 {
|
||||
if let Some(ipv6_gw) = ipv6_gw {
|
||||
common::cmd::delete_default_ipv6_route(None).unwrap();
|
||||
@@ -189,6 +224,17 @@ pub fn post_tun_completion_setup(net_info: &NetInfo) {
|
||||
common::cmd::delete_default_ipv6_rule(a.parse::<Ipv6Addr>().unwrap()).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
if *option::GATEWAY_MODE && !ipv6_forwarding {
|
||||
common::cmd::set_ipv6_forwarding(false).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
if *option::GATEWAY_MODE {
|
||||
common::cmd::delete_iptable_forward(option::DEFAULT_TUN_NAME).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user