|
| 1 | +# std lib |
| 2 | +import sys |
| 3 | +import os |
| 4 | +import json |
| 5 | +import pexpect |
| 6 | +import subprocess |
| 7 | + |
| 8 | +# local includes |
| 9 | +import client.logic.rcm_utils as rcm_utils |
| 10 | +from client.miscellaneous.logger import logic_logger |
| 11 | +from client.utils.pyinstaller_utils import resource_path |
| 12 | +import client.logic.cipher as cipher |
| 13 | +from client.miscellaneous.config_parser import parser, defaults |
| 14 | + |
| 15 | + |
| 16 | +class Executable(object): |
| 17 | + """Class representing a program that can be run on the command line.""" |
| 18 | + |
| 19 | + def __init__(self, name): |
| 20 | + self.exe = name.split(' ') |
| 21 | + self.default_env = {} |
| 22 | + self.returncode = None |
| 23 | + |
| 24 | + # if not self.exe: |
| 25 | + # raise ProcessError("Cannot construct executable for '%s'" % name) |
| 26 | + |
| 27 | + def add_default_arg(self, arg): |
| 28 | + """Add a default argument to the command.""" |
| 29 | + self.exe.append(arg) |
| 30 | + |
| 31 | + def add_arg_value(self, arg, value): |
| 32 | + """Add a default argument to the command.""" |
| 33 | + self.exe.append(arg) |
| 34 | + self.exe.append(value) |
| 35 | + |
| 36 | + def add_default_env(self, key, value): |
| 37 | + """Set an environment variable when the command is run. |
| 38 | +
|
| 39 | + Parameters: |
| 40 | + key: The environment variable to set |
| 41 | + value: The value to set it to |
| 42 | + """ |
| 43 | + self.default_env[key] = value |
| 44 | + |
| 45 | + @property |
| 46 | + def command(self): |
| 47 | + """The command-line string. |
| 48 | +
|
| 49 | + Returns: |
| 50 | + str: The executable and default arguments |
| 51 | + """ |
| 52 | + return ' '.join(self.exe) |
| 53 | + |
| 54 | + @property |
| 55 | + def name(self): |
| 56 | + """The executable name. |
| 57 | +
|
| 58 | + Returns: |
| 59 | + str: The basename of the executable |
| 60 | + """ |
| 61 | + return os.path.basename(self.path) |
| 62 | + |
| 63 | + @property |
| 64 | + def path(self): |
| 65 | + """The path to the executable. |
| 66 | +
|
| 67 | + Returns: |
| 68 | + str: The path to the executable |
| 69 | + """ |
| 70 | + return self.exe[0] |
| 71 | + |
| 72 | + |
| 73 | +class TurboVNCExecutable(Executable): |
| 74 | + def __init__(self): |
| 75 | + |
| 76 | + self.set_env() |
| 77 | + |
| 78 | + if sys.platform.startswith('darwin'): |
| 79 | + exe = "open" |
| 80 | + else: |
| 81 | + exe = rcm_utils.which('vncviewer') |
| 82 | + if not exe : |
| 83 | + logic_logger.error("vncviewer not found! Check the PATH environment variable.") |
| 84 | + if sys.platform == 'win32': |
| 85 | + # if the executable path contains spaces, it has to be put inside apexes |
| 86 | + exe = "\"" + exe + "\"" |
| 87 | + # self.exe = exe |
| 88 | + logic_logger.debug("vncviewer path: " + exe) |
| 89 | + |
| 90 | + super(TurboVNCExecutable, self).__init__(exe) |
| 91 | + |
| 92 | + def set_env(self): |
| 93 | + # set the environment |
| 94 | + if getattr(sys, 'frozen', False): |
| 95 | + logic_logger.debug("Running in a bundle") |
| 96 | + # if running in a bundle, we hardcode the path |
| 97 | + # of the built-in vnc viewer and plink (windows only) |
| 98 | + os.environ['JAVA_HOME'] = resource_path('turbovnc') |
| 99 | + if sys.platform == 'win32': |
| 100 | + # on windows 10, administration policies prevent execution of external programs |
| 101 | + # located in %TEMP% ... it seems that it cannot be loaded |
| 102 | + |
| 103 | + home_path = os.path.expanduser('~') |
| 104 | + desktop_path = os.path.join(home_path, 'Desktop') |
| 105 | + exe_dir_path = os.path.dirname(sys.executable) |
| 106 | + if os.path.exists(desktop_path): |
| 107 | + rcm_unprotected_path = os.path.join(exe_dir_path, '.rcm', 'executables') |
| 108 | + os.makedirs(rcm_unprotected_path, exist_ok=True) |
| 109 | + dest_dir = os.path.join(rcm_unprotected_path, 'turbovnc') |
| 110 | + rcm_utils.copytree(resource_path('turbovnc'), dest_dir) |
| 111 | + os.environ['JAVA_HOME'] = dest_dir |
| 112 | + |
| 113 | + os.environ['JDK_HOME'] = os.environ['JAVA_HOME'] |
| 114 | + os.environ['JRE_HOME'] = os.path.join(os.environ['JAVA_HOME'], 'jre') |
| 115 | + os.environ['CLASSPATH'] = os.path.join(os.environ['JAVA_HOME'], 'lib') + \ |
| 116 | + os.pathsep + os.path.join(os.environ['JRE_HOME'], 'lib') |
| 117 | + os.environ['PATH'] = os.path.join(os.environ['JAVA_HOME'], 'bin') + os.pathsep + os.environ['PATH'] |
| 118 | + logic_logger.debug("JAVA_HOME: " + str(os.environ['JAVA_HOME'])) |
| 119 | + logic_logger.debug("JRE_HOME: " + str(os.environ['JRE_HOME'])) |
| 120 | + logic_logger.debug("JDK_HOME: " + str(os.environ['JDK_HOME'])) |
| 121 | + logic_logger.debug("CLASSPATH: " + str(os.environ['CLASSPATH'])) |
| 122 | + logic_logger.debug("PATH: " + str(os.environ['PATH'])) |
| 123 | + |
| 124 | + def build(self, session, local_portnumber): |
| 125 | + nodelogin = session.hash['nodelogin'] |
| 126 | + # local_portnumber = rcm_utils.get_unused_portnumber() |
| 127 | + |
| 128 | + tunnel = session.hash['tunnel'] |
| 129 | + try: |
| 130 | + tunnelling_method = json.loads(parser.get('Settings', 'ssh_client')) |
| 131 | + except Exception: |
| 132 | + tunnelling_method = "internal" |
| 133 | + logic_logger.info("Using " + str(tunnelling_method) + " ssh tunnelling") |
| 134 | + |
| 135 | + # Decrypt password |
| 136 | + vncpassword = session.hash.get('vncpassword', '') |
| 137 | + rcm_cipher = cipher.RCMCipher() |
| 138 | + vncpassword_decrypted = rcm_cipher.decrypt(vncpassword) |
| 139 | + |
| 140 | + # Darwin |
| 141 | + if sys.platform.startswith('darwin'): |
| 142 | + self.add_arg_value("-W", "vnc://:" + vncpassword_decrypted + "@127.0.0.1:" + str(local_portnumber)) |
| 143 | + |
| 144 | + # Win64 |
| 145 | + elif sys.platform == 'win32': |
| 146 | + self.add_default_arg("/nounixlogin") |
| 147 | + self.add_default_arg("/noreconnect") |
| 148 | + self.add_default_arg("/nonewconn") |
| 149 | + self.add_arg_value("/loglevel", str(rcm_utils.vnc_loglevel)) |
| 150 | + self.add_arg_value("/password", vncpassword_decrypted) |
| 151 | + |
| 152 | + # Linux |
| 153 | + else: |
| 154 | + self.add_arg_value("-quality", "80") |
| 155 | + self.add_arg_value("-password", vncpassword_decrypted) |
| 156 | + self.add_default_arg("-noreconnect") |
| 157 | + self.add_default_arg("-nonewconn") |
| 158 | + |
| 159 | + if not sys.platform.startswith('darwin'): |
| 160 | + if tunnel == 'y': |
| 161 | + self.add_default_arg("127.0.0.1:" + str(local_portnumber)) |
| 162 | + else: |
| 163 | + self.add_default_arg(nodelogin + ":" + session.hash['display']) |
| 164 | + |
| 165 | + |
| 166 | +class SSHExecutable(Executable): |
| 167 | + def __init__(self): |
| 168 | + |
| 169 | + self.set_env() |
| 170 | + |
| 171 | + # ssh executable |
| 172 | + if sys.platform == 'win32': |
| 173 | + exe = rcm_utils.which('PLINK') |
| 174 | + else: |
| 175 | + exe = rcm_utils.which('ssh') |
| 176 | + if not exe: |
| 177 | + if sys.platform == 'win32': |
| 178 | + logic_logger.error("plink.exe not found! Check the PATH environment variable.") |
| 179 | + else: |
| 180 | + logic_logger.error("ssh not found!") |
| 181 | + return |
| 182 | + if sys.platform == 'win32': |
| 183 | + # if the executable path contains spaces, it has to be put inside apexes |
| 184 | + exe = "\"" + exe + "\"" |
| 185 | + |
| 186 | + super(SSHExecutable, self).__init__(exe) |
| 187 | + |
| 188 | + def set_env(self): |
| 189 | + return |
| 190 | + |
| 191 | + def build(self, user, password, session, local_portnumber): |
| 192 | + node = session.hash['node'] |
| 193 | + nodelogin = session.hash['nodelogin'] |
| 194 | + |
| 195 | + portstring = session.hash.get('port', '') |
| 196 | + if portstring: |
| 197 | + portnumber = int(portstring) |
| 198 | + else: |
| 199 | + portnumber = 5900 + int(session.hash['display']) |
| 200 | + |
| 201 | + self.add_arg_value("-L", "127.0.0.1:" + str(local_portnumber) + ":" + node + ":" + str(portnumber) ) |
| 202 | + self.add_default_arg(user + "@" + nodelogin) |
| 203 | + |
| 204 | + if sys.platform == 'win32': |
| 205 | + self.add_default_arg("-ssh") |
| 206 | + self.add_arg_value("-pw", str(password)) |
| 207 | + |
| 208 | + |
| 209 | +class NativeSSHTunnelForwarder(object): |
| 210 | + def __init__(self, tunnel_command, password): |
| 211 | + self.tunnel_command = tunnel_command |
| 212 | + self.tunnel_process = None |
| 213 | + self.password = password |
| 214 | + |
| 215 | + |
| 216 | + def __enter__(self): |
| 217 | + logic_logger.debug(self.tunnel_command) |
| 218 | + if sys.platform == 'win32': |
| 219 | + self.tunnel_process = subprocess.Popen(self.tunnel_command, |
| 220 | + bufsize=1, |
| 221 | + stdout=subprocess.PIPE, |
| 222 | + stderr=subprocess.PIPE, |
| 223 | + stdin=subprocess.PIPE, |
| 224 | + shell=False, |
| 225 | + universal_newlines=True, |
| 226 | + env=os.environ) |
| 227 | + while True: |
| 228 | + o = self.tunnel_process.stderr.readline() |
| 229 | + logic_logger.debug("tunnel process stderr: " + str(o.strip())) |
| 230 | + |
| 231 | + if o.strip().split()[0] == 'Store': |
| 232 | + break |
| 233 | + if o.strip().split()[0] == 'Using': |
| 234 | + break |
| 235 | + if o.strip().split()[0] == 'connection.': |
| 236 | + self.tunnel_process.stdin.write("yes\r\n") |
| 237 | + continue |
| 238 | + |
| 239 | + else: |
| 240 | + self.tunnel_process = pexpect.spawn(self.tunnel_command, |
| 241 | + timeout=50) |
| 242 | + |
| 243 | + i = self.tunnel_process.expect(['continue connecting', |
| 244 | + 'password', |
| 245 | + r'.+', |
| 246 | + pexpect.TIMEOUT, |
| 247 | + pexpect.EOF], |
| 248 | + timeout=5) |
| 249 | + |
| 250 | + if i == 0: |
| 251 | + self.tunnel_process.sendline('yes') |
| 252 | + i = self.tunnel_process.expect(['continue connecting', |
| 253 | + 'password', |
| 254 | + pexpect.TIMEOUT, |
| 255 | + pexpect.EOF], |
| 256 | + timeout=5) |
| 257 | + |
| 258 | + if i == 1: |
| 259 | + self.tunnel_process.sendline(self.password) |
| 260 | + |
| 261 | + def __exit__(self, exc_type, exc_value, tb): |
| 262 | + self.stop() |
| 263 | + |
| 264 | + def stop(self): |
| 265 | + logic_logger.debug("Stopping ssh tunnelling") |
| 266 | + |
| 267 | + if self.tunnel_process: |
| 268 | + if sys.platform == 'win32': |
| 269 | + logic_logger.debug("Killing ssh tunnel process " + |
| 270 | + str(self.tunnel_process.pid)) |
| 271 | + self.tunnel_process.terminate() |
| 272 | + else: |
| 273 | + self.tunnel_process.close(force=True) |
0 commit comments