diff options
author | sskaje <sskaje@gmail.com> | 2024-12-31 10:44:01 +0800 |
---|---|---|
committer | Christian Breunig <christian@breunig.cc> | 2025-01-19 00:17:12 +0100 |
commit | 2212a438b234f34f32e08efef2f841ba55a3b6a0 (patch) | |
tree | 47528dafb6733efb134a5ceee51e52118be7896f /src/op_mode/reset_wireguard.py | |
parent | 4d3e976271e30d70c8b2660d869a220de98d8c59 (diff) | |
download | vyos-1x-2212a438b234f34f32e08efef2f841ba55a3b6a0.tar.gz vyos-1x-2212a438b234f34f32e08efef2f841ba55a3b6a0.zip |
wireguard: T4930: allow peers via FQDN
* set interfaces wireguard wgXX peer YY hostname <fqdn>
Diffstat (limited to 'src/op_mode/reset_wireguard.py')
-rwxr-xr-x | src/op_mode/reset_wireguard.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/op_mode/reset_wireguard.py b/src/op_mode/reset_wireguard.py new file mode 100755 index 000000000..1fcfb31b5 --- /dev/null +++ b/src/op_mode/reset_wireguard.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 +# +# Copyright (C) 2025 VyOS maintainers and contributors +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 or later as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +import sys +import typing + +import vyos.opmode + +from vyos.ifconfig import WireGuardIf +from vyos.configquery import ConfigTreeQuery + + +def _verify(func): + """Decorator checks if WireGuard interface config exists""" + from functools import wraps + + @wraps(func) + def _wrapper(*args, **kwargs): + config = ConfigTreeQuery() + interface = kwargs.get('interface') + if not config.exists(['interfaces', 'wireguard', interface]): + unconf_message = f'WireGuard interface {interface} is not configured' + raise vyos.opmode.UnconfiguredSubsystem(unconf_message) + return func(*args, **kwargs) + + return _wrapper + + +@_verify +def reset_peer(interface: str, peer: typing.Optional[str] = None): + intf = WireGuardIf(interface, create=False, debug=False) + return intf.operational.reset_peer(peer) + + +if __name__ == '__main__': + try: + res = vyos.opmode.run(sys.modules[__name__]) + if res: + print(res) + except (ValueError, vyos.opmode.Error) as e: + print(e) + sys.exit(1) |