Skip to content

Commit

Permalink
openwrt: bind to logical interface
Browse files Browse the repository at this point in the history
The interface name defined in /etc/config/network is called
logic interface name in OpenWRT. Usually, it didn't present
the interface name in Linux system.

When we configure the smartdns bind to a interface, it usually
means only the addresses assgined with that interface should be
listened. We could have many applications bind to the same port.
  • Loading branch information
hizukiayaka committed Aug 17, 2024
1 parent 75649b6 commit 4d3a72b
Showing 1 changed file with 50 additions and 4 deletions.
54 changes: 50 additions & 4 deletions package/openwrt/files/etc/init.d/smartdns
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,43 @@ conf_append_bind()
done
}

conf_append_bind_interface()
{
local bind_type="$1"
local port="$2"
local interfaces="$3"
local ipv6_server="$4"
local ARGS="$5"
local intf=""

. /lib/functions/network.sh

for intf in ${interfaces}; do
local __device
local __addrs
network_get_device __device $intf
[ -z "$__device" ] && continue

if [ "$ipv6_server" = "1" ]; then
local __addr
__addr=$(ifconfig "$__device"|grep 'Scope:Link' \
| sed 's:.*\(fe[8ab].*\)/.*:\1:')

conf_append "$bind_type" "[${__addr}]:${port}@${__device} $ARGS"

network_get_ipaddrs6 __addrs "$intf"
for __addr in ${__addrs}; do
conf_append "$bind_type" "[${__addr}]:${port}@${__device} $ARGS"
done
fi

network_get_ipaddrs __addrs "$intf"
for __addr in ${__addrs}; do
conf_append "$bind_type" "[${__addr}]:${port}@${__device} $ARGS"
done
done
}

load_second_server()
{
local section="$1"
Expand Down Expand Up @@ -739,6 +776,7 @@ load_service()

config_get_bool bind_device "$section" "bind_device" "0"
config_get bind_device_name "$section" "bind_device_name" "${lan_device}"
config_get bind_interfaces "$section" "bind_interface" ""
[ ! -z "$bind_device_name" ] && [ "$bind_device" = "1" ] && device="${bind_device_name}"

config_get cache_file "$section" "cache_file" "$SMARTDNS_CONF_DIR/smartdns.cache"
Expand Down Expand Up @@ -829,10 +867,18 @@ load_service()
[ "$auto_set_dnsmasq" = "0" ] && [ "$old_auto_set_dnsmasq" = "1" ] && stop_forward_dnsmasq "$old_port" "0"
}

conf_append_bind "bind" "$port" "$device" "$ipv6_server" "$server_flags"
[ "$tcp_server" = "1" ] && conf_append_bind "bind-tcp" "$port" "$device" "$ipv6_server" "$server_flags"
[ "$tls_server" = "1" ] && conf_append_bind "bind-tls" "$tls_server_port" "$device" "$ipv6_server" "$server_flags"
[ "$doh_server" = "1" ] && conf_append_bind "bind-https" "$doh_server_port" "$device" "$ipv6_server" "$server_flags"
local __conf_bin_func
if [ ! -z $bind_interfaces ]; then
__conf_bin_func="conf_append_bind_interface"
device=${bind_interfaces}
else
__conf_bin_func="conf_append_bind"
fi

$__conf_bin_func "bind" "$port" "$device" "$ipv6_server" "$server_flags"
[ "$tcp_server" = "1" ] && $__conf_bin_func "bind-tcp" "$port" "$device" "$ipv6_server" "$server_flags"
[ "$tls_server" = "1" ] && $__conf_bin_func "bind-tls" "$tls_server_port" "$device" "$ipv6_server" "$server_flags"
[ "$doh_server" = "1" ] && $__conf_bin_func "bind-https" "$doh_server_port" "$device" "$ipv6_server" "$server_flags"

[ ! -z "$bind_cert" ] && conf_append "bind-cert-file" "$bind_cert"
[ ! -z "$bind_cert_key" ] && conf_append "bind-cert-key-file" "$bind_cert_key"
Expand Down

0 comments on commit 4d3a72b

Please sign in to comment.