core: pass other peers to interface config

This commit is contained in:
Shiz 2022-01-18 12:33:15 +01:00
parent 3eac566d25
commit d128a837ba
2 changed files with 7 additions and 4 deletions

View File

@ -386,7 +386,7 @@ class WeegeeInterface(WeegeePublicInterface):
def hosts(self) -> List[WeegeeHost]:
return [WeegeeHost(self.context, x) for x in self.item['hosts']]
def gen_config(self, format: WireguardConfigFormat) -> str:
def gen_config(self, peers: Set['WeegeePeer'], format: WireguardConfigFormat) -> str:
if format == WireguardConfigFormat.WG:
template_meta = WEEGEE_INTERFACE_CONF_WG
elif format == WireguardConfigFormat.WGQuick:
@ -397,6 +397,7 @@ class WeegeeInterface(WeegeePublicInterface):
template = Template.load(self.context.instance, template_meta.get_name())
config = template_meta.make_config(self.context.instance,
interface=self.item,
peers={p.name: p.item for p in peers},
)
return template.render(config)
@ -517,10 +518,12 @@ class WeegeeConnection(WeegeeBase):
def do_config_interface(interface: WeegeeInterface, format: WireguardConfigFormat, peers: Set[WeegeePeer], connections: Set[WeegeeConnection]) -> str:
peer_configs = []
other_peers = set()
for c in connections:
for p in set(c.peers) & peers:
peer_configs.append(c.gen_config(p, format))
interface_config = interface.gen_config(format)
other_peers.update(set(c.peers) - peers)
interface_config = interface.gen_config(other_peers, format)
if format in (WireguardConfigFormat.WG, WireguardConfigFormat.WGQuick):
template_meta = WEEGEE_CONF_WG

View File

@ -151,7 +151,7 @@ ListenPort = {{interface.port}}
{% endif -%}
PrivateKey = {{interface.private_key}}
""".strip(),
variables={'interface': WEEGEE_INTERFACE},
variables={'interface': WEEGEE_INTERFACE, 'peers': {str: WEEGEE_PEER}},
)
WEEGEE_INTERFACE_CONF_WGQUICK = WeegeeTemplate(
@ -165,7 +165,7 @@ ListenPort = {{interface.port}}
{% endif -%}
PrivateKey = {{interface.private_key}}
""".strip(),
variables={'interface': WEEGEE_INTERFACE},
variables={'interface': WEEGEE_INTERFACE, 'peers': {str: WEEGEE_PEER}},
)
WEEGEE_PEER_CONF_WG = WeegeeTemplate(