From 7b73d6ca89c4a45e7c161d4f5a4866b0d8385bb5 Mon Sep 17 00:00:00 2001 From: lirundong Date: Tue, 6 Feb 2024 22:46:44 +0800 Subject: [PATCH] [confgen] add a hacky approach to overwrite child config dns setups --- conf-gen/generator/__init__.py | 1 + conf-gen/generator/sing_box_generator.py | 10 +++++++++- conf-gen/source.yaml | 5 +++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/conf-gen/generator/__init__.py b/conf-gen/generator/__init__.py index d9189b6..a4a2023 100644 --- a/conf-gen/generator/__init__.py +++ b/conf-gen/generator/__init__.py @@ -44,6 +44,7 @@ def generate_conf( if gen_info.get("base"): gen = SingBoxGenerator.from_base( base_object=generators[gen_info["base"]], + dns=gen_info["dns"], inbounds=gen_info["inbounds"], route=gen_info["route"], experimental=gen_info["experimental"], diff --git a/conf-gen/generator/sing_box_generator.py b/conf-gen/generator/sing_box_generator.py index 5af3ac5..6d2cfb2 100644 --- a/conf-gen/generator/sing_box_generator.py +++ b/conf-gen/generator/sing_box_generator.py @@ -134,8 +134,16 @@ def _build_route(self): # TODO: Make this method more general and robust. @classmethod - def from_base(cls, base_object: Self, inbounds, route, experimental): + def from_base(cls, base_object: Self, dns, inbounds, route, experimental): new_object = copy(base_object) + # TODO: Define a former behavior of replacements and overwrites. + # `dns` only overwrites or appends DNS servers. + if dns.get("servers"): + old_servers = {s["tag"]: s for s in base_object.dns["servers"]} + for new_server in dns["servers"]: + tag = new_server["tag"] + old_servers.setdefault(tag, {}).clear() + old_servers[tag].update(new_server) new_object.inbounds = inbounds new_object.route.update(route) if experimental is None: diff --git a/conf-gen/source.yaml b/conf-gen/source.yaml index a1cb017..3550916 100644 --- a/conf-gen/source.yaml +++ b/conf-gen/source.yaml @@ -645,6 +645,11 @@ generates: - name: sing-box-clients type: sing-box base: *SING_BOX_CONF_BASE + dns: + servers: + - address: tls://1.12.12.12 + detour: DIRECT + tag: DIRECT inbounds: - tag: tun type: tun