FreedomBox/actions/wireguard
Sunil Mohan Adapa 6e1b0a3642
wireguard: Handle client connections through network manager
- Any changes done directly using 'wg' command need to be redone after a reboot
  and disable/enable sequence. Let that duty be handled by network manager.

- Handle (none) values for keys and 0 values for latest handshake from 'wg'
  dump command output.

- Don't store public/private keys for wireguard in /var/lib. Let Network Manager
  deal with the storage of secrets.

- Create client connections in the 'external' zone.

- Show allowed IPs for each client in the main page.

- Show server connection public key only for clients. We use different key pairs
  when connecting to each of the servers.

- Separate out configuration information and status information in the show
  page.

- Allocate IP addresses to each of the clients.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2020-01-18 13:53:35 -05:00

97 lines
2.9 KiB
Python
Executable File

#!/usr/bin/python3
#
# This file is part of FreedomBox.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Configuration helper for WireGuard.
"""
import argparse
import json
import subprocess
SERVER_INTERFACE = 'wg0'
def parse_arguments():
"""Return parsed command line arguments as dictionary."""
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
subparsers.add_parser('get-info',
help='Get info for each configured interface')
subparsers.required = True
return parser.parse_args()
def _get_info():
"""Return info for each configured interface."""
output = subprocess.check_output(['wg', 'show', 'all',
'dump']).decode().strip()
lines = output.split('\n')
interfaces = {}
for line in lines:
if not line:
continue
fields = line.split()
fields = [field if field != '(none)' else None for field in fields]
interface_name = fields[0]
if interface_name in interfaces:
latest_handshake = int(fields[5]) if int(fields[5]) else None
peer = {
'public_key': fields[1],
'preshared_key': fields[2],
'endpoint': fields[3],
'allowed_ips': fields[4],
'latest_handshake': latest_handshake,
'transfer_rx': fields[6],
'transfer_tx': fields[7],
'persistent_keepalive': fields[8],
}
interfaces[interface_name]['peers'].append(peer)
else:
interfaces[interface_name] = {
'interface_name': interface_name,
'private_key': fields[1],
'public_key': fields[2],
'listen_port': fields[3],
'fwmark': fields[4],
'peers': [],
}
return interfaces
def subcommand_get_info(_):
"""Print info for each configured interface."""
print(json.dumps(_get_info()))
def main():
"""Parse arguments and perform all duties."""
arguments = parse_arguments()
subcommand = arguments.subcommand.replace('-', '_')
subcommand_method = globals()['subcommand_' + subcommand]
subcommand_method(arguments)
if __name__ == '__main__':
main()