ISTS22_pfSense/push.py

53 lines
1.2 KiB
Python

#! /usr/bin/python3
import requests
import json
import time
def post_req(url, data={}):
response = requests.post(url, json=data)
try:
response.raise_for_status()
except requests.exceptions.HTTPError as err:
print(err)
else:
print(f"post success | HTTP {response.status_code}")
def main(x):
data = {}
rules = {}
with open("data.json","r") as file:
data = json.load(file)
with open("rules.json","r") as file:
rules = json.load(file)
x = data["TEAM_NUM"]
GATEWAY = f"10.{x}.1.254"
auth_data = {"client-id":data["USER"],"client-token":data["PASS"]}
#First enable the firewall
post_req(f"http://{GATEWAY}/api/v1/firewall/apply", data=auth_data)
#Implement all the firewall rules
for rule in rules["RULES"]:
if rule["TAG"] == x:
print("skipping rule bc of tag match")
continue
data = {}
for item in rule["DATA"]:
data[item] = rule["DATA"][item]
for item in auth_data:
data[item] = auth_data[item]
URL = rule["URL"].format(GATEWAY)
post_req(URL, data=data)
time.sleep(0.25)
if __name__ == "__main__":
main()