From 05e79a4bb2137ee3ea563ced7f64b033c41eb438 Mon Sep 17 00:00:00 2001 From: Thomas Mangin Date: Tue, 31 Mar 2020 16:59:33 +0100 Subject: ifconfig: T2057: allow to disable interface debugging In order to be able to use the interface class with operational mode, these commands must not log as it would otherwise mess with the output on the screen. --- python/vyos/ifconfig/control.py | 12 +++++++++++- python/vyos/ifconfig/dhcp.py | 4 +++- python/vyos/ifconfig/interface.py | 10 ++++++---- 3 files changed, 20 insertions(+), 6 deletions(-) (limited to 'python/vyos/ifconfig') diff --git a/python/vyos/ifconfig/control.py b/python/vyos/ifconfig/control.py index 1c9f7e284..c1a073aef 100644 --- a/python/vyos/ifconfig/control.py +++ b/python/vyos/ifconfig/control.py @@ -24,8 +24,18 @@ class Control(Register): _command_get = {} _command_set = {} + def __init__(self, **kargs): + # some commands (such as operation comands - show interfaces, etc.) + # need to query the interface statistics. If the interface + # code is used and the debugging is enabled, the screen output + # will include both the command but also the debugging for that command + # to prevent this, debugging can be explicitely disabled + + # if debug is not explicitely disabled the the config, enable it + self.debug = kargs.get('debug', True) + def _debug_msg(self, msg): - if os.path.isfile('/tmp/vyos.ifconfig.debug'): + if os.path.isfile('/tmp/vyos.ifconfig.debug') and self.debug: print('DEBUG/{:<6} {}'.format(self.config['ifname'], msg)) def _popen(self, command): diff --git a/python/vyos/ifconfig/dhcp.py b/python/vyos/ifconfig/dhcp.py index 8d3653433..8ec8263b5 100644 --- a/python/vyos/ifconfig/dhcp.py +++ b/python/vyos/ifconfig/dhcp.py @@ -50,7 +50,9 @@ interface "{{ intf }}" { class DHCP (Control): client_base = r'/var/lib/dhcp/dhclient_' - def __init__ (self, ifname): + def __init__ (self, ifname, **kargs): + super().__init__(**kargs) + # per interface DHCP config files self._dhcp = { 4: { diff --git a/python/vyos/ifconfig/interface.py b/python/vyos/ifconfig/interface.py index 8b41d6158..4a34f96d6 100644 --- a/python/vyos/ifconfig/interface.py +++ b/python/vyos/ifconfig/interface.py @@ -162,15 +162,17 @@ class Interface(DHCP): >>> i = Interface('eth0') """ - DHCP.__init__(self, ifname) - self.config = deepcopy(self.default) - self.config['ifname'] = ifname - for k in self.options: if k in kargs: self.config[k] = kargs[k] + # make sure the ifname is the first argument and not from the dict + self.config['ifname'] = ifname + + # we must have updated config before initialising the Interface + super().__init__(ifname, **kargs) + if not os.path.exists('/sys/class/net/{}'.format(self.config['ifname'])): if not self.config['type']: raise Exception('interface "{}" not found'.format(self.config['ifname'])) -- cgit v1.2.3 From 10f31524db1623bf115f88c3a432b5c37522d2e4 Mon Sep 17 00:00:00 2001 From: Thomas Mangin Date: Mon, 30 Mar 2020 18:05:40 +0100 Subject: ifconfig: T2057: centralise wireguard code Some left over wireguard code was left in the interface.py file. The code was moved into the wireguard.py file and the now empty interface.py file removed as no longer holdin any code. --- python/vyos/ifconfig/wireguard.py | 56 +++++++++++++++++++++++++++++---- python/vyos/interfaces.py | 65 --------------------------------------- src/completion/list_interfaces.py | 1 - src/op_mode/wireguard.py | 2 +- 4 files changed, 51 insertions(+), 73 deletions(-) delete mode 100644 python/vyos/interfaces.py (limited to 'python/vyos/ifconfig') diff --git a/python/vyos/ifconfig/wireguard.py b/python/vyos/ifconfig/wireguard.py index 7f123f9b4..def5ab7c5 100644 --- a/python/vyos/ifconfig/wireguard.py +++ b/python/vyos/ifconfig/wireguard.py @@ -15,14 +15,15 @@ import os +import time +import subprocess +from datetime import timedelta -import vyos +from vyos.config import Config from vyos.ifconfig.interface import Interface -from vyos.interfaces import wireguard_dump -from datetime import timedelta -import time from hurry.filesize import size,alternative + @Interface.register class WireGuardIf(Interface): default = { @@ -108,10 +109,10 @@ class WireGuardIf(Interface): return self._cmd(cmd) def op_show_interface(self): - wgdump = wireguard_dump().get( + wgdump = self._dump().get( self.config['ifname'], None) - c = vyos.config.Config() + c = Config() c.set_level(["interfaces", "wireguard", self.config['ifname']]) description = c.return_effective_value(["description"]) ips = c.return_effective_values(["address"]) @@ -177,3 +178,46 @@ class WireGuardIf(Interface): wgpeer['persistent_keepalive'])) print() super().op_show_interface_stats() + + def _dump(self): + """Dump wireguard data in a python friendly way.""" + last_device = None + output = {} + + # Dump wireguard connection data + _f = self._cmd('wg show all dump') + for line in _f.split('\n'): + if not line: + # Skip empty lines and last line + continue + items = line.split('\t') + + if last_device != items[0]: + # We are currently entering a new node + device, private_key, public_key, listen_port, fw_mark = items + last_device = device + + output[device] = { + 'private_key': None if private_key == '(none)' else private_key, + 'public_key': None if public_key == '(none)' else public_key, + 'listen_port': int(listen_port), + 'fw_mark': None if fw_mark == 'off' else int(fw_mark), + 'peers': {}, + } + else: + # We are entering a peer + device, public_key, preshared_key, endpoint, allowed_ips, latest_handshake, transfer_rx, transfer_tx, persistent_keepalive = items + if allowed_ips == '(none)': + allowed_ips = [] + else: + allowed_ips = allowed_ips.split('\t') + output[device]['peers'][public_key] = { + 'preshared_key': None if preshared_key == '(none)' else preshared_key, + 'endpoint': None if endpoint == '(none)' else endpoint, + 'allowed_ips': allowed_ips, + 'latest_handshake': None if latest_handshake == '0' else int(latest_handshake), + 'transfer_rx': int(transfer_rx), + 'transfer_tx': int(transfer_tx), + 'persistent_keepalive': None if persistent_keepalive == 'off' else int(persistent_keepalive), + } + return output diff --git a/python/vyos/interfaces.py b/python/vyos/interfaces.py deleted file mode 100644 index 4697c0acc..000000000 --- a/python/vyos/interfaces.py +++ /dev/null @@ -1,65 +0,0 @@ -# Copyright 2018 VyOS maintainers and contributors -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library 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 -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library. If not, see . - -import re -import json - -from vyos.ifconfig import Interface -import subprocess -import netifaces - - -def wireguard_dump(): - """Dump wireguard data in a python friendly way.""" - last_device=None - output = {} - - # Dump wireguard connection data - _f = subprocess.check_output(["wg", "show", "all", "dump"]).decode() - for line in _f.split('\n'): - if not line: - # Skip empty lines and last line - continue - items = line.split('\t') - - if last_device != items[0]: - # We are currently entering a new node - device, private_key, public_key, listen_port, fw_mark = items - last_device = device - - output[device] = { - 'private_key': None if private_key == '(none)' else private_key, - 'public_key': None if public_key == '(none)' else public_key, - 'listen_port': int(listen_port), - 'fw_mark': None if fw_mark == 'off' else int(fw_mark), - 'peers': {}, - } - else: - # We are entering a peer - device, public_key, preshared_key, endpoint, allowed_ips, latest_handshake, transfer_rx, transfer_tx, persistent_keepalive = items - if allowed_ips == '(none)': - allowed_ips = [] - else: - allowed_ips = allowed_ips.split('\t') - output[device]['peers'][public_key] = { - 'preshared_key': None if preshared_key == '(none)' else preshared_key, - 'endpoint': None if endpoint == '(none)' else endpoint, - 'allowed_ips': allowed_ips, - 'latest_handshake': None if latest_handshake == '0' else int(latest_handshake), - 'transfer_rx': int(transfer_rx), - 'transfer_tx': int(transfer_tx), - 'persistent_keepalive': None if persistent_keepalive == 'off' else int(persistent_keepalive), - } - return output diff --git a/src/completion/list_interfaces.py b/src/completion/list_interfaces.py index 77de4e327..98b32797a 100755 --- a/src/completion/list_interfaces.py +++ b/src/completion/list_interfaces.py @@ -2,7 +2,6 @@ import sys import argparse -import vyos.interfaces from vyos.ifconfig import Interface parser = argparse.ArgumentParser() diff --git a/src/op_mode/wireguard.py b/src/op_mode/wireguard.py index 38c061cf4..512c80dda 100755 --- a/src/op_mode/wireguard.py +++ b/src/op_mode/wireguard.py @@ -150,7 +150,7 @@ if __name__ == '__main__': if args.listkdir: list_key_dirs() if args.showinterface: - intf = WireGuardIf(args.showinterface) + intf = WireGuardIf(args.showinterface, debug=False) intf.op_show_interface() if args.delkdir: if args.location: -- cgit v1.2.3