# -*- coding: UTF-8 -*- import subprocess import trex_replay import json from flask import Flask, request, jsonify from datetime import datetime from http_player import HttpPlayer from ssl_player import SSLPlayer from ftp_player import FtpPlayer from dns_player import DnsPlayer from mail_player import MailPlayer # from dtls_player import DtlsPlayer import quic_player as quic_player from doh_player import DohPlayer from mpls_one_label_player import MplsOneLabelPlayer from gre_over_gre_player import GreOverGrePlayer from unordered_tcp_player import UnorderedTcpPlayer app = Flask(__name__) @app.route('/v1/traffic', methods=["POST"]) def run_traffic_env(): try: print("Receive messages from api client.") json_data = request.get_json() type = json_data.get("type") tool = json_data.get("tool") temp_data = json_data if tool == "http": print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Simulate http traffic.", flush=True) player = HttpPlayer() result = player.send_http_traffic(temp_data, "", "") elif tool == "https" or tool == "ssl": print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Simulate ssl traffic.", flush=True) player = SSLPlayer() result = player.send_ssl_traffic(temp_data, "", "") elif tool == "ftp": print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Simulate ftp traffic.", flush=True) player = FtpPlayer() result = player.send_ftp_traffic(temp_data, "", "") elif tool == "dns": print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Simulate dns traffic.", flush=True) player = DnsPlayer() result = player.send_dns_query(temp_data, "", "") elif tool == "mail": print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Simulate mail traffic.", flush=True) player = MailPlayer() result = player.send_mail_traffic(temp_data, "", "") elif tool == "doh": print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Simulate doh traffic.", flush=True) player = DohPlayer() result = player.send_doh_query(temp_data, "", "") elif tool == "dtls": print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Simulate dtls traffic by client.", flush=True) # player = DtlsPlayer() # result = player.send_dtls_traffic(temp_data, "", "") elif tool == "quic": print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Simulate quic traffic by client.", flush=True) result = quic_player.asyncio.run(quic_player.send_quic_traffic(temp_data, "", "")) elif tool == "mpls_one_label": print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Simulate mpls traffic by tcpreplay.", flush=True) player = MplsOneLabelPlayer() result = player.send_mpls_one_label_traffic(temp_data, "", "") elif tool == "gre_over_gre": print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Simulate gre traffic by tcpreplay.", flush=True) player = GreOverGrePlayer() result = player.send_gre_over_gre_traffic(temp_data, "", "") elif tool == "unordered_tcp": print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Simulate unordered tcp by tcpreplay.", flush=True) player = UnorderedTcpPlayer() result = player.send_unordered_tcp_traffic(temp_data, "", "") elif tool == "vxlan": print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), str(datetime.now().microsecond)[:3], "Simulate vxlan traffic by tcpreplay.", flush=True) print("to be added") elif tool == "trex": print("Execute {} operation.".format(type)) pcap_name = json_data.get("pcap_name") + ".pcap" yaml_name = json_data.get("yaml_name") + ".yaml" m = json_data.get("m") d = json_data.get("d") if "active_flows" in json_data: active_flows = json_data.get("active_flows") else: active_flows = 0 clients_start_ip = json_data.get("clients_start_ip") clients_end_ip = json_data.get("clients_end_ip") servers_start_ip = json_data.get("servers_start_ip") servers_end_ip = json_data.get("servers_end_ip") path_dict = { "trex_workpath": "/opt/trex/v3.06", "traffic_playback_workpath": "/opt/traffic_replay" } tr = trex_replay.TrafficReplay(path_dict) result = tr.trex_playback(yaml_name, pcap_name, active_flows, m, d, clients_start_ip, clients_end_ip, servers_start_ip, servers_end_ip) # if flag == False and (type == "curl" or type == "wget" or type == "nslookup"): # print("Execute {} operation.".format(type)) # command = json_data.get("command") # p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8") # output, error = p.communicate() # result = output + error response_result = {} response_result["code"] = 200 response_result["result"] = result response = jsonify(response_result) response.status_code = response_result["code"] return response except Exception as e: print(e) response_result = {} response_result["code"] = 419 response_result["result"] = "Fail to execute {} operation.".format(type) response = jsonify(response_result) response.status_code = response_result["code"] return response if __name__ == '__main__': app.run(host = "0.0.0.0", port = 8900)