1
0
mirror of https://github.com/dkmstr/openuds.git synced 2024-12-22 13:34:04 +03:00

Fixed so now UDSClient can run with python 2 or python 3

This commit is contained in:
Adolfo Gómez García 2020-01-22 14:41:09 +01:00
parent 26e429019e
commit 142f7122fc
7 changed files with 67 additions and 65 deletions

View File

@ -30,21 +30,22 @@
''' '''
@author: Adolfo Gómez, dkmaster at dkmon dot com @author: Adolfo Gómez, dkmaster at dkmon dot com
''' '''
# pylint: disable=c-extension-no-member
from __future__ import unicode_literals from __future__ import unicode_literals
import sys import sys
import webbrowser
import json
from PyQt4 import QtCore, QtGui # @UnresolvedImport from PyQt4 import QtCore, QtGui # @UnresolvedImport
import six import six
from uds.rest import RestRequest from uds.rest import RestRequest
from uds.forward import forward from uds.forward import forward # pylint: disable=unused-import
from uds.log import logger from uds.log import logger
from uds import tools from uds import tools
from uds import VERSION from uds import VERSION
import webbrowser
import json
from UDSWindow import Ui_MainWindow from UDSWindow import Ui_MainWindow
# Server before this version uses "unsigned" scripts # Server before this version uses "unsigned" scripts
@ -62,6 +63,7 @@ class UDSClient(QtGui.QMainWindow):
anim = 0 anim = 0
animInverted = False animInverted = False
serverVersion = 'X.Y.Z' # Will be overwriten on getVersion serverVersion = 'X.Y.Z' # Will be overwriten on getVersion
req = None
def __init__(self): def __init__(self):
QtGui.QMainWindow.__init__(self) QtGui.QMainWindow.__init__(self)
@ -103,8 +105,8 @@ class UDSClient(QtGui.QMainWindow):
# self.closeWindow() # self.closeWindow()
# return # return
def showError(self, e): def showError(self, error):
logger.error('got error: {}'.format(e)) logger.error('got error: %s', error)
self.stopAnim() self.stopAnim()
self.ui.info.setText('UDS Plugin Error') # In fact, main window is hidden, so this is not visible... :) self.ui.info.setText('UDS Plugin Error') # In fact, main window is hidden, so this is not visible... :)
self.closeWindow() self.closeWindow()
@ -169,7 +171,7 @@ class UDSClient(QtGui.QMainWindow):
self.req = RestRequest('/{}/{}'.format(self.ticket, self.scrambler), self, self.transportDataReceived, params={'hostname': tools.getHostName(), 'version': VERSION}) self.req = RestRequest('/{}/{}'.format(self.ticket, self.scrambler), self, self.transportDataReceived, params={'hostname': tools.getHostName(), 'version': VERSION})
self.req.get() self.req.get()
except Exception as e: except Exception as e:
logger.exception('Got exception: {}'.format(e)) logger.exception('Got exception on getTransportData')
raise e raise e
@ -254,17 +256,17 @@ def done(data):
sys.exit(0) sys.exit(0)
# Ask user to approve endpoint # Ask user to approve endpoint
def approveHost(host, parentWindow=None): def approveHost(hostName, parentWindow=None):
settings = QtCore.QSettings() settings = QtCore.QSettings()
settings.beginGroup('endpoints') settings.beginGroup('endpoints')
approved = settings.value(host, False).toBool() approved = settings.value(hostName, False).toBool()
errorString = '<p>The server <b>{}</b> must be approved:</p>'.format(host) errorString = '<p>The server <b>{}</b> must be approved:</p>'.format(hostName)
errorString += '<p>Only approve UDS servers that you trust to avoid security issues.</p>' errorString += '<p>Only approve UDS servers that you trust to avoid security issues.</p>'
if approved or QtGui.QMessageBox.warning(parentWindow, 'ACCESS Warning', errorString, QtGui.QMessageBox.Yes | QtGui.QMessageBox.No) == QtGui.QMessageBox.Yes: if approved or QtGui.QMessageBox.warning(parentWindow, 'ACCESS Warning', errorString, QtGui.QMessageBox.Yes | QtGui.QMessageBox.No) == QtGui.QMessageBox.Yes:
settings.setValue(host, True) settings.setValue(hostName, True)
approved = True approved = True
settings.endGroup() settings.endGroup()
@ -286,7 +288,7 @@ if __name__ == "__main__":
if six.PY3 is False: if six.PY3 is False:
logger.debug('Fixing threaded execution of commands') logger.debug('Fixing threaded execution of commands')
import threading import threading
threading._DummyThread._Thread__stop = lambda x: 42 threading._DummyThread._Thread__stop = lambda x: 42 # type: ignore, pylint: disable=protected-access
# First parameter must be url # First parameter must be url
try: try:
@ -295,14 +297,13 @@ if __name__ == "__main__":
if uri == '--test': if uri == '--test':
sys.exit(0) sys.exit(0)
logger.debug('URI: {}'.format(uri)) logger.debug('URI: %s', uri)
if uri[:6] != 'uds://' and uri[:7] != 'udss://': if uri[:6] != 'uds://' and uri[:7] != 'udss://':
raise Exception() raise Exception()
ssl = uri[3] == 's' ssl = uri[3] == 's'
host, UDSClient.ticket, UDSClient.scrambler = uri.split('//')[1].split('/') host, UDSClient.ticket, UDSClient.scrambler = uri.split('//')[1].split('/') # type: ignore
logger.debug('ssl: {}, host:{}, ticket:{}, scrambler:{}'.format(ssl, host, UDSClient.ticket, UDSClient.scrambler)) logger.debug('ssl:%s, host:%s, ticket:%s, scrambler:%s', ssl, host, UDSClient.ticket, UDSClient.scrambler)
except Exception: except Exception:
logger.debug('Detected execution without valid URI, exiting') logger.debug('Detected execution without valid URI, exiting')
QtGui.QMessageBox.critical(None, 'Notice', 'UDS Client Version {}'.format(VERSION), QtGui.QMessageBox.Ok) QtGui.QMessageBox.critical(None, 'Notice', 'UDS Client Version {}'.format(VERSION), QtGui.QMessageBox.Ok)
@ -310,7 +311,7 @@ if __name__ == "__main__":
# Setup REST api endpoint # Setup REST api endpoint
RestRequest.restApiUrl = '{}://{}/rest/client'.format(['http', 'https'][ssl], host) RestRequest.restApiUrl = '{}://{}/rest/client'.format(['http', 'https'][ssl], host)
logger.debug('Setting request URL to {}'.format(RestRequest.restApiUrl)) logger.debug('Setting request URL to %s', RestRequest.restApiUrl)
# RestRequest.restApiUrl = 'https://172.27.0.1/rest/client' # RestRequest.restApiUrl = 'https://172.27.0.1/rest/client'
try: try:
@ -323,7 +324,6 @@ if __name__ == "__main__":
win = UDSClient() win = UDSClient()
win.show() win.show()
win.start() win.start()
exitVal = app.exec_() exitVal = app.exec_()
@ -336,4 +336,3 @@ if __name__ == "__main__":
logger.debug('Exiting') logger.debug('Exiting')
sys.exit(exitVal) sys.exit(exitVal)

