fix(uninstall): improve config loading and error handling; reset ACL rules after WARP uninstall
This commit is contained in:
@ -22,10 +22,14 @@ def systemctl_active(service: str) -> bool:
|
|||||||
|
|
||||||
|
|
||||||
def load_config(path: Path):
|
def load_config(path: Path):
|
||||||
if path.exists():
|
if not path.exists():
|
||||||
|
print(f"❌ Config file not found: {path}")
|
||||||
|
return None
|
||||||
|
try:
|
||||||
with path.open("r", encoding="utf-8") as f:
|
with path.open("r", encoding="utf-8") as f:
|
||||||
return json.load(f)
|
return json.load(f)
|
||||||
print(f"❌ Config file not found: {path}")
|
except json.JSONDecodeError:
|
||||||
|
print(f"❌ Could not decode JSON from config file: {path}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
@ -36,55 +40,62 @@ def save_config(config: dict, path: Path):
|
|||||||
|
|
||||||
|
|
||||||
def reset_acl_inline(config: dict):
|
def reset_acl_inline(config: dict):
|
||||||
default = [
|
"""
|
||||||
"reject(geosite:ir)", "reject(geoip:ir)",
|
Dynamically cleans up ACL rules after WARP uninstall.
|
||||||
"reject(geosite:category-ads-all)", "reject(geoip:private)",
|
- Removes popular site and 'all traffic' rules.
|
||||||
"reject(geosite:google@ads)"
|
- Converts any domestic 'warps' rules back to 'reject'.
|
||||||
]
|
"""
|
||||||
updated = []
|
acl_inline = config.get("acl", {}).get("inline", [])
|
||||||
for item in config.get("acl", {}).get("inline", []):
|
if not acl_inline:
|
||||||
if item in [
|
return config
|
||||||
"warps(all)", "warps(geoip:google)", "warps(geosite:google)",
|
|
||||||
|
new_rules = []
|
||||||
|
|
||||||
|
rules_to_remove = {
|
||||||
|
"warps(all)",
|
||||||
|
"warps(geoip:google)", "warps(geosite:google)",
|
||||||
"warps(geosite:netflix)", "warps(geosite:spotify)",
|
"warps(geosite:netflix)", "warps(geosite:spotify)",
|
||||||
"warps(geosite:openai)", "warps(geoip:openai)"
|
"warps(geosite:openai)", "warps(geoip:openai)"
|
||||||
]:
|
}
|
||||||
updated.append("direct")
|
|
||||||
elif item == "warps(geosite:ir)":
|
|
||||||
updated.append("reject(geosite:ir)")
|
|
||||||
elif item == "warps(geoip:ir)":
|
|
||||||
updated.append("reject(geoip:ir)")
|
|
||||||
else:
|
|
||||||
updated.append(item)
|
|
||||||
|
|
||||||
final_inline = default + [i for i in updated if i not in default and i != "direct"]
|
for rule in acl_inline:
|
||||||
config["acl"]["inline"] = final_inline
|
if rule in rules_to_remove:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if rule.startswith("warps("):
|
||||||
|
new_rules.append(rule.replace("warps(", "reject(", 1))
|
||||||
|
else:
|
||||||
|
new_rules.append(rule)
|
||||||
|
|
||||||
|
config["acl"]["inline"] = new_rules
|
||||||
|
print("🔧 ACL rules reset from WARP-specific settings.")
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
def remove_warp_outbound(config: dict):
|
def remove_warp_outbound(config: dict):
|
||||||
config["outbounds"] = [
|
config["outbounds"] = [
|
||||||
o for o in config.get("outbounds", [])
|
o for o in config.get("outbounds", [])
|
||||||
if not (
|
if not (o.get("name") == "warps")
|
||||||
o.get("name") == "warps" and
|
|
||||||
o.get("type") == "direct" and
|
|
||||||
o.get("direct", {}).get("mode") == 4 and
|
|
||||||
o.get("direct", {}).get("bindDevice") == "wgcf"
|
|
||||||
)
|
|
||||||
]
|
]
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
def remove_porn_blocking(config: dict):
|
def remove_adult_content_blocking_rule(config: dict):
|
||||||
|
"""
|
||||||
|
Removes the adult content blocking rule ('reject(geosite:nsfw)')
|
||||||
|
as it's coupled with the DNS reset.
|
||||||
|
"""
|
||||||
inline = config.get("acl", {}).get("inline", [])
|
inline = config.get("acl", {}).get("inline", [])
|
||||||
if "reject(geosite:category-porn)" in inline:
|
rule_to_remove = "reject(geosite:nsfw)"
|
||||||
config["acl"]["inline"] = [i for i in inline if i != "reject(geosite:category-porn)"]
|
if rule_to_remove in inline:
|
||||||
print("🔒 Adult content blocking removed.")
|
config["acl"]["inline"] = [i for i in inline if i != rule_to_remove]
|
||||||
|
print("🔒 Adult content blocking rule removed.")
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
def set_dns(config: dict):
|
def set_dns(config: dict):
|
||||||
config.setdefault("resolver", {}).setdefault("tls", {})["addr"] = "1.1.1.1:853"
|
config.setdefault("resolver", {}).setdefault("tls", {})["addr"] = "1.1.1.1:853"
|
||||||
print("🔧 DNS resolver changed to 1.1.1.1:853.")
|
print("🔧 DNS resolver reset to 1.1.1.1.")
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
@ -101,7 +112,7 @@ def main():
|
|||||||
if config:
|
if config:
|
||||||
config = reset_acl_inline(config)
|
config = reset_acl_inline(config)
|
||||||
config = remove_warp_outbound(config)
|
config = remove_warp_outbound(config)
|
||||||
config = remove_porn_blocking(config)
|
config = remove_adult_content_blocking_rule(config)
|
||||||
config = set_dns(config)
|
config = set_dns(config)
|
||||||
save_config(config, TEMP_CONFIG)
|
save_config(config, TEMP_CONFIG)
|
||||||
restart_hysteria()
|
restart_hysteria()
|
||||||
|
|||||||
Reference in New Issue
Block a user