67 lines
1.6 KiB
Python
67 lines
1.6 KiB
Python
|
from celery import shared_task
|
||
|
from .models import *
|
||
|
|
||
|
@shared_task
|
||
|
def configure_wireguard_server(vpnpool):
|
||
|
print(f"Configuring {vpnpool.vpn_server_hostname}")
|
||
|
|
||
|
wireguard_config_filename = '/etc/wireguard/{}.conf'.format(vpnpool.network)
|
||
|
|
||
|
@property
|
||
|
def wireguard_config(self):
|
||
|
wireguard_config = [
|
||
|
"""
|
||
|
[Interface]
|
||
|
ListenPort = 51820
|
||
|
PrivateKey = {privatekey}
|
||
|
""".format(privatekey=self.wireguard_private_key) ]
|
||
|
|
||
|
peers = []
|
||
|
|
||
|
for reservation in self.vpnnetworkreservation_set.filter(status='used'):
|
||
|
public_key = reservation.vpnnetwork_set.first().wireguard_public_key
|
||
|
peer_network = "{}/{}".format(reservation.address, self.subnetwork_size)
|
||
|
owner = reservation.vpnnetwork_set.first().owner
|
||
|
|
||
|
peers.append("""
|
||
|
# Owner: {owner}
|
||
|
[Peer]
|
||
|
PublicKey = {public_key}
|
||
|
AllowedIPs = {peer_network}
|
||
|
""".format(
|
||
|
owner=owner,
|
||
|
public_key=public_key,
|
||
|
peer_network=peer_network))
|
||
|
|
||
|
wireguard_config.extend(peers)
|
||
|
|
||
|
return "\n".join(wireguard_config)
|
||
|
|
||
|
|
||
|
def configure_wireguard_vpnserver(self):
|
||
|
"""
|
||
|
This method is designed to run as a celery task and should
|
||
|
not be called directly from the web
|
||
|
"""
|
||
|
|
||
|
# subprocess, ssh
|
||
|
|
||
|
pass
|
||
|
|
||
|
|
||
|
|
||
|
def num_maximum_networks(self):
|
||
|
"""
|
||
|
sample:
|
||
|
network_size = 40
|
||
|
subnetwork_size = 48
|
||
|
maximum_networks = 2^(48-40)
|
||
|
|
||
|
2nd sample:
|
||
|
network_size = 8
|
||
|
subnetwork_size = 24
|
||
|
maximum_networks = 2^(24-8)
|
||
|
"""
|
||
|
|
||
|
return 2**(self.subnetwork_mask - self.network_mask)
|