|
| 1 | +from os import path, environ, kill, system, chdir, access, X_OK |
| 2 | +import string |
| 3 | +import signal |
| 4 | +from time import sleep |
| 5 | +import uuid |
| 6 | +import threading |
| 7 | +import configparser |
| 8 | +from vncdesk.util import exit_on_error |
| 9 | + |
| 10 | +def read_settings(): |
| 11 | + global width, height |
| 12 | + |
| 13 | + try: |
| 14 | + config = configparser.ConfigParser() |
| 15 | + config.read('settings.ini') |
| 16 | + width = int(config['desktop']['width']) |
| 17 | + height = int(config['desktop']['height']) |
| 18 | + except: |
| 19 | + exit_on_error("Cannot read settings or settings are corrupt") |
| 20 | + |
| 21 | +def set_environ(): |
| 22 | + global _display, width, height |
| 23 | + environ["WIDTH"] = str(width) |
| 24 | + environ["HEIGHT"] = str(height) |
| 25 | + environ["GUEST_DISPLAY"] = environ["DISPLAY"] |
| 26 | + environ["DISPLAY"] = _display |
| 27 | + |
| 28 | +def terminate(): |
| 29 | + global _xvnc_lock_filename |
| 30 | + |
| 31 | + if path.isfile(_xvnc_lock_filename): |
| 32 | + pid = int(open(_xvnc_lock_filename, 'r').read()) |
| 33 | + kill(pid, signal.SIGTERM) |
| 34 | + |
| 35 | +def wait_for_xvnc(): |
| 36 | + while not path.isfile(_xvnc_lock_filename): |
| 37 | + sleep(0.1) |
| 38 | + |
| 39 | +def start_xvnc(): |
| 40 | + global _display, width, height, _number, port |
| 41 | + |
| 42 | + port = 5900 + _number |
| 43 | + cmd = " ".join(["Xvnc", |
| 44 | + _display, |
| 45 | + "-desktop xfig", |
| 46 | + "-geometry " + str(width) + "x" + str(height), |
| 47 | + "-rfbauth " + _password_filename, |
| 48 | + "-rfbport " + str(port), |
| 49 | + "-pn", |
| 50 | + "&"]) |
| 51 | + terminate() |
| 52 | + system(cmd) |
| 53 | + wait_for_xvnc() |
| 54 | + |
| 55 | +def write_password_to_file(): |
| 56 | + global _password_filename |
| 57 | + _password_filename = ".passwd" |
| 58 | + cmd = ";".join([ |
| 59 | + "rm -f " + _password_filename, |
| 60 | + "umask 177", |
| 61 | + "|".join([ |
| 62 | + "echo '" + password + "'", |
| 63 | + "vncpasswd -f >" + _password_filename |
| 64 | + ]) |
| 65 | + ]) |
| 66 | + system(cmd) |
| 67 | + |
| 68 | +def create_password(): |
| 69 | + global password |
| 70 | + password = str(uuid.uuid4()) |
| 71 | + write_password_to_file() |
| 72 | + |
| 73 | +def check_startup(filename): |
| 74 | + if not path.isfile(filename) or not access(filename, X_OK): |
| 75 | + exit_on_error("Cannot find executable startup script") |
| 76 | + |
| 77 | +def startup(filename): |
| 78 | + set_environ() |
| 79 | + check_startup(filename) |
| 80 | + system(filename) |
| 81 | + terminate() |
| 82 | + quit() |
| 83 | + |
| 84 | +def run_startup(): |
| 85 | + filename = "./startup" |
| 86 | + check_startup(filename) |
| 87 | + t1 = threading.Thread(target = startup, args=(filename,)) |
| 88 | + t1.start() |
| 89 | + |
| 90 | +def configure_xvnc(): |
| 91 | + global _number |
| 92 | + system("vncconfig -nowin -display=:" + str(_number) + " &") |
| 93 | + |
| 94 | +def change_to_configuration_dir(): |
| 95 | + global _number |
| 96 | + dirname = path.join(environ["HOME"], ".vncdesk", str(_number)) |
| 97 | + try: |
| 98 | + chdir(dirname) |
| 99 | + except: |
| 100 | + exit_on_error("Cannot access directory " + dirname) |
| 101 | + |
| 102 | +def start(number): |
| 103 | + global _number, _display, _xvnc_lock_filename |
| 104 | + |
| 105 | + _number = number |
| 106 | + _display = ':' + str(_number) |
| 107 | + _xvnc_lock_filename = "/tmp/.X" + str(_number) + "-lock" |
| 108 | + |
| 109 | + change_to_configuration_dir() |
| 110 | + read_settings() |
| 111 | + create_password() |
| 112 | + start_xvnc() |
| 113 | + configure_xvnc() |
| 114 | + run_startup() |
0 commit comments