2020-04-07 17:45:16 +00:00
|
|
|
import base64
|
|
|
|
|
2020-04-03 17:27:49 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
2020-04-07 17:45:16 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-04-03 17:27:49 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
|
from .models import *
|
|
|
|
|
2020-04-06 20:30:01 +00:00
|
|
|
class VPNPoolSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = VPNPool
|
|
|
|
fields = '__all__'
|
|
|
|
|
|
|
|
class VPNNetworkSerializer(serializers.ModelSerializer):
|
2020-04-03 17:27:49 +00:00
|
|
|
|
|
|
|
class Meta:
|
2020-04-06 20:30:01 +00:00
|
|
|
model = VPNNetwork
|
2020-04-03 17:27:49 +00:00
|
|
|
fields = '__all__'
|
2020-04-07 17:45:16 +00:00
|
|
|
|
|
|
|
# This is required for finding the VPN pool, but does not
|
|
|
|
# exist in the model
|
|
|
|
network_size = serializers.IntegerField(min_value=0,
|
|
|
|
max_value=128)
|
|
|
|
|
|
|
|
def validate_wireguard_public_key(self, value):
|
|
|
|
msg = _("Supplied key is not a valid wireguard public key")
|
|
|
|
|
|
|
|
""" FIXME: verify that this does not create broken wireguard config files,
|
|
|
|
i.e. contains \n or similar!
|
|
|
|
We might even need to be more strict to not break wireguard...
|
|
|
|
"""
|
|
|
|
print(value)
|
|
|
|
|
|
|
|
try:
|
|
|
|
base64.standard_b64decode(value)
|
|
|
|
except Exception as e:
|
|
|
|
raise serializers.ValidationError(msg)
|
|
|
|
|
|
|
|
if '\n' in value:
|
|
|
|
raise serializers.ValidationError(msg)
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
def validate(self, data):
|
|
|
|
|
|
|
|
# FIXME: filter for status = active or similar
|
|
|
|
all_pools = VPNPool.objects.all()
|
|
|
|
sizes = [ p.subnetwork_size for p in all_pools ]
|
|
|
|
|
|
|
|
pools = VPNPool.objects.filter(subnetwork_size=data['network_size'])
|
|
|
|
|
|
|
|
if len(pools) == 0:
|
|
|
|
msg = _("No pool available for networks with size = {}. Available are: {}".format(data['network_size'], sizes))
|
|
|
|
raise serializers.ValidationError(msg)
|
|
|
|
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
from_pool =
|