quicktool/singbox/singbox_merge_rules.py

49 lines
1.8 KiB
Python

import json
def merge_rules(singbox_file, rules_file, output_file):
# Read the singbox configuration
with open(singbox_file, 'r', encoding='utf-8') as f:
singbox_config = json.load(f)
# Read the rules
with open(rules_file, 'r', encoding='utf-8') as f:
rules_config = json.load(f)
# Merge and replace the rules into singbox_merged.json
singbox_config['route']['rules'] = singbox_config['route']['rules']+rules_config['route']['rules']
# singbox_config['route']['rules'] = rules_config['route']['rules']
# Write the merged configuration
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(singbox_config, f, ensure_ascii=False, indent=2)
def merge_rule_set(singbox_file, rule_set_file, output_file):
# Read the singbox configuration
with open(singbox_file, 'r', encoding='utf-8') as f:
singbox_config = json.load(f)
# Read the rules
with open(rule_set_file, 'r', encoding='utf-8') as f:
rule_set_file = json.load(f)
# Merge and replace the rules into singbox_merged.json
# singbox_config['route']['rules'] = rules_config['route']['rules'] + singbox_config['route']['rules']
singbox_config['route']['rule_set'] = rule_set_file['route']['rule_set']
# Write the merged configuration
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(singbox_config, f, ensure_ascii=False, indent=2)# Read the singbox configuration
# Usage
singbox_file = 'singbox.json'
rules_file = 'singbox_rules.json'
rule_set_file = 'singbox_rule_set.json'
output_file = 'singbox_merged.json'
merge_rules(singbox_file, rules_file, output_file)
print(f"Merged configuration saved to {output_file}")
# merge_rule_set(output_file, rule_set_file, output_file)
# print(f"Merged configuration ruleset saved to {output_file}")