core: add different config format support, implement wg-quick
This commit is contained in:
parent
5c16a6ff7f
commit
3eac566d25
|
@ -2,7 +2,7 @@ from __future__ import annotations
|
|||
from logging import getLogger
|
||||
|
||||
from .dazy import Meta, Template
|
||||
from .desc import WEEGEE_INTERFACE_CONF_WG, WEEGEE_PEER_CONF_WG, WEEGEE_CONF_WG
|
||||
from .desc import WEEGEE_INTERFACE_CONF_WG, WEEGEE_INTERFACE_CONF_WGQUICK, WEEGEE_PEER_CONF_WG, WEEGEE_CONF_WG
|
||||
|
||||
from .config import WeegeeConfig, WeegeeContext
|
||||
from .core import (
|
||||
|
@ -23,7 +23,7 @@ def setup(context: WeegeeContext) -> None:
|
|||
Meta.parse(context.instance, meta.BASE.get_name(), meta.BASE.spec).save()
|
||||
|
||||
logger.info('setup: templates')
|
||||
for temp in (WEEGEE_INTERFACE_CONF_WG, WEEGEE_PEER_CONF_WG, WEEGEE_CONF_WG):
|
||||
for temp in (WEEGEE_INTERFACE_CONF_WG, WEEGEE_INTERFACE_CONF_WGQUICK, WEEGEE_PEER_CONF_WG, WEEGEE_CONF_WG):
|
||||
logger.debug(' ' + temp.name)
|
||||
Template(temp.get_name(), temp.template, context.instance).save()
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ from typing import Optional as O, Any, Tuple, List, Dict, Set
|
|||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
from .dazy import Item, export_items, import_items
|
||||
from .wireguard import WireguardHostType
|
||||
from .wireguard import WireguardHostType, WireguardConfigFormat
|
||||
from . import (
|
||||
WeegeeContext, WeegeeConfig,
|
||||
WeegeeHook, WeegeeHost, WeegeePublicInterface, WeegeeInterface, WeegeePeer, WeegeeConnection,
|
||||
|
@ -493,9 +493,10 @@ def main():
|
|||
|
||||
def do_print_config(parser: argparse.ArgumentParser, args: argparse.Namespace, ctx: WeegeeContext) -> None:
|
||||
peer = args.objtype.load(ctx, args.name)
|
||||
print(config_interface(peer.interface.to_full()))
|
||||
print(config_interface(peer.interface.to_full(), args.format))
|
||||
|
||||
print_config = sparser.add_parser('print-config', help='show WireGuard configuration')
|
||||
print_config.add_argument('-f', '--format', type=WireguardConfigFormat, default=WireguardConfigFormat.WG, help='configuration format to use')
|
||||
print_config.add_argument('name', help='peer name')
|
||||
print_config.set_defaults(func=do_print_config, parser=print_config)
|
||||
|
||||
|
|
|
@ -7,13 +7,14 @@ from typing import Optional as O, Union as U, Any, TypeVar, Type as TypeOf, Tupl
|
|||
from .dazy import Type, Meta, Item, Template
|
||||
from .wireguard import (
|
||||
IPAddress, IPNetwork, IPInterface,
|
||||
WireguardHostType, WireguardHost, WireguardPeer, WireguardConnection, which_connection,
|
||||
WireguardHostType, WireguardHost, WireguardConfigFormat,
|
||||
WireguardPeer, WireguardConnection, which_connection,
|
||||
)
|
||||
from .config import WeegeeContext
|
||||
from .desc import (
|
||||
WeegeeMeta,
|
||||
WEEGEE_HOOK, WEEGEE_HOST, WEEGEE_PUB_INTERFACE, WEEGEE_INTERFACE, WEEGEE_PEER, WEEGEE_CONNECTION,
|
||||
WEEGEE_INTERFACE_CONF_WG, WEEGEE_PEER_CONF_WG, WEEGEE_CONF_WG,
|
||||
WEEGEE_INTERFACE_CONF_WG, WEEGEE_INTERFACE_CONF_WGQUICK, WEEGEE_PEER_CONF_WG, WEEGEE_CONF_WG,
|
||||
)
|
||||
|
||||
logger = getLogger(__name__)
|
||||
|
@ -385,9 +386,16 @@ class WeegeeInterface(WeegeePublicInterface):
|
|||
def hosts(self) -> List[WeegeeHost]:
|
||||
return [WeegeeHost(self.context, x) for x in self.item['hosts']]
|
||||
|
||||
def gen_config(self) -> str:
|
||||
template = Template.load(self.context.instance, WEEGEE_INTERFACE_CONF_WG.get_name())
|
||||
config = WEEGEE_INTERFACE_CONF_WG.make_config(self.context.instance,
|
||||
def gen_config(self, format: WireguardConfigFormat) -> str:
|
||||
if format == WireguardConfigFormat.WG:
|
||||
template_meta = WEEGEE_INTERFACE_CONF_WG
|
||||
elif format == WireguardConfigFormat.WGQuick:
|
||||
template_meta = WEEGEE_INTERFACE_CONF_WGQUICK
|
||||
else:
|
||||
raise ValueError('unimplemented config format: {}'.format(format))
|
||||
|
||||
template = Template.load(self.context.instance, template_meta.get_name())
|
||||
config = template_meta.make_config(self.context.instance,
|
||||
interface=self.item,
|
||||
)
|
||||
return template.render(config)
|
||||
|
@ -491,25 +499,36 @@ class WeegeeConnection(WeegeeBase):
|
|||
def peers(self, value: List[WeegeePeer]) -> None:
|
||||
self.items['peers'] = [x.item for x in value]
|
||||
|
||||
def gen_config(self, target: WeegeePeer) -> str:
|
||||
def gen_config(self, target: WeegeePeer, format: WireguardConfigFormat) -> str:
|
||||
peers = [p for p in self.peers if p != target]
|
||||
template = Template.load(self.context.instance, WEEGEE_PEER_CONF_WG.get_name())
|
||||
config = WEEGEE_PEER_CONF_WG.make_config(self.context.instance,
|
||||
|
||||
if format in (WireguardConfigFormat.WG, WireguardConfigFormat.WGQuick):
|
||||
template_meta = WEEGEE_PEER_CONF_WG
|
||||
else:
|
||||
raise ValueError('unimplemented config format: {}'.format(format))
|
||||
|
||||
template = Template.load(self.context.instance, template_meta.get_name())
|
||||
config = template_meta.make_config(self.context.instance,
|
||||
connection=self.item,
|
||||
peers={p.name: p.item for p in peers},
|
||||
)
|
||||
return template.render(config)
|
||||
|
||||
|
||||
def do_config_interface(interface: WeegeeInterface, peers: Set[WeegeePeer], connections: Set[WeegeeConnection]) -> str:
|
||||
def do_config_interface(interface: WeegeeInterface, format: WireguardConfigFormat, peers: Set[WeegeePeer], connections: Set[WeegeeConnection]) -> str:
|
||||
peer_configs = []
|
||||
for c in connections:
|
||||
for p in set(c.peers) & peers:
|
||||
peer_configs.append(c.gen_config(p))
|
||||
interface_config = interface.gen_config()
|
||||
peer_configs.append(c.gen_config(p, format))
|
||||
interface_config = interface.gen_config(format)
|
||||
|
||||
template = Template.load(interface.context.instance, WEEGEE_CONF_WG.get_name())
|
||||
config = WEEGEE_CONF_WG.make_config(interface.context.instance,
|
||||
if format in (WireguardConfigFormat.WG, WireguardConfigFormat.WGQuick):
|
||||
template_meta = WEEGEE_CONF_WG
|
||||
else:
|
||||
raise ValueError('unimplemented config format: {}'.format(format))
|
||||
|
||||
template = Template.load(interface.context.instance, template_meta.get_name())
|
||||
config = template_meta.make_config(interface.context.instance,
|
||||
interface_config=interface_config,
|
||||
peer_configs=peer_configs,
|
||||
)
|
||||
|
@ -570,6 +589,6 @@ def sync_all_interfaces(context: WeegeeContext, auto=False, log=None) -> None:
|
|||
for interface in WeegeeInterface.find_all(context):
|
||||
sync_interface(interface, auto=auto, log=log)
|
||||
|
||||
def config_interface(interface: WeegeeInterface) -> str:
|
||||
def config_interface(interface: WeegeeInterface, format: WireguardConfigFormat) -> str:
|
||||
peers, connections = find_interface_connections(interface.to_public())
|
||||
return do_config_interface(interface, peers, connections)
|
||||
return do_config_interface(interface, format, peers, connections)
|
||||
|
|
|
@ -154,6 +154,20 @@ PrivateKey = {{interface.private_key}}
|
|||
variables={'interface': WEEGEE_INTERFACE},
|
||||
)
|
||||
|
||||
WEEGEE_INTERFACE_CONF_WGQUICK = WeegeeTemplate(
|
||||
name='wg/interface-conf/wg-quick',
|
||||
version=1,
|
||||
template="""
|
||||
[Interface]
|
||||
Address = {{ interface.addresses | join(", ") }}
|
||||
{% if interface.port -%}
|
||||
ListenPort = {{interface.port}}
|
||||
{% endif -%}
|
||||
PrivateKey = {{interface.private_key}}
|
||||
""".strip(),
|
||||
variables={'interface': WEEGEE_INTERFACE},
|
||||
)
|
||||
|
||||
WEEGEE_PEER_CONF_WG = WeegeeTemplate(
|
||||
name='wg/peer-conf/wg',
|
||||
version=1,
|
||||
|
|
|
@ -274,3 +274,7 @@ def which_connection(type: WireguardHostType) -> Type[WireguardConnectionBase]:
|
|||
return {
|
||||
WireguardHostType.Linux: WireguardLinuxConnection,
|
||||
}.get(type, WireguardConnectionBase)
|
||||
|
||||
class WireguardConfigFormat(enum.Enum):
|
||||
WG = 'wg'
|
||||
WGQuick = 'wg-quick'
|
||||
|
|
Loading…
Reference in New Issue