{% set V1_7= g.user_agent['is_singbox'] and g.user_agent['singbox_version'][0]==1 and g.user_agent['singbox_version'][1]<8 %}
{% set V1_9= g.user_agent['is_singbox'] and g.user_agent['singbox_version'][0]==1 and g.user_agent['singbox_version'][1]<10 %}
{% set V1_10= g.user_agent['is_singbox'] and g.user_agent['singbox_version'][0]==1 and g.user_agent['singbox_version'][1]<11 %}

{
    "outbounds": [
        {
            "tag": "direct",
            "type": "direct"
        },
        {% if V1_10 %}
        {
            "tag": "block",
            "type": "block"
        },
        {
            "tag": "dns-out",
            "type": "dns"
        },
        {%endif%}
        {
            "tag": "bypass",
            "type": "direct"
        }
    ],
    "route": {
        "auto_detect_interface": true,
        "override_android_vpn": true,
        "final": "Select",
        {% if V1_7 %}
        "geoip": {
               "download_url": "https://github.com/SagerNet/sing-geoip/releases/latest/download/geoip.db",
               "download_detour": {% if hconfig(ConfigEnum.country)=='zh' %}"Select" {%else%}"bypass"{%endif%} 
            },
        "geosite": {
            "download_url": "https://github.com/SagerNet/sing-geosite/releases/latest/download/geosite.db",
            "download_detour": {% if hconfig(ConfigEnum.country)=='zh' %}"Select" {%else%}"bypass"{%endif%} 
        }, 
        {%else%}
        "rule_set": [

            {%if 0 and hconfig(ConfigEnum.country) in ["ir","zh","ru"]%}
            {
                "tag": "geosite-{{hconfig(ConfigEnum.country)|replace('zh', 'cn')}}",
                "type": "remote",
                "format": "binary",
                "url": "https://raw.githubusercontent.com/hiddify/hiddify-geo/rule-set/country/geosite-{{hconfig(ConfigEnum.country)|replace('zh', 'cn')}}.srs",
                "download_detour": "Select"
            },
            {
            "tag": "geoip-{{hconfig(ConfigEnum.country)|replace('zh', 'cn')}}",
            "type": "remote",
            "format": "binary",
            "url": "https://raw.githubusercontent.com/hiddify/hiddify-geo/rule-set/country/geoip-{{hconfig(ConfigEnum.country)|replace('zh', 'cn')}}.srs",
            "download_detour": "Select"
            }
            {%endif%}               
            {# {
                "tag": "geosite-category-ads-all",
                "type": "remote",
                "format": "binary",
                "url": "https://raw.githubusercontent.com/Chocolate4U/Iran-sing-box-rules/rule-set/geosite-category-ads-all.srs", 
                "download_detour": "Select"
            } #}
        ],
        {%endif%}
        "rules": [
          
            {% if V1_10 %}
            {
                "outbound": "dns-out",
                "port": [53]
            },
            {
                "inbound": ["dns-in"],
                "outbound": "dns-out"
            },
            {%else%}
            {
                "inbound":["tun-in","mixed-in"],
                "port": [53],
                "action": "hijack-dns"
            },
            {
                "inbound": ["dns-in"],
                "action": "hijack-dns"
            },
            {%endif%}
            {% if not V1_10 %}
                
                {
                    "inbound": ["tun-in","mixed-in"],
                    "action": "sniff",
                    "timeout": "1s"
                },
            {%endif%}
            {%if 0 and hconfig(ConfigEnum.country)in ["ir","zh","ru"]%}
            {
                "domain_suffix": [
                    "{{hconfig(ConfigEnum.country)|replace('zh', 'cn')}}"
                ],
                "outbound": "bypass"
            },
            {%endif%}
            
            {# {
                {% if V1_7 %}
                "geosite": "category-ads-all",
                {%else%}
                "rule_set": "geosite-category-ads-all",
                {%endif%}
                {% if V1_10 %}
                "outbound": "block"
                {%else%}
                "action": "reject"
                {%endif%}
            }, #}
            {%if 0 and hconfig(ConfigEnum.country) in ["ir","zh","ru"]%}
            {% if V1_7 %}
            {
                "geoip": ["{{hconfig(ConfigEnum.country)|replace('zh', 'cn')}}"],
                "outbound": "bypass"
            },
            {%else%}
            {
                "rule_set": "geoip-{{hconfig(ConfigEnum.country)|replace('zh', 'cn')}}",
                "outbound": "bypass"
            },
            {
                "rule_set": "geosite-{{hconfig(ConfigEnum.country)|replace('zh', 'cn')}}",
                "outbound": "bypass"
            },
            {%endif%}
            {%endif%}
            {
                "protocol": "quic",
                "port": [
                    443
                ],
                {% if V1_10 %}
                "outbound": "block"
                {%else%}
                "action": "reject"
                {%endif%}
            },
            {
                "ip_cidr": [
                    "224.0.0.0\/3",
                    "ff00::\/8"
                ],
                "outbound": "block",
                "source_ip_cidr": [
                    "224.0.0.0\/3",
                    "ff00::\/8"
                ]
            }
        ]
    },
    "experimental": {
        "clash_api": {
            "external_controller": "127.0.0.1:9090",
            "external_ui_download_url": "https:\/\/github.com\/MetaCubeX\/Yacd-meta\/archive\/gh-pages.zip"
            {% if V1_7 %}
            ,"cache_file": "cache.db", 
            "cache_id": "{{g.account.uuid}}",
            "store_mode": true,
            "store_selected": true,
            "store_fakeip": true
            {%endif%}
            }
        {% if not V1_7 %}
        ,"cache_file": {
            "enabled": true,
            "path": "cache.db",
            "cache_id": "{{g.account.uuid}}",
            "store_fakeip": true
        }
        {%endif%}
    },
    "dns": {
        "servers": [
            {
                "address": "tcp://1.1.1.1",
                "address_resolver": "dns-local",
                "strategy": "prefer_ipv4",
                "tag": "dns-remote",
                "detour": "Select"
            },
            {
                "address": "8.8.8.8",
                "detour": "direct",
                "tag": "dns-local"
            },
            {
                "address": "rcode:\/\/success",
                "tag": "dns-block"
            }
        ],
        "rules": [
            {
                "domain": [
                    "github.com",
                    "githubusercontent.com",
                    "raw.githubusercontent.com",
                    "1.1.1.1"
                ],
                "server": "dns-local"
            },
            {%if 0 and hconfig(ConfigEnum.country)in ["ir","zh","ru"]%}
            {
                "domain_suffix": [
                    "{{hconfig(ConfigEnum.country)|replace('zh', 'cn')}}"
                ],
                "server": "dns-local"
            },
                {%endif%}
            {
                "outbound": "direct",
                "server": "dns-local"
            }
        ],
        "final": "dns-local",
        "reverse_mapping": true,
        "strategy": "prefer_ipv4",
        "independent_cache": true
    },
    "inbounds": [
        {
            "listen": "127.0.0.1",
            "listen_port": 6450,
            "override_address": "8.8.8.8",
            "override_port": 53,
            "tag": "dns-in",
            "type": "direct"
        },
        
        {
            "type": "tun",
            "tag": "tun-in",
            "interface_name": "tun0",
            {% if V1_9 %}
            "inet4_address": "172.19.0.1\/30",
            {%else%}
            "address": ["172.19.0.1\/30"],
            {%endif%}
            "mtu": 9000,
            "auto_route": true,
            "strict_route": true,
            "stack": "system",
            {% if V1_10 %}
            "sniff": true,
            "domain_strategy": "prefer_ipv4",
            "sniff_override_destination": false,
            {% endif %}
            "endpoint_independent_nat": true
        },
        {
            "domain_strategy": "prefer_ipv4",
            "listen": "127.0.0.1",
            "listen_port": 2334,
            "sniff": true,
            "sniff_override_destination": false,
            "tag": "mixed-in",
            "type": "mixed"
        }
    ]
}
