Keep It Simple Stupid. Started using the CherryPy server and made classes from the parts.

This commit is contained in:
kalzu rekku
2024-10-22 22:06:19 +03:00
parent 6db5290cca
commit e620c28648
12 changed files with 600 additions and 0 deletions

View File

@ -0,0 +1,35 @@
import cherrypy
from services.topic_service import TopicService
from auth.token_auth import validate_token
class GeneralTOMLController:
@cherrypy.expose
@cherrypy.tools.json_in()
@cherrypy.tools.json_out()
def index(self):
if cherrypy.request.method != "POST":
raise cherrypy.HTTPError(405, "Method Not Allowed. Use POST.")
auth_header = cherrypy.request.headers.get('Authorization')
encrypted_data = cherrypy.request.body.read()
try:
decrypted_data = validate_token(auth_header, encrypted_data)
action = decrypted_data.get('action')
file_path = decrypted_data.get('file_path')
# Handle any TOML file actions
if action == 'read_file':
config = read_toml_file(file_path)
return {"content": config}
elif action == 'write_file':
data_to_write = decrypted_data.get('data')
write_toml_file(file_path, data_to_write)
return {"message": f"File {file_path} updated successfully"}
else:
return {"error": "Invalid action"}
except Exception as e:
cherrypy.log(f"Error processing request: {str(e)}")
return {"error": "Invalid request"}, 400

View File

@ -0,0 +1,43 @@
import cherrypy
from services.wireguard_service import read_config, write_config, create_backup, reload_wireguard_service
from auth.token_auth import validate_token
from utils.crypto_utils import encrypt_data
class WireGuardController:
@cherrypy.expose
@cherrypy.tools.json_in()
@cherrypy.tools.json_out()
def index(self):
if cherrypy.request.method != "POST":
raise cherrypy.HTTPError(405, "Method Not Allowed. Use POST.")
auth_header = cherrypy.request.headers.get('Authorization')
encrypted_data = cherrypy.request.body.read()
try:
# Decrypt the request and get the symmetric key
decrypted_data, symmetric_key = validate_token(auth_header, encrypted_data)
action = decrypted_data.get('action')
response_data = {}
# Handle different actions (as an example, adding a peer)
if action == 'add_peer':
create_backup()
new_peer = decrypted_data.get('peer')
config = read_config("/etc/wireguard/wg0.conf")
config += "\n\n" + peer_to_string(new_peer)
write_config("/etc/wireguard/wg0.conf", config)
reload_wireguard_service("/etc/wireguard/wg0.conf")
response_data = {"message": "Peer added successfully"}
else:
response_data = {"error": "Invalid action"}
# Encrypt the response data before sending
encrypted_response = encrypt_data(response_data, symmetric_key)
return {"data": encrypted_response}
except Exception as e:
cherrypy.log(f"Error processing request: {str(e)}")
return {"error": "Invalid request"}, 400