Last active
January 11, 2023 20:33
-
-
Save Bluscream/f83f4fd0d9216bc2c1af8774abe96849 to your computer and use it in GitHub Desktop.
Bulk-Add headers to requestly exported rules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$headers = Get-Content "Desktop\headers.txt" | |
$json = Get-Content "Desktop\requestly_rules.json" | ConvertFrom-Json | |
# Create a new array to hold the modifications | |
$modifications = @() | |
# Create a variable to keep track of the last id number | |
$idNumber = 0 | |
foreach ($header in $headers) { | |
$modification = @{ | |
"Request" = @() | |
"Response" = @{ | |
"header" = $header | |
"value" = "" | |
"type" = "Remove" | |
"id" = "g" + ("{0:D3}" -f $idNumber++) | |
} | |
} | |
# Append the modification to the array | |
$modifications += $modification | |
Write-Output "Added header: $header with id: $modification.id" | |
} | |
$json[0].pairs.modifications = $modifications | |
$json | ConvertTo-Json | Set-Content "Desktop\requestly_rules.json" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import random | |
import string | |
from typing import Any, List, TypeVar, Callable, Type, cast | |
from requestly_rules import * | |
headers_file = "headers.txt" | |
headers_mode = "Remove" | |
rules_file = "requestly_rules.json" | |
headers = [] | |
with open(headers_file, "r") as file: | |
headers = file.read().splitlines() | |
print(f"Loaded list of {len(headers)} headers to add") | |
rules: List[RequestlyRule] = [] | |
with open(rules_file, "r") as file: | |
rules = requestly_rules_from_dict(json.load(file)) | |
print(f"Loaded list of {len(rules)} rules") | |
responses : List[Response] = rules[0].pairs[0].modifications.response | |
print(f"Loaded list of {len(responses)} responses from rules") | |
id_number = 0 | |
for header in headers: | |
header = header.title() | |
if any(r.header.title() == header for r in responses): | |
print(f"Skipping header: {header}, already exists.") | |
continue | |
id = "g" + format(id_number, "03d") | |
id_number += 1 | |
response = Response(header, "", headers_mode, id) | |
responses.append(response) | |
print(f"Added header: {response.header} ({id})") | |
with open(rules_file, "w") as file: | |
json.dump(requestly_rules_to_dict(rules), file, indent=4) | |
print(f"Wrote {len(responses)} responses to {rules_file}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
access-control-allow-origin | |
access-control-allow-credentials | |
access-control-allow-headers | |
access-control-allow-methods | |
access-control-expose-headers | |
access-control-max-age | |
access-control-request-headers | |
access-control-request-method | |
origin | |
timing-allow-origin | |
allow | |
cross-origin-embedder-policy | |
cross-origin-opener-policy | |
cross-origin-resource-policy | |
content-security-policy | |
content-security-policy-report-only | |
expect-ct | |
feature-policy | |
origin-isolation | |
strict-transport-security | |
upgrade-insecure-requests | |
x-content-type-options | |
x-download-options | |
x-frame-options | |
x-permitted-cross-domain-policies | |
x-powered-by | |
x-xss-protection | |
public-key-pins | |
public-key-pins-report-only | |
sec-websocket-key | |
sec-websocket-extensions | |
sec-websocket-accept | |
sec-websocket-protocol | |
sec-websocket-version | |
p3p | |
sec-fetch-mode | |
sec-fetch-dest | |
sec-fetch-site | |
sec-fetch-user | |
referrer-policy | |
content-type | |
content-length | |
accept | |
accept-encoding | |
host | |
connection | |
transfer-encoding | |
upgrade |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generated by https://quicktype.io | |
# | |
# To change quicktype's target language, run command: | |
# | |
# "Set quicktype target language" | |
from typing import Any, List, TypeVar, Callable, Type, cast | |
T = TypeVar("T") | |
def from_str(x: Any) -> str: | |
assert isinstance(x, str) | |
return x | |
def from_list(f: Callable[[Any], T], x: Any) -> List[T]: | |
assert isinstance(x, list) | |
return [f(y) for y in x] | |
def to_class(c: Type[T], x: Any) -> dict: | |
assert isinstance(x, c) | |
return cast(Any, x).to_dict() | |
def from_int(x: Any) -> int: | |
assert isinstance(x, int) and not isinstance(x, bool) | |
return x | |
def from_bool(x: Any) -> bool: | |
assert isinstance(x, bool) | |
return x | |
def from_none(x: Any) -> Any: | |
assert x is None | |
return x | |
class Response: | |
header: str | |
value: str | |
type: str | |
id: str | |
def __init__(self, header: str, value: str, type: str, id: str) -> None: | |
self.header = header | |
self.value = value | |
self.type = type | |
self.id = id | |
@staticmethod | |
def from_dict(obj: Any) -> 'Response': | |
assert isinstance(obj, dict) | |
header = from_str(obj.get("header")) | |
value = from_str(obj.get("value")) | |
type = from_str(obj.get("type")) | |
id = from_str(obj.get("id")) | |
return Response(header, value, type, id) | |
def to_dict(self) -> dict: | |
result: dict = {} | |
result["header"] = from_str(self.header) | |
result["value"] = from_str(self.value) | |
result["type"] = from_str(self.type) | |
result["id"] = from_str(self.id) | |
return result | |
class Modifications: | |
request: List[Any] | |
response: List[Response] | |
def __init__(self, request: List[Any], response: List[Response]) -> None: | |
self.request = request | |
self.response = response | |
@staticmethod | |
def from_dict(obj: Any) -> 'Modifications': | |
assert isinstance(obj, dict) | |
request = from_list(lambda x: x, obj.get("Request")) | |
response = from_list(Response.from_dict, obj.get("Response")) | |
return Modifications(request, response) | |
def to_dict(self) -> dict: | |
result: dict = {} | |
result["Request"] = from_list(lambda x: x, self.request) | |
result["Response"] = from_list(lambda x: to_class(Response, x), self.response) | |
return result | |
class Source: | |
filters: List[Any] | |
key: str | |
operator: str | |
value: str | |
def __init__(self, filters: List[Any], key: str, operator: str, value: str) -> None: | |
self.filters = filters | |
self.key = key | |
self.operator = operator | |
self.value = value | |
@staticmethod | |
def from_dict(obj: Any) -> 'Source': | |
assert isinstance(obj, dict) | |
filters = from_list(lambda x: x, obj.get("filters")) | |
key = from_str(obj.get("key")) | |
operator = from_str(obj.get("operator")) | |
value = from_str(obj.get("value")) | |
return Source(filters, key, operator, value) | |
def to_dict(self) -> dict: | |
result: dict = {} | |
result["filters"] = from_list(lambda x: x, self.filters) | |
result["key"] = from_str(self.key) | |
result["operator"] = from_str(self.operator) | |
result["value"] = from_str(self.value) | |
return result | |
class Pair: | |
modifications: Modifications | |
source: Source | |
id: str | |
def __init__(self, modifications: Modifications, source: Source, id: str) -> None: | |
self.modifications = modifications | |
self.source = source | |
self.id = id | |
@staticmethod | |
def from_dict(obj: Any) -> 'Pair': | |
assert isinstance(obj, dict) | |
modifications = Modifications.from_dict(obj.get("modifications")) | |
source = Source.from_dict(obj.get("source")) | |
id = from_str(obj.get("id")) | |
return Pair(modifications, source, id) | |
def to_dict(self) -> dict: | |
result: dict = {} | |
result["modifications"] = to_class(Modifications, self.modifications) | |
result["source"] = to_class(Source, self.source) | |
result["id"] = from_str(self.id) | |
return result | |
class RequestlyRule: | |
creation_date: int | |
description: str | |
group_id: str | |
id: str | |
is_sample: bool | |
name: str | |
object_type: str | |
pairs: List[Pair] | |
rule_type: str | |
status: str | |
version: int | |
created_by: None | |
current_owner: None | |
last_modified_by: None | |
modification_date: int | |
def __init__(self, creation_date: int, description: str, group_id: str, id: str, is_sample: bool, name: str, object_type: str, pairs: List[Pair], rule_type: str, status: str, version: int, created_by: None, current_owner: None, last_modified_by: None, modification_date: int) -> None: | |
self.creation_date = creation_date | |
self.description = description | |
self.group_id = group_id | |
self.id = id | |
self.is_sample = is_sample | |
self.name = name | |
self.object_type = object_type | |
self.pairs = pairs | |
self.rule_type = rule_type | |
self.status = status | |
self.version = version | |
self.created_by = created_by | |
self.current_owner = current_owner | |
self.last_modified_by = last_modified_by | |
self.modification_date = modification_date | |
@staticmethod | |
def from_dict(obj: Any) -> 'RequestlyRule': | |
assert isinstance(obj, dict) | |
creation_date = from_int(obj.get("creationDate")) | |
description = from_str(obj.get("description")) | |
group_id = from_str(obj.get("groupId")) | |
id = from_str(obj.get("id")) | |
is_sample = from_bool(obj.get("isSample")) | |
name = from_str(obj.get("name")) | |
object_type = from_str(obj.get("objectType")) | |
pairs = from_list(Pair.from_dict, obj.get("pairs")) | |
rule_type = from_str(obj.get("ruleType")) | |
status = from_str(obj.get("status")) | |
version = from_int(obj.get("version")) | |
created_by = from_none(obj.get("createdBy")) | |
current_owner = from_none(obj.get("currentOwner")) | |
last_modified_by = from_none(obj.get("lastModifiedBy")) | |
modification_date = from_int(obj.get("modificationDate")) | |
return RequestlyRule(creation_date, description, group_id, id, is_sample, name, object_type, pairs, rule_type, status, version, created_by, current_owner, last_modified_by, modification_date) | |
def to_dict(self) -> dict: | |
result: dict = {} | |
result["creationDate"] = from_int(self.creation_date) | |
result["description"] = from_str(self.description) | |
result["groupId"] = from_str(self.group_id) | |
result["id"] = from_str(self.id) | |
result["isSample"] = from_bool(self.is_sample) | |
result["name"] = from_str(self.name) | |
result["objectType"] = from_str(self.object_type) | |
result["pairs"] = from_list(lambda x: to_class(Pair, x), self.pairs) | |
result["ruleType"] = from_str(self.rule_type) | |
result["status"] = from_str(self.status) | |
result["version"] = from_int(self.version) | |
result["createdBy"] = from_none(self.created_by) | |
result["currentOwner"] = from_none(self.current_owner) | |
result["lastModifiedBy"] = from_none(self.last_modified_by) | |
result["modificationDate"] = from_int(self.modification_date) | |
return result | |
def requestly_rules_from_dict(s: Any) -> List[RequestlyRule]: | |
return from_list(RequestlyRule.from_dict, s) | |
def requestly_rules_to_dict(x: List[RequestlyRule]) -> Any: | |
return from_list(lambda x: to_class(RequestlyRule, x), x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment