-
Notifications
You must be signed in to change notification settings - Fork 0
/
openvpn.py
executable file
·43 lines (33 loc) · 1.2 KB
/
openvpn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/home/development/covpn/venv/bin/python
import django
import os
import sys
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'covpn.settings')
django.setup()
from django.contrib.auth.models import User
from group.models import Config, Route
from log.models import create_log_entry
class Dispatcher:
def __init__(self, *args, **kwargs):
type = kwargs.get('script_type')
if not type:
raise Exception("`script_type` empty")
create_log_entry(type, **kwargs)
fn = getattr(self, type.replace('-', '_'))
if not fn:
raise Exception("`script_type` not defined: {0.script_type}".format(kwargs))
fn(*args, **kwargs)
def client_connect(self, *args, **kwargs):
(_, ccd) = args
groups = User.objects.get(username=kwargs.get('common_name')).groups.all()
rules = [
*Config.objects.filter(group__in=groups),
*Route.objects.filter(group__in=groups),
]
with open(ccd, 'w') as f:
for rule in rules:
f.write('push "{}"\n'.format(rule))
def client_disconnect(self, *args, **kwargs):
pass
if __name__ == '__main__':
Dispatcher(*sys.argv, **os.environ)