mirror of
https://github.com/itdoginfo/podkop.git
synced 2026-06-25 20:03:10 +03:00
feat: Add sing-box config path option (#128)
This commit is contained in:
@@ -36,7 +36,8 @@ config main 'main'
|
||||
option split_dns_type 'udp'
|
||||
option split_dns_server '1.1.1.1'
|
||||
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'
|
||||
option mon_restart_ifaces '0'
|
||||
#list restart_ifaces 'wan'
|
||||
|
||||
@@ -7,8 +7,8 @@ PODKOP_LIB="/usr/lib/podkop"
|
||||
. "$PODKOP_LIB/helpers.sh"
|
||||
. "$PODKOP_LIB/sing_box_config_manager.sh"
|
||||
. "$PODKOP_LIB/sing_box_config_facade.sh"
|
||||
|
||||
config_load "/etc/config/podkop"
|
||||
PODKOP_CONFIG="/etc/config/podkop"
|
||||
config_load "$PODKOP_CONFIG"
|
||||
|
||||
GITHUB_RAW_URL="https://raw.githubusercontent.com/itdoginfo/allow-domains/main"
|
||||
SRS_MAIN_URL="https://github.com/itdoginfo/allow-domains/releases/latest/download"
|
||||
@@ -101,6 +101,7 @@ start_main() {
|
||||
sleep 1
|
||||
|
||||
mkdir -p /tmp/podkop
|
||||
mkdir -p /tmp/sing-box
|
||||
|
||||
# base
|
||||
route_table_rule_mark
|
||||
@@ -145,15 +146,14 @@ start_main() {
|
||||
}
|
||||
|
||||
start() {
|
||||
start_main
|
||||
|
||||
local proxy_string interface outbound_json dont_touch_dhcp
|
||||
config_get proxy_string "main" "proxy_string"
|
||||
config_get interface "main" "interface"
|
||||
config_get outbound_json "main" "outbound_json"
|
||||
|
||||
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
|
||||
dnsmasq_add_resolver
|
||||
fi
|
||||
@@ -271,25 +271,28 @@ migration() {
|
||||
# corntab init.d
|
||||
(crontab -l | grep -v "/etc/init.d/podkop list_update") | crontab -
|
||||
|
||||
migrate_config_key "$CONFIG" "option" "domain_list_enabled" "community_lists_enabled"
|
||||
migrate_config_key "$CONFIG" "list" "domain_list" "community_lists"
|
||||
migration_rename_config_key "$CONFIG" "option" "domain_list_enabled" "community_lists_enabled"
|
||||
migration_rename_config_key "$CONFIG" "list" "domain_list" "community_lists"
|
||||
|
||||
migrate_config_key "$CONFIG" "option" "custom_domains_list_type" "user_domain_list_type"
|
||||
migrate_config_key "$CONFIG" "option" "custom_domains_text" "user_domains_text"
|
||||
migrate_config_key "$CONFIG" "list" "custom_domains" "user_domains"
|
||||
migration_rename_config_key "$CONFIG" "option" "custom_domains_list_type" "user_domain_list_type"
|
||||
migration_rename_config_key "$CONFIG" "option" "custom_domains_text" "user_domains_text"
|
||||
migration_rename_config_key "$CONFIG" "list" "custom_domains" "user_domains"
|
||||
|
||||
migrate_config_key "$CONFIG" "option" "custom_subnets_list_enabled" "user_subnet_list_type"
|
||||
migrate_config_key "$CONFIG" "option" "custom_subnets_text" "user_subnets_text"
|
||||
migrate_config_key "$CONFIG" "list" "custom_subnets" "user_subnets"
|
||||
migration_rename_config_key "$CONFIG" "option" "custom_subnets_list_enabled" "user_subnet_list_type"
|
||||
migration_rename_config_key "$CONFIG" "option" "custom_subnets_text" "user_subnets_text"
|
||||
migration_rename_config_key "$CONFIG" "list" "custom_subnets" "user_subnets"
|
||||
|
||||
migrate_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" "option" "custom_local_domains_list_enabled" "local_domain_lists_enabled"
|
||||
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"
|
||||
migrate_config_key "$CONFIG" "list" "custom_download_domains" "remote_domain_lists"
|
||||
migration_rename_config_key "$CONFIG" "option" "custom_download_domains_list_enabled" "remote_domain_lists_enabled"
|
||||
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"
|
||||
migrate_config_key "$CONFIG" "list" "custom_download_subnets" "remote_subnet_lists"
|
||||
migration_rename_config_key "$CONFIG" "option" "custom_download_subnets_list_enabled" "remote_subnet_lists_enabled"
|
||||
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() {
|
||||
@@ -611,21 +614,32 @@ find_working_resolver() {
|
||||
# sing-box funcs
|
||||
|
||||
sing_box_uci() {
|
||||
local config="/etc/config/sing-box"
|
||||
if grep -q "option enabled '0'" "$config" ||
|
||||
grep -q "option user 'sing-box'" "$config"; then
|
||||
sed -i \
|
||||
-e "s/option enabled '0'/option enabled '1'/" \
|
||||
-e "s/option user 'sing-box'/option user 'root'/" $config
|
||||
log "Change sing-box UCI config"
|
||||
local sing_box_enabled sing_box_user sing_box_config_path sing_box_conffile
|
||||
sing_box_enabled=$(uci get "sing-box.main.enabled")
|
||||
sing_box_user=$(uci get "sing-box.main.user")
|
||||
if [ "$sing_box_enabled" -ne 1 ]; then
|
||||
uci set "sing-box.main.enabled=1"
|
||||
uci commit "sing-box"
|
||||
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
|
||||
|
||||
[ -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() {
|
||||
@@ -642,7 +656,7 @@ sing_box_init_config() {
|
||||
# TODO: remove after refactoring
|
||||
nolog "$config"
|
||||
|
||||
sing_box_cm_save_config_to_file "$config" "$SB_CONFIG"
|
||||
sing_box_save_config
|
||||
}
|
||||
|
||||
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() {
|
||||
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]"
|
||||
exit 1
|
||||
fi
|
||||
@@ -1413,19 +1436,22 @@ nft_add_podkop_subnet() {
|
||||
|
||||
# Diagnotics
|
||||
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
|
||||
nolog "sing-box is not installed"
|
||||
return 1
|
||||
fi
|
||||
|
||||
if [ ! -f $SB_CONFIG ]; then
|
||||
if [ ! -f "$sing_box_config_path" ]; then
|
||||
nolog "Configuration file not found"
|
||||
return 1
|
||||
fi
|
||||
|
||||
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"
|
||||
return 1
|
||||
fi
|
||||
@@ -1453,7 +1479,7 @@ check_proxy() {
|
||||
else . end
|
||||
)
|
||||
else . end
|
||||
)' $SB_CONFIG
|
||||
)' "$sing_box_config_path"
|
||||
|
||||
nolog "Checking proxy connection..."
|
||||
|
||||
@@ -1721,9 +1747,11 @@ check_logs() {
|
||||
}
|
||||
|
||||
show_sing_box_config() {
|
||||
local sing_box_config_path
|
||||
config_get sing_box_config_path "main" "config_path"
|
||||
nolog "Current sing-box configuration:"
|
||||
|
||||
if [ ! -f "$SB_CONFIG" ]; then
|
||||
if [ ! -f "$sing_box_config_path" ]; then
|
||||
nolog "Configuration file not found"
|
||||
return 1
|
||||
fi
|
||||
@@ -1751,7 +1779,7 @@ show_sing_box_config() {
|
||||
else . end
|
||||
)
|
||||
else . end
|
||||
)' "$SB_CONFIG"
|
||||
)' "$sing_box_config_path"
|
||||
}
|
||||
|
||||
show_config() {
|
||||
@@ -2090,10 +2118,13 @@ global_check() {
|
||||
else
|
||||
print_global " 🤔 sing-box is running, checking configuration"
|
||||
|
||||
if [ -f "$SB_CONFIG" ]; then
|
||||
local fakeip_enabled=$(jq -r '.dns.fakeip.enabled' "$SB_CONFIG")
|
||||
local fakeip_range=$(jq -r '.dns.fakeip.inet4_range' "$SB_CONFIG")
|
||||
local dns_rules=$(jq -r '.dns.rules[] | select(.server == "fakeip-server") | .domain' "$SB_CONFIG")
|
||||
local sing_box_config_path
|
||||
config_get sing_box_config_path "main" "config_path"
|
||||
if [ -f "$sing_box_config_path" ]; then
|
||||
# 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 range: $fakeip_range"
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
SB_CONFIG="/etc/sing-box/config.json"
|
||||
# Log
|
||||
SB_DEFAULT_LOG_LEVEL="info"
|
||||
SB_DEFAULT_LOG_LEVEL="warn"
|
||||
# DNS
|
||||
SB_DNS_SERVER_TAG="dns-server"
|
||||
SB_SPLIT_DNS_SERVER_TAG="split-dns-server"
|
||||
|
||||
@@ -170,15 +170,34 @@ gen_id() {
|
||||
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
|
||||
migrate_config_key() {
|
||||
migration_rename_config_key() {
|
||||
local config="$1"
|
||||
local key_type="$2"
|
||||
local old_key_name="$3"
|
||||
local new_key_name="$4"
|
||||
|
||||
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"
|
||||
fi
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user