Skip to content

Commit

Permalink
Fix get LAN IPs
Browse files Browse the repository at this point in the history
  • Loading branch information
Acris committed Feb 27, 2020
1 parent 0e4964f commit 5f12c6c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
2 changes: 1 addition & 1 deletion etc/ss-merlin.sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ udp=0
## Configure which LAN IP will pass transparent proxy.
## Default is 0.0.0.0/0, means all LAN devices will affected.
## You can assign a LAN IP like 192.169.1.125 means only this device can pass transparent proxy.
lan_ips=192.168.0.0/24
lan_ips=0.0.0.0/0

# China DNS IP
## Default is 119.29.29.29
Expand Down
31 changes: 9 additions & 22 deletions scripts/apply_iptables_rule.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,15 @@ if [[ ! -f ${SS_MERLIN_HOME}/etc/shadowsocks/config.json ]]; then
fi
. ${SS_MERLIN_HOME}/etc/ss-merlin.conf

mask2cdr() {
get_lan_ips() {
lan_ipaddr=$(nvram get lan_ipaddr)
lan_netmask=$(nvram get lan_netmask)
# Assumes there's no "255." after a non-255 byte in the mask
readonly local _maskmap="____128_192_224_240_248_252_254_"

local _submask=${1##*255.}
local _lastmask=${_submask%%.*}

local _lastmask_maplen=${_maskmap%%$_lastmask*}
local _premask_cdr=$(( (${#1} - ${#_submask}) * 2 )) _lastmask_cdr=$(( ${#_lastmask_maplen} / 4 ))

echo $(( $_premask_cdr + $_lastmask_cdr ))
}

get_lan_subnet() {
local _lan_ipaddr=$(nvram show 2>/dev/null | sed -n "s/lan_ipaddr=\(.*\)/\1/p")
local _lan_netmask=$(nvram show 2>/dev/null | sed -n "s/lan_netmask=\(.*\)/\1/p")

[[ -n "$_lan_netmask" && -n "$_lan_netmask" ]] || echo "192.168.0.0/24"

_lan_ipaddr="${_lan_ipaddr%.*}.0"

echo "$_lan_ipaddr"/$(mask2cdr "$_lan_netmask")
local x=${lan_netmask##*255.}
set -- 0^^^128^192^224^240^248^252^254^ $(( (${#lan_netmask} - ${#x})*2 )) "${x%%.*}"
x=${1%%$3*}
cidr=$(($2 + (${#x}/4)))
echo "${lan_ipaddr%.*}".0/$cidr
}

modprobe ip_set
Expand Down Expand Up @@ -122,7 +109,7 @@ fi
local_redir_port=$(cat ${SS_MERLIN_HOME}/etc/shadowsocks/config.json | grep 'local_port' | cut -d ':' -f 2 | grep -o '[0-9]*')

if [[ ! ${lan_ips} || ${lan_ips} == '0.0.0.0/0' ]]; then
lan_ips=$(get_lan_subnet)
lan_ips=$(get_lan_ips)
fi
echo "LAN IPs are ${lan_ips}"

Expand Down

0 comments on commit 5f12c6c

Please sign in to comment.