2021-11-11 18:31:28 +00:00
|
|
|
#!/usr/bin/env python3
|
2021-11-16 16:14:01 +00:00
|
|
|
"""OpenConnect VPN server management module"""
|
2021-11-15 13:49:06 +00:00
|
|
|
import json
|
2021-11-16 16:14:01 +00:00
|
|
|
import portalocker
|
|
|
|
from flask_restful import Resource
|
2021-11-11 18:31:28 +00:00
|
|
|
|
|
|
|
from selfprivacy_api.resources.services import api
|
|
|
|
|
2021-11-16 16:14:01 +00:00
|
|
|
|
2021-11-11 18:31:28 +00:00
|
|
|
class EnableOcserv(Resource):
|
2021-11-16 16:14:01 +00:00
|
|
|
"""Enable OpenConnect VPN server"""
|
|
|
|
|
2021-11-11 18:31:28 +00:00
|
|
|
def post(self):
|
2021-11-16 16:14:01 +00:00
|
|
|
"""
|
|
|
|
Enable OCserv
|
|
|
|
---
|
|
|
|
tags:
|
|
|
|
- OCserv
|
|
|
|
security:
|
|
|
|
- bearerAuth: []
|
|
|
|
responses:
|
|
|
|
200:
|
|
|
|
description: OCserv enabled
|
|
|
|
401:
|
|
|
|
description: Unauthorized
|
|
|
|
"""
|
|
|
|
with open(
|
|
|
|
"/etc/nixos/userdata/userdata.json", "r+", encoding="utf-8"
|
|
|
|
) as userdata_file:
|
|
|
|
portalocker.lock(userdata_file, portalocker.LOCK_EX)
|
2021-11-15 13:49:06 +00:00
|
|
|
try:
|
2021-11-16 16:14:01 +00:00
|
|
|
data = json.load(userdata_file)
|
2021-11-15 13:49:06 +00:00
|
|
|
if "ocserv" not in data:
|
|
|
|
data["ocserv"] = {}
|
|
|
|
data["ocserv"]["enable"] = True
|
2021-11-16 16:14:01 +00:00
|
|
|
userdata_file.seek(0)
|
|
|
|
json.dump(data, userdata_file, indent=4)
|
|
|
|
userdata_file.truncate()
|
2021-11-15 13:49:06 +00:00
|
|
|
finally:
|
2021-11-16 16:14:01 +00:00
|
|
|
portalocker.unlock(userdata_file)
|
2021-11-11 18:31:28 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
"status": 0,
|
|
|
|
"message": "OpenConnect VPN server enabled",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class DisableOcserv(Resource):
|
2021-11-16 16:14:01 +00:00
|
|
|
"""Disable OpenConnect VPN server"""
|
|
|
|
|
2021-11-11 18:31:28 +00:00
|
|
|
def post(self):
|
2021-11-16 16:14:01 +00:00
|
|
|
"""
|
|
|
|
Disable OCserv
|
|
|
|
---
|
|
|
|
tags:
|
|
|
|
- OCserv
|
|
|
|
security:
|
|
|
|
- bearerAuth: []
|
|
|
|
responses:
|
|
|
|
200:
|
|
|
|
description: OCserv disabled
|
|
|
|
401:
|
|
|
|
description: Unauthorized
|
|
|
|
"""
|
|
|
|
with open(
|
|
|
|
"/etc/nixos/userdata/userdata.json", "r+", encoding="utf-8"
|
|
|
|
) as userdata_file:
|
|
|
|
portalocker.lock(userdata_file, portalocker.LOCK_EX)
|
2021-11-15 13:49:06 +00:00
|
|
|
try:
|
2021-11-16 16:14:01 +00:00
|
|
|
data = json.load(userdata_file)
|
2021-11-15 13:49:06 +00:00
|
|
|
if "ocserv" not in data:
|
|
|
|
data["ocserv"] = {}
|
|
|
|
data["ocserv"]["enable"] = False
|
2021-11-16 16:14:01 +00:00
|
|
|
userdata_file.seek(0)
|
|
|
|
json.dump(data, userdata_file, indent=4)
|
|
|
|
userdata_file.truncate()
|
2021-11-15 13:49:06 +00:00
|
|
|
finally:
|
2021-11-16 16:14:01 +00:00
|
|
|
portalocker.unlock(userdata_file)
|
2021-11-11 18:31:28 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
"status": 0,
|
|
|
|
"message": "OpenConnect VPN server disabled",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
api.add_resource(EnableOcserv, "/ocserv/enable")
|
|
|
|
api.add_resource(DisableOcserv, "/ocserv/disable")
|