| 34 | return any(char in regex_chars for char in line) |
| 35 | |
| 36 | def load_whitelist(file_path): |
| 37 | plain_domains = set() |
| 38 | regex_rules = [] |
| 39 | |
| 40 | if not os.path.exists(file_path): |
| 41 | log_info(f"错误: 找不到白名单文件: {file_path}") |
| 42 | sys.exit(1) |
| 43 | |
| 44 | try: |
| 45 | with open(file_path, 'r', encoding='utf-8') as f: |
| 46 | for line in f: |
| 47 | line_content = line.strip() |
| 48 | # 跳过空行和注释 |
| 49 | if not line_content or line_content.startswith('!') or line_content.startswith('#'): |
| 50 | continue |
| 51 | |
| 52 | if is_regex(line_content): |
| 53 | try: |
| 54 | # 编译正则,提高后续匹配效率 |
| 55 | regex_rules.append(re.compile(line_content, re.IGNORECASE)) |
| 56 | except re.error as e: |
| 57 | log_info(f"警告: 忽略无效的正则白名单: {line_content} ({e})") |
| 58 | else: |
| 59 | plain_domains.add(line_content) |
| 60 | except Exception as e: |
| 61 | log_info(f"读取白名单失败: {e}") |
| 62 | sys.exit(1) |
| 63 | |
| 64 | log_info(f"白名单加载完毕: 普通域名 {len(plain_domains)} 条, 正则规则 {len(regex_rules)} 条") |
| 65 | return plain_domains, regex_rules |
| 66 | |
| 67 | def clean_blacklist(blacklist_path, plain_wl, regex_wl): |
| 68 | if not os.path.exists(blacklist_path): |