From 586536c708b0e2e6dbe3b59b0150b05aabbad184 Mon Sep 17 00:00:00 2001 From: Will Nilges Date: Thu, 7 Jul 2022 22:58:22 -0400 Subject: [PATCH] Joe figured it out --- Dockerfile | 2 +- config.py | 1 + gunicorn_conf.py | 4 ++++ proxstar/__init__.py | 9 ++++++++- proxstar/static/js/script.js | 7 +++---- proxstar/vm.py | 3 ++- 6 files changed, 19 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index 67e86d6..013a80d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,5 +9,5 @@ COPY *.py . COPY proxstar ./proxstar RUN touch proxmox_ssh_key targets && chmod a+w proxmox_ssh_key targets # This is some OKD shit. # This is so cringe, but it's for development. Comment this before pushing. -#COPY HACKING/ssh_key proxmox_ssh_key +COPY HACKING/ssh_key proxmox_ssh_key ENTRYPOINT ddtrace-run python3 wsgi.py diff --git a/config.py b/config.py index 48fb66f..88558b3 100644 --- a/config.py +++ b/config.py @@ -64,6 +64,7 @@ REDIS_PORT = int(environ.get('PROXSTAR_REDIS_PORT', '6379')) # VNC WEBSOCKIFY_PATH = environ.get('PROXSTAR_WEBSOCKIFY_PATH', '/usr/local/bin/websockify') WEBSOCKIFY_TARGET_FILE = environ.get('PROXSTAR_WEBSOCKIFY_TARGET_FILE', '/opt/proxstar/targets') +VNC_HOST = environ.get('PROXSTAR_VNC_HOST', 'proxmox-vnc.csh.rit.edu') # SENTRY # If you set the sentry dsn locally, make sure you use the local-dev or some diff --git a/gunicorn_conf.py b/gunicorn_conf.py index 0f569c9..f0e6451 100644 --- a/gunicorn_conf.py +++ b/gunicorn_conf.py @@ -16,6 +16,7 @@ timeout = app.config['TIMEOUT'] def start_websockify(websockify_path, target_file): result = subprocess.run(['pgrep', 'websockify'], stdout=subprocess.PIPE) if not result.stdout: + print("Websockify is stopped. Starting websockify.") subprocess.call( [ websockify_path, @@ -28,7 +29,10 @@ def start_websockify(websockify_path, target_file): ], stdout=subprocess.PIPE, ) + else: + print("Websockify started.") def on_starting(server): + print("Starting Websockify...") start_websockify(app.config['WEBSOCKIFY_PATH'], app.config['WEBSOCKIFY_TARGET_FILE']) diff --git a/proxstar/__init__.py b/proxstar/__init__.py index 54de896..36ba277 100644 --- a/proxstar/__init__.py +++ b/proxstar/__init__.py @@ -6,6 +6,7 @@ import logging import subprocess import psutil import psycopg2 +from gunicorn_conf import start_websockify import rq_dashboard from rq import Queue from redis import Redis @@ -60,6 +61,11 @@ app.config['GIT_REVISION'] = ( subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('utf-8').rstrip() ) +# Probably cursed. +if 'localhost' in app.config['SERVER_NAME']: + print("Server name is localhost. Starting websockify...") + start_websockify(app.config['WEBSOCKIFY_PATH'], app.config['WEBSOCKIFY_TARGET_FILE']) + # Sentry setup sentry_sdk.init( dsn=app.config['SENTRY_DSN'], @@ -289,7 +295,8 @@ def vm_console(vmid): vm.configure_vnc_in_vm_config(app.config['PROXMOX_SSH_USER'], app.config['PROXMOX_SSH_KEY_PASS']) ssh_tunnels.append(tunnel) # vm.start_vnc(port) # Broken :( - return token, 200 + # return json.dumps([app.config['VNC_HOST'], token]), 200 + return {'host' : app.config['VNC_HOST'], 'token' : token}, 200 else: return '', 403 diff --git a/proxstar/static/js/script.js b/proxstar/static/js/script.js index 77f610a..aebb86e 100644 --- a/proxstar/static/js/script.js +++ b/proxstar/static/js/script.js @@ -650,12 +650,11 @@ $("#console-vm").click(function(){ credentials: 'same-origin', method: 'post' }).then((response) => { - return response.text() - }).then((token) => { - // window.open(`/static/noVNC/vnc.html?autoconnect=true&encrypt=true&host=proxstar-vnc.csh.rit.edu&port=443&path=path?token=${token}`, '_blank'); + return response.json() + }).then((vnc_params) => { // TODO (willnilges): encrypt=true // TODO (willnilges): set host and port to an env variable - window.open(`/static/noVNC/vnc.html?autoconnect=true&host=freedom.csh.rit.edu&port=8081&path=path?token=${token}`, '_blank'); + window.open(`/static/noVNC/vnc.html?autoconnect=true&password=${vmid}&host=${vnc_params.host}&port=8081&path=path?token=${vnc_params.token}`, '_blank'); }).catch(err => { if (err) { swal("Uh oh...", `Unable to start console for ${vmname}. Please try again later.`, "error"); diff --git a/proxstar/vm.py b/proxstar/vm.py index 0e260bd..a7efca1 100644 --- a/proxstar/vm.py +++ b/proxstar/vm.py @@ -279,11 +279,12 @@ class VM: TODO (willnilges): Current password is "chomchom1", but should be changed lol """ # proxmox = connect_proxmox() - config = f'args: -object secret,id=secvnc{self.id},data=chomchom1 -vnc 127.0.0.1:{int(self.id)+5900},password-secret=secvnc{self.id}' + config = f'args: -object secret,id=secvnc{self.id},data={self.id} -vnc 127.0.0.1:{int(self.id)+5900},password-secret=secvnc{self.id}' path = f'/etc/pve/local/qemu-server/{self.id}.conf' with paramiko.SSHClient() as ssh: ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(self.node, port=22, username=ssh_user, key_filename='proxmox_ssh_key', passphrase=ssh_pass) + ssh.exec_command(f"if grep -- '{config}' {path}; then echo identical config found; else sed -i /dev/null '/-vnc/d' {path}") #YOLO ssh.exec_command(f"if grep -- '-vnc' {path}; then echo found config; else echo {config} >> {path}; fi")