Mercurial > code > home > repos > infra
annotate wireguard.py @ 238:a521d08d2b0d
upgrade syncthing
author | drewp@bigasterisk.com |
---|---|
date | Mon, 27 Nov 2023 22:15:10 -0800 |
parents | db8787bd800e |
children | e45e93a797b0 |
rev | line source |
---|---|
5 | 1 import subprocess |
2 | |
3 from pyinfra import host | |
4 from pyinfra.facts.files import FindInFile | |
76
de387eae06cf
still trying to sequence dhcp->wireguard->dns startup
drewp@bigasterisk.com
parents:
71
diff
changeset
|
5 from pyinfra.operations import apt, files, server, systemd |
215
db8787bd800e
wireguard now uses ditto (and prime) as hubs for home/remote
drewp@bigasterisk.com
parents:
115
diff
changeset
|
6 import wireguard_pubkey |
5 | 7 |
8 # other options: | |
9 # https://www.reddit.com/r/WireGuard/comments/fkr240/shortest_path_between_peers/ | |
10 # https://github.com/k4yt3x/wireguard-mesh-configurator | |
11 # https://github.com/mawalu/wireguard-private-networking | |
12 # | |
13 | |
14 | |
215
db8787bd800e
wireguard now uses ditto (and prime) as hubs for home/remote
drewp@bigasterisk.com
parents:
115
diff
changeset
|
15 def peer_block(hostname, allowed_ips, endpoint=None, keepalive=None): |
89
2fddde57231b
no connman to surprisingly rewrite net configs
drewp@bigasterisk.com
parents:
76
diff
changeset
|
16 # if allowed_ips.startswith('10.5'): |
2fddde57231b
no connman to surprisingly rewrite net configs
drewp@bigasterisk.com
parents:
76
diff
changeset
|
17 # # k3s nets also need to travel over wg |
2fddde57231b
no connman to surprisingly rewrite net configs
drewp@bigasterisk.com
parents:
76
diff
changeset
|
18 # allowed_ips += ', 10.42.0.0/24, 10.43.0.0/24' |
2fddde57231b
no connman to surprisingly rewrite net configs
drewp@bigasterisk.com
parents:
76
diff
changeset
|
19 |
215
db8787bd800e
wireguard now uses ditto (and prime) as hubs for home/remote
drewp@bigasterisk.com
parents:
115
diff
changeset
|
20 public_key = wireguard_pubkey.pubkey[hostname] |
5 | 21 out = f'''\ |
22 | |
23 [Peer] | |
24 # {hostname} | |
25 PublicKey = {public_key} | |
26 AllowedIPs = {allowed_ips} | |
27 ''' | |
28 if endpoint is not None: | |
29 out += f'Endpoint = {endpoint}\n' | |
30 if keepalive is not None: | |
31 out += f'PersistentKeepalive = {keepalive}\n' | |
32 return out | |
33 | |
34 | |
35 for wireguard_interface in ['wg0', 'bogasterisk']: | |
36 if wireguard_interface == 'bogasterisk' and host.name != 'prime': | |
37 continue | |
38 | |
39 # note- this is specific to the wg0 setup. Other conf files don't use it. | |
40 wireguard_ip = host.host_data['wireguard_address'] | |
41 | |
42 # new pi may fail with 'Unable to access interface: Protocol not supported'. reboot fixes. | |
43 | |
44 priv_key_lines = host.get_fact(FindInFile, path=f'/etc/wireguard/{wireguard_interface}.conf', pattern=r'PrivateKey.*') | |
45 if not priv_key_lines: | |
46 priv_key = subprocess.check_output(['wg', 'genkey']).strip().decode('ascii') | |
47 else: | |
48 priv_key = priv_key_lines[0].split(' = ')[1] | |
49 | |
50 pub_key = subprocess.check_output(['wg', 'pubkey'], input=priv_key.encode('ascii')).strip().decode('ascii') | |
115 | 51 # todo: if this was new, it should be added to a file of pubkeys that |
52 # peer_block can refer to. meanwhile, edit the template. | |
5 | 53 |
54 files.template( | |
12 | 55 src=f'templates/wireguard/{wireguard_interface}.conf.j2', |
5 | 56 dest=f'/etc/wireguard/{wireguard_interface}.conf', |
57 mode='600', | |
58 wireguard_ip=wireguard_ip, | |
59 priv_key=priv_key, | |
60 peer_block=peer_block, | |
61 ) | |
62 svc = f'wg-quick@{wireguard_interface}.service' | |
76
de387eae06cf
still trying to sequence dhcp->wireguard->dns startup
drewp@bigasterisk.com
parents:
71
diff
changeset
|
63 |
de387eae06cf
still trying to sequence dhcp->wireguard->dns startup
drewp@bigasterisk.com
parents:
71
diff
changeset
|
64 files.template(src='templates/wireguard/wg.service.j2', |
de387eae06cf
still trying to sequence dhcp->wireguard->dns startup
drewp@bigasterisk.com
parents:
71
diff
changeset
|
65 dest=f'/etc/systemd/system/{svc}', |
de387eae06cf
still trying to sequence dhcp->wireguard->dns startup
drewp@bigasterisk.com
parents:
71
diff
changeset
|
66 wireguard_interface=wireguard_interface) |
de387eae06cf
still trying to sequence dhcp->wireguard->dns startup
drewp@bigasterisk.com
parents:
71
diff
changeset
|
67 systemd.service(service=f'{svc}', enabled=True, restarted=True, daemon_reload=True) |
de387eae06cf
still trying to sequence dhcp->wireguard->dns startup
drewp@bigasterisk.com
parents:
71
diff
changeset
|
68 |
de387eae06cf
still trying to sequence dhcp->wireguard->dns startup
drewp@bigasterisk.com
parents:
71
diff
changeset
|
69 # files.link(path=f'/etc/systemd/system/multi-user.target.wants/{svc}', target='/lib/systemd/system/wg-quick@.service') |
5 | 70 |
9 | 71 systemd.service(service=svc, daemon_reload=True, restarted=True, enabled=True) |
71 | 72 |
97
9b7d7ea79f16
stop trying separate dns on 10.5 net. just use names like 'bang5'
drewp@bigasterisk.com
parents:
89
diff
changeset
|
73 # if host.name == 'bang': |
9b7d7ea79f16
stop trying separate dns on 10.5 net. just use names like 'bang5'
drewp@bigasterisk.com
parents:
89
diff
changeset
|
74 # systemd.service(service=f'dnsmasq_10.5', enabled=True, restarted=True, daemon_reload=True) |