diff --git a/Utils/geosite.py b/Utils/geosite.py index 2a708c9..5ca8546 100644 --- a/Utils/geosite.py +++ b/Utils/geosite.py @@ -50,4 +50,5 @@ def batch_convert(categories: list, tools: list, exclusions=None) -> None: for category in categories: src_geosite = set(open(const.PATH_SOURCE_V2FLY/category, mode="r", encoding="utf-8").read().splitlines()) ruleset_geosite = parse(src_geosite, exclusions) + ruleset_geosite.sort() rule.dump(ruleset_geosite, tool, const.PATH_DIST/tool, category) diff --git a/Utils/rule.py b/Utils/rule.py index 55807e2..13b9fb0 100644 --- a/Utils/rule.py +++ b/Utils/rule.py @@ -32,7 +32,7 @@ def includes(self, other): class RuleSet: - Type: str # DOMAIN / IP / CLASSIC + Type: str # DOMAIN / IP / CLASSIC Payload: list[Rule] def __init__(self, ruleset_type: str, payload: list): @@ -69,6 +69,9 @@ def add(self, rule): def remove(self, rule): self.Payload.remove(rule) + def sort(self): + self.Payload.sort(key=lambda item: str(item)) + def custom_convert(src: Path) -> RuleSet: src_custom = open(src, mode="r", encoding="utf-8").read().splitlines() @@ -216,13 +219,6 @@ def batch_dump(src: RuleSet, targets: list, dst_path: Path, filename: str) -> No dump(src, target, dst_path/target, filename) -def set_to_ruleset(ruleset_type: str, src: set) -> RuleSet: - list_sorted = [item for item in src] - list_sorted.sort(key=lambda item: str(item)) - ruleset = RuleSet(ruleset_type, list_sorted) - return ruleset - - def apply_patch(src: RuleSet, name: str) -> RuleSet: try: patch = open(const.PATH_SOURCE_PATCH/(name + ".txt"), mode="r").read().splitlines() diff --git a/generate.py b/generate.py index 6a04407..71ef35a 100644 --- a/generate.py +++ b/generate.py @@ -84,6 +84,9 @@ logger.info(f"Generated {len(ruleset_rejections)} reject rules.") logger.info(f"Generated {len(ruleset_exclusions)} exclude rules.") +ruleset_rejections.sort() +ruleset_exclusions.sort() + rule.batch_dump(ruleset_rejections, const.TARGETS, const.PATH_DIST, "reject") rule.batch_dump(ruleset_exclusions, const.TARGETS, const.PATH_DIST, "exclude") @@ -112,6 +115,7 @@ logger.debug(f"{item} removed for having a overseas TLD.") logger.info(f"Generated {len(ruleset_domestic)} domestic rules.") +ruleset_domestic.sort() rule.batch_dump(ruleset_domestic, const.TARGETS, const.PATH_DIST, "domestic") END_TIME = time_ns() @@ -126,6 +130,8 @@ if not line.startswith("#"): ruleset_cidr.add(rule.Rule("IPCIDR", line)) logger.info(f"Generated {len(ruleset_cidr)} domestic IPv4 rules.") + +ruleset_cidr.sort() rule.batch_dump(ruleset_cidr, const.TARGETS, const.PATH_DIST, "domestic_ip") src_cidr6 = connection.get(const.URL_CHNROUTES_V6).text.splitlines() @@ -139,7 +145,10 @@ for cidr in list_cidr6_raw: ruleset_cidr6.add(rule.Rule("IPCIDR6", cidr)) logger.info(f"Generated {len(ruleset_cidr6)} domestic IPv6 rules.") + +ruleset_cidr6.sort() rule.batch_dump(ruleset_cidr6, const.TARGETS, const.PATH_DIST, "domestic_ip6") + END_TIME = time_ns() logger.info(f"Finished. Total time: {format((END_TIME - START_TIME) / 1e9, '.3f')}s\n") @@ -175,6 +184,7 @@ if filename.is_file(): logger.debug(f'Start converting "{filename.name}".') ruleset_custom = rule.custom_convert(filename) + ruleset_custom.sort() rule.batch_dump(ruleset_custom, const.TARGETS, const.PATH_DIST, filename.stem) logger.debug(f"Converted {len(ruleset_custom)} rules.") @@ -183,6 +193,7 @@ for filename in list_file_personal: logger.debug(f'Start converting "{filename.name}".') ruleset_personal = rule.custom_convert(filename) + ruleset_personal.sort() rule.batch_dump(ruleset_personal, ["text", "text-plus", "yaml", "surge-compatible", "clash-compatible"], const.PATH_DIST/"personal", filename.stem) rule.dump(ruleset_personal, "geosite", const.PATH_DIST/"geosite", ("personal-" + filename.stem))