2020-11-16 03:40:33 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
2020-11-16 04:23:23 +01:00
|
|
|
Name: wg-meshconf
|
2021-01-13 02:47:40 +01:00
|
|
|
Creator: K4YT3X
|
2020-11-16 03:40:33 +01:00
|
|
|
Date Created: July 19, 2020
|
2021-01-13 02:47:40 +01:00
|
|
|
Last Modified: January 12, 2021
|
2020-11-16 03:40:33 +01:00
|
|
|
|
|
|
|
Licensed under the GNU General Public License Version 3 (GNU GPL v3),
|
|
|
|
available at: https://www.gnu.org/licenses/gpl-3.0.txt
|
2021-01-13 02:47:40 +01:00
|
|
|
(C) 2018-2021 K4YT3X
|
2020-11-16 03:40:33 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
# built-in imports
|
|
|
|
import argparse
|
|
|
|
import pathlib
|
|
|
|
import sys
|
|
|
|
|
|
|
|
# local imports
|
2021-01-12 06:13:11 +01:00
|
|
|
from .database_manager import DatabaseManager
|
2020-11-16 03:40:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
def parse_arguments():
|
2021-01-13 02:47:40 +01:00
|
|
|
"""parse CLI arguments"""
|
2020-11-16 03:40:33 +01:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
prog="wg-meshconf", formatter_class=argparse.ArgumentDefaultsHelpFormatter
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"-d",
|
|
|
|
"--database",
|
|
|
|
type=pathlib.Path,
|
|
|
|
help="path where the database file is stored",
|
|
|
|
default=pathlib.Path("database.json"),
|
|
|
|
)
|
|
|
|
|
|
|
|
# add subparsers for commands
|
|
|
|
subparsers = parser.add_subparsers(dest="command")
|
|
|
|
|
|
|
|
# add new peer
|
|
|
|
addpeer = subparsers.add_parser("addpeer")
|
|
|
|
addpeer.add_argument("name", help="Name used to identify this node")
|
|
|
|
addpeer.add_argument(
|
|
|
|
"--address", help="address of the server", action="append", required=True
|
|
|
|
)
|
|
|
|
addpeer.add_argument("--endpoint", help="peer's public endpoint address")
|
2021-01-13 02:47:40 +01:00
|
|
|
addpeer.add_argument(
|
|
|
|
"--allowedips", help="additional allowed IP addresses", action="append"
|
|
|
|
)
|
2020-11-16 03:40:33 +01:00
|
|
|
addpeer.add_argument("--privatekey", help="private key of server interface")
|
|
|
|
addpeer.add_argument("--listenport", help="port to listen on", default=51820)
|
|
|
|
addpeer.add_argument("--fwmark", help="fwmark for outgoing packets")
|
|
|
|
addpeer.add_argument("--dns", help="server interface DNS servers")
|
|
|
|
addpeer.add_argument("--mtu", help="server interface MTU")
|
|
|
|
addpeer.add_argument("--table", help="server routing table")
|
|
|
|
addpeer.add_argument("--preup", help="command to run before interface is up")
|
|
|
|
addpeer.add_argument("--postup", help="command to run after interface is up")
|
|
|
|
addpeer.add_argument("--predown", help="command to run before interface is down")
|
|
|
|
addpeer.add_argument("--postdown", help="command to run after interface is down")
|
|
|
|
addpeer.add_argument(
|
|
|
|
"--saveconfig",
|
|
|
|
action="store_true",
|
|
|
|
help="save server interface to config upon shutdown",
|
|
|
|
default=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
# update existing peer information
|
|
|
|
updatepeer = subparsers.add_parser("updatepeer")
|
|
|
|
updatepeer.add_argument("name", help="Name used to identify this node")
|
|
|
|
updatepeer.add_argument("--address", help="address of the server", action="append")
|
|
|
|
updatepeer.add_argument("--endpoint", help="peer's public endpoint address")
|
2021-01-13 02:47:40 +01:00
|
|
|
updatepeer.add_argument(
|
|
|
|
"--allowedips", help="additional allowed IP addresses", action="append"
|
|
|
|
)
|
2020-11-16 03:40:33 +01:00
|
|
|
updatepeer.add_argument("--privatekey", help="private key of server interface")
|
|
|
|
updatepeer.add_argument("--listenport", help="port to listen on")
|
|
|
|
updatepeer.add_argument("--fwmark", help="fwmark for outgoing packets")
|
|
|
|
updatepeer.add_argument("--dns", help="server interface DNS servers")
|
|
|
|
updatepeer.add_argument("--mtu", help="server interface MTU")
|
|
|
|
updatepeer.add_argument("--table", help="server routing table")
|
|
|
|
updatepeer.add_argument("--preup", help="command to run before interface is up")
|
|
|
|
updatepeer.add_argument("--postup", help="command to run after interface is up")
|
|
|
|
updatepeer.add_argument("--predown", help="command to run before interface is down")
|
|
|
|
updatepeer.add_argument("--postdown", help="command to run after interface is down")
|
|
|
|
updatepeer.add_argument(
|
|
|
|
"--saveconfig",
|
|
|
|
action="store_true",
|
|
|
|
help="save server interface to config upon shutdown",
|
|
|
|
default=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
# delpeer deletes a peer form the database
|
|
|
|
delpeer = subparsers.add_parser("delpeer")
|
|
|
|
delpeer.add_argument("name", help="Name of peer to delete")
|
|
|
|
|
|
|
|
# showpeers prints a table of all peers and their configurations
|
|
|
|
showpeers = subparsers.add_parser("showpeers")
|
|
|
|
showpeers.add_argument(
|
2021-01-13 02:47:40 +01:00
|
|
|
"name",
|
|
|
|
help="Name of the peer to query",
|
|
|
|
nargs="?",
|
2020-11-16 03:40:33 +01:00
|
|
|
)
|
|
|
|
showpeers.add_argument(
|
|
|
|
"--style",
|
|
|
|
choices=["table", "text"],
|
|
|
|
help="peers information printing style",
|
|
|
|
default="table",
|
|
|
|
)
|
|
|
|
showpeers.add_argument(
|
|
|
|
"-s",
|
|
|
|
"--simplify",
|
|
|
|
help="do not print columns that are all None",
|
|
|
|
action="store_true",
|
|
|
|
)
|
|
|
|
|
|
|
|
# generate config
|
|
|
|
genconfig = subparsers.add_parser("genconfig")
|
|
|
|
genconfig.add_argument(
|
|
|
|
"name",
|
|
|
|
help="Name of the peer to generate configuration for, \
|
|
|
|
configuration for all peers are generated if omitted",
|
|
|
|
nargs="?",
|
|
|
|
)
|
|
|
|
genconfig.add_argument(
|
|
|
|
"-o",
|
|
|
|
"--output",
|
|
|
|
help="configuration file output directory",
|
|
|
|
type=pathlib.Path,
|
|
|
|
default=pathlib.Path(__file__).parent.absolute() / "output",
|
|
|
|
)
|
|
|
|
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
# if the file is not being imported
|
2021-01-12 06:13:11 +01:00
|
|
|
def main():
|
2020-11-16 03:40:33 +01:00
|
|
|
|
|
|
|
args = parse_arguments()
|
|
|
|
|
|
|
|
database_manager = DatabaseManager(args.database)
|
|
|
|
|
|
|
|
if args.command == "addpeer":
|
|
|
|
database_manager.addpeer(
|
|
|
|
args.name,
|
|
|
|
args.address,
|
|
|
|
args.endpoint,
|
2020-11-16 18:23:58 +01:00
|
|
|
args.allowedips,
|
2020-11-16 03:40:33 +01:00
|
|
|
args.listenport,
|
|
|
|
args.fwmark,
|
|
|
|
args.privatekey,
|
|
|
|
args.dns,
|
|
|
|
args.mtu,
|
|
|
|
args.table,
|
|
|
|
args.preup,
|
|
|
|
args.postup,
|
|
|
|
args.predown,
|
|
|
|
args.postdown,
|
|
|
|
args.saveconfig,
|
|
|
|
)
|
|
|
|
|
|
|
|
elif args.command == "updatepeer":
|
|
|
|
database_manager.updatepeer(
|
|
|
|
args.name,
|
|
|
|
args.address,
|
|
|
|
args.endpoint,
|
2020-11-16 18:23:58 +01:00
|
|
|
args.allowedips,
|
2020-11-16 03:40:33 +01:00
|
|
|
args.listenport,
|
|
|
|
args.fwmark,
|
|
|
|
args.privatekey,
|
|
|
|
args.dns,
|
|
|
|
args.mtu,
|
|
|
|
args.table,
|
|
|
|
args.preup,
|
|
|
|
args.postup,
|
|
|
|
args.predown,
|
|
|
|
args.postdown,
|
|
|
|
args.saveconfig,
|
|
|
|
)
|
|
|
|
|
|
|
|
elif args.command == "delpeer":
|
|
|
|
database_manager.delpeer(args.name)
|
|
|
|
|
|
|
|
elif args.command == "showpeers":
|
|
|
|
database_manager.showpeers(args.name, args.style, args.simplify)
|
|
|
|
|
|
|
|
elif args.command == "genconfig":
|
|
|
|
database_manager.genconfig(args.name, args.output)
|
|
|
|
|
|
|
|
# if no commands are specified
|
|
|
|
else:
|
|
|
|
print(
|
2021-01-13 03:26:01 +01:00
|
|
|
"No command specified\nUse wg-meshconf --help to see available commands",
|
2020-11-16 03:40:33 +01:00
|
|
|
file=sys.stderr,
|
|
|
|
)
|