Skip to content

Commit

Permalink
Enhancements
Browse files Browse the repository at this point in the history
Fix base sing-box configuration.
Add support for adding Clash modes to sing-box configs.
Optimize codes.
  • Loading branch information
tindy2013 committed Nov 10, 2023
1 parent 8b45e06 commit 51b65ec
Show file tree
Hide file tree
Showing 9 changed files with 306 additions and 98 deletions.
134 changes: 110 additions & 24 deletions base/base/all_base.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -269,30 +269,116 @@ enhanced-mode-by-rule = true
{% if request.target == "singbox" %}

{
"log": {
"disabled": false,
"level": "info",
"output": "box.log",
"timestamp": true
},
"dns": {},
"ntp": {
"enabled": false,
"server": "time.apple.com",
"server_port": 123,
"interval": "30m"
},
"inbounds": [
{
"type": "socks",
"tag": "socks-in",
"listen": "127.0.0.1",
"listen_port": 2080
}
],
"outbounds": [],
"route": {},
"experimental": {}
"log": {
"disabled": false,
"level": "info",
"timestamp": true
},
"dns": {
"servers": [
{
"tag": "dns_proxy",
"address": "tls://1.1.1.1",
"address_resolver": "dns_resolver"
},
{
"tag": "dns_direct",
"address": "h3://dns.alidns.com/dns-query",
"address_resolver": "dns_resolver",
"detour": "DIRECT"
},
{
"tag": "dns_fakeip",
"address": "fakeip"
},
{
"tag": "dns_resolver",
"address": "223.5.5.5",
"detour": "DIRECT"
},
{
"tag": "block",
"address": "rcode://success"
}
],
"rules": [
{
"outbound": [
"any"
],
"server": "dns_resolver"
},
{
"geosite": [
"category-ads-all"
],
"server": "dns_block",
"disable_cache": true
},
{
"geosite": [
"geolocation-!cn"
],
"query_type": [
"A",
"AAAA"
],
"server": "dns_fakeip"
},
{
"geosite": [
"geolocation-!cn"
],
"server": "dns_proxy"
}
],
"final": "dns_direct",
"independent_cache": true,
"fakeip": {
"enabled": true,
{% if default(request.singbox.ipv6, "") == "1" %}
"inet6_range": "fc00::\/18",
{% endif %}
"inet4_range": "198.18.0.0\/15"
}
},
"ntp": {
"enabled": true,
"server": "time.apple.com",
"server_port": 123,
"interval": "30m",
"detour": "DIRECT"
},
"inbounds": [
{
"type": "mixed",
"tag": "mixed-in",
{% if bool(default(global.singbox.allow_lan, "")) %}
"listen": "0.0.0.0",
{% else %}
"listen": "127.0.0.1",
{% endif %}
"listen_port": {{ default(global.singbox.mixed_port, "2080") }}
},
{
"type": "tun",
"tag": "tun-in",
"inet4_address": "172.19.0.1/30",
{% if default(request.singbox.ipv6, "") == "1" %}
"inet6_address": "fdfe:dcba:9876::1/126",
{% endif %}
"auto_route": true,
"strict_route": true,
"stack": "mixed",
"sniff": true
}
],
"outbounds": [],
"route": {
"rules": [],
"auto_detect_interface": true
},
"experimental": {}
}

{% endif %}
128 changes: 103 additions & 25 deletions base/base/singbox.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,104 @@
{
"log": {
"disabled": false,
"level": "info",
"output": "box.log",
"timestamp": true
},
"dns": {},
"ntp": {
"enabled": false,
"server": "time.apple.com",
"server_port": 123,
"interval": "30m"
},
"inbounds": [
{
"type": "socks",
"tag": "socks-in",
"listen": "127.0.0.1",
"listen_port": 2080
}
],
"outbounds": [],
"route": {},
"experimental": {}
}
"log": {
"disabled": false,
"level": "info",
"timestamp": true
},
"dns": {
"servers": [
{
"tag": "dns_proxy",
"address": "tls://1.1.1.1",
"address_resolver": "dns_resolver"
},
{
"tag": "dns_direct",
"address": "h3://dns.alidns.com/dns-query",
"address_resolver": "dns_resolver",
"detour": "DIRECT"
},
{
"tag": "dns_fakeip",
"address": "fakeip"
},
{
"tag": "dns_resolver",
"address": "223.5.5.5",
"detour": "DIRECT"
},
{
"tag": "block",
"address": "rcode://success"
}
],
"rules": [
{
"outbound": [
"any"
],
"server": "dns_resolver"
},
{
"geosite": [
"category-ads-all"
],
"server": "dns_block",
"disable_cache": true
},
{
"geosite": [
"geolocation-!cn"
],
"query_type": [
"A",
"AAAA"
],
"server": "dns_fakeip"
},
{
"geosite": [
"geolocation-!cn"
],
"server": "dns_proxy"
}
],
"final": "dns_direct",
"independent_cache": true,
"fakeip": {
"enabled": true,
"inet6_range": "fc00::\/18",
"inet4_range": "198.18.0.0\/15"
}
},
"ntp": {
"enabled": true,
"server": "time.apple.com",
"server_port": 123,
"interval": "30m",
"detour": "DIRECT"
},
"inbounds": [
{
"type": "mixed",
"tag": "mixed-in",
"listen": "0.0.0.0",
"listen_port": 2080
},
{
"type": "tun",
"tag": "tun-in",
"inet4_address": "172.19.0.1/30",
"inet6_address": "fdfe:dcba:9876::1/126",
"auto_route": true,
"strict_route": true,
"stack": "mixed",
"sniff": true
}
],
"outbounds": [],
"route": {
"rules": [],
"auto_detect_interface": true
},
"experimental": {}
}
5 changes: 5 additions & 0 deletions base/pref.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ clash_use_new_field_name=true
; key: value
clash_proxies_style=flow

