From d8d5950260d3c26b3cfe0c80fb4aa92624b45f76 Mon Sep 17 00:00:00 2001
From: Ubuntu <ubuntu@sv2test.novalocal>
Date: Tue, 27 Nov 2018 04:13:49 +0000
Subject: [PATCH] increased logging, fixed port for connections

---
 twsproxy/__init__.py        | 142 +++++++++++++++++++++---------------
 twsproxy/server/__init__.py |   2 +-
 2 files changed, 86 insertions(+), 58 deletions(-)

diff --git a/twsproxy/__init__.py b/twsproxy/__init__.py
index 10722c8..26181f5 100644
--- a/twsproxy/__init__.py
+++ b/twsproxy/__init__.py
@@ -2,8 +2,7 @@ import threading
 import socket, array
 import select
 import logging
-#TES =  'http://localhost:8080/'
-TES =  'http://localhost:5000/'
+TES =  'http://localhost:8080/'
 failthresh = 10
 class TWSProxy(threading.Thread):
 
@@ -29,6 +28,7 @@ class TWSProxy(threading.Thread):
             open a new socket to the server and return this
         """
 
+        logger = logging.getLogger()
         bytessofar = 0
         header=bytearray(TWSProxy.MAXHEADERS)
         keepreading = True
@@ -40,13 +40,19 @@ class TWSProxy(threading.Thread):
                 partial = self.csock.recv(TWSProxy.MAXBUFF)
                 header[bytessofar:bytessofar+len(partial)] = partial
                 bytessofar = bytessofar + len(partial)
+                logger.debug('inittws, checking headers')
                 port = TWSProxy.verifyauth(header[0:bytessofar])
                 if port is not None:
+                    logger.debug('inittws, found auth token and got port {}'.format(port))
                     keepreading = False
+                if port is None:
+                    logger.debug('inittws, no authtok found in the first {} bytes'.format(bytessofar))
             else:
+                logger.debug('inittws, select returned with no more info, verifying headers for the last time')
                 port = TWSProxy.verifyauth(header[0:bytessofar])
                 keepreading = False
             if initcount > failthresh:
+                logger.debug('inittws, checked headers enough times, got {} bytes with no success'.format(bytessofar))
                 keepreading = False
 
         if port is not None:
@@ -55,31 +61,48 @@ class TWSProxy(threading.Thread):
             try:
                 self.ssock.connect(('127.0.0.1',port))
                 if bytessofar > 0:
-                    TWSProxy.reliablesend(self.ssock,header[0:bytessofar],bytessofar)
+                    logger.debug('inittws: returning the initial {} bytes'.format(bytessofar))
+                    return (header,bytessofar)
+                    #TWSProxy.reliablesend(self.ssock,header[0:bytessofar],bytessofar)
             except ConnectionRefusedError as e:
+                logger.error('inittws, got my tokens, got my port attempted to send data and it all went pear shaped')
                 self.ssock.close()
                 self.csock.close()
                 closed.set()
         else:
+            logger.debug('inittws, unable to determine correct port, closing connection')
             self.csock.close()
             closed.set()
 
         
 
     def run(self):
+        import logging
+        logger = logging.getLogger()
+        logger.debug('starting new thread listening on {}'.format(self.csock))
         initshutdown = threading.Event()
         initshutdown.clear()
-        self.inittws(initshutdown)
+        (header, bytessofar) = self.inittws(initshutdown)
+        logger.debug('connecting {} to {}'.format(self.csock,self.ssock))
+        if initshutdown.isSet():
+            logger.debug('NOT connecting {} inittws did not connect us'.format(self.csock))
+
         t1 = None
         t2 = None
+        logger.debug('testing initshutdown')
         if not initshutdown.isSet():
+            logger.debug('creating threads')
 #            TWSProxy.twosocks(self.csock,self.ssock,initshutdown)
             t1 = threading.Thread(target=TWSProxy.sockcopy, args=(self.ssock, self.csock, initshutdown),name='s2c')
             t2 = threading.Thread(target=TWSProxy.sockcopy, args=(self.csock, self.ssock, initshutdown),name='c2s')
             t1.start()
             t2.start()
+            logger.debug('connections made, threads listening, passing along the initial headers')
+            TWSProxy.reliablesend(self.ssock,header[0:bytessofar],bytessofar)
             t1.join()
             t2.join()
+        else:
+            logger.debug('not creating threads, shutdown is set')
         if self.ssock is not None:
             self.ssock.close()
         if self.csock is not None:
@@ -89,17 +112,25 @@ class TWSProxy(threading.Thread):
     def verifyauth(header):
         import re
         import requests
+        logger = logging.getLogger()
         token = b'twsproxyauth=(?P<authtok>\w+)[\W|$]'
         m = re.search(token,header)
         if m:
             authtok = m.groupdict()['authtok']
+            logger.debug('authtok found {}'.format(authtok))
             s = requests.Session()
             url = TES+'tunnelstat/'+authtok.decode()
             try:
+                logger.debug('verify auth querying url {}'.format(url))
                 r = s.get(url)
                 port = r.json()
+                if port is None:
+                    logger.debug('authtok found but no tunnel to connect to {}'.format(authtok))
+                else:
+                    logger.debug('authtok found port found {}'.format(port))
             except:
-                raise Exception('unable to get a port number for the authtok')
+                logger.error('authtok found port found {}'.format(port))
+                raise Exception('unable to get a port number for the authtok {}'.format(r.text))
             return port
         return None
 #        if m:
@@ -118,57 +149,53 @@ class TWSProxy(threading.Thread):
                 raise RuntimeError("socket connection broken")
             totalsent = totalsent + sent
 
-#    @staticmethod
-#    def twosocks(client,server,initshutdown):
-#        import threading
-#        logger=logging.getLogger()
-#        closed = False
-#        clientopen = True
-#        serveropen = True
-#        shuttype = socket.SHUT_RD
-#        while serveropen or clientopen:
-#            r,w,e = select.select([client,server],[],[],TWSProxy.TIMEOUT)
-#            print(r)
-#            if client in r:
-#                try:
-#                    buff = client.recv(TWSProxy.MAXBUFF)
-#                    msglength = len(buff)
-#                    if msglength > 0:
-#                        TWSProxy.reliablesend(server,buff,msglength)
-#                    else:
-#                        print("client closed socket for reading")
-#                        clientopen = False
-#                        server.shutdown(shuttype)
-#                except:
-#                        clientopen = False
-#            if server in r:
-#                try:
-#                    buff = server.recv(TWSProxy.MAXBUFF)
-#                    msglength = len(buff)
-#                    if msglength > 0:
-#                        TWSProxy.reliablesend(client,buff,msglength)
-#                    else:
-#                        print("server sent zero bytes ... what does it mean?")
-#                        client.shutdown(shuttype)
-##                        print("server closed socket for reading")
-#                        serveropen = False
-#                except:
-#                        serveropen = False
-#        # If the client has finished sending, and we've finished transmitting to the server
-#        # The server may still have some data to transmit to the client
-#        closed = False
-#        while not closed:
-#            r,w,e = select.select([server],[],[],TWSProxy.TIMEOUT)
-#            print("cleanup loop")
-#            buff = server.recv(TWSProxy.MAXBUFF)
-#            msglength = len(buff)
-#            if msglength > 0:
-#                try:
-#                    TWSProxy.reliablesend(client,buff,msglength)
-#                except BrokenPipeError as e:
-#                    pass
-#            else:
-#                closed = True
+    @staticmethod
+    def twosocks(client,server,initshutdown):
+        import threading
+        logger=logging.getLogger()
+        closed = False
+        clientopen = True
+        serveropen = True
+        shuttype = socket.SHUT_RD
+        while serveropen or clientopen:
+            r,w,e = select.select([client,server],[],[],TWSProxy.TIMEOUT)
+            if client in r:
+                try:
+                    buff = client.recv(TWSProxy.MAXBUFF)
+                    msglength = len(buff)
+                    if msglength > 0:
+                        TWSProxy.reliablesend(server,buff,msglength)
+                    else:
+                        clientopen = False
+                        server.shutdown(shuttype)
+                except:
+                        clientopen = False
+            if server in r:
+                try:
+                    buff = server.recv(TWSProxy.MAXBUFF)
+                    msglength = len(buff)
+                    if msglength > 0:
+                        TWSProxy.reliablesend(client,buff,msglength)
+                    else:
+                        client.shutdown(shuttype)
+#                        print("server closed socket for reading")
+                        serveropen = False
+                except:
+                        serveropen = False
+        # If the client has finished sending, and we've finished transmitting to the server
+        # The server may still have some data to transmit to the client
+        closed = False
+        while not closed:
+            r,w,e = select.select([server],[],[],TWSProxy.TIMEOUT)
+            buff = server.recv(TWSProxy.MAXBUFF)
+            msglength = len(buff)
+            if msglength > 0:
+                try:
+                    TWSProxy.reliablesend(client,buff,msglength)
+                except BrokenPipeError as e:
+                    pass
+            else:
+                closed = True
 
 
 
@@ -177,6 +204,7 @@ class TWSProxy(threading.Thread):
         shuttype = socket.SHUT_RD
         import threading
         logger = logging.getLogger()
+        logger.debug('sock copy started')
         closed = False
         name = threading.current_thread().name
         failcount=0
@@ -196,7 +224,7 @@ class TWSProxy(threading.Thread):
                     continue
                 except Exception as e:
                     import traceback
-                    print(traceback.format_exc())
+                    logger.error(traceback.format_exc())
 #                    closed = True
                     continue
                 msglength = len(buff)
diff --git a/twsproxy/server/__init__.py b/twsproxy/server/__init__.py
index b46119b..99ca5fb 100644
--- a/twsproxy/server/__init__.py
+++ b/twsproxy/server/__init__.py
@@ -14,7 +14,7 @@ class TWSServer:
                     socket.AF_INET, socket.SOCK_STREAM)
         #bind the socket to a public host,
         # and a well-known port
-        for port in range(self.LISTENPORT,self.LISTENPORT+10):
+        for port in range(self.LISTENPORT,self.LISTENPORT+1):
             try:
                 serversocket.bind(('127.0.0.1', port))
         #become a server socket
-- 
GitLab