Change ip commands to replace

This commit is contained in:
LilyRose2798 2024-04-17 02:13:43 +10:00
parent c608379ec2
commit 97171ec799
1 changed files with 6 additions and 14 deletions

View File

@ -98,8 +98,9 @@ def generate_wg_configs(config: WovenConfig):
name_a = f"{id_a}-{id_b}-loop" name_a = f"{id_a}-{id_b}-loop"
name_b = f"{id_b}-{id_a}-loop" name_b = f"{id_b}-{id_a}-loop"
preup_a = [f"ip ro replace {node_b.listen_address}/32 dev {node_a.interface_name} via {node_a.listen_gateway} metric 10 src {node_a.listen_address}"]
predown_a = [f"ip ro del {node_b.listen_address}/32 dev {node_a.interface_name} via {node_a.listen_gateway} metric 10 src {node_a.listen_address}"] predown_a = [f"ip ro del {node_b.listen_address}/32 dev {node_a.interface_name} via {node_a.listen_gateway} metric 10 src {node_a.listen_address}"]
postup_a = [f"ip ro add {sn} dev {name_a} via {ipv4_b} metric 10" for sn in node_b.routed_ipv4_subnets] + [f"ip -6 ro add {sn} dev {name_a} via {ipv6_b} metric 10" for sn in node_b.routed_ipv6_subnets] postup_a = [f"ip ro replace {sn} dev {name_a} via {ipv4_b} metric 10" for sn in node_b.routed_ipv4_subnets] + [f"ip -6 ro replace {sn} dev {name_a} via {ipv6_b} metric 10" for sn in node_b.routed_ipv6_subnets]
postdown_a = [f"ip ro del {sn} dev {name_a} via {ipv4_b} metric 10" for sn in node_b.routed_ipv4_subnets] + [f"ip -6 ro del {sn} dev {name_a} via {ipv6_b} metric 10" for sn in node_b.routed_ipv6_subnets] postdown_a = [f"ip ro del {sn} dev {name_a} via {ipv4_b} metric 10" for sn in node_b.routed_ipv4_subnets] + [f"ip -6 ro del {sn} dev {name_a} via {ipv6_b} metric 10" for sn in node_b.routed_ipv6_subnets]
config_a = WireguardConfig( config_a = WireguardConfig(
@ -107,7 +108,7 @@ def generate_wg_configs(config: WovenConfig):
listen_port = port, listen_port = port,
private_key = key_a, private_key = key_a,
table = False, table = False,
preup = [f"ip ro add {node_b.listen_address}/32 dev {node_a.interface_name} via {node_a.listen_gateway} metric 10 src {node_a.listen_address}"], preup = preup_a,
predown = predown_a, predown = predown_a,
postup = postup_a, postup = postup_a,
postdown = postdown_a, postdown = postdown_a,
@ -122,8 +123,9 @@ def generate_wg_configs(config: WovenConfig):
} }
) )
preup_b = [f"ip ro replace {node_a.listen_address}/32 dev {node_b.interface_name} via {node_b.listen_gateway} metric 10 src {node_b.listen_address}"]
predown_b = [f"ip ro del {node_a.listen_address}/32 dev {node_b.interface_name} via {node_b.listen_gateway} metric 10 src {node_b.listen_address}"] predown_b = [f"ip ro del {node_a.listen_address}/32 dev {node_b.interface_name} via {node_b.listen_gateway} metric 10 src {node_b.listen_address}"]
postup_b = [f"ip ro add {sn} dev {name_b} via {ipv4_a} metric 10" for sn in node_a.routed_ipv4_subnets] + [f"ip -6 ro add {sn} dev {name_b} via {ipv6_a} metric 10" for sn in node_a.routed_ipv6_subnets] postup_b = [f"ip ro replace {sn} dev {name_b} via {ipv4_a} metric 10" for sn in node_a.routed_ipv4_subnets] + [f"ip -6 ro replace {sn} dev {name_b} via {ipv6_a} metric 10" for sn in node_a.routed_ipv6_subnets]
postdown_b = [f"ip ro del {sn} dev {name_b} via {ipv4_a} metric 10" for sn in node_a.routed_ipv4_subnets] + [f"ip -6 ro del {sn} dev {name_b} via {ipv6_a} metric 10" for sn in node_a.routed_ipv6_subnets] postdown_b = [f"ip ro del {sn} dev {name_b} via {ipv4_a} metric 10" for sn in node_a.routed_ipv4_subnets] + [f"ip -6 ro del {sn} dev {name_b} via {ipv6_a} metric 10" for sn in node_a.routed_ipv6_subnets]
config_b = WireguardConfig( config_b = WireguardConfig(
@ -131,7 +133,7 @@ def generate_wg_configs(config: WovenConfig):
listen_port = port, listen_port = port,
private_key = key_b, private_key = key_b,
table = False, table = False,
preup = [f"ip ro add {node_a.listen_address}/32 dev {node_b.interface_name} via {node_b.listen_gateway} metric 10 src {node_b.listen_address}"], preup = preup_b,
predown = predown_b, predown = predown_b,
postup = postup_b, postup = postup_b,
postdown = postdown_b, postdown = postdown_b,
@ -148,19 +150,9 @@ def generate_wg_configs(config: WovenConfig):
cs[id_a].put(StringIO(config_a.to_wgconfig(wgquick_format = True)), f"/etc/wireguard/{name_a}.conf") cs[id_a].put(StringIO(config_a.to_wgconfig(wgquick_format = True)), f"/etc/wireguard/{name_a}.conf")
for p in predown_a + postdown_a:
try:
cs[id_a].run(p)
except UnexpectedExit:
pass
cs[id_a].run(f"systemctl start wg-quick@{name_a}.service") cs[id_a].run(f"systemctl start wg-quick@{name_a}.service")
cs[id_b].put(StringIO(config_b.to_wgconfig(wgquick_format = True)), f"/etc/wireguard/{name_b}.conf") cs[id_b].put(StringIO(config_b.to_wgconfig(wgquick_format = True)), f"/etc/wireguard/{name_b}.conf")
for p in predown_b + postdown_b:
try:
cs[id_b].run(p)
except UnexpectedExit:
pass
cs[id_b].run(f"systemctl start wg-quick@{name_b}.service") cs[id_b].run(f"systemctl start wg-quick@{name_b}.service")