runtime/Stunnel.py
author Andrey Skvortsov <andrej.skvortzov@gmail.com>
Wed, 13 Mar 2019 10:19:30 +0300
changeset 2536 2747d6e72eb8
parent 2492 7dd551ac2fa0
child 2542 a3ec35ee94e7
permissions -rw-r--r--
Fix invalid python3 syntax

Syntax checking using python3 ...
Python 3.7.2+
File "./connectors/PYRO/__init__.py", line 76
except Exception, e:
^
SyntaxError: invalid syntax

Syntax error in ./connectors/PYRO/__init__.py
File "./connectors/PYRO/PSK_Adapter.py", line 40
except ProtocolError, x:
^
SyntaxError: invalid syntax

Syntax error in ./s.py
File "./runtime/Worker.py", line 67
raise _job.exc_info[0], _job.exc_info[1], _job.exc_info[2]
^
SyntaxError: invalid syntax
from __future__ import absolute_import
import os
from binascii import b2a_hqx
try:
    from runtime.spawn_subprocess import call
except ImportError:
    from subprocess import call

restart_stunnel_cmdline = ["/etc/init.d/S50stunnel", "restart"]

_PSKpath = None


def PSKgen(ID, PSKpath):

    # b2a_hqx output len is 4/3 input len
    secret = os.urandom(192)  # int(256/1.3333)
    secretstring = b2a_hqx(secret)

    PSKstring = ID+":"+secretstring
    with open(PSKpath, 'w') as f:
        f.write(PSKstring)
    call(restart_stunnel_cmdline)


def ensurePSK(ID, PSKpath):
    global _PSKpath
    _PSKpath = PSKpath
    # check if already there
    if not os.path.exists(PSKpath):
        # create if needed
        PSKgen(ID, PSKpath)


def getPSKID(errorlog):
    if _PSKpath is not None:
        if not os.path.exists(_PSKpath):
            errorlog(
                'Error: Pre-Shared-Key Secret in %s is missing!\n' % _PSKpath)
            return None
        ID, _sep, PSK = open(_PSKpath).read().partition(':')
        PSK = PSK.rstrip('\n\r')
        return (ID, PSK)
    return None