|
| 1 | +import os |
| 2 | +import logging |
| 3 | + |
| 4 | +logger = logging.getLogger(__name__) |
| 5 | +logger.setLevel('INFO') |
| 6 | + |
| 7 | +HERE = os.path.dirname(os.path.abspath(__file__)) |
| 8 | + |
| 9 | +def _xprahtml5_mappath(path): |
| 10 | + from getpass import getuser |
| 11 | + |
| 12 | + uri_parms = '?' + '&'.join([ |
| 13 | + 'username=' + getuser(), |
| 14 | +# 'password=' + _xprahtml5_passwd, |
| 15 | +# 'encryption=AES', |
| 16 | +# 'key=' + _xprahtml5_aeskey, |
| 17 | +# 'sharing=true', |
| 18 | + ]) |
| 19 | + |
| 20 | + if path in ('/', '/index.html', ): |
| 21 | + path = '/index.html' + uri_parms |
| 22 | + logger.info('Xpra URI: ' + path) |
| 23 | + |
| 24 | + return path |
| 25 | + |
| 26 | + |
| 27 | +def setup_xprahtml5(): |
| 28 | + """ Setup commands and and return a dictionary compatible |
| 29 | + with jupyter-server-proxy. |
| 30 | + """ |
| 31 | + from pathlib import Path |
| 32 | + from tempfile import gettempdir, mkstemp |
| 33 | + from random import choice |
| 34 | + from string import ascii_letters, digits |
| 35 | + from getpass import getuser |
| 36 | + |
| 37 | + global _xprahtml5_passwd, _xprahtml5_aeskey |
| 38 | + |
| 39 | + # password generator |
| 40 | + def _get_random_alphanumeric_string(length): |
| 41 | + letters_and_digits = ascii_letters + digits |
| 42 | + return (''.join((choice(letters_and_digits) for i in range(length)))) |
| 43 | + |
| 44 | + # ensure a known secure sockets directory exists, as /run/user/$UID might not be available |
| 45 | + socket_path = os.path.join(gettempdir(), 'xpra_sockets_' + str(os.getuid())) |
| 46 | + Path(socket_path).mkdir(mode=0o700, parents=True, exist_ok=True) |
| 47 | + logger.info('Created secure socket directory for Xpra: ' + socket_path) |
| 48 | + |
| 49 | + # generate file with random one-time-password |
| 50 | + _xprahtml5_passwd = _get_random_alphanumeric_string(16) |
| 51 | + try: |
| 52 | + fd_passwd, fpath_passwd = mkstemp() |
| 53 | + logger.info('Created secure password file for Xpra: ' + fpath_passwd) |
| 54 | + |
| 55 | + with open(fd_passwd, 'w') as f: |
| 56 | + f.write(_xprahtml5_passwd) |
| 57 | + |
| 58 | + except: |
| 59 | + logger.error("Passwd generation in temp file FAILED") |
| 60 | + raise FileNotFoundError("Passwd generation in temp file FAILED") |
| 61 | + |
| 62 | + # generate file with random encryption key |
| 63 | + _xprahtml5_aeskey = _get_random_alphanumeric_string(16) |
| 64 | + try: |
| 65 | + fd_aeskey, fpath_aeskey = mkstemp() |
| 66 | + logger.info('Created secure encryption key file for Xpra: ' + fpath_aeskey) |
| 67 | + |
| 68 | + with open(fd_aeskey, 'w') as f: |
| 69 | + f.write(_xprahtml5_aeskey) |
| 70 | + except: |
| 71 | + logger.error("Encryption key generation in temp file FAILED") |
| 72 | + raise FileNotFoundError("Encryption key generation in temp file FAILED") |
| 73 | + |
| 74 | + # create command |
| 75 | + cmd = [ os.path.join(HERE, 'share/launch_xpra.sh'), |
| 76 | + 'start', |
| 77 | + '--html=on', |
| 78 | + '--bind-tcp=0.0.0.0:{port}', |
| 79 | +# '--socket-dir="' + socket_path + '/"', fixme: socket_dir not recognized |
| 80 | + '--server-idle-timeout=86400', # stop server after 24h with no client connection |
| 81 | + '--start=xterm', |
| 82 | +# '--start-child=xterm', '--exit-with-children', |
| 83 | +# '--tcp-auth=file:filename=' + fpath_passwd, |
| 84 | +# '--tcp-encryption=AES', |
| 85 | +# '--tcp-encryption-keyfile=' + fpath_aeskey, |
| 86 | + '--clipboard-direction=both', |
| 87 | + '--no-bell', |
| 88 | + '--no-speaker', |
| 89 | + '--no-printing', |
| 90 | + '--no-microphone', |
| 91 | + '--no-notifications', |
| 92 | + '--dpi=96', |
| 93 | +# '--sharing' |
| 94 | + ] |
| 95 | + logger.info('Xpra command: ' + ' '.join(cmd)) |
| 96 | + |
| 97 | + return { |
| 98 | + 'environment': { # as '--socket-dir' does not work as expected, we set this |
| 99 | + 'XDG_RUNTIME_DIR': socket_path, |
| 100 | + }, |
| 101 | + 'command': cmd, |
| 102 | + 'mappath': _xprahtml5_mappath, |
| 103 | + 'absolute_url': False, |
| 104 | + 'timeout': 30, |
| 105 | + 'new_browser_tab': True, |
| 106 | + 'launcher_entry': { |
| 107 | + 'enabled': True, |
| 108 | + 'icon_path': os.path.join(HERE, 'share/xpra-logo.svg'), |
| 109 | + 'title': 'Xpra Desktop', |
| 110 | + }, |
| 111 | + } |
0 commit comments