Andrea Zucchelli
bce55b225f
Squashed commit of the following: commitf3c87deb32
Author: Andrea Zucchelli <zukka77@gmail.com> Date: Sun Oct 13 23:47:36 2024 +0200 chore: refresh button commite7c831e0c2
Author: Andrea Zucchelli <zukka77@gmail.com> Date: Sun Oct 13 23:37:56 2024 +0200 feat: wrapping up containers commite571b00c3d
Author: Andrea Zucchelli <zukka77@gmail.com> Date: Sun Oct 13 21:27:52 2024 +0200 fix: sort out sub commit4a75d4561b
Author: Andrea Zucchelli <zukka77@gmail.com> Date: Sun Oct 13 21:24:56 2024 +0200 fix: sort out sub commit3c1030a2fe
Author: Andrea Zucchelli <zukka77@gmail.com> Date: Sun Oct 13 20:29:29 2024 +0200 chore: optimize image build commitf63d1b185b
Author: Andrea Zucchelli <zukka77@gmail.com> Date: Sun Oct 13 20:28:11 2024 +0200 chore: optimize image build commit0d9a175438
Author: Andrea Zucchelli <zukka77@gmail.com> Date: Sun Oct 13 20:26:16 2024 +0200 chore: optimize image build commitbb510718d2
Author: Andrea Zucchelli <zukka77@gmail.com> Date: Sun Oct 13 20:08:22 2024 +0200 fix: toggle_connection commitb48a12c1f3
Author: Andrea Zucchelli <zukka77@gmail.com> Date: Sun Oct 13 20:02:30 2024 +0200 fix: toggle_connection commit343c36002b
Author: Andrea Zucchelli <zukka77@gmail.com> Date: Sun Oct 13 19:27:14 2024 +0200 fix: serialization commit880909912c
Author: Andrea Zucchelli <zukka77@gmail.com> Date: Sun Oct 13 19:24:06 2024 +0200 fix: sync command commitfbabd0cfca
Author: Andrea Zucchelli <zukka77@gmail.com> Date: Sun Oct 13 19:13:10 2024 +0200 fix: import Peer commit444f612452
Author: Andrea Zucchelli <zukka77@gmail.com> Date: Sun Oct 13 19:09:28 2024 +0200 chore: use celery commit2f96991294
Author: Andrea Zucchelli <zukka77@gmail.com> Date: Sun Oct 13 19:02:40 2024 +0200 fix: make gunicorn listen on 0.0.0.0 commit313f23e23c
Author: Andrea Zucchelli <zukka77@gmail.com> Date: Sun Oct 13 18:59:22 2024 +0200 chore: docker stuff compat commitc17153112b
Author: Andrea Zucchelli <zukka77@gmail.com> Date: Sun Oct 13 18:55:39 2024 +0200 feat: celery decoupling
67 lines
2.7 KiB
Python
67 lines
2.7 KiB
Python
from django.db import transaction
|
|
from .models import UserConnection
|
|
from pywireguard.factory import Peer
|
|
import environ
|
|
from celery import Celery
|
|
from .wg_manager import WGManager
|
|
|
|
|
|
env = environ.Env(WG_INTERFACE=(str, "wg0"), CELERY_BROKER=(str, "redis://localhost"), CELERY_BACKEND=(str, "redis://localhost"))
|
|
environ.Env.read_env(".env")
|
|
WG_INTERFACE = env("WG_INTERFACE")
|
|
CELERY_BROKER = env("CELERY_BROKER")
|
|
CELERY_BACKEND = env("CELERY_BACKEND")
|
|
|
|
|
|
class DJWGManager:
|
|
wg_manager: WGManager
|
|
app: Celery
|
|
|
|
def __init__(self):
|
|
self.app = Celery("wg_manager_tasks", broker=CELERY_BROKER, backend=CELERY_BACKEND)
|
|
self.app.conf.event_serializer = (
|
|
"pickle" # this event_serializer is optional. somehow i missed this when writing this solution and it still worked without.
|
|
)
|
|
self.app.conf.task_serializer = "pickle"
|
|
self.app.conf.result_serializer = "pickle"
|
|
self.app.conf.accept_content = ["application/json", "application/x-python-serialize"]
|
|
|
|
def sync(self):
|
|
with transaction.atomic():
|
|
UserConnection.objects.filter(active=True).update(active=False)
|
|
res = self.app.send_task("wg_connection_manager_worker.tasks.get_peers")
|
|
peers = res.get()
|
|
for peer in peers:
|
|
pk = peer.public_key.decode("ascii")
|
|
psk = peer.preshared_key.decode("ascii")
|
|
if not peer.allowed_ips:
|
|
continue
|
|
connection = UserConnection.objects.filter(public_key=pk)
|
|
if len(connection) == 1:
|
|
connection = connection[0]
|
|
else:
|
|
connection = UserConnection()
|
|
connection.public_key = pk
|
|
connection.preshared_key = psk
|
|
connection.active = True
|
|
connection.vpn_ip = peer.allowed_ips[0]
|
|
connection.save()
|
|
|
|
def add_peer(self, user_connection: UserConnection):
|
|
self.app.send_task(
|
|
"wg_connection_manager_worker.tasks.add_peer",
|
|
[Peer(public_key=user_connection.public_key, preshared_key=user_connection.preshared_key, allowed_ips=[user_connection.vpn_ip])],
|
|
)
|
|
self.sync()
|
|
|
|
def remove_peer(self, user_connection: UserConnection):
|
|
pk = user_connection.public_key
|
|
res = self.app.send_task("wg_connection_manager_worker.tasks.get_peers")
|
|
peers = res.get()
|
|
peer = list(filter(lambda x: x.public_key.decode("ascii") == pk, peers))
|
|
if not peer:
|
|
# TODO raise exception/ignore?
|
|
return
|
|
peer = peer[0]
|
|
res = self.app.send_task("wg_connection_manager_worker.tasks.remove_peer", [peer])
|
|
self.sync()
|