diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml old mode 100644 new mode 100755 diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 index d017266..0f760fc --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ ## build -此分支将[rule](./rule)中的 `domain.txt` `domain_regex.txt` 等基础规则转换 +此分支将构建[rule](./rule)中的基础规则 -插件文档见[script](./script) +转换插件文档见[script](./script) 添加规则文档见[rule](./rule) diff --git a/build.json b/build.json deleted file mode 100644 index 918a58c..0000000 --- a/build.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "version": "1.4.8", - "out_path": "/out", - "domain_name": "domain.txt", - "regex_name": "domain_regex.txt", - "title": "Title: AWAvenue 秋风广告规则(AWAvenue-Ads-Rule)\n规则版本: version\n更新时间: format_time\n\n项目地址:https://github.com/TG-Twilight/AWAvenue-Ads-Rule\n\n如果需要在其它规则中混合此规则,请在您的规则显眼处注明本规则的出处,谢谢!\n加入Telegram群组 秋風がく山道 (@AWAvenueAdsChat) 与编写者交流,期待着您的到来!\n群组链接:https://t.me/AWAvenueAdsChat\n订阅Telegram频道 AWAvenue Ads Rule (@AWAvenueAdsRule) 获取最新公告,期待着您的订阅!\n频道链接:https://t.me/AWAvenueAdsRule\n\nThis project is licensed under the Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) license.\n\n倾城极速 - 畅游世界,高速互联!官网:https://wcnmdmht.lol/sb 官网2:https://panel.qc02.xyz\n", - "suffix": [ - {"name": "AdClose", "suffix": ".txt", "comment": "!"}, - {"name": "Adblock", "suffix": ".txt", "comment": "!"}, - {"name": "Clash", "suffix": ".yaml", "comment": "#"}, - {"name": "hosts", "suffix": ".txt" , "comment": "#"}, - {"name": "RouterOS", "suffix": ".txt" , "comment": "#"}, - {"name": "Surge", "suffix": ".txt" , "comment": "#"}, - {"name": "Singbox", "suffix": ".json" , "comment": "//"}, - {"name": "Singbox-regex", "suffix": ".json" , "comment": "//"}, - {"name": "QuantumultX", "suffix": ".list" , "comment": "##"}, - {"name": "RouterOS-Adlist", "suffix": ".txt" , "comment": "#"}, - {"name": "Mosdns_v5", "suffix": ".txt" , "comment": "#"} - ] - } diff --git a/main.py b/main.py index 832fb3c..e264c69 100755 --- a/main.py +++ b/main.py @@ -3,40 +3,37 @@ import sys import json import importlib from datetime import datetime - - -try: - with open("build.json", 'r', encoding="utf-8") as file: - config = json.loads(file.read()) -except Exception as e: - print(f"读取json失败: {e}") - sys.exit(1) - global output, domain_file, domain_file, suffix_file, regex_file -current_time = datetime.now() -format_time = current_time.strftime("%Y-%m-%d") + SCRIPT_PATH = os.path.join(os.getcwd(), "script") RULE_PATH = os.path.join(os.getcwd(), "rule") -OUT_PATH = os.getcwd() + config["out_path"] -domain_file=RULE_PATH + "/" + config["domain_name"] -regex_file=RULE_PATH + "/" + config["regex_name"] +OUT_PATH = os.getcwd() + "/out" +domain_file=RULE_PATH + "/domain.txt" +regex_file=RULE_PATH + "/domain_regex.txt" +ip_file=RULE_PATH + "/ip.txt" if not os.path.exists(OUT_PATH): print(f"{OUT_PATH} 目录不存在!") sys.exit(1) -def WriteFile(name, text): +class RuleList: + def __init__(self, domain_file, regex_file, ip_file): + with open(domain_file, 'r') as file: + self.domain_list = file.readlines() + with open(regex_file, 'r') as file: + self.regex_list = file.readlines() + with open(ip_file, 'r') as file: + self.ip_list = file.readlines() + +rule = RuleList(domain_file, regex_file, ip_file) + +def WriteFile(name, text, suffix): try: - for File in config["suffix"]: - if name == File["name"]: - with open(OUT_PATH + "/AWAvenue-Ads-Rule-" + name + File["suffix"], 'w', encoding="utf-8") as file: - title = [File["comment"] + " " + line for line in config["title"].split('\n')] - title = '\n'.join(title) - file.write(title.replace("version", config["version"]).replace("format_time", format_time) + "\n\n") - for line in text: - file.write(line + "\n") + with open(OUT_PATH + "/AWAvenue-Ads-Rule-" + name + suffix, 'w', encoding="utf-8") as file: + for line in text: + file.write(line + "\n") except Exception as e: - print(f"读取json失败: {e}") + print(f"写入插件:{module_name}执行失败: {e}") def RunScript(): for filename in os.listdir(SCRIPT_PATH): @@ -45,10 +42,9 @@ def RunScript(): full_module_name = f"script.{module_name}" try: module = importlib.import_module(full_module_name) - print(f"正在转换:{module_name}") - module_list = module.build(domain_file, regex_file) - WriteFile(module_name, module_list) - except ImportError as e: + module_list, module_suffix = module.build(rule) + WriteFile(module_name, module_list, module_suffix) + except Exception as e: print(f"转换插件:{module_name}执行失败: {e}") diff --git a/rule/README.md b/rule/README.md old mode 100644 new mode 100755 index b035517..21d828d --- a/rule/README.md +++ b/rule/README.md @@ -6,4 +6,6 @@ fork本分支,修改本目录的文件 `domain.txt` 文件为域名列表 一行一个 不会匹配子域名 -`domain_regex.txt` 文件为`domain.txt`的正则表达式版本 +`domain_regex.txt` 文件为`domain.txt`的正则表达式版本(并不是所有插件都会调用此规则) + +`ip.txt` 文件为ip列表(并不是所有插件都会调用此规则) diff --git a/rule/domain.txt b/rule/domain.txt index 4c12d67..ef79176 100755 --- a/rule/domain.txt +++ b/rule/domain.txt @@ -1,7 +1,4 @@ 1010pic.com -123.56.23.22 -182.254.116.116 -203.205.137.89 8le8le.com a0.app.xiaomi.com aaid.umeng.com diff --git a/rule/ip.txt b/rule/ip.txt new file mode 100644 index 0000000..9c78fa9 --- /dev/null +++ b/rule/ip.txt @@ -0,0 +1,3 @@ +123.56.23.22 +182.254.116.116 +203.205.137.89 \ No newline at end of file diff --git a/script/AdClose.py b/script/AdClose.py old mode 100644 new mode 100755 index c63fb81..5f67801 --- a/script/AdClose.py +++ b/script/AdClose.py @@ -1,23 +1,18 @@ -def format_domain(domain_file): +def format_domain(List): domain = [] - with open(domain_file, 'r') as file: - lines = file.readlines() - for line in lines: + for line in List: domain_lines = f"{line.strip()}" domain.append(domain_lines) return domain -def format_regex(regex_file): +def format_regex(List): regex = [] - - with open(regex_file, 'r') as file: - lines = file.readlines() - for line in lines: + for line in List: regex_lines = f"{line.strip()}" regex.append(regex_lines) return regex -def build(domain_file, regex_file): - Adclose_list = format_domain(domain_file) + format_regex(regex_file) - return Adclose_list \ No newline at end of file +def build(rule): + Adclose_list = format_domain(rule.domain_list) + format_regex(rule.regex_list) + return Adclose_list, ".txt" \ No newline at end of file diff --git a/script/Adblock.py b/script/Adblock.py old mode 100644 new mode 100755 index 91ba067..da92450 --- a/script/Adblock.py +++ b/script/Adblock.py @@ -1,23 +1,18 @@ -def format_domain(domain_file): +def format_domain(List): domain = [] - with open(domain_file, 'r') as file: - lines = file.readlines() - for line in lines: + for line in List: domain_lines = f"||{line.strip()}^" domain.append(domain_lines) return domain -def format_regex(regex_file): +def format_regex(List): regex = [] - - with open(regex_file, 'r') as file: - lines = file.readlines() - for line in lines: + for line in List: regex_lines = f"/{line.strip()}/".replace("\.","\\\.").replace("\*","\.\*") regex.append(regex_lines) return regex -def build(domain_file, regex_file): - Adblock_list = format_domain(domain_file) + format_regex(regex_file) - return Adblock_list \ No newline at end of file +def build(rule): + Adblock_list = format_domain(rule.domain_list) + format_regex(rule.regex_list) + return Adblock_list, ".txt" \ No newline at end of file diff --git a/script/Adguard.py b/script/Adguard.py old mode 100644 new mode 100755 index 21623be..5208758 --- a/script/Adguard.py +++ b/script/Adguard.py @@ -1,23 +1,18 @@ -def format_domain(domain_file): +def format_domain(List): domain = [] - with open(domain_file, 'r') as file: - lines = file.readlines() - for line in lines: + for line in List: domain_lines = f"||{line.strip()}^" domain.append(domain_lines) return domain -def format_regex(regex_file): +def format_regex(List): regex = [] - - with open(regex_file, 'r') as file: - lines = file.readlines() - for line in lines: + for line in List: regex_lines = f"/{line.strip()}/".replace("\.","\\\.").replace("\*","\.\*") regex.append(regex_lines) return regex -def build(domain_file, regex_file): - Adguard_list = format_domain(domain_file) + format_regex(regex_file) - return Adguard_list \ No newline at end of file +def build(rule): + Adguard_list = format_domain(rule.domain_list) + format_regex(rule.regex_list) + return Adguard_list, ".txt" \ No newline at end of file diff --git a/script/Clash.py b/script/Clash.py index 7a3fe72..052962a 100755 --- a/script/Clash.py +++ b/script/Clash.py @@ -1,23 +1,25 @@ -def format_domain(domain_file): +def format_domain(List): domain = [] - with open(domain_file, 'r') as file: - lines = file.readlines() - for line in lines: + for line in List: domain_lines = f" - '{line.strip()}'" domain.append(domain_lines) return domain -def format_regex(regex_file): +def format_regex(List): regex = [] - - with open(regex_file, 'r') as file: - lines = file.readlines() - for line in lines: + for line in List: regex_lines = f" - '{line.strip()}'".replace("$","").replace("^","") regex.append(regex_lines) return regex -def build(domain_file, regex_file): - clash_list = ["payload:"] + format_domain(domain_file) + format_regex(regex_file) - return clash_list \ No newline at end of file +def format_ip(List): + ip = [] + for line in List: + ip_lines = f" - '{line.strip()}'".replace("$","").replace("^","") + ip.append(ip_lines) + return ip + +def build(rule): + clash_list = ["payload:"] + format_ip(rule.ip_list) + format_domain(rule.domain_list) + format_regex(rule.regex_list) + return clash_list, ".yaml" \ No newline at end of file diff --git a/script/Mosdns_v5.py b/script/Mosdns_v5.py index 36e1049..76a2a09 100644 --- a/script/Mosdns_v5.py +++ b/script/Mosdns_v5.py @@ -1,15 +1,12 @@ import json -def format_domain(domain_file): +def format_domain(List): domain = [] - with open(domain_file, 'r') as file: - lines = file.readlines() - - for line in lines: + for line in List: domain_lines = f"domain:{line.strip()}" domain.append(domain_lines) return domain -def build(domain_file, regex_file): - return format_domain(domain_file) +def build(rule): + return format_domain(rule.domain_list), ".txt" diff --git a/script/QuantumultX.py b/script/QuantumultX.py old mode 100644 new mode 100755 index 5024877..e7a46a0 --- a/script/QuantumultX.py +++ b/script/QuantumultX.py @@ -1,22 +1,24 @@ -def format_domain(domain_file): +def format_domain(List): domain = [] - with open(domain_file, 'r') as file: - lines = file.readlines() - for line in lines: + for line in List: domain_lines = f"DOMAIN,{line.strip()},reject" domain.append(domain_lines) return domain -def format_regex(regex_file): +def format_regex(List): regex = [] - - with open(regex_file, 'r') as file: - lines = file.readlines() - for line in lines: + for line in List: regex_lines = f"DOMAIN-KEYWORD,{line.strip()},reject".replace("$","").replace("^","").replace("*","") regex.append(regex_lines) return regex -def build(domain_file, regex_file): - return format_domain(domain_file) + format_regex(regex_file) \ No newline at end of file +def format_ip(List): + ip = [] + for line in List: + ip_lines = f"IP-CIDR,{line.strip()},reject" + ip.append(ip_lines) + return ip + +def build(rule): + return format_ip(rule.ip_list) + format_domain(rule.domain_list) + format_regex(rule.regex_list), ".list" \ No newline at end of file diff --git a/script/README.md b/script/README.md old mode 100644 new mode 100755 index aeb487a..b8aec67 --- a/script/README.md +++ b/script/README.md @@ -1,23 +1,28 @@ ## 插件开发文档 -插件入口函数为`build()` +插件入口函数为`build(rule)` -参考[Clash.py](./Clash.py) - -插件返回的list格式参考下面的模板 +插件返回的list格式参考: `Adguard.py`: ```python -["||xxx.com^","||xxx.cc^"] +["||xxx.com^","||xxx.cc^"], ".txt" ``` -> 后缀名以及注释符请在build.json中添加 如果不存在则不执行此插件 +> **有两个返回值** -提供的变量: +API: +- rule.domain_list(获取域名列表) +- rule.regex_list(获取正则表达式的域名列表) +- rule.ip_list(获取ip列表) +> 所有变量均为列表 无需读取文件 -## domain_file -> 获取 rule目录的 domain.txt 路径 - -## regex_file ->获取 rule目录的 domain_regex.txt 路径 \ No newline at end of file +模板: +``` +# 主函数并且引入rule(对象) +def build(rule): + list = rule.domain_list + rule.regex_list + rule.ip_list + return list, ".yaml" # 返回处理后的列表和文件后缀名 +``` +> 输出文件名=插件名 \ No newline at end of file diff --git a/script/RouterOS-Adlist.py b/script/RouterOS-Adlist.py old mode 100644 new mode 100755 index 18dcff4..a3d8326 --- a/script/RouterOS-Adlist.py +++ b/script/RouterOS-Adlist.py @@ -1,15 +1,12 @@ import json -def format_domain(domain_file): +def format_domain(List): domain = ["0.0.0.0 localhost", "::1 localhost", "", ""] - with open(domain_file, 'r') as file: - lines = file.readlines() - - for line in lines: + for line in List: domain_lines = f"0.0.0.0 {line.strip()}" domain.append(domain_lines) return domain -def build(domain_file, regex_file): - return format_domain(domain_file) +def build(rule): + return format_domain(rule.domain_list), ".txt" \ No newline at end of file diff --git a/script/RouterOS.py b/script/RouterOS.py old mode 100644 new mode 100755 index af33043..723587c --- a/script/RouterOS.py +++ b/script/RouterOS.py @@ -1,25 +1,11 @@ import json -def format_domain(domain_file): - domain = [] - with open(domain_file, 'r') as file: - lines = file.readlines() +def format_domain(List): + domain = [] + for line in List: + domain_line = f"ip dns static add address=240.0.0.1 name={line.strip()}" + domain.append(domain_line) + return domain - for line in lines: - domain_line = f"ip dns static add address=240.0.0.1 name={line.strip()}" - domain.append(domain_line) - - return domain - -def build(domain_file, regex_file): - return format_domain(domain_file) - -if __name__ == '__main__': - domain_file = 'rule/domain.txt' - regex_file = 'rule/domain_regex.txt' - mikrotik_ros= build(domain_file, regex_file) - - with open('mikrotik_ros.rsc', 'w') as file: - file.write('\n'.join(mikrotik_ros)) - - print('生成 mikrotik_ros.rsc 文件成功!') +def build(rule): + return format_domain(rule.regex_list), ".txt" diff --git a/script/Singbox-regex.py b/script/Singbox-regex.py old mode 100644 new mode 100755 index f1a7a86..5b344c8 --- a/script/Singbox-regex.py +++ b/script/Singbox-regex.py @@ -1,48 +1,33 @@ import json -def format_domain(domain_file): +def format_domain(List): domain = [] - with open(domain_file, 'r') as file: - lines = file.readlines() - for line in lines: + for line in List: domain_lines = f"{line.strip()}" domain.append(domain_lines) return domain -def format_suffix(domain_file): - suffix = [] - - with open(domain_file, 'r') as file: - lines = file.readlines() - for line in lines: - suffix_lines = f".{line.strip()}" - suffix.append(suffix_lines) - return suffix -def format_regex(regex_file): +def format_regex(List): regex = [] - - with open(regex_file, 'r') as file: - lines = file.readlines() - for line in lines: + for line in List: regex_lines = f"{line.strip()}" regex.append(regex_lines) return regex -def build(domain_file, regex_file): - domain_list = format_domain(domain_file) - domain_regex_list = format_regex(regex_file) - domain_suffix_list = format_suffix(domain_file) - rule = { +def build(rule): + domain_list = format_domain(rule.domain_list) + regex_list = format_regex(rule.regex_list) + List = { "version": 1, "rules": [ { "domain": domain_list, - "domain_regex": domain_regex_list, - "domain_suffix": domain_suffix_list + "domain_regex": regex_list, + "domain_suffix": [] } ] } - json_data = json.dumps(rule, indent=2) - return [json_data] \ No newline at end of file + json_data = json.dumps(List, indent=2) + return [json_data], ".json" \ No newline at end of file diff --git a/script/Singbox.py b/script/Singbox.py old mode 100644 new mode 100755 index de7350f..e4e89d5 --- a/script/Singbox.py +++ b/script/Singbox.py @@ -1,37 +1,24 @@ import json -def format_domain(domain_file): +def format_domain(List): domain = [] - with open(domain_file, 'r') as file: - lines = file.readlines() - for line in lines: + for line in List: domain_lines = f"{line.strip()}" domain.append(domain_lines) return domain -def format_suffix(domain_file): - suffix = [] - - with open(domain_file, 'r') as file: - lines = file.readlines() - for line in lines: - suffix_lines = f".{line.strip()}" - suffix.append(suffix_lines) - return suffix - -def build(domain_file, regex_file): - domain_list = format_domain(domain_file) - domain_suffix_list = format_suffix(domain_file) +def build(rule): + domain_list = format_domain(rule.domain_list) rule = { "version": 1, "rules": [ { "domain": domain_list, "domain_regex": [], - "domain_suffix": domain_suffix_list + "domain_suffix": [] } ] } json_data = json.dumps(rule, indent=2) - return [json_data] \ No newline at end of file + return [json_data], ".json" \ No newline at end of file diff --git a/script/Surge-DOMAIN-SET.py b/script/Surge-DOMAIN-SET.py new file mode 100755 index 0000000..b7121d9 --- /dev/null +++ b/script/Surge-DOMAIN-SET.py @@ -0,0 +1,10 @@ +def format_domain(List): + domain = [] + for line in List: + domain_lines = f".{line.strip()}" + domain.append(domain_lines) + return domain + +def build(rule): + surge_list = format_domain(rule.domain_list) + return surge_list, ".list" \ No newline at end of file diff --git a/script/Surge-RULE-SET.py b/script/Surge-RULE-SET.py new file mode 100755 index 0000000..4704dde --- /dev/null +++ b/script/Surge-RULE-SET.py @@ -0,0 +1,23 @@ +def format_domain(List): + domain = [] + for line in List: + domain_lines = f"DOMAIN,{line.strip()},reject" + domain.append(domain_lines) + return domain + +def format_regex(List): + regex = [] + for line in List: + regex_lines = f"DOMAIN-KEYWORD,{line.strip()},reject".replace("$","").replace("^","").replace("*","") + regex.append(regex_lines) + return regex + +def format_ip(List): + ip = [] + for line in List: + ip_lines = f"IP-CIDR,{line.strip()},reject" + ip.append(ip_lines) + return ip + +def build(rule): + return format_ip(rule.ip_list) + format_domain(rule.domain_list) + format_regex(rule.regex_list), ".list" \ No newline at end of file diff --git a/script/Surge.py b/script/Surge.py deleted file mode 100644 index dfbc38c..0000000 --- a/script/Surge.py +++ /dev/null @@ -1,12 +0,0 @@ -def format_domain(domain_file): - domain = [] - with open(domain_file, 'r') as file: - lines = file.readlines() - for line in lines: - domain_lines = f".{line.strip()}" - domain.append(domain_lines) - return domain - -def build(domain_file, regex_file): - surge_list = format_domain(domain_file) - return surge_list \ No newline at end of file diff --git a/script/hosts.py b/script/hosts.py old mode 100644 new mode 100755 index 646a380..741913e --- a/script/hosts.py +++ b/script/hosts.py @@ -1,15 +1,11 @@ -import json -def format_domain(domain_file): +def format_domain(List): domain = ["127.0.0.1 localhost", "::1 localhost", "", ""] - with open(domain_file, 'r') as file: - lines = file.readlines() - - for line in lines: + for line in List: domain_lines = f"127.0.0.1 {line.strip()}" domain.append(domain_lines) return domain -def build(domain_file, regex_file): - return format_domain(domain_file) \ No newline at end of file +def build(rule): + return format_domain(rule.domain_list), ".txt" \ No newline at end of file