summaryrefslogtreecommitdiff
path: root/app.py
blob: 423c0b0c29877a2f1e92a514b46e80e7ef751472 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -*- 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)