;add Clash mode to sing-box rules, and add a GLOBAL group to end of outbounds
singbox_add_clash_modes=true

;Rename remarks with the following patterns. Supports regular expression.
;Format: Search_Pattern@Replace_Pattern
;rename_node=IPLC@专线
Expand Down Expand Up @@ -226,6 +229,8 @@ clash.http_port=7890
clash.socks_port=7891
clash.allow_lan=true
clash.log_level=info
singbox.allow_lan=true
singbox.mixed_port=2080

[aliases]
;Aliases for accessing interfaces. Can be used to shorten the URI.
Expand Down
11 changes: 11 additions & 0 deletions base/pref.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ clash_use_new_field_name = true
# key: value
clash_proxies_style = "flow"

# add Clash mode to sing-box rules, and add a GLOBAL group to end of outbounds
singbox_add_clash_modes = true

[[node_pref.rename_node]]
match = '\(?((x|X)?(\d+)(\.?\d+)?)((\s?倍率?)|(x|X))\)?'
replace = "$1x"
Expand Down Expand Up @@ -237,6 +240,14 @@ value = "true"
key = "clash.log_level"
value = "info"

[[template.globals]]
key = "singbox.allow_lan"
value = "true"

[[template.globals]]
key = "singbox.mixed_port"
value = "2080"

[[aliases]]
uri = "/clash"
target = "/sub?target=clash"
Expand Down
3 changes: 3 additions & 0 deletions base/pref.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ node_pref:
append_sub_userinfo: true
clash_use_new_field_name: true
clash_proxies_style: flow
singbox_add_clash_modes: true
rename_node:
# - {match: "\\(?((x|X)?(\\d+)(\\.?\\d+)?)((\\s?倍率?)|(x|X))\\)?", replace: "$1x"}
# - {script: "function rename(node){}"}
Expand Down Expand Up @@ -106,6 +107,8 @@ template:
- {key: clash.socks_port, value: 7891}
- {key: clash.allow_lan, value: true}
- {key: clash.log_level, value: info}
- {key: singbox.allow_lan, value: true}
- {key: singbox.mixed_port, value: 2080}

aliases:
- {uri: /v, target: /version}
Expand Down
10 changes: 9 additions & 1 deletion src/generator/config/ruleconvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ void rulesetToSingBox(rapidjson::Document &base_rule, std::vector<RulesetContent
std::string rule_group, retrieved_rules, strLine, final;
std::stringstream strStrm;
size_t total_rules = 0;
rapidjson::MemoryPoolAllocator<>& allocator = base_rule.GetAllocator();
auto &allocator = base_rule.GetAllocator();

rapidjson::Value rules(rapidjson::kArrayType);
if (!overwrite_original_rules)
Expand All @@ -496,6 +496,14 @@ void rulesetToSingBox(rapidjson::Document &base_rule, std::vector<RulesetContent
rules.Swap(base_rule["route"]["rules"]);
}

if (global.singBoxAddClashModes)
{
auto global_object = buildObject(allocator, "clash_mode", "Global", "outbound", "GLOBAL");
auto direct_object = buildObject(allocator, "clash_mode", "Direct", "outbound", "DIRECT");
rules.PushBack(global_object, allocator);
rules.PushBack(direct_object, allocator);
}

for(RulesetContent &x : ruleset_content_array)
{
if(global.maxAllowedRules && total_rules > global.maxAllowedRules)
Expand Down
Loading

0 comments on commit 51b65ec

Please sign in to comment.