refactor: configuring dnsmasq after starting sing-box

This commit is contained in:
Andrey Petelin
2025-11-25 13:53:24 +05:00
parent 031c419ffb
commit 626ac981eb

View File

@@ -104,7 +104,7 @@ has_outbound_section() {
return $section_exists return $section_exists
} }
start_main() { start() {
log "Starting podkop" log "Starting podkop"
check_requirements check_requirements
@@ -125,37 +125,31 @@ start_main() {
# base # base
route_table_rule_mark route_table_rule_mark
create_nft_table create_nft_rules
sing_box_uci sing_box_uci
# sing-box # sing-box
sing_box_init_config sing_box_init_config
config_foreach add_cron_job "section" config_foreach add_cron_job "section"
/etc/init.d/sing-box start /etc/init.d/sing-box start
if [ $? -ne 0 ]; then
local exclude_ntp echo "Failed to start sing-box service"
config_get_bool exclude_ntp "settings" "exclude_ntp" "0" exit 1
if [ "$exclude_ntp" -eq 1 ]; then
log "NTP traffic exclude for proxy"
nft insert rule inet "$NFT_TABLE_NAME" mangle udp dport 123 return
fi fi
config_get_bool dont_touch_dhcp "settings" "dont_touch_dhcp" 0
if [ "$dont_touch_dhcp" -eq 0 ]; then
dnsmasq_configure
fi
uci_set "podkop" "settings" "shutdown_correctly" 0
uci commit "podkop" && config_load "$PODKOP_CONFIG"
log "Nice" log "Nice"
list_update & list_update &
echo $! > /var/run/podkop_list_update.pid echo $! > /var/run/podkop_list_update.pid
} }
start() { stop() {
start_main
config_get_bool dont_touch_dhcp "settings" "dont_touch_dhcp" 0
if [ "$dont_touch_dhcp" -eq 0 ]; then
dnsmasq_add_resolver
fi
uci_set "podkop" "settings" "shutdown_correctly" 0
uci commit "podkop" && config_load "$PODKOP_CONFIG"
}
stop_main() {
log "Stopping the podkop" log "Stopping the podkop"
if [ -f /var/run/podkop_list_update.pid ]; then if [ -f /var/run/podkop_list_update.pid ]; then
@@ -186,25 +180,23 @@ stop_main() {
ip route flush table podkop ip route flush table podkop
fi fi
log "Stop sing-box"
/etc/init.d/sing-box stop
}
stop() {
local dont_touch_dhcp local dont_touch_dhcp
config_get_bool dont_touch_dhcp "settings" "dont_touch_dhcp" 0 config_get_bool dont_touch_dhcp "settings" "dont_touch_dhcp" 0
if [ "$dont_touch_dhcp" -eq 0 ]; then if [ "$dont_touch_dhcp" -eq 0 ]; then
dnsmasq_restore dnsmasq_restore
fi fi
stop_main
log "Stop sing-box"
/etc/init.d/sing-box stop
uci_set "podkop" "settings" "shutdown_correctly" 1 uci_set "podkop" "settings" "shutdown_correctly" 1
uci commit "podkop" && config_load "$PODKOP_CONFIG" uci commit "podkop"
} }
reload() { reload() {
log "Podkop reload" log "Podkop reload"
stop_main stop
start_main start
} }
restart() { restart() {
@@ -281,7 +273,7 @@ nft_init_interfaces_set() {
done done
} }
create_nft_table() { create_nft_rules() {
log "Create nft table" log "Create nft table"
nft_create_table "$NFT_TABLE_NAME" nft_create_table "$NFT_TABLE_NAME"
@@ -329,6 +321,13 @@ create_nft_table() {
nft add rule inet "$NFT_TABLE_NAME" mangle_output ip daddr "@$NFT_COMMON_SET_NAME" meta l4proto udp meta mark set 0x105 counter nft add rule inet "$NFT_TABLE_NAME" mangle_output ip daddr "@$NFT_COMMON_SET_NAME" meta l4proto udp meta mark set 0x105 counter
nft add rule inet "$NFT_TABLE_NAME" mangle_output ip daddr "$SB_FAKEIP_INET4_RANGE" meta l4proto tcp meta mark set 0x105 counter nft add rule inet "$NFT_TABLE_NAME" mangle_output ip daddr "$SB_FAKEIP_INET4_RANGE" meta l4proto tcp meta mark set 0x105 counter
nft add rule inet "$NFT_TABLE_NAME" mangle_output ip daddr "$SB_FAKEIP_INET4_RANGE" meta l4proto tcp meta mark set 0x105 counter nft add rule inet "$NFT_TABLE_NAME" mangle_output ip daddr "$SB_FAKEIP_INET4_RANGE" meta l4proto tcp meta mark set 0x105 counter
local exclude_ntp
config_get_bool exclude_ntp "settings" "exclude_ntp" "0"
if [ "$exclude_ntp" -eq 1 ]; then
log "NTP traffic exclude for proxy"
nft insert rule inet "$NFT_TABLE_NAME" mangle udp dport 123 return
fi
} }
backup_dnsmasq_config_option() { backup_dnsmasq_config_option() {
@@ -342,7 +341,7 @@ backup_dnsmasq_config_option() {
fi fi
} }
dnsmasq_add_resolver() { dnsmasq_configure() {
local shutdown_correctly local shutdown_correctly
config_get shutdown_correctly "settings" "shutdown_correctly" config_get shutdown_correctly "settings" "shutdown_correctly"
if [ "$shutdown_correctly" -eq 0 ]; then if [ "$shutdown_correctly" -eq 0 ]; then
@@ -527,7 +526,6 @@ list_update() {
} }
# sing-box funcs # sing-box funcs
sing_box_uci() { sing_box_uci() {
local sing_box_enabled sing_box_user sing_box_config_path sing_box_conffile 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_enabled=$(uci get "sing-box.main.enabled")