Skip to content
Snippets Groups Projects
Commit bb65eb31 authored by Chris Hines's avatar Chris Hines
Browse files

improved, fixed a shutdown bug wth teh connection was never going to authorise

parent 47f7efda
No related branches found
No related tags found
No related merge requests found
......@@ -2,7 +2,9 @@ import threading
import socket, array
import select
import logging
#TES = 'http://localhost:8080/'
TES = 'http://localhost:5000/'
failthresh = 10
class TWSProxy(threading.Thread):
TIMEOUT = 10
......@@ -30,7 +32,9 @@ class TWSProxy(threading.Thread):
bytessofar = 0
header=bytearray(TWSProxy.MAXHEADERS)
keepreading = True
initcount=0
while keepreading:
initcount=initcount+1
r,w,e = select.select([self.csock],[],[],5)
if len(r) > 0:
partial = self.csock.recv(TWSProxy.MAXBUFF)
......@@ -42,6 +46,8 @@ class TWSProxy(threading.Thread):
else:
port = TWSProxy.verifyauth(header[0:bytessofar])
keepreading = False
if initcount > failthresh:
keepreading = False
if port is not None:
self.ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
......@@ -83,19 +89,17 @@ class TWSProxy(threading.Thread):
def verifyauth(header):
import re
import requests
print("in verifyauth")
token = b'twsproxyauth=(?P<authtok>\w+)[\W|$]'
m = re.search(token,header)
if m:
print(m.groupdict()['authtok'])
authtok = m.groupdict()['authtok']
s = requests.Session()
url = TES+'tunnelstat/'+authtok.decode()
print(url)
r = s.get(url)
print(r.text)
port = r.json()
try:
r = s.get(url)
port = r.json()
except:
raise Exception('unable to get a port number for the authtok')
return port
return None
# if m:
......@@ -175,21 +179,22 @@ class TWSProxy(threading.Thread):
logger = logging.getLogger()
closed = False
name = threading.current_thread().name
while not closed:
failcount=0
failthresh = 10
while not closed and failcount < failthresh:
r,w,e = select.select([src],[],[],TWSProxy.TIMEOUT)
if len(r) > 0:
failcount=0
buff = None
msglength = -1
try:
buff = src.recv(TWSProxy.MAXBUFF)
if buff is None:
print("buff is none ... is this normal?")
continue
except ConnectionResetError as e:
close = True
continue
except Exception as e:
print(e)
import traceback
print(traceback.format_exc())
# closed = True
......@@ -201,15 +206,20 @@ class TWSProxy(threading.Thread):
dest.shutdown(shuttype)
initshutdown.set()
closed = True
if len(w) == 0 and len(r) == 0 and len(r) == 0:
if initshutdown.isSet():
print("or possibly initshutdown")
else:
failcount=failcount+1
if failcount > failthresh:
dest.shutdown(shuttype)
initshutdown.set()
def main():
from . import server
import logging
logging.basicConfig(filename="/var/log/tws.log",format="%(asctime)s %(levelname)s:%(process)s: %(message)s")
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.debug("starting TWS proxy")
server = server.TWSServer()
logger.debug("initialised server object")
server.run()
from . import server
import logging
logging.basicConfig(filename="/var/log/tws.log",format="%(asctime)s %(levelname)s:%(process)s: %(message)s")
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.debug("starting TWS proxy")
print("starting TWS proxy")
server = server.TWSServer()
server.run()
import socket
from .. import TWSProxy
import logging
class TWSServer:
import socket
......@@ -7,6 +8,8 @@ class TWSServer:
MAXCONN = 5
def run(self):
logger = logging.getLogger()
logger.debug("starting up server")
serversocket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
#bind the socket to a public host,
......@@ -16,18 +19,17 @@ class TWSServer:
serversocket.bind(('127.0.0.1', port))
#become a server socket
serversocket.listen(self.MAXCONN)
print("bind success on ",port)
logger.debug("Server listening on port {}".format(port))
break
except Exception as e:
print("bind failure")
print(e)
pass
print("listening on port",port)
openconnections = []
logger.debug("waiting for a connection")
while 1:
(clientsocket, address) = serversocket.accept()
logger.debug("accepted connection on {}".format(clientsocket))
clientsocket.setblocking(True)
print("accepting connection")
tunnel = TWSProxy(clientsocket)
tunnel.daemon = True
tunnel.start()
......@@ -36,5 +38,4 @@ class TWSServer:
if not c.is_alive():
c.join()
openconnections.remove(c)
print("there are ",len(openconnections),"current connections")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment