Created
May 21, 2020 08:59
-
-
Save outloudvi/47b8ed655333354550b5362733c8425f to your computer and use it in GitHub Desktop.
dn42 add nodes for test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from collections import defaultdict | |
from subprocess import check_output, run, PIPE | |
from os import system | |
SUPPORTED_VPN = ["wireguard"] | |
SUPPORTED_ROUTE = ["bird2"] | |
data = defaultdict(str) | |
def vpn_route_common(): | |
data["peer_as"] = input("Peer ASN:") | |
data["peer_name"] = input("Peer name:") | |
data["int_name"] = data["peer_name"].replace("_", "-") | |
while True: | |
print("Interface name:", data["int_name"]) | |
tmp = input("Is it okay? Or another one?") | |
if tmp == "": | |
break | |
data["int_name"] = tmp | |
data["peer_endpoint"] = input("Peer endpoint address:") | |
data["local_tun_ip"] = input("Local v4 tunnel IP:") | |
data["local_tun_prefix"] = input("Local v4 tunnel prefix:") | |
print("[PUB] Local tunnel IP/pref: {}/{}".format( | |
data["local_tun_ip"], data["local_tun_prefix"])) | |
data["remote_tun_ip"] = input("Remote v4 tunnel IP:") | |
data["remote_tun_prefix"] = input("Remote v4 tunnel prefix:") | |
data["local_6_tun_ip"] = input("Local v6 tunnel IP:") | |
data["local_6_tun_prefix"] = input("Local v6 tunnel prefix:") | |
print("[PUB] Local v6 tunnel IP/pref: {}/{}".format( | |
data["local_6_tun_ip"], data["local_6_tun_prefix"])) | |
data["remote_6_tun_ip"] = input("Remote v6 tunnel IP:") | |
data["remote_6_tun_prefix"] = input("Remote v6 tunnel prefix:") | |
def vpn_wg(): | |
wg_privkey_byt = check_output(["wg", "genkey"]) | |
data["local_wg_privkey"] = wg_privkey_byt.decode().replace("\n", "") | |
wg_pubkey_str = run(["wg", "pubkey"], input=wg_privkey_byt).stdout | |
print("[PUB] Your WireGuard public key:", wg_pubkey_str) | |
print("Your port usage information:") | |
system("cat /etc/systemd/network/* | grep ListenPort") | |
data["local_wg_port"] = input("Local WireGuard port:") | |
print("[PUB] Your WireGuard port:", data["local_wg_port"]) | |
data["peer_wg_port"] = input("Peer WireGuard port:") | |
data["peer_wg_pubkey"] = input("Peer WireGuard public key:") | |
netdev_config = r"""\#{data["int_name"]}.netdev | |
[NetDev] | |
Name=dn-asfr | |
Kind=wireguard | |
Description=dn42: asfr | |
[WireGuard] | |
PrivateKey = {data["local_wg_privkey"]} | |
ListenPort = {data["local_wg_port"]} | |
[WireGuardPeer] | |
PublicKey = {data["peer_wg_pubkey"]} | |
Endpoint = {data["peer_endpoint"]}:{data["peer_wg_port"]} | |
AllowedIPs = 0.0.0.0/0,::/0 | |
""".format(data=data) | |
network_config = r"""\#{data["int_name"]}.network | |
[Match] | |
Name=dn-asfr | |
[Address] | |
Address={data["local_tun_ip"]}/{data["local_tun_prefix"]} | |
Peer={data["remote_tun_ip"]}/{data["remote_tun_prefix"]} | |
[Address] | |
Address={data["local_6_tun_ip"]}/{data["local_6_tun_prefix"]} | |
Peer={data["remote_6_tun_ip"]}/{data["remote_6_tun_prefix"]} | |
""".format(data=data) | |
printf(netdev_config + "\n" + network_config) | |
def route_bird(): | |
data["bird_prot_name"] = data["peer_name"].replace("-", "_") | |
while True: | |
print("Bird protocol name:", data["bird_prot_name"]) | |
tmp = input("Is it okay? Or another one?") | |
if tmp == "": | |
break | |
data["bird_prot_name"] = tmp | |
config1 = r"""protocol bgp {} from dnpeers {{ | |
neighbor {} % '{}' as {}; | |
}};""".format(data["bird_prot_name"], data["remote_tun_ip"], data["int_name"], data["peer_as"]) | |
config2 = r"""protocol bgp {}_v6 from dnpeers {{ | |
neighbor {} % '{}' as {}; | |
}};""".format(data["bird_prot_name"], data["remote_6_tun_ip"], data["int_name"], data["peer_as"]) | |
print("BIRD2 config:") | |
print(config1 + "\n\n" + config2) | |
def main(): | |
print("----- peer tool -----") | |
print("Configuration we are supporting:") | |
for i in SUPPORTED_VPN: | |
print(" - vpn:{}".format(i)) | |
for i in SUPPORTED_ROUTE: | |
print(" - route:{}".format(i)) | |
# data["vpn"] = input("Select a vpn: ") | |
# data["route"] = input("Select a route: ") | |
vpn_route_common() | |
if data["vpn"] == "wireguard" or True: | |
vpn_wg() | |
if data["route"] == "bird2" or True: | |
route_bird() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment