Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

适配了新版 Foxyproxy 的 JSON 格式 #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 23 additions & 34 deletions base.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
{
"whitePatterns": [
{
"title": "all URLs",
"active": true,
"pattern": "*",
"type": 1,
"protocols": 1
}
],
"blackPatterns": [
{
"title": "local hostnames (usually no dots in the name). Pattern exists because 'Do not use this proxy for localhost and intranet/private IP addresses' is checked.",
"pattern": "^(?:[^:@/]+(?::[^@/]+)?@)?(?:localhost|127\\.\\d+\\.\\d+\\.\\d+)(?::\\d+)?(?:/.*)?$",
"active": true,
"type": 2,
"protocols": 1
},
{
"title": "local subnets (IANA reserved address space). Pattern exists because 'Do not use this proxy for localhost and intranet/private IP addresses' is checked.",
"pattern": "^(?:[^:@/]+(?::[^@/]+)?@)?(?:192\\.168\\.\\d+\\.\\d+|10\\.\\d+\\.\\d+\\.\\d+|172\\.(?:1[6789]|2[0-9]|3[01])\\.\\d+\\.\\d+)(?::\\d+)?(?:/.*)?$",
"active": true,
"type": 2,
"protocols": 1
},
{
"title": "localhost - matches the local host optionally prefixed by a user:password authentication string and optionally suffixed by a port number. The entire local subnet (127.0.0.0/8) matches. Pattern exists because 'Do not use this proxy for localhost and intranet/private IP addresses' is checked.",
"pattern": "^(?:[^:@/]+(?::[^@/]+)?@)?[\\w-]+(?::\\d+)?(?:/.*)?$",
"active": true,
"type": 2,
"protocols": 1
}
]
}
[
{
"title": "local hostnames (usually no dots in the name). Pattern exists because 'Do not use this proxy for localhost and intranet/private IP addresses' is checked.",
"pattern": "^(?:[^:@/]+(?::[^@/]+)?@)?(?:localhost|127\\.\\d+\\.\\d+\\.\\d+)(?::\\d+)?(?:/.*)?$",
"active": true,
"include": "exclude",
"type": "regex"
},
{
"title": "local subnets (IANA reserved address space). Pattern exists because 'Do not use this proxy for localhost and intranet/private IP addresses' is checked.",
"pattern": "^(?:[^:@/]+(?::[^@/]+)?@)?(?:192\\.168\\.\\d+\\.\\d+|10\\.\\d+\\.\\d+\\.\\d+|172\\.(?:1[6789]|2[0-9]|3[01])\\.\\d+\\.\\d+)(?::\\d+)?(?:/.*)?$",
"active": true,
"include": "exclude",
"type": "regex"
},
{
"title": "localhost - matches the local host optionally prefixed by a user:password authentication string and optionally suffixed by a port number. The entire local subnet (127.0.0.0/8) matches. Pattern exists because 'Do not use this proxy for localhost and intranet/private IP addresses' is checked.",
"pattern": "^(?:[^:@/]+(?::[^@/]+)?@)?[\\w-]+(?::\\d+)?(?:/.*)?$",
"active": true,
"include": "exclude",
"type": "regex"
}
]
41 changes: 14 additions & 27 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,57 +83,47 @@
with open("base.json") as f:
base = load(f)

base["whitePatterns"] = []


def get_dict(rule: str) -> (int, dict):
def get_dict(rule: str) -> (dict):
# Get pattern dict
result = (0, {})

try:
protocol = 1
white = 1
white = "exclude"

# White rule
if rule.startswith("@@"):
rule = rule[2:]
else:
white = 0
white = "include"

# Get protocol
# Get match type and format the rule
if rule.startswith("|http://"):
protocol = 2
rule = sub("^\\|http://", "|", rule)
elif rule.startswith("|https://"):
protocol = 4
rule = sub("^\\|https://", "||", rule)

# Get match type
if rule.startswith("||"):
rule = f"*.{rule[2:]}"
elif rule.startswith("|"):
rule = rule[1:]

# Format the rule
rule = rule.split("/")[0]

if rule.startswith("."):
rule = f"*{rule}"
if "." not in rule:
rule = f"*{rule}*"

rule = rule.split("/")[0]

the_dict = {
"title": rule,
"pattern": rule,
"type": 1,
"protocols": protocol,
"include": white,
"type": "wildcard",
"active": True
}

result = (white, the_dict)
except Exception as ee:
print(f"[ERROR] Get dict error: {e}")
logger.warning(f"Get dict error: {ee}", exc_info=True)

return result
return the_dict


def main() -> bool:
Expand Down Expand Up @@ -171,20 +161,17 @@ def main() -> bool:
if rule.startswith("/") and rule.endswith("/"):
continue

white, the_dict = get_dict(rule)
the_dict = get_dict(rule)

if not the_dict:
continue

if white:
base["blackPatterns"].append(the_dict)
else:
base["whitePatterns"].append(the_dict)
base.append(the_dict)

print("[INFO] Saving the output file...")

with open("output.json", "w") as ff:
dump(base, ff, indent=4)
dump(base, ff, indent=2)

print("[INFO] Succeeded!")
print("[INFO] Please check the file: output.json")
Expand Down
10 changes: 5 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
certifi==2022.12.7
chardet==4.0.0
idna==2.10
certifi==2024.8.30
charset-normalizer==3.4.0
idna==3.10
PySocks==1.7.1
requests==2.25.1
urllib3==1.26.5
requests==2.32.3
urllib3==2.2.3