feat: Add sing-box config path option (#128)

This commit is contained in:
Andrey Petelin
2025-09-07 12:12:08 +05:00
parent 49836e4adc
commit 9d861cf3e0
7 changed files with 114 additions and 51 deletions

View File

@@ -134,10 +134,17 @@ function createAdditionalSection(mainSection, network) {
return true; return true;
}; };
o = mainSection.taboption('additional', form.Value, 'cache_file', _('Cache File Path'), _('Select or enter path for sing-box cache file. Change this ONLY if you know what you are doing')); o = mainSection.taboption('additional', form.ListValue, 'config_path', _('Config File Path'), _('Select path for sing-box config file. Change this ONLY if you know what you are doing'));
o.value('/tmp/cache.db', 'RAM (/tmp/cache.db)'); o.value('/etc/sing-box/config.json', 'Flash (/etc/sing-box/config.json)');
o.value('/tmp/sing-box/config.json', 'RAM (/tmp/sing-box/config.json)');
o.default = '/etc/sing-box/config.json';
o.rmempty = false;
o.ucisection = 'main';
o = mainSection.taboption('additional', form.Value, 'cache_path', _('Cache File Path'), _('Select or enter path for sing-box cache file. Change this ONLY if you know what you are doing'));
o.value('/tmp/sing-box/cache.db', 'RAM (/tmp/sing-box/cache.db)');
o.value('/usr/share/sing-box/cache.db', 'Flash (/usr/share/sing-box/cache.db)'); o.value('/usr/share/sing-box/cache.db', 'Flash (/usr/share/sing-box/cache.db)');
o.default = '/tmp/cache.db'; o.default = '/tmp/sing-box/cache.db';
o.rmempty = false; o.rmempty = false;
o.ucisection = 'main'; o.ucisection = 'main';
o.validate = function (section_id, value) { o.validate = function (section_id, value) {

View File

@@ -902,4 +902,7 @@ msgid "Local Subnet Lists"
msgstr "Локальные списки подсетей" msgstr "Локальные списки подсетей"
msgid "Local Subnet List Paths" msgid "Local Subnet List Paths"
msgstr "Пути к локальным спискам доменов" msgstr "Пути к локальным спискам доменов"
msgid "Config File Path"
msgstr "Путь к файлу конфигурации"

View File

@@ -1256,4 +1256,7 @@ msgid "Local Subnet Lists"
msgstr "" msgstr ""
msgid "Local Subnet List Paths" msgid "Local Subnet List Paths"
msgstr ""
msgid "Config File Path"
msgstr "" msgstr ""

View File

@@ -36,7 +36,8 @@ config main 'main'
option split_dns_type 'udp' option split_dns_type 'udp'
option split_dns_server '1.1.1.1' option split_dns_server '1.1.1.1'
option dns_rewrite_ttl '60' option dns_rewrite_ttl '60'
option cache_file '/tmp/cache.db' option config_path '/etc/sing-box/config.json'
option cache_path '/tmp/sing-box/cache.db'
list iface 'br-lan' list iface 'br-lan'
option mon_restart_ifaces '0' option mon_restart_ifaces '0'
#list restart_ifaces 'wan' #list restart_ifaces 'wan'

View File

@@ -7,8 +7,8 @@ PODKOP_LIB="/usr/lib/podkop"
. "$PODKOP_LIB/helpers.sh" . "$PODKOP_LIB/helpers.sh"
. "$PODKOP_LIB/sing_box_config_manager.sh" . "$PODKOP_LIB/sing_box_config_manager.sh"
. "$PODKOP_LIB/sing_box_config_facade.sh" . "$PODKOP_LIB/sing_box_config_facade.sh"
PODKOP_CONFIG="/etc/config/podkop"
config_load "/etc/config/podkop" config_load "$PODKOP_CONFIG"
GITHUB_RAW_URL="https://raw.githubusercontent.com/itdoginfo/allow-domains/main" GITHUB_RAW_URL="https://raw.githubusercontent.com/itdoginfo/allow-domains/main"
SRS_MAIN_URL="https://github.com/itdoginfo/allow-domains/releases/latest/download" SRS_MAIN_URL="https://github.com/itdoginfo/allow-domains/releases/latest/download"
@@ -101,6 +101,7 @@ start_main() {
sleep 1 sleep 1
mkdir -p /tmp/podkop mkdir -p /tmp/podkop
mkdir -p /tmp/sing-box
# base # base
route_table_rule_mark route_table_rule_mark
@@ -145,15 +146,14 @@ start_main() {
} }
start() { start() {
start_main
local proxy_string interface outbound_json dont_touch_dhcp local proxy_string interface outbound_json dont_touch_dhcp
config_get proxy_string "main" "proxy_string" config_get proxy_string "main" "proxy_string"
config_get interface "main" "interface" config_get interface "main" "interface"
config_get outbound_json "main" "outbound_json" config_get outbound_json "main" "outbound_json"
if [ -n "$proxy_string" ] || [ -n "$interface" ] || [ -n "$outbound_json" ]; then if [ -n "$proxy_string" ] || [ -n "$interface" ] || [ -n "$outbound_json" ]; then
config_get_bool dont_touch_dhcp "main" "dont_touch_dhcp" "0" start_main
config_get_bool dont_touch_dhcp "main" "dont_touch_dhcp" 0
if [ "$dont_touch_dhcp" -eq 0 ]; then if [ "$dont_touch_dhcp" -eq 0 ]; then
dnsmasq_add_resolver dnsmasq_add_resolver
fi fi
@@ -271,25 +271,28 @@ migration() {
# corntab init.d # corntab init.d
(crontab -l | grep -v "/etc/init.d/podkop list_update") | crontab - (crontab -l | grep -v "/etc/init.d/podkop list_update") | crontab -
migrate_config_key "$CONFIG" "option" "domain_list_enabled" "community_lists_enabled" migration_rename_config_key "$CONFIG" "option" "domain_list_enabled" "community_lists_enabled"
migrate_config_key "$CONFIG" "list" "domain_list" "community_lists" migration_rename_config_key "$CONFIG" "list" "domain_list" "community_lists"
migrate_config_key "$CONFIG" "option" "custom_domains_list_type" "user_domain_list_type" migration_rename_config_key "$CONFIG" "option" "custom_domains_list_type" "user_domain_list_type"
migrate_config_key "$CONFIG" "option" "custom_domains_text" "user_domains_text" migration_rename_config_key "$CONFIG" "option" "custom_domains_text" "user_domains_text"
migrate_config_key "$CONFIG" "list" "custom_domains" "user_domains" migration_rename_config_key "$CONFIG" "list" "custom_domains" "user_domains"
migrate_config_key "$CONFIG" "option" "custom_subnets_list_enabled" "user_subnet_list_type" migration_rename_config_key "$CONFIG" "option" "custom_subnets_list_enabled" "user_subnet_list_type"
migrate_config_key "$CONFIG" "option" "custom_subnets_text" "user_subnets_text" migration_rename_config_key "$CONFIG" "option" "custom_subnets_text" "user_subnets_text"
migrate_config_key "$CONFIG" "list" "custom_subnets" "user_subnets" migration_rename_config_key "$CONFIG" "list" "custom_subnets" "user_subnets"
migrate_config_key "$CONFIG" "option" "custom_local_domains_list_enabled" "local_domain_lists_enabled" migration_rename_config_key "$CONFIG" "option" "custom_local_domains_list_enabled" "local_domain_lists_enabled"
migrate_config_key "$CONFIG" "list" "custom_local_domains" "local_domain_lists" migration_rename_config_key "$CONFIG" "list" "custom_local_domains" "local_domain_lists"
migrate_config_key "$CONFIG" "option" "custom_download_domains_list_enabled" "remote_domain_lists_enabled" migration_rename_config_key "$CONFIG" "option" "custom_download_domains_list_enabled" "remote_domain_lists_enabled"
migrate_config_key "$CONFIG" "list" "custom_download_domains" "remote_domain_lists" migration_rename_config_key "$CONFIG" "list" "custom_download_domains" "remote_domain_lists"
migrate_config_key "$CONFIG" "option" "custom_download_subnets_list_enabled" "remote_subnet_lists_enabled" migration_rename_config_key "$CONFIG" "option" "custom_download_subnets_list_enabled" "remote_subnet_lists_enabled"
migrate_config_key "$CONFIG" "list" "custom_download_subnets" "remote_subnet_lists" migration_rename_config_key "$CONFIG" "list" "custom_download_subnets" "remote_subnet_lists"
migration_rename_config_key "$CONFIG" "option" "cache_file" "cache_path"
migration_add_new_option "podkop" "main" "config_path" "/etc/sing-box/config.json" && config_load "$PODKOP_CONFIG"
} }
validate_service() { validate_service() {
@@ -611,21 +614,32 @@ find_working_resolver() {
# sing-box funcs # sing-box funcs
sing_box_uci() { sing_box_uci() {
local config="/etc/config/sing-box" local sing_box_enabled sing_box_user sing_box_config_path sing_box_conffile
if grep -q "option enabled '0'" "$config" || sing_box_enabled=$(uci get "sing-box.main.enabled")
grep -q "option user 'sing-box'" "$config"; then sing_box_user=$(uci get "sing-box.main.user")
sed -i \ if [ "$sing_box_enabled" -ne 1 ]; then
-e "s/option enabled '0'/option enabled '1'/" \ uci set "sing-box.main.enabled=1"
-e "s/option user 'sing-box'/option user 'root'/" $config uci commit "sing-box"
log "Change sing-box UCI config" log "sing-box service has been enabled"
fi
if [ "$sing_box_user" != "root" ]; then
uci set "sing-box.main.user=root"
uci commit "sing-box"
log "sing-box service user has been changed to root"
fi
config_get sing_box_config_path "main" "config_path"
sing_box_conffile=$(uci get "sing-box.main.conffile")
log "sing-box config path: $sing_box_config_path" "debug"
log "sing-box service conffile: $sing_box_conffile" "debug"
if [ "$sing_box_conffile" != "$sing_box_config_path" ]; then
uci set "sing-box.main.conffile=$sing_box_config_path"
uci commit "sing-box"
log "Configuration file path has been set to $sing_box_config_path"
fi fi
[ -f /etc/rc.d/S99sing-box ] && log "Disable sing-box" && /etc/init.d/sing-box disable [ -f /etc/rc.d/S99sing-box ] && log "Disable sing-box" && /etc/init.d/sing-box disable
# if grep -q '#\s*list ifaces' "$config"; then
# sed -i '/ifaces/s/#//g' $config
# log "Uncommented list ifaces"
# fi
} }
sing_box_init_config() { sing_box_init_config() {
@@ -642,7 +656,7 @@ sing_box_init_config() {
# TODO: remove after refactoring # TODO: remove after refactoring
nolog "$config" nolog "$config"
sing_box_cm_save_config_to_file "$config" "$SB_CONFIG" sing_box_save_config
} }
sing_box_configure_log() { sing_box_configure_log() {
@@ -1140,8 +1154,17 @@ sing_box_additional_inbounds() {
) )
} }
sing_box_save_config() {
local sing_box_config_path
config_get sing_box_config_path "main" "config_path"
log "Save sing-box config to $sing_box_config_path"
sing_box_cm_save_config_to_file "$config" "$sing_box_config_path"
}
sing_box_config_check() { sing_box_config_check() {
if ! sing-box -c $SB_CONFIG check >/dev/null 2>&1; then local sing_box_config_path
config_get sing_box_config_path "main" "config_path"
if ! sing-box -c "$sing_box_config_path" check >/dev/null 2>&1; then
log "Sing-box configuration is invalid" "[fatal]" log "Sing-box configuration is invalid" "[fatal]"
exit 1 exit 1
fi fi
@@ -1413,19 +1436,22 @@ nft_add_podkop_subnet() {
# Diagnotics # Diagnotics
check_proxy() { check_proxy() {
local sing_box_config_path
config_get sing_box_config_path "main" "config_path"
if ! command -v sing-box >/dev/null 2>&1; then if ! command -v sing-box >/dev/null 2>&1; then
nolog "sing-box is not installed" nolog "sing-box is not installed"
return 1 return 1
fi fi
if [ ! -f $SB_CONFIG ]; then if [ ! -f "$sing_box_config_path" ]; then
nolog "Configuration file not found" nolog "Configuration file not found"
return 1 return 1
fi fi
nolog "Checking sing-box configuration..." nolog "Checking sing-box configuration..."
if ! sing-box -c $SB_CONFIG check >/dev/null; then if ! sing-box -c "$sing_box_config_path" check >/dev/null; then
nolog "Invalid configuration" nolog "Invalid configuration"
return 1 return 1
fi fi
@@ -1453,7 +1479,7 @@ check_proxy() {
else . end else . end
) )
else . end else . end
)' $SB_CONFIG )' "$sing_box_config_path"
nolog "Checking proxy connection..." nolog "Checking proxy connection..."
@@ -1721,9 +1747,11 @@ check_logs() {
} }
show_sing_box_config() { show_sing_box_config() {
local sing_box_config_path
config_get sing_box_config_path "main" "config_path"
nolog "Current sing-box configuration:" nolog "Current sing-box configuration:"
if [ ! -f "$SB_CONFIG" ]; then if [ ! -f "$sing_box_config_path" ]; then
nolog "Configuration file not found" nolog "Configuration file not found"
return 1 return 1
fi fi
@@ -1751,7 +1779,7 @@ show_sing_box_config() {
else . end else . end
) )
else . end else . end
)' "$SB_CONFIG" )' "$sing_box_config_path"
} }
show_config() { show_config() {
@@ -2090,10 +2118,13 @@ global_check() {
else else
print_global " 🤔 sing-box is running, checking configuration" print_global " 🤔 sing-box is running, checking configuration"
if [ -f "$SB_CONFIG" ]; then local sing_box_config_path
local fakeip_enabled=$(jq -r '.dns.fakeip.enabled' "$SB_CONFIG") config_get sing_box_config_path "main" "config_path"
local fakeip_range=$(jq -r '.dns.fakeip.inet4_range' "$SB_CONFIG") if [ -f "$sing_box_config_path" ]; then
local dns_rules=$(jq -r '.dns.rules[] | select(.server == "fakeip-server") | .domain' "$SB_CONFIG") # TODO(ampetelin): need fix jq after refactoring
local fakeip_enabled=$(jq -r '.dns.fakeip.enabled' "$sing_box_config_path")
local fakeip_range=$(jq -r '.dns.fakeip.inet4_range' "$sing_box_config_path")
local dns_rules=$(jq -r '.dns.rules[] | select(.server == "fakeip-server") | .domain' "$sing_box_config_path")
print_global " 📦 FakeIP enabled: $fakeip_enabled" print_global " 📦 FakeIP enabled: $fakeip_enabled"
print_global " 📦 FakeIP range: $fakeip_range" print_global " 📦 FakeIP range: $fakeip_range"

View File

@@ -1,6 +1,5 @@
SB_CONFIG="/etc/sing-box/config.json"
# Log # Log
SB_DEFAULT_LOG_LEVEL="info" SB_DEFAULT_LOG_LEVEL="warn"
# DNS # DNS
SB_DNS_SERVER_TAG="dns-server" SB_DNS_SERVER_TAG="dns-server"
SB_SPLIT_DNS_SERVER_TAG="split-dns-server" SB_SPLIT_DNS_SERVER_TAG="split-dns-server"

View File

@@ -170,15 +170,34 @@ gen_id() {
printf '%s%s' "$(date +%s)" "$RANDOM" | md5sum | cut -c1-16 printf '%s%s' "$(date +%s)" "$RANDOM" | md5sum | cut -c1-16
} }
# Adds a missing UCI option with the given value if it does not exist
migration_add_new_option() {
local package="$1"
local section="$2"
local option="$3"
local value="$4"
local current
current="$(uci -q get "$package.$section.$option")"
if [ -z "$current" ]; then
log "Adding missing option '$option' with value '$value'"
uci set "$package.$section.$option=$value"
uci commit "$package"
return 0
else
return 1
fi
}
# Migrates a configuration key in an OpenWrt config file from old_key_name to new_key_name # Migrates a configuration key in an OpenWrt config file from old_key_name to new_key_name
migrate_config_key() { migration_rename_config_key() {
local config="$1" local config="$1"
local key_type="$2" local key_type="$2"
local old_key_name="$3" local old_key_name="$3"
local new_key_name="$4" local new_key_name="$4"
if grep -q "$key_type $old_key_name" "$config"; then if grep -q "$key_type $old_key_name" "$config"; then
log "Deprecated $key_type found: $old_key_name migrating to $new_key_name" "migration" log "Deprecated $key_type found: $old_key_name migrating to $new_key_name"
sed -i "s/$key_type $old_key_name/$key_type $new_key_name/g" "$config" sed -i "s/$key_type $old_key_name/$key_type $new_key_name/g" "$config"
fi fi
} }