def list_rules_from(s): data = s.recv(1024) print 'step 0 Received', repr(data) rules = [] if not data.startswith('ofp_socket v1'): return rules send_command(s, "list") data = s.recv(1024) current_remaining = "" #var to store what data is currently remaining, thoses data will need to be prepend to next incoming data print 'step 1 Received', repr(data) while True: elements = data.split('\n') previous_remaining = current_remaining; #store remaining data so we can clear safely current_remaining current_remaining = "" if len(elements): if previous_remaining: elements[0] = previous_remaining + elements[0]; #if there was data remaining from a previous recv(), then prepend it to first item #if last element (index -1) does not finish by \n, it's an incomplete data, so we store its content, and it will be prepend to next received data if not data.endswith("\n"): current_remaining = elements[-1] del elements[-1] #remove this incomplete data for element in elements: element = element.strip(' ') if not element: continue print 'parsed', repr(element) if element.startswith('list start'): pass elif element.startswith('list end'): print_out("read_lines() done") return rules else: rules.append(element) data = s.recv(1024) print 'step 2 Received', repr(data)
def socket_send_test_lines(lines): if not len(lines): lines = [ '+x 10.254.1.1 http://www.example_1.com/newone.html', '+x 10.254.1.2 http://www.example_2.com/newone.html', '+x 10.254.1.3 http://www.example_3.com/newone.html', '+x 10.254.1.4 http://www.example_4.com/newone.html', '+x 10.254.1.5 http://www.example_5.com/newone.html', '+x 10.254.1.6 http://www.example_6.com/newone.html', '+x 10.254.1.7 http://www.example_7.com/newone.html', '+x 10.254.1.8 http://www.example_8.com/newone.html', '+x 10.254.1.9 http://www.example_9.com/newone.html', '-x 10.254.1.1 http://www.example_1.com/newone.html', '-x 10.254.1.2 http://www.example_2.com/newone.html', '-x 10.254.1.3 http://www.example_3.com/newone.html', '-x 10.254.1.4 http://www.example_4.com/newone.html', '-x 10.254.1.5 http://www.example_5.com/newone.html', '-x 10.254.1.6 http://www.example_6.com/newone.html', '-x 10.254.1.7 http://www.example_7.com/newone.html', '-x 10.254.1.8 http://www.example_8.com/newone.html', '-x 10.254.1.9 http://www.example_9.com/newone.html', ] send_lines(lines) print_out("socket_test...")
def send_lines(lines): HOST = '127.0.0.1' # The remote host PORT = 9996 # The same port as used by the server s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) send_lines_from(s, lines) s.close() print_out("send_lines() done")
def main(): parse_options() print_out("Starting server on port " + str(app_port)); print_out("Ssl : {0}".format("Enabled" if use_ssl else "Disabled")); app = make_app() ssl_ctx = { "certfile": crt_cert_file, "keyfile": key_cert_file, } if use_ssl: http_server = HTTPServer(app, ssl_options=ssl_ctx) else: http_server = HTTPServer(app) http_server.listen(app_port) IOLoop.instance().start()
def main(): parse_options() print_out("Starting server on port " + str(app_port)) print_out("Ssl : {0}".format("Enabled" if use_ssl else "Disabled")) app = make_app() ssl_ctx = { "certfile": crt_cert_file, "keyfile": key_cert_file, } if use_ssl: http_server = HTTPServer(app, ssl_options=ssl_ctx) else: http_server = HTTPServer(app) http_server.listen(app_port) IOLoop.instance().start()
def list_rules_from(s): data = s.recv(1024) print 'step 0 Received', repr(data) rules = [] if not data.startswith('ofp_socket v1'): return rules send_command(s, "list") data = s.recv(1024) current_remaining = "" #var to store what data is currently remaining, thoses data will need to be prepend to next incoming data print 'step 1 Received', repr(data) while True: elements = data.split('\n') previous_remaining = current_remaining #store remaining data so we can clear safely current_remaining current_remaining = "" if len(elements): if previous_remaining: elements[0] = previous_remaining + elements[0] #if there was data remaining from a previous recv(), then prepend it to first item #if last element (index -1) does not finish by \n, it's an incomplete data, so we store its content, and it will be prepend to next received data if not data.endswith("\n"): current_remaining = elements[-1] del elements[-1] #remove this incomplete data for element in elements: element = element.strip(' ') if not element: continue print 'parsed', repr(element) if element.startswith('list start'): pass elif element.startswith('list end'): print_out("read_lines() done") return rules else: rules.append(element) data = s.recv(1024) print 'step 2 Received', repr(data)
def parse_options(): try: opts, args = getopt.getopt(sys.argv[1:],"hup:",["port=","unsecure"]) except getopt.GetoptError: print_out("-p <port>") sys.exit(2) for opt, arg in opts: if opt == '-h': print_out("-p <port> : (--port) specify http server port : default : 443") print_out("-u : (--unsecure) use http instead of https : default : disabled") sys.exit() elif opt in ("-p", "--port"): global app_port app_port = int(arg) elif opt in ("-u", "--unsecure"): global use_ssl use_ssl = False
def api_add_test(): print_out("api_add_test..."); app_target_host = "localhost"; app_port = 443; if not tilegx: app_target_host = "localhost"; app_port = 4443; try: opts, args = getopt.getopt(sys.argv[1:],"hp:t:",["port=", "target="]) except getopt.GetoptError: print '{0} -t <target host> -p <port>'.format(sys.argv[0]) sys.exit(2) for opt, arg in opts: if opt == '-h': print '{0} -t <target host> -p <port>'.format(sys.argv[0]) sys.exit() elif opt in ("-p", "--port"): app_port = int(arg) elif opt in ("-t", "--target"): app_target_host = arg print 'app_port %d' % app_port print 'app_target_host %s' % app_target_host data = { 'targets': [ { "ip" : "192.168.0.123", #"url" : "http://www.example2.com", #"url" : "http://vps117860.ovh.net/test/ÃÂ/phishing2", "url" : "http://vps117860.ovh.net/test/ß/phishing2", } ] } url = 'https://{0}:{1}/v1/targets/add'.format(app_target_host, app_port); #requests.post(url, json=data) req = urllib2.Request(url, json.dumps(data), {'Content-Type': 'application/json'} ) response = urllib2.urlopen(req) print_out("urlopen() done") print_out(response)
def parse_options(): try: opts, args = getopt.getopt(sys.argv[1:], "hup:", ["port=", "unsecure"]) except getopt.GetoptError: print_out("-p <port>") sys.exit(2) for opt, arg in opts: if opt == '-h': print_out( "-p <port> : (--port) specify http server port : default : 443" ) print_out( "-u : (--unsecure) use http instead of https : default : disabled" ) sys.exit() elif opt in ("-p", "--port"): global app_port app_port = int(arg) elif opt in ("-u", "--unsecure"): global use_ssl use_ssl = False
def socket_send_test_from_file(file_name): print_out("socket_send_test_from_file...") with open(file_name) as f: lines = [ '+{0}'.format(line.rstrip('\n')) for line in f] # lines = f.read().splitlines() socket_send_test_lines(lines)
def socket_list_test(): lines = list_rules() print_out("result : ") for line in lines: print_out(line) print_out("socket_list_test...")