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
'''
# pylint: disable=c-extension-no-member
from __future__ import unicode_literals
import sys
import webbrowser
import json
from PyQt4 import QtCore, QtGui # @UnresolvedImport
import six
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 import tools
from uds import VERSION
import webbrowser
import json
from UDSWindow import Ui_MainWindow
# Server before this version uses "unsigned" scripts
@ -62,6 +63,7 @@ class UDSClient(QtGui.QMainWindow):
anim = 0
animInverted = False
serverVersion = 'X.Y.Z' # Will be overwriten on getVersion
req = None
def __init__(self):
QtGui.QMainWindow.__init__(self)
@ -103,8 +105,8 @@ class UDSClient(QtGui.QMainWindow):
# self.closeWindow()
# return
def showError(self, e):
logger.error('got error: {}'.format(e))
def showError(self, error):
logger.error('got error: %s', error)
self.stopAnim()
self.ui.info.setText('UDS Plugin Error') # In fact, main window is hidden, so this is not visible... :)
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.get()
except Exception as e:
logger.exception('Got exception: {}'.format(e))
logger.exception('Got exception on getTransportData')
raise e
@ -254,17 +256,17 @@ def done(data):
sys.exit(0)
# Ask user to approve endpoint
def approveHost(host, parentWindow=None):
def approveHost(hostName, parentWindow=None):
settings = QtCore.QSettings()
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>'
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
settings.endGroup()
@ -286,7 +288,7 @@ if __name__ == "__main__":
if six.PY3 is False:
logger.debug('Fixing threaded execution of commands')
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
try:
@ -295,14 +297,13 @@ if __name__ == "__main__":
if uri == '--test':
sys.exit(0)
logger.debug('URI: {}'.format(uri))
logger.debug('URI: %s', uri)
if uri[:6] != 'uds://' and uri[:7] != 'udss://':
raise Exception()
ssl = uri[3] == 's'
host, UDSClient.ticket, UDSClient.scrambler = uri.split('//')[1].split('/')
logger.debug('ssl: {}, host:{}, ticket:{}, scrambler:{}'.format(ssl, host, UDSClient.ticket, UDSClient.scrambler))
host, UDSClient.ticket, UDSClient.scrambler = uri.split('//')[1].split('/') # type: ignore
logger.debug('ssl:%s, host:%s, ticket:%s, scrambler:%s', ssl, host, UDSClient.ticket, UDSClient.scrambler)
except Exception:
logger.debug('Detected execution without valid URI, exiting')
QtGui.QMessageBox.critical(None, 'Notice', 'UDS Client Version {}'.format(VERSION), QtGui.QMessageBox.Ok)
@ -310,7 +311,7 @@ if __name__ == "__main__":
# Setup REST api endpoint
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'
try:
@ -323,7 +324,6 @@ if __name__ == "__main__":
win = UDSClient()
win.show()
win.start()
exitVal = app.exec_()
@ -336,4 +336,3 @@ if __name__ == "__main__":
logger.debug('Exiting')
sys.exit(exitVal)

View File

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

View File

@ -4,22 +4,28 @@
from __future__ import unicode_literals
import select
import SocketServer
import paramiko
import threading
import random
import time
import select
import paramiko
import six
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
allow_reuse_address = True
class Handler(SocketServer.BaseRequestHandler):
class Handler(socketserver.BaseRequestHandler):
def handle(self):
self.thread.currentConnections += 1
@ -29,29 +35,25 @@ class Handler(SocketServer.BaseRequestHandler):
(self.chain_host, self.chain_port),
self.request.getpeername())
except Exception as e:
logger.exception('Incoming request to %s:%d failed: %s' % (self.chain_host,
self.chain_port,
repr(e)))
logger.exception('Incoming request to %s:%d failed: %s', self.chain_host, self.chain_port, repr(e))
return
if chan is None:
logger.error('Incoming request to %s:%d was rejected by the SSH server.' %
(self.chain_host, self.chain_port))
logger.error('Incoming request to %s:%d was rejected by the SSH server.', self.chain_host, self.chain_port)
return
logger.debug('Connected! Tunnel open %r -> %r -> %r' % (self.request.getpeername(),
chan.getpeername(), (self.chain_host, self.chain_port)))
logger.debug('Connected! Tunnel open %r -> %r -> %r', self.request.getpeername(), chan.getpeername(), (self.chain_host, self.chain_port))
try:
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:
data = self.request.recv(1024)
if len(data) == 0:
if not data:
break
chan.send(data)
if chan in r:
data = chan.recv(1024)
if len(data) == 0:
if not data:
break
self.request.send(data)
except Exception:
@ -61,7 +63,7 @@ class Handler(SocketServer.BaseRequestHandler):
peername = self.request.getpeername()
chan.close()
self.request.close()
logger.debug('Tunnel closed from %r' % (peername,))
logger.debug('Tunnel closed from %r', peername,)
except Exception:
pass
@ -114,7 +116,7 @@ class ForwardThread(threading.Thread):
def _timerFnc(self):
self.timer = None
logger.debug('Timer fnc: {}'.format(self.currentConnections))
logger.debug('Timer fnc: %s', self.currentConnections)
self.stoppable = True
if self.currentConnections <= 0:
self.stop()
@ -126,11 +128,11 @@ class ForwardThread(threading.Thread):
self.client.load_system_host_keys()
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:
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: ')
self.status = 2 # Error
return
@ -142,7 +144,7 @@ class ForwardThread(threading.Thread):
event = self.stopEvent
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.start()
@ -166,7 +168,6 @@ class ForwardThread(threading.Thread):
self.client = None # Clean up
except Exception:
logger.exception('Exception stopping')
pass
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:
localPort = random.randrange(40000, 50000)
logger.debug('Connecting to {}:{} using {}/{} redirecting to {}:{}, listening on 127.0.0.1:{}'.format(
server, port, username, password, redirectHost, redirectPort, localPort))
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)
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)
return (ft, localPort)

View File

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

View File

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

View File

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

View File

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