class TestThroughProxy(BaseTestCase): def setUp(self): super(BaseTestCase, self).setUp() self.PARAMS = dict(self.PARAMS) s = socket.socket() s.bind(('127.0.0.1', 0)) _, self.port = s.getsockname() s.close() self.proxy = ProxyServer( self.PARAMS['host'], self.PARAMS['port'], session_factory=SessionFactory(), io_loop=self.io_loop, ) self.proxy.listen(self.port, address="127.0.0.1") self.PARAMS['port'] = self.port def _close_proxy_sessions(self): for sock in self.proxy.SessionsList: try: sock.c2p_stream.close() except: pass def tearDown(self): super(BaseTestCase, self).tearDown() try: self.proxy.stop() except: pass @gen_test def test_connection_closing(self): connection = yield Connection(**self.PARAMS) cursor = connection.cursor() self._close_proxy_sessions() try: yield cursor.execute('SELECT 1') except OperationalError: pass else: raise AssertionError("Unexpected normal situation") @gen_test def test_connection_closed(self): self.proxy.stop() try: yield Connection(**self.PARAMS) except OperationalError: pass else: raise AssertionError("Unexpected normal situation") @gen_test def test_remote_closing(self): pool = ConnectionPool(max_connections=int(os.getenv("MYSQL_POOL", "5")), idle_seconds=7200, **self.PARAMS) try: self.proxy.stop() yield pool.Connection() except OperationalError: pass else: raise AssertionError("Unexpected normal situation") finally: pool.close() @gen_test def test_pool_closing(self): pool = ConnectionPool(max_connections=int(os.getenv("MYSQL_POOL", "5")), idle_seconds=7200, **self.PARAMS) try: with (yield pool.Connection()) as connect: with connect.cursor() as cursor: self._close_proxy_sessions() yield cursor.execute("SELECT 1 as test") except (OperationalError, ConnectionNotFoundError) as e: pass else: raise AssertionError("Unexpected normal situation") finally: pool.close()
#!/usr/bin/env python import tornado.ioloop from maproxy.proxyserver import ProxyServer # HTTP->HTTPS # "server_ssl_options=True" simply means "connect to server with SSL" server = ProxyServer("www.google.com",443, server_ssl_options=True) server.listen(82) print("http://127.0.0.1:82 -> https://www.google.com:443") tornado.ioloop.IOLoop.instance().start();
DoT_Port = int(os.getenv('DoT_Port', 853)) DNS_Port = int(os.getenv('DNS_Port', 53)) DoT_SSL_cert = os.getenv('DoT_SSL_cert', '/etc/ssl/certs/ca-certificates.crt') if __name__ == '__main__': # calls the "stop()" when asked to exit signal.signal(signal.SIGINT, lambda sig, frame: iomanager.stop()) signal.signal(signal.SIGTERM, lambda sig, frame: iomanager.stop()) # define SSL context ssl_ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23) ssl_ctx.verify_mode = ssl.CERT_REQUIRED ssl_ctx.load_verify_locations(DoT_SSL_cert) DoT_SSL_options = ssl_ctx # define proxy server settings server = ProxyServer(DoT_IP, DoT_Port, server_ssl_options=DoT_SSL_options) server.listen(DNS_Port) iomanager.add(server) print(''' [dns2dot-proxy] tcp://127.0.0.1:%s -> tcp+tls://%s:%s with SSL Cert verification. You'll get an error, if the target server's CA is not listed in %s''' % (str(DNS_Port), DoT_IP, str(DoT_Port), str(DoT_SSL_cert))) # the next call to start() is blocking (thread=False) iomanager.start(thread=False) print("[dns2dot-proxy] Stopping...") iomanager.stop(gracefully=True, wait=False) print("[dns2dot-proxy] Stopped !")
class TestThroughProxy(BaseTestCase): def setUp(self): super(BaseTestCase, self).setUp() self.PARAMS = dict(self.PARAMS) s = socket.socket() s.bind(('127.0.0.1', 0)) _, self.port = s.getsockname() s.close() self.proxy = ProxyServer( self.PARAMS['host'], self.PARAMS['port'], session_factory=SessionFactory(), io_loop=self.io_loop, ) self.proxy.listen(self.port, address="127.0.0.1") self.PARAMS['port'] = self.port def _close_proxy_sessions(self): for sock in self.proxy.SessionsList: try: sock.c2p_stream.close() except: pass def tearDown(self): super(BaseTestCase, self).tearDown() try: self.proxy.stop() except: pass @gen_test def test_connection_closing(self): connection = yield Connection(**self.PARAMS) cursor = connection.cursor() self._close_proxy_sessions() try: yield cursor.execute('SELECT 1') except OperationalError: pass else: raise AssertionError("Unexpected normal situation") @gen_test def test_connection_closed(self): self.proxy.stop() try: yield Connection(**self.PARAMS) except OperationalError: pass else: raise AssertionError("Unexpected normal situation") @gen_test def test_remote_closing(self): pool = ConnectionPool( max_connections=int(os.getenv("MYSQL_POOL", "5")), idle_seconds=7200, **self.PARAMS ) try: self.proxy.stop() yield pool.Connection() except OperationalError: pass else: raise AssertionError("Unexpected normal situation") finally: pool.close() @gen_test def test_pool_closing(self): pool = ConnectionPool( max_connections=int(os.getenv("MYSQL_POOL", "5")), idle_seconds=7200, **self.PARAMS ) try: with (yield pool.Connection()) as connect: with connect.cursor() as cursor: self._close_proxy_sessions() yield cursor.execute("SELECT 1 as test") except (OperationalError, ConnectionNotFoundError) as e: pass else: raise AssertionError("Unexpected normal situation") finally: pool.close(10)
signal.signal(signal.SIGTERM, lambda sig,frame: g_IOManager.stop()) bUseSSL=True ssl_certs={ "certfile": os.path.join(os.path.dirname(sys.argv[0]), "certificate.pem"), "keyfile": os.path.join(os.path.dirname(sys.argv[0]), "privatekey.pem") } if not os.path.isfile(ssl_certs["certfile"]) or \ not os.path.isfile(ssl_certs["keyfile"]): print("Warning: SSL-Proxy is disabled . certificate file(s) not found") bUseSSL=False # HTTP->HTTP # On your computer, browse to "http://127.0.0.1:81/" and you'll get http://www.google.com server = ProxyServer("www.google.com",80) server.listen(81) g_IOManager.add(server) print("http://127.0.0.1:81 -> http://www.google.com") # HTTP->HTTPS # "server_ssl_options=True" simply means "connect to server with SSL" server = ProxyServer("www.google.com",443, server_ssl_options=True) server.listen(82) g_IOManager.add(server) print("http://127.0.0.1:82 -> https://www.google.com:443") if bUseSSL: # HTTPS->HTTP # "client_ssl_options=ssl_certs" simply means "listen using SSL" server = ProxyServer("www.google.com",80, client_ssl_options=ssl_certs)
#!/usr/bin/env python SSL_MQTT_SERVER_IP = "127.0.0.1" SSL_MQTT_SERVER_PORT = 8883 import tornado.ioloop from maproxy.proxyserver import ProxyServer # HTTP->HTTPS # "server_ssl_options=True" simply means "connect to server with SSL" #sl_certs={ "certfile": "./certificate.pem", # "keyfile": "./privatekey.pem" } # "client_ssl_options=ssl_certs" simply means "listen using SSL" #server = maproxy.proxyserver.ProxyServer("www.baidu.com",80, # client_ssl_options=ssl_certs) server = ProxyServer(SSL_MQTT_SERVER_IP, SSL_MQTT_SERVER_PORT, server_ssl_options=True) server.listen(1883, address='0.0.0.0') print("http://0.0.0.0:1883") tornado.ioloop.IOLoop.instance().start()
#!/usr/bin/env python import tornado.ioloop from maproxy.proxyserver import ProxyServer # HTTP->HTTPS # "server_ssl_options=True" simply means "connect to server with SSL" server = ProxyServer("www.google.com", 443, server_ssl_options=True) server.listen(82) print("http://127.0.0.1:82 -> https://www.google.com:443") tornado.ioloop.IOLoop.instance().start()
bUseSSL = True ssl_certs = { "certfile": os.path.join(os.path.dirname(sys.argv[0]), "certificate.pem"), "keyfile": os.path.join(os.path.dirname(sys.argv[0]), "privatekey.pem") } if not os.path.isfile(ssl_certs["certfile"]) or \ not os.path.isfile(ssl_certs["keyfile"]): print("Warning: SSL-Proxy is disabled . certificate file(s) not found") bUseSSL = False # HTTP->HTTP # On your computer, browse to "http://127.0.0.1:81/" and you'll get http://www.google.com server = ProxyServer("www.google.com", 80) server.listen(81) g_IOManager.add(server) print("http://127.0.0.1:81 -> http://www.google.com") # HTTP->HTTPS # "server_ssl_options=True" simply means "connect to server with SSL" server = ProxyServer("www.google.com", 443, server_ssl_options=True) server.listen(82) g_IOManager.add(server) print("http://127.0.0.1:82 -> https://www.google.com:443") if bUseSSL: # HTTPS->HTTP # "client_ssl_options=ssl_certs" simply means "listen using SSL" server = ProxyServer("www.google.com", 80,
#!/usr/bin/env python import signal from maproxy.iomanager import IOManager from maproxy.proxyserver import ProxyServer g_IOManager = IOManager() if __name__ == '__main__': # calls the "stop()" when asked to exit signal.signal(signal.SIGINT, lambda sig, frame: g_IOManager.stop()) signal.signal(signal.SIGTERM, lambda sig, frame: g_IOManager.stop()) # does the reverse proxy to server with TLS server = ProxyServer("1.1.1.1", 853, server_ssl_options=True) server.listen(53) g_IOManager.add(server) print("[dnsproxy] tcp://127.0.0.1:53 -> tcp+tls://1.1.1.1:853") # Blocking thread g_IOManager.start(thread=False) print(" Stopping the proxy") g_IOManager.stop(gracefully=True, wait=False) print("Stoppped the proxy server")