Skip to content

Commit 0ed1821

Browse files
fix bug on network (#23)
* fix bug on network Signed-off-by: andrewmatilde <davis6813585853062@outlook.com> * minor fix Signed-off-by: andrewmatilde <davis6813585853062@outlook.com> * block none port Signed-off-by: andrewmatilde <davis6813585853062@outlook.com> * block none port Signed-off-by: andrewmatilde <davis6813585853062@outlook.com> * main > all Signed-off-by: andrewmatilde <davis6813585853062@outlook.com>
1 parent 50e1d1c commit 0ed1821

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Support json and yaml config.
8383
Example of config could be found in `./config-examples`
8484
## Yaml config file example
8585
```yaml
86-
proxy_ports: [80] # option u16 vec ; proxy all tcp packet if not provided
86+
proxy_ports: [80] # option u16 vec ; Do nothing if not provided
8787
interface: eth33 # option string
8888
rules: # option rule vec
8989
- target: Request # Request or Response.

rs-tproxy-controller/src/proxy/exec.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ impl Proxy {
143143

144144
pub async fn reload(&mut self, config: ProxyRawConfig) -> anyhow::Result<()> {
145145
self.stop().await?;
146+
if config.proxy_ports.is_none() {
147+
return Ok(());
148+
}
146149
if self.task.is_none() {
147150
let mut new = Self::new(self.opt.verbose);
148151
self.opt = new.opt;

rs-tproxy-controller/src/proxy/net/bridge.rs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::process::Command;
2+
use std::net::Ipv4Addr;
23

34
use anyhow::{anyhow, Result};
45
use default_net;
@@ -38,7 +39,7 @@ impl NetEnv {
3839
break key;
3940
}
4041
};
41-
let ip_route_store = Uuid::new_v4().to_string();
42+
let ip_route_store = "ip_route_store".to_string() + &Uuid::new_v4().to_string();
4243
let device = get_default_interface().unwrap();
4344
let netns = prefix.clone() + "ns";
4445
let bridge1 = prefix.clone() + "b1";
@@ -177,19 +178,33 @@ impl NetEnv {
177178
}
178179

179180
pub fn clear_bridge(&self) -> Result<()> {
180-
let restore = format!("ip route restore < {}", &self.ip_route_store);
181181
let restore_dns = "cp /etc/resolv.conf.bak /etc/resolv.conf";
182182
let remove_store = format!("rm -f {}", &self.ip_route_store);
183+
184+
let net: Ipv4Network = self.ip.parse().unwrap();
185+
let net_domain = Ipv4Addr::from(u32::from(net.ip()) & u32::from(net.mask())).to_string()
186+
+ "/"
187+
+ &net.prefix().to_string();
188+
let del_default_route = format!("ip route del {} dev {} proto kernel scope link src {}", &net_domain, &self.device, &net.ip().to_string());
189+
183190
let cmdvv = vec![
184191
ip_netns_del(&self.netns),
185192
ip_link_del_bridge(&self.bridge1),
186193
ip_address("add", &self.ip, &self.device),
187194
bash_c(restore_dns),
188-
bash_c(&restore),
189-
bash_c(&remove_store),
195+
bash_c(&del_default_route),
190196
clear_ebtables(),
191197
];
192198
execute_all_with_log_error(cmdvv)?;
199+
200+
let ip_routes= restore_all_ip_routes(&self.ip_route_store)?;
201+
let iproute_cmds: Vec<Vec<&str>> = ip_routes.iter().map(|s| bash_c(&**s)).collect();
202+
execute_all_with_log_error(iproute_cmds)?;
203+
204+
let cmdvv = vec![
205+
bash_c(&remove_store),
206+
];
207+
execute_all_with_log_error(cmdvv)?;
193208
Ok(())
194209
}
195210
}
@@ -353,3 +368,22 @@ pub fn get_default_interface() -> Result<NetworkInterface> {
353368
}
354369
Err(anyhow!("no valid interface"))
355370
}
371+
372+
pub fn restore_all_ip_routes(path : &str) -> Result<Vec<String>> {
373+
let cmd_string = format!("ip route showdump < {}", path);
374+
let mut cmd = Command::new("sh");
375+
cmd.arg("-c")
376+
.arg(cmd_string);
377+
let stdo = cmd.output()?.stdout;
378+
let out = String::from_utf8_lossy(stdo.as_slice());
379+
380+
let mut ip_routes: Vec<_> = out.split('\n').collect();
381+
ip_routes.reverse();
382+
let mut route_cmds: Vec<String> = Vec::new();
383+
for ip_route in ip_routes {
384+
if !ip_route.is_empty() {
385+
route_cmds.push(format!("{} {}", "ip route add", ip_route));
386+
}
387+
}
388+
Ok(route_cmds)
389+
}

0 commit comments

Comments
 (0)