Files

144 lines
5.2 KiB
Python
Raw Permalink Normal View History

2022-06-25 17:37:21 +02:00
#!/usr/bin/env python3
from utils.colors import colors, puts, sep
from utils.firegexapi import FiregexAPI
2022-08-01 23:59:32 +02:00
from multiprocessing import Process
2022-06-25 17:37:21 +02:00
from time import sleep
import iperf3
import csv
import argparse
import secrets
2022-08-01 23:59:32 +02:00
2022-06-25 17:37:21 +02:00
parser = argparse.ArgumentParser()
2022-06-26 02:12:57 +02:00
parser.add_argument("--address", "-a", type=str , required=False, help='Address of firegex backend', default="http://127.0.0.1:4444/")
2022-08-01 23:59:32 +02:00
parser.add_argument("--port", "-P", type=int , required=False, help='Port of the Benchmark service', default=1337)
2022-08-02 15:04:15 +02:00
parser.add_argument("--service-name", "-n", type=str , required=False, help='Name of the Benchmark service', default="Benchmark Service")
2022-06-25 17:37:21 +02:00
parser.add_argument("--password", "-p", type=str, required=True, help='Firegex password')
2022-08-02 15:04:15 +02:00
parser.add_argument("--num-of-regexes", "-r", type=int, required=True, help='Number of regexes to benchmark with')
2022-06-25 17:37:21 +02:00
parser.add_argument("--duration", "-d", type=int, required=False, help='Duration of the Benchmark in seconds', default=5)
2022-08-02 15:04:15 +02:00
parser.add_argument("--output-file", "-o", type=str, required=False, help='Output results csv file', default="benchmark.csv")
2025-02-05 18:43:04 +01:00
parser.add_argument("--num-of-streams", "-s", type=int, required=False, help='Number of concurrent streams', default=1)
2022-06-25 17:37:21 +02:00
args = parser.parse_args()
sep()
2022-06-26 00:56:03 +02:00
puts(f"Benchmarking with {args.num_of_regexes} regexes will start on ", color=colors.cyan, end="")
2022-06-25 17:37:21 +02:00
puts(f"{args.address}", color=colors.yellow)
2022-06-29 11:50:29 +02:00
firegex = FiregexAPI(args.address)
2022-06-25 17:37:21 +02:00
#Connect to Firegex
if (firegex.login(args.password)):
puts("Sucessfully logged in ✔", color=colors.green)
else:
puts("Benchmark Failed: Unknown response or wrong passowrd ✗", color=colors.red)
exit(1)
def exit_test(code):
if service_id:
2025-02-28 21:14:09 +01:00
server.kill()
if(firegex.nfregex_delete_service(service_id)):
puts("Sucessfully deleted service ✔", color=colors.green)
else:
puts("Test Failed: Coulnd't delete serivce ✗", color=colors.red)
exit_test(1)
exit(code)
2022-06-29 11:50:29 +02:00
2022-08-01 23:59:32 +02:00
#Create new Service
srvs = firegex.nfregex_get_services()
for ele in srvs:
if ele['name'] == args.service_name:
firegex.nfregex_delete_service(ele['service_id'])
service_id = firegex.nfregex_add_service(args.service_name, args.port, "tcp", "127.0.0.1/24")
if service_id:
puts(f"Sucessfully created service {service_id}", color=colors.green)
2022-08-02 15:04:15 +02:00
else:
puts("Test Failed: Failed to create service ✗", color=colors.red)
exit(1)
2022-06-25 17:37:21 +02:00
args.port = int(args.port)
args.duration = int(args.duration)
args.num_of_streams = int(args.num_of_streams)
2022-06-25 17:37:21 +02:00
#Start iperf3
def startServer():
server = iperf3.Server()
server.bind_address = '127.0.0.1'
server.port = args.port
2022-06-25 17:37:21 +02:00
server.verbose = False
while True:
server.run()
def getReading(port):
client = iperf3.Client()
client.duration = args.duration
client.server_hostname = '127.0.0.1'
client.port = port
client.zerocopy = True
client.verbose = False
2022-06-25 17:37:21 +02:00
client.protocol = 'tcp'
2022-06-25 20:25:19 +02:00
client.num_streams = args.num_of_streams
2022-06-26 00:56:03 +02:00
return round(client.run().json['end']['sum_received']['bits_per_second']/8e+6 , 3)
2022-06-25 17:37:21 +02:00
server = Process(target=startServer)
server.start()
sleep(1)
2025-02-18 23:03:46 +01:00
custom_regex = [
2025-03-24 19:26:01 +01:00
'(?:[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*|"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])'
2025-02-18 23:03:46 +01:00
]
def gen_regex():
2025-02-18 23:03:46 +01:00
"""
if len(custom_regex) == 0:
regex = secrets.token_hex(8)
else:
regex = custom_regex.pop()
"""
return secrets.token_hex(20)
2022-06-25 17:37:21 +02:00
#Get baseline reading
puts("Baseline without proxy: ", color=colors.blue, end='')
print(f"{getReading(args.port)} MB/s")
2022-06-25 17:37:21 +02:00
#Start firewall
2022-08-02 15:04:15 +02:00
if firegex.nfregex_start_service(service_id):
puts(f"Sucessfully started service with id {service_id}", color=colors.green)
2022-08-02 15:04:15 +02:00
else:
puts("Benchmark Failed: Coulnd't start the service ✗", color=colors.red)
exit_test(1)
2022-06-25 17:37:21 +02:00
#Get no regexs reading
results = []
puts("Performance with no regexes: ", color=colors.yellow , end='')
2022-08-01 23:59:32 +02:00
results.append(getReading(args.port))
2022-06-25 17:59:33 +02:00
print(f"{results[0]} MB/s")
2022-06-25 17:37:21 +02:00
#Add all the regexs
for i in range(1,args.num_of_regexes+1):
regex = gen_regex()
if not firegex.nfregex_add_regex(service_id,regex,"B",active=True,is_case_sensitive=False):
2025-02-03 02:04:10 +01:00
puts("Benchmark Failed: Couldn't add the regex ✗", color=colors.red)
exit_test(1)
2022-06-25 17:37:21 +02:00
puts(f"Performance with {i} regex(s): ", color=colors.red, end='')
2022-08-01 23:59:32 +02:00
results.append(getReading(args.port))
2022-06-25 17:59:33 +02:00
print(f"{results[i]} MB/s")
2022-06-25 17:37:21 +02:00
2022-06-25 17:59:33 +02:00
with open(args.output_file,'w') as f:
writer = csv.writer(f)
for i,result in enumerate(results):
writer.writerow([i,result])
puts(f"Sucessfully written results to {args.output_file}", color=colors.magenta)
2022-06-25 17:37:21 +02:00
2022-08-01 23:59:32 +02:00
#Delete the Service
if firegex.nfregex_delete_service(service_id):
2022-08-01 23:59:32 +02:00
puts(f"Sucessfully delete service with id {service_id}", color=colors.green)
else:
puts("Test Failed: Couldn't delete service ✗", color=colors.red)
exit(1)
2022-06-25 17:37:21 +02:00
server.terminate()