def test_http_forward(self): response = 'cha cha cha' port = self.osinteraction.get_open_port() s = self.start_http_server(port, response) session = Session() session.local_port = port session.server_port = 80 session.server_session_token = None session.http_forward = True self.app = start_openport_session(self, session) i = 0 while i < 20 and not session.http_forward_address: i += 1 sleep(1) # remote_port = session.server_port # self.assertEqual(80, remote_port) remote_host = session.http_forward_address print('remote host:' + remote_host) self.assertTrue('.u.' in remote_host, 'expect .u. in remote_host: %s' % remote_host) c = SimpleHTTPClient() actual_response = c.get('http://localhost:%s' % port) self.assertEqual(actual_response, response.strip()) actual_response = c.get('http://%s' % remote_host) self.assertEqual(actual_response, response.strip())
def test_brute_force_blocked__not_for_http_forward(self): port = self.osinteraction.get_open_port() response = 'cha cha cha' s = self.start_http_server(port, response) session = Session() session.local_port = port session.server_port = 80 session.server_session_token = None session.http_forward = True self.app = start_openport_session(self, session) click_open_for_ip_link(session.open_port_for_ip_link) link = session.http_forward_address print('link: %s' % link) c = SimpleHTTPClient() actual_response = c.get('http://localhost:%s' % port) self.assertEqual(actual_response, response.strip()) i = -1 try: for i in range(20): print("connection %s" % i) actual_response = c.get('http://%s' % link) self.assertEqual(actual_response, response.strip()) except (urllib2.HTTPError, urllib2.URLError) as e: self.fail('url error on connection nr %s' % i)
def test_long_key(self): private_key_file = os.path.join(os.path.dirname(__file__), 'testfiles', 'tmp', 'id_rsa_tmp') public_key_file = os.path.join(os.path.dirname(__file__), 'testfiles', 'tmp', 'id_rsa_tmp.pub') logger.debug('getting key pair') private_key, public_key = create_new_key_pair(4096) with open(private_key_file, 'w') as f: f.write(private_key) with open(public_key_file, 'w') as f: f.write(public_key) port_out = self.osinteraction.get_open_port() out_session = Session() out_session.local_port = port_out out_session.server_session_token = None out_session.public_key_file = public_key_file out_session.private_key_file = private_key_file out_app = None try: out_app = start_openport_session(self, out_session) remote_host, remote_port, link = out_session.server, out_session.server_port, out_session.open_port_for_ip_link click_open_for_ip_link(link) print(remote_port) sleep(10) #sleep(1000) check_tcp_port_forward(self, remote_host=remote_host, local_port=port_out, remote_port=remote_port) finally: if out_app: out_app.stop()
def test_request_restart_while_still_running(self): port_out = self.osinteraction.get_open_port() session = Session() session.local_port = port_out session.server_session_token = None openport = start_openport_session(self, session) print('session started') sleep(3) session2 = Session() session2.local_port = port_out session2.server_session_token = session.server_session_token openport2 = start_openport_session(self, session)
def start_openport_process(self, port): session = Session() session.local_port = port self.start_openport_process_from_session(session)
def start(self): # print('sys.argv: %s' % sys.argv) self.init_app(self.args) if self.args.daemonize: args = self.os_interaction.get_openport_exec() args.extend(sys.argv[1:]) args = self.os_interaction.unset_variable(args, '--daemonize') args = self.os_interaction.unset_variable(args, '-d') self.os_interaction.spawn_daemon(args) logger.info('App started in background.') logger.debug(args) sys.exit(0) key_registration_service.register_key(self.args, self.args.server) self.db_handler = dbhandler.DBHandler(self.args.database) self.server.db_handler = self.db_handler self.config.manager_port = self.args.listener_port self.config.openport_address = self.args.server if self.args.config_file: self.config.config = self.args.config_file logger.debug('db location: ' + self.db_handler.db_location) if self.args.list: self.print_shares() sys.exit() if self.args.kill: self.kill(self.args.kill) sys.exit() if self.args.kill_all: self.kill_all() sys.exit() if self.args.restart_shares: self.restart_sharing() logger.debug('exiting') sys.exit() if self.args.create_migrations: from openport.services import migration_service migration_service.create_migrations(self.db_handler.db_location) sys.exit() session = Session() session.local_port = int(self.args.local_port) session.server_port = self.args.request_port session.server_session_token = self.args.request_token session.forward_tunnel = self.args.forward_tunnel session.active = False # Will be set active in start_callback. if session.forward_tunnel: session.server_port = self.args.remote_port if self.args.local_port < 0: session.local_port = self.os_interaction.get_open_port() else: db_share = self.db_handler.get_share_by_local_port( session.local_port, filter_active=False) if db_share: logger.debug('previous share found in database') if is_running(db_share): logger.info('Port forward already running for port %s' % self.args.local_port) sys.exit(6) if db_share.restart_command and not self.args.restart_on_reboot: logger.warn( 'Port forward for port %s that would be restarted on reboot will not be restarted anymore.' % self.args.local_port) if not session.server_session_token: logger.debug("retrieved db share session token: %s" % db_share.server_session_token) session.server_session_token = db_share.server_session_token session.server_port = db_share.server_port else: logger.debug('No db share session could be found.') session.http_forward = self.args.http_forward if self.args.restart_on_reboot: session.restart_command = self.app_service.get_restart_command( session, database=self.args.database, verbose=self.args.verbose, server=self.args.server, ) self.app_service.check_username_in_config_file() session.ip_link_protection = self.args.ip_link_protection session.stop_observers.append(self.stop_callback) session.start_observers.append(self.save_share) session.error_observers.append(self.error_callback) session.success_observers.append(self.success_callback) session.app_management_port = self.server.get_port() session.start_observers.append(self.server.inform_start) session.success_observers.append(self.server.inform_success) session.error_observers.append(self.server.inform_failure) session.stop_observers.append(self.server.inform_stop) ensure_keys_exist(*get_default_key_locations()) self.session = session self.server.run_threaded() session.active = True self.save_share(session) self.openport.start_port_forward(session, server=self.args.server)
def foo(): session = Session() session.local_port = dialog.port_input.GetValue() session.http_forward = dialog.http_forward_checkbox.GetValue() p = self.app_service.start_openport_process_from_session( session)
def convert_session_from_db(self, openport_session): if openport_session is None: return None logger.debug('convert_session_from_db') share = Session() share.id = openport_session.id share.server = openport_session.server share.server_port = openport_session.remote_port share.server_session_token = openport_session.session_token share.local_port = openport_session.local_port share.pid = openport_session.pid share.active = openport_session.active share.account_id = openport_session.account_id share.key_id = openport_session.key_id share.http_forward = openport_session.http_forward share.http_forward_address = openport_session.http_forward_address share.app_management_port = openport_session.app_management_port share.open_port_for_ip_link = openport_session.open_port_for_ip_link share.restart_command = openport_session.restart_command try: share.restart_command = pickle.loads( share.restart_command.encode('ascii', 'ignore')) pass except Exception as e: pass return share
def test_forward_tunnel(self): port_out = self.osinteraction.get_open_port() out_session = Session() out_session.local_port = port_out out_session.server_session_token = None out_app, in_app = None, None try: out_app = start_openport_session(self, out_session) remote_host, remote_port, link = out_session.server, out_session.server_port, out_session.open_port_for_ip_link click_open_for_ip_link(link) check_tcp_port_forward(self, remote_host=remote_host, local_port=port_out, remote_port=remote_port) port_in = self.osinteraction.get_open_port() logger.info('port_in: %s' % port_in) in_session = Session() in_session.forward_tunnel = True in_session.server_port = out_session.server_port in_session.local_port = port_in in_app = start_openport_session(self, in_session) sleep(10) check_tcp_port_forward(self, remote_host='127.0.0.1', local_port=port_out, remote_port=port_in) port_bad_in = self.osinteraction.get_open_port() bad_session = Session() bad_session.forward_tunnel = True bad_session.server_port = out_session.server_port bad_session.local_port = port_bad_in keys = create_new_key_pair() private_key_file = 'testfiles/tmp/tmp_key' with open(private_key_file, 'w') as f: f.write(keys[0]) public_key_file = 'testfiles/tmp/tmp_key.pub' with open(public_key_file, 'w') as f: f.write(keys[1]) bad_session.public_key_file = public_key_file bad_session.private_key_file = private_key_file fail = False try: in_app = start_openport_session(self, bad_session) fail = True except AssertionError: pass self.assertFalse(fail) self.assertFalse( check_tcp_port_forward(self, remote_host='127.0.0.1', local_port=port_out, remote_port=port_bad_in, fail_on_error=False)) finally: if out_app: out_app.stop() if in_app: in_app.stop()
def test_brute_force_blocked(self): port = self.osinteraction.get_open_port() expected_response = 'cha cha cha' server1 = self.start_http_server(port, expected_response) session = Session() session.local_port = port session.server_session_token = None #session.http_forward = True self.app = start_openport_session(self, session) click_open_for_ip_link(session.open_port_for_ip_link) link = session.get_link() print('link: %s' % link) self.assertTrue(session.server_port > 1000) c = SimpleHTTPClient() actual_response = c.get('http://localhost:%s' % port) self.assertEqual(actual_response, expected_response.strip()) i = -1 try: for i in range(20): print("connection %s" % i) actual_response = c.get('http://%s' % link) self.assertEqual(actual_response, expected_response.strip()) except (urllib2.HTTPError, urllib2.URLError) as e: print(e) self.assertTrue(5 < i < 20, 'i should be around 10 but was %s' % i) # check download on different port is still ok port2 = self.osinteraction.get_open_port() session2 = Session() session2.local_port = port2 session2.server_session_token = None server2 = self.start_http_server(port2, expected_response) openport2 = start_openport_session(self, session2) sleep(3) print('http://%s' % session2.get_link()) click_open_for_ip_link(session2.open_port_for_ip_link) actual_response = c.get('http://%s' % session2.get_link()) self.assertEqual(actual_response, expected_response.strip()) server1.stop() server2.stop() openport2.stop_port_forward()
def test_http_forward__same_address(self): response = 'cha cha cha' port = self.osinteraction.get_open_port() s = self.start_http_server(port, response) session = Session() session.local_port = port session.server_session_token = None session.http_forward = True self.app = start_openport_session(self, session) remote_host = session.http_forward_address print('remote host:' + remote_host) self.assertTrue('.u.' in remote_host, 'expect .u. in remote_host: %s' % remote_host) c = SimpleHTTPClient() actual_response = c.get('http://localhost:%s' % port) self.assertEqual(actual_response, response.strip()) actual_response = c.get('http://%s' % remote_host) self.assertEqual(actual_response, response.strip()) session2 = Session() session2.local_port = port session2.server_session_token = None session2.http_forward = True session2.server_port = session.server_port session2.server_session_token = session.server_session_token self.app = start_openport_session(self, session2) # self.assertEqual(session.server_port, session2.server_port) self.assertEqual(session.http_forward_address, session2.http_forward_address)
def test_as_dict(self): share = Session() share.account_id = 6 share.key_id = 14 share.local_port = 2022 share.id = -1 share.server_session_token = 'abcd' share.server = 'a.openport.io' share.server_port = 1234 share.pid = 234 share.active = True share.restart_command = ['restart', 'command'] share.http_forward = True share.http_forward_address = 'http://jan.u.openport.io' share.open_port_for_ip_link = 'http//openport.io/l/1234/zerazer' share.keep_alive_interval_seconds = 123 share2 = Session().from_dict(share.as_dict()) self.assertEquals(share.id, share2.id) self.assertEquals(share.server, share2.server) self.assertEquals(share.server_port, share2.server_port) self.assertEquals(share.pid, share2.pid) self.assertEquals(share.active, share2.active) self.assertEquals(share.account_id, share2.account_id) self.assertEquals(share.key_id, share2.key_id) self.assertEquals(share.local_port, share2.local_port) self.assertEquals(share.server_session_token, share2.server_session_token) self.assertEquals(share.restart_command, share2.restart_command) self.assertEquals(share.http_forward, share2.http_forward) self.assertEquals(share.http_forward_address, share2.http_forward_address) self.assertEquals(share.open_port_for_ip_link, share2.open_port_for_ip_link) self.assertEquals(share.keep_alive_interval_seconds, share2.keep_alive_interval_seconds)