Exemplo n.º 1
0
def new(
    config_path: Path = common.OPTION_CONFIG_PATH,
    ipv4_network: Optional[str] = common.OPTION_IPV4_NETWORK,
    ipv6_network: Optional[str] = common.OPTION_IPV6_NETWORK,
    default_port: Optional[int] = common.OPTION_PORT,
    force: bool = Option(False,
                         "-f",
                         "--force",
                         help="Force overwriting of existing config file"),
):
    """
    Create a new empty config file.
    """
    if config_path.exists() and (not force):
        echo(
            f'config file "{config_path}" exists, add -f/--force flag to overwrite',
            err=True,
        )
        exit(1)

    if default_port is None:
        default_port = 51820

    if ipv4_network is None:
        ipv4_network = "10.0.0.0/24"

    if ipv6_network is None:
        ipv6_network = "fd00:641:c767:bc00::/64"

    config = MainConfig(default_port, IPv4Network(ipv4_network),
                        IPv6Network(ipv6_network))
    config.save(config_path)
Exemplo n.º 2
0
def set(
    config_path: Path = common.OPTION_CONFIG_PATH,
    port: Optional[int] = common.OPTION_PORT,
    ipv4_network: Optional[str] = common.OPTION_IPV4_NETWORK,
    ipv6_network: Optional[str] = common.OPTION_IPV6_NETWORK,
):
    """
    Change global and default settings.

    If a new IPv4/IPv6 subnet is specified, all automatically assigned IPv4/IPv6
    addresses will be regenerated. Manually set IP addresses will be overwritten
    if they are not contained in the new subnet.

    If a new port is specified, all peers that do not have a manually assigned port
    will be set to use the new default.
    """

    config = MainConfig.load(config_path)
    if port is not None:
        config.set_default_port(port)
    if ipv4_network is not None:
        config.set_ipv4_network(IPv4Network(ipv4_network))
    if ipv6_network is not None:
        config.set_ipv6_network(IPv6Network(ipv6_network))
    config.save(config_path)
Exemplo n.º 3
0
def generate_config(
    name: str = Argument(..., help="Name of the peer."),
    config_type: PeerConfigType = PeerConfigType.wg_quick,
    config_path: Path = common.OPTION_CONFIG_PATH,
):
    """
    Generate config file for a peer.
    """
    config = MainConfig.load(config_path)
    echo(config.generate_peer_config(name, config_type))
Exemplo n.º 4
0
def list(
        config_path: Path = common.OPTION_CONFIG_PATH,
        verbose: bool = Option(False, "-v", "--verbose"),
):
    """
    List point-to-point connections
    """
    config = MainConfig.load(config_path)
    for p2p in config.point_to_point:
        if verbose:
            echo(p2p.serialize())
        else:
            echo(f"{p2p.peer1_name} ↔ {p2p.peer2_name}")
Exemplo n.º 5
0
def list(
    config_path: Path = common.OPTION_CONFIG_PATH,
    verbose: bool = Option(False, "-v", "--verbose"),
):
    """
    List peers in the config.
    """
    config = MainConfig.load(config_path)
    for peer in config.peers:
        if verbose:
            echo(peer.serialize())
        else:
            echo(peer.name)
Exemplo n.º 6
0
def remove(
    name: str = Argument(..., help="Name of the peer."),
    config_path: Path = common.OPTION_CONFIG_PATH,
):
    """
    Remove a new peer.
    """
    config = MainConfig.load(config_path)
    try:
        config.remove_peer(name)
    except UnknownPeerError:
        echo(f"no such peer: {name}", err=True)
    config.save(config_path)
Exemplo n.º 7
0
def add(
    peer1: str = Argument(..., help="Name of one peer."),
    peer2: str = Argument(..., help="Name of the other peer."),
    endpoint1: Optional[str] = Option(
        None, help="Endpoint address for peer2 to reach peer1."),
    endpoint2: Optional[str] = Option(
        None, help="Endpoint address for peer1 to reach peer2."),
    config_path: Path = common.OPTION_CONFIG_PATH,
):
    """
    Add a new point-to-point connection.
    """
    config = MainConfig.load(config_path)
    config.add_p2p(peer1, peer2, endpoint1, endpoint2)
    config.save(config_path)
Exemplo n.º 8
0
def add(
    name: str = Argument(..., help="Name of the peer."),
    config_path: Path = common.OPTION_CONFIG_PATH,
    port: Optional[int] = common.OPTION_PORT,
    ipv4_address: Optional[str] = common.OPTION_IPV4_ADDRESS,
    ipv6_address: Optional[str] = common.OPTION_IPV6_ADDRESS,
    site: Optional[str] = None,
):
    """
    Add a new peer.
    """
    config = MainConfig.load(config_path)
    try:
        config.add_peer(
            name,
            IPv4Address(ipv4_address) if ipv4_address else None,
            IPv6Address(ipv6_address) if ipv6_address else None,
            port,
        )
    except DuplicatePeerError as e:
        echo(str(e), err=True)

    config.save(config_path)
Exemplo n.º 9
0
def migrate(config_path: Path = common.OPTION_CONFIG_PATH, ):
    """
    Load config file, migrate it to the newest version and save it.
    """
    config = MainConfig.load(config_path)
    config.save(config_path)