View File

@ -31,9 +31,6 @@
''' '''
from __future__ import unicode_literals from __future__ import unicode_literals
# On centos, old six release does not includes byte2int, nor six.PY2
import six
VERSION = '3.0.0' VERSION = '3.0.0'
__title__ = 'udclient' __title__ = 'udclient'

View File

@ -4,22 +4,28 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import select
import SocketServer
import paramiko
import threading import threading
import random import random
import time import time
import select
import paramiko
import six
from .log import logger from .log import logger
class ForwardServer(SocketServer.ThreadingTCPServer): if six.PY2:
import SocketServer as socketserver # pylint: disable=import-error
else:
import socketserver
class ForwardServer(socketserver.ThreadingTCPServer):
daemon_threads = True daemon_threads = True
allow_reuse_address = True allow_reuse_address = True
class Handler(SocketServer.BaseRequestHandler): class Handler(socketserver.BaseRequestHandler):
def handle(self): def handle(self):
self.thread.currentConnections += 1 self.thread.currentConnections += 1
@ -29,29 +35,25 @@ class Handler(SocketServer.BaseRequestHandler):
(self.chain_host, self.chain_port), (self.chain_host, self.chain_port),
self.request.getpeername()) self.request.getpeername())
except Exception as e: except Exception as e:
logger.exception('Incoming request to %s:%d failed: %s' % (self.chain_host, logger.exception('Incoming request to %s:%d failed: %s', self.chain_host, self.chain_port, repr(e))
self.chain_port,
repr(e)))
return return
if chan is None: if chan is None:
logger.error('Incoming request to %s:%d was rejected by the SSH server.' % logger.error('Incoming request to %s:%d was rejected by the SSH server.', self.chain_host, self.chain_port)
(self.chain_host, self.chain_port))
return return
logger.debug('Connected! Tunnel open %r -> %r -> %r' % (self.request.getpeername(), logger.debug('Connected! Tunnel open %r -> %r -> %r', self.request.getpeername(), chan.getpeername(), (self.chain_host, self.chain_port))
chan.getpeername(), (self.chain_host, self.chain_port)))
try: try:
while self.event.is_set() is False: while self.event.is_set() is False:
r, _w, _x = select.select([self.request, chan], [], [], 1) r, _w, _x = select.select([self.request, chan], [], [], 1) # pylint: disable=unused-variable
if self.request in r: if self.request in r:
data = self.request.recv(1024) data = self.request.recv(1024)
if len(data) == 0: if not data:
break break
chan.send(data) chan.send(data)
if chan in r: if chan in r:
data = chan.recv(1024) data = chan.recv(1024)
if len(data) == 0: if not data:
break break
self.request.send(data) self.request.send(data)
except Exception: except Exception:
@ -61,7 +63,7 @@ class Handler(SocketServer.BaseRequestHandler):
peername = self.request.getpeername() peername = self.request.getpeername()
chan.close() chan.close()
self.request.close() self.request.close()
logger.debug('Tunnel closed from %r' % (peername,)) logger.debug('Tunnel closed from %r', peername,)
except Exception: except Exception:
pass pass
@ -114,7 +116,7 @@ class ForwardThread(threading.Thread):
def _timerFnc(self): def _timerFnc(self):
self.timer = None self.timer = None
logger.debug('Timer fnc: {}'.format(self.currentConnections)) logger.debug('Timer fnc: %s', self.currentConnections)
self.stoppable = True self.stoppable = True
if self.currentConnections <= 0: if self.currentConnections <= 0:
self.stop() self.stop()
@ -126,11 +128,11 @@ class ForwardThread(threading.Thread):
self.client.load_system_host_keys() self.client.load_system_host_keys()
self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
logger.debug('Connecting to ssh host %s:%d ...' % (self.server, self.port)) logger.debug('Connecting to ssh host %s:%d ...', self.server, self.port)
try: try:
self.client.connect(self.server, self.port, username=self.username, password=self.password, timeout=5) self.client.connect(self.server, self.port, username=self.username, password=self.password, timeout=5)
except Exception as e: except Exception:
logger.exception('Exception connecting: ') logger.exception('Exception connecting: ')
self.status = 2 # Error self.status = 2 # Error
return return
@ -142,7 +144,7 @@ class ForwardThread(threading.Thread):
event = self.stopEvent event = self.stopEvent
thread = self thread = self
logger.debug('Wait Time: {}'.format(self.waitTime)) logger.debug('Wait Time: %s', self.waitTime)
self.timer = threading.Timer(self.waitTime, self._timerFnc) self.timer = threading.Timer(self.waitTime, self._timerFnc)
self.timer.start() self.timer.start()
@ -166,7 +168,6 @@ class ForwardThread(threading.Thread):
self.client = None # Clean up self.client = None # Clean up
except Exception: except Exception:
logger.exception('Exception stopping') logger.exception('Exception stopping')
pass
def forward(server, port, username, password, redirectHost, redirectPort, localPort=None, waitTime=10): def forward(server, port, username, password, redirectHost, redirectPort, localPort=None, waitTime=10):
@ -179,8 +180,8 @@ def forward(server, port, username, password, redirectHost, redirectPort, localP
if localPort is None: if localPort is None:
localPort = random.randrange(40000, 50000) localPort = random.randrange(40000, 50000)
logger.debug('Connecting to {}:{} using {}/{} redirecting to {}:{}, listening on 127.0.0.1:{}'.format( logger.debug('Connecting to %s:%s using %s/%s redirecting to %s:%s, listening on 127.0.0.1:%s',
server, port, username, password, redirectHost, redirectPort, localPort)) server, port, username, password, redirectHost, redirectPort, localPort)
ft = ForwardThread(server, port, username, password, localPort, redirectHost, redirectPort, waitTime) ft = ForwardThread(server, port, username, password, localPort, redirectHost, redirectPort, waitTime)
@ -190,4 +191,3 @@ def forward(server, port, username, password, redirectHost, redirectPort, localP
time.sleep(0.1) time.sleep(0.1)
return (ft, localPort) return (ft, localPort)

View File

@ -37,7 +37,7 @@ import sys
import tempfile import tempfile
if sys.platform.startswith('linux'): if sys.platform.startswith('linux'):
from os.path import expanduser from os.path import expanduser # pylint: disable=ungrouped-imports
logFile = expanduser('~/udsclient.log') logFile = expanduser('~/udsclient.log')
else: else:
logFile = os.path.join(tempfile.gettempdir(), b'udsclient.log') logFile = os.path.join(tempfile.gettempdir(), b'udsclient.log')

View File

@ -34,16 +34,15 @@ from __future__ import unicode_literals
import sys import sys
LINUX = 'Linux' LINUX = 'Linux'
WINDOWS = 'Windows' WINDOWS = 'Windows'
MAC_OS_X = 'Mac os x' MAC_OS_X = 'Mac os x'
def getOs(): def getOs():
if sys.platform.startswith('linux'): if sys.platform.startswith('linux'):
return LINUX return LINUX
elif sys.platform.startswith('win'): if sys.platform.startswith('win'):
return WINDOWS return WINDOWS
elif sys.platform.startswith('darwin'): if sys.platform.startswith('darwin'):
return MAC_OS_X return MAC_OS_X
return 'other'

View File

@ -30,19 +30,24 @@
''' '''
@author: Adolfo Gómez, dkmaster at dkmon dot com @author: Adolfo Gómez, dkmaster at dkmon dot com
''' '''
# pylint: disable=c-extension-no-member,no-name-in-module
from __future__ import unicode_literals from __future__ import unicode_literals
import json
import urllib
import six
from PyQt4.QtCore import pyqtSignal, pyqtSlot from PyQt4.QtCore import pyqtSignal, pyqtSlot
from PyQt4.QtCore import QObject, QUrl, QSettings from PyQt4.QtCore import QObject, QUrl, QSettings
from PyQt4.QtCore import Qt from PyQt4.QtCore import Qt
from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkReply, QSslCertificate from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkReply, QSslCertificate
from PyQt4.QtGui import QMessageBox from PyQt4.QtGui import QMessageBox
from . import osDetector
from . import VERSION from . import VERSION
import json
import osDetector
import six
import urllib
class RestRequest(QObject): class RestRequest(QObject):

View File

@ -40,11 +40,12 @@ import random
import os import os
import socket import socket
import stat import stat
import six
import sys import sys
import time import time
from log import logger import six
from .log import logger
_unlinkFiles = [] _unlinkFiles = []
_tasksToWait = [] _tasksToWait = []
@ -132,7 +133,7 @@ def getHostName():
hostname = hostname.decode(sys_fs_enc) hostname = hostname.decode(sys_fs_enc)
hostname = six.text_type(hostname) hostname = six.text_type(hostname)
logger.info('Hostname: {}'.format(hostname)) logger.info('Hostname: %s', hostname)
return hostname return hostname
# Queing operations (to be executed before exit) # Queing operations (to be executed before exit)
@ -149,13 +150,14 @@ def unlinkFiles():
''' '''
Removes all wait-and-unlink files Removes all wait-and-unlink files
''' '''
if len(_unlinkFiles) > 0: if _unlinkFiles:
time.sleep(5) # Wait 5 seconds before deleting anything time.sleep(5) # Wait 5 seconds before deleting anything
for f in _unlinkFiles:
try: for f in _unlinkFiles:
os.unlink(f) try:
except Exception: os.unlink(f)
pass except Exception:
pass
def addTaskToWait(taks): def addTaskToWait(taks):