feat: add support for outbound JSON configuration in sing-box

This commit is contained in:
Ivan K
2025-02-22 14:15:27 +03:00
parent 615928db4e
commit d8d8d79d68
2 changed files with 29 additions and 49 deletions

View File

@@ -53,23 +53,6 @@ function getNetworkInterfaces(o) {
}); });
} }
function getNetworkInterfaces(o) {
const excludeInterfaces = ['br-lan', 'eth0', 'eth1', 'wan', 'phy0-ap0', 'phy1-ap0', 'pppoe-wan'];
return network.getDevices().then(devices => {
devices.forEach(device => {
if (device.dev && device.dev.name) {
const deviceName = device.dev.name;
if (!excludeInterfaces.includes(deviceName) && !/^lan\d+$/.test(deviceName)) {
o.value(deviceName, deviceName);
}
}
});
}).catch(error => {
console.error('Failed to get network devices:', error);
});
}
// Общая функция для создания конфигурационных секций // Общая функция для создания конфигурационных секций
function createConfigSection(section, map, network) { function createConfigSection(section, map, network) {
const s = section; const s = section;
@@ -226,7 +209,6 @@ function createConfigSection(section, map, network) {
o.depends('mode', 'vpn'); o.depends('mode', 'vpn');
o.ucisection = s.section; o.ucisection = s.section;
getNetworkInterfaces(o); getNetworkInterfaces(o);
getNetworkInterfaces(o);
o = s.taboption('basic', form.Flag, 'domain_list_enabled', _('Community Lists')); o = s.taboption('basic', form.Flag, 'domain_list_enabled', _('Community Lists'));
o.default = '0'; o.default = '0';

View File

@@ -212,8 +212,9 @@ main() {
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"
if [ -n "$proxy_string" ] || [ -n "$interface" ]; then if [ -n "$proxy_string" ] || [ -n "$interface" ] || [ -n "$outbound_json" ]; then
config_get_bool dont_touch_dhcp "main" "dont_touch_dhcp" "0" 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
@@ -798,7 +799,7 @@ sing_box_outdound() {
config_get outbound_json $section "outbound_json" config_get outbound_json $section "outbound_json"
if [ -n "$outbound_json" ]; then if [ -n "$outbound_json" ]; then
log "Using JSON outbound configuration" log "Using JSON outbound configuration"
sing_box_config_outbound_json "$outbound_json" sing_box_config_outbound_json "$outbound_json" "$section"
else else
log "Missing outbound JSON configuration" log "Missing outbound JSON configuration"
return return
@@ -880,37 +881,34 @@ sing_box_config_check() {
sing_box_config_outbound_json() { sing_box_config_outbound_json() {
local json_config="$1" local json_config="$1"
local listen_port="$2" local section="$2"
cat > /tmp/base_config.json << EOF # Create new object with tag first, then merge with the rest of the config
{ local modified_config=$(echo "$json_config" | jq --arg section "$section" \
"log": { 'del(.tag) | {"tag": $section} + .')
"level": "warn"
},
"inbounds": [
{
"type": "tproxy",
"listen": "::",
"listen_port": $listen_port,
"tcp_fast_open": true,
"udp_fragment": true
},
{
"tag": "dns-in",
"type": "direct",
"listen": "127.0.0.42",
"listen_port": 53
}
],
"outbounds": [],
"route": {
"auto_detect_interface": true
}
}
EOF
jq --argjson outbound "$json_config" '.outbounds += [$outbound]' /tmp/base_config.json > $SING_BOX_CONFIG jq --argjson outbound "$modified_config" \
rm -f /tmp/base_config.json --arg section "$section" \
'. |
.outbounds |= (
map(
if .tag == $section then
$outbound
else . end
) +
(
if (map(select(.tag == $section)) | length) == 0 then
[$outbound]
else [] end
)
)' $SING_BOX_CONFIG >/tmp/sing-box-config-tmp.json && mv /tmp/sing-box-config-tmp.json $SING_BOX_CONFIG
if [ $? -eq 0 ]; then
log "Outbound config updated successfully"
else
log "Error: Invalid JSON config generated"
return 1
fi
} }