From 4f26897d1a672391be730f0bf920ad23e3abd47a Mon Sep 17 00:00:00 2001 From: "Ycarus (Yannick Chabanois)" Date: Wed, 24 Jan 2024 15:30:38 +0100 Subject: [PATCH 1/5] Use -e on multipath binary --- mptcp/files/usr/bin/multipath | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/mptcp/files/usr/bin/multipath b/mptcp/files/usr/bin/multipath index 0e3d230ca..3735748b6 100755 --- a/mptcp/files/usr/bin/multipath +++ b/mptcp/files/usr/bin/multipath @@ -95,7 +95,7 @@ DEVICE="$1" TYPE="$2" #FLAG_PATH=`find /sys/devices/ -path "*/net/$DEVICE/flags"` -[ -d "/sys/class/net/$DEVICE/" ] || { +[ -e "/sys/class/net/$DEVICE/" ] || { #echo "Device '$DEVICE' can't found!" #echo "Use the hardware name like in ifconfig" exit 1 @@ -142,18 +142,18 @@ else } endpoint="$(ip mptcp endpoint show)" [ -z "$endpoint" ] && exit 0 - ID=$(echo "${endpoint}" | sort | grep "dev $DEVICE " | awk '{print $3}') - IFF=$(echo "${endpoint}" | sort | grep -m 1 -E "dev $DEVICE " | awk '{print $4}') - #IP=$(ip a show $DEVICE | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p') - if [ -f /usr/bin/jsonfilter ]; then + ID=$(echo "${endpoint}" | sort | grep "dev $DEVICE " | awk '{print $3}') + IFF=$(echo "${endpoint}" | sort | grep -m 1 -E "dev $DEVICE " | awk '{print $4}') + #IP=$(ip a show $DEVICE | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p') + if [ -f /usr/bin/jsonfilter ]; then IP=$(ip -j a show $DEVICE scope global | jsonfilter -e '@[0].addr_info[*].local') elif [ -f /usr/bin/jq ]; then IP=$(ip -j a show $DEVICE scope global | jq -r '.[0].addr_info[].local') fi - [ -z "$ID" ] && [ -n "$IP" ] && ID=$(echo "${endpoint}" show | grep "$IP " | awk '{print $3}') - RMID=$(echo "${endpoint}" | grep '::ffff' | awk '{ print $3 }') - [ -n "$RMID" ] && ip mptcp endpoint delete id $RMID 2>&1 >/dev/null - case $TYPE in + [ -z "$ID" ] && [ -n "$IP" ] && ID=$(echo "${endpoint}" show | grep "$IP " | awk '{print $3}') + RMID=$(echo "${endpoint}" | grep '::ffff' | awk '{ print $3 }') + [ -n "$RMID" ] && ip mptcp endpoint delete id $RMID 2>&1 >/dev/null + case $TYPE in "off") [ -n "$ID" ] && { for i in $ID; do From c68a5f0db89395802ee1055266aeeb00b6688e93 Mon Sep 17 00:00:00 2001 From: "Ycarus (Yannick Chabanois)" Date: Wed, 24 Jan 2024 15:31:56 +0100 Subject: [PATCH 2/5] Add more check for IPv4/IPv6 in post tracking --- .../omr/post-tracking.d/001-post-tracking | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/mptcp/files/usr/share/omr/post-tracking.d/001-post-tracking b/mptcp/files/usr/share/omr/post-tracking.d/001-post-tracking index 9786041c3..e43a4618c 100755 --- a/mptcp/files/usr/share/omr/post-tracking.d/001-post-tracking +++ b/mptcp/files/usr/share/omr/post-tracking.d/001-post-tracking @@ -708,7 +708,7 @@ dns_flush() { # Get the current multipath status multipath_status="off" [ "$OMR_TRACKER_INTERFACE" = "omrvpn" ] && multipath $OMR_TRACKER_DEVICE off 2>&1 >/dev/null -if [ -d "/sys/class/net/$OMR_TRACKER_DEVICE" ]; then +if [ -e "/sys/class/net/$OMR_TRACKER_DEVICE" ]; then case "$(multipath "$OMR_TRACKER_DEVICE")" in *default*) multipath_status="on" ;; *backup*) multipath_status="backup" ;; @@ -1102,10 +1102,10 @@ if [ "$multipath_config" = "master" ]; then ip -6 route flush cache 2>&1 >/dev/null fi #if [ "$(uci -q get openmptcprouter.settings.master)" = "balancing" ] && [ "$(ip route show default | grep weight)" = "" ] && [ "$(uci -q get openmptcprouter.settings.defaultgw)" != "0" ] && [ "$(uci -q get openmptcprouter.settings.vpn)" != "mlvpn" ]; then - if [ "$(uci -q get openmptcprouter.settings.master)" = "balancing" ] && ([ "$(ip route show default | grep weight)" = "" ] || [ "$(ip -6 route show default | grep weight)" = "" ]) && [ "$(uci -q get openmptcprouter.settings.defaultgw)" != "0" ]; then + if [ "$(uci -q get openmptcprouter.settings.master)" = "balancing" ] && (([ -n "$OMR_TRACKER_DEVICE_IP" ] && [ "$(ip route show default | grep weight)" = "" ]) || ([ -n "$OMR_TRACKER_DEVICE_IP6" ] && [ "$(ip -6 route show default | grep weight)" = "" ])) && [ "$(uci -q get openmptcprouter.settings.defaultgw)" != "0" ]; then omrvpn_intf=$(uci -q get "network.omrvpn.device" || echo "tun0") [ -z "$omrvpn_intf" ] && omrvpn_intf=$(uci -q get "network.omrvpn.ifname" || echo "tun0") - if [ -n "$omrvpn_intf" ] && [ "$(ip route show default | grep -v metric | awk '/default/ {print $5}' | grep $omrvpn_intf)" = "" ]; then + if [ -n "$omrvpn_intf" ] && [ -n "$OMR_TRACKER_DEVICE_IP" ] && [ "$(ip route show default | grep -v metric | awk '/default/ {print $5}' | grep $omrvpn_intf)" = "" ]; then routesbalancing="" routesbalancingbackup="" nbintf=0 @@ -1141,7 +1141,7 @@ if [ "$multipath_config" = "master" ]; then # ip -6 route replace default scope global metric 999 $routesbalancingbackup6 2>&1 >/dev/null # } #} - elif [ -n "$omrvpn_intf" ] && [ "$(ip -6 route show default | grep -v metric | awk '/default/ {print $5}' | grep $omrvpn_intf)" = "" ]; then + elif [ -n "$omrvpn_intf" ] && [ -n "$OMR_TRACKER_DEVICE_IP6" ] && [ "$(ip -6 route show default | grep -v metric | awk '/default/ {print $5}' | grep $omrvpn_intf)" = "" ]; then routesbalancing6="" routesbalancingbackup6="" nbintf6=0 @@ -1161,16 +1161,16 @@ if [ "$multipath_config" = "master" ]; then } } fi - ip route flush cache 2>&1 >/dev/null - ip -6 route flush cache 2>&1 >/dev/null + #ip route flush cache 2>&1 >/dev/null + #ip -6 route flush cache 2>&1 >/dev/null fi if [ -n "$OMR_TRACKER_DEVICE_GATEWAY" ] && [ -n "$OMR_TRACKER_DEVICE" ] && [ "$(ip r show table 991337)" != "default via $OMR_TRACKER_DEVICE_GATEWAY dev $OMR_TRACKER_DEVICE " ]; then ip route replace default via $OMR_TRACKER_DEVICE_GATEWAY dev $OMR_TRACKER_DEVICE table 991337 $initcwrwnd 2>&1 >/dev/null - ip route flush cache 2>&1 >/dev/null + #ip route flush cache 2>&1 >/dev/null fi if [ -n "$OMR_TRACKER_DEVICE_GATEWAY6" ] && [ -n "$OMR_TRACKER_DEVICE" ] && [ "$(ip -6 r show table 991337)" != "default via $OMR_TRACKER_DEVICE_GATEWAY6 dev $OMR_TRACKER_DEVICE " ]; then ip -6 route replace default via $OMR_TRACKER_DEVICE_GATEWAY6 dev $OMR_TRACKER_DEVICE table 991337 $initcwrwnd 2>&1 >/dev/null - ip -6 route flush cache 2>&1 >/dev/null + #ip -6 route flush cache 2>&1 >/dev/null fi if ([ -n "$OMR_TRACKER_INTERFACE" ] && [ "$(uci -q get openmptcprouter.$OMR_TRACKER_INTERFACE.lc)" = "" ]) || [ $(($(date +"%s") + $((10 + RANDOM % 31)) - $(uci -q get openmptcprouter.$OMR_TRACKER_INTERFACE.lc))) -gt 3600 ] || [ "$(uci -q show openmptcprouter | grep get_config=\'1\')" != "" ] || [ "$(uci -q show openmptcprouter | grep admin_error=\'1\')" != "" ]; then [ "$(pgrep -f openmptcprouter-vps)" = "" ] && /etc/init.d/openmptcprouter-vps restart >/dev/null 2>&1 & @@ -1201,7 +1201,7 @@ if [ -n "$OMR_TRACKER_DEVICE_IP6" ] && [ -n "$OMR_TRACKER_DEVICE_GATEWAY6" ]; th config_foreach set_server_route6 server fi if [ "$(uci -q get openmptcprouter.settings.defaultgw)" != "0" ] && [ -n "$OMR_TRACKER_DEVICE_GATEWAY6" ] && [ -n "$OMR_TRACKER_DEVICE" ] && [ "$(ip -6 r show dev $OMR_TRACKER_DEVICE | grep default)" = "" ] && [ -n "$OMR_TRACKER_INTERFACE" ] && [ "$(uci -q get network.$OMR_TRACKER_INTERFACE.metric)" != "" ]; then - ip -6 r replace default via $OMR_TRACKER_DEVICE_GATEWAY6 dev $OMR_TRACKER_DEVICE metric $(uci -q get network.$OMR_TRACKER_INTERFACE.metric) >/dev/null 2>&1 + ip -6 r replace default via $OMR_TRACKER_DEVICE_GATEWAY6 dev $OMR_TRACKER_DEVICE metric 6$(uci -q get network.$OMR_TRACKER_INTERFACE.metric) >/dev/null 2>&1 ip -6 route flush cache 2>&1 >/dev/null fi fi From 56de8fc06def1ec854cc6317b8bb25f46ef4b412 Mon Sep 17 00:00:00 2001 From: "Ycarus (Yannick Chabanois)" Date: Wed, 24 Jan 2024 15:32:32 +0100 Subject: [PATCH 3/5] Fix when there is multiple redir listener --- shadowsocks-libev/files/shadowsocks-libev.init-nft | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shadowsocks-libev/files/shadowsocks-libev.init-nft b/shadowsocks-libev/files/shadowsocks-libev.init-nft index 97740176f..f8121fe7c 100755 --- a/shadowsocks-libev/files/shadowsocks-libev.init-nft +++ b/shadowsocks-libev/files/shadowsocks-libev.init-nft @@ -202,8 +202,8 @@ ss_rules_nft_gen() { local tmp="/tmp/ssrules" json_init json_add_string o_remote_servers "$remote_servers" - json_add_int o_redir_tcp_port "$local_port_tcp" - json_add_int o_redir_udp_port "$local_port_udp" + json_add_string o_redir_tcp_port "$local_port_tcp" + json_add_string o_redir_udp_port "$local_port_udp" json_add_string o_ifnames "$ifnames" json_add_string o_local_default "$local_default" json_add_string o_src_bypass "$src_ips_bypass" From 7c40ecf71758700c80c3dfd4f4a31182d4ccac94 Mon Sep 17 00:00:00 2001 From: "Ycarus (Yannick Chabanois)" Date: Wed, 24 Jan 2024 15:32:52 +0100 Subject: [PATCH 4/5] Add multiple redir listener in shadowsocks-rust --- .../files/shadowsocks-rust.init-nft | 44 ++++++++++++++++--- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/shadowsocks-rust/files/shadowsocks-rust.init-nft b/shadowsocks-rust/files/shadowsocks-rust.init-nft index c5b77740e..7a48c152f 100755 --- a/shadowsocks-rust/files/shadowsocks-rust.init-nft +++ b/shadowsocks-rust/files/shadowsocks-rust.init-nft @@ -118,6 +118,20 @@ ss_rules_cb() { fi } +ss_redir_ports() { + port=$(uci -q get shadowsocks-rust.$1.local_port) + server=$(uci -q get shadowsocks-rust.$1.server) + disabled=$(uci -q get shadowsocks-rust.$1.disabled) + if [ "$disabled" != "1" ] && ([ "$server" = "$2" ] || [ "$2" = "" ]); then + if [ "$port" -lt "$min_ss_redir_ports" ]; then + min_ss_redir_ports=$port + fi + if [ "$port" -gt "$max_ss_redir_ports" ]; then + max_ss_redir_ports=$port + fi + fi +} + ss_rules_nft_gen() { local cfg="ss_rules" local cfgtype @@ -129,8 +143,26 @@ ss_rules_nft_gen() { eval "$(validate_ss_rules_section "$cfg" ss_validate_mklocal)" validate_ss_rules_section "$cfg" || return 1 [ "$disabled" = 0 ] || return 2 - eval local_port_tcp="\$ss_rules_redir_tcp_$redir_tcp" - eval local_port_udp="\$ss_rules_redir_udp_$redir_udp" + + if [ "$ss_rules_redir_tcp_$redir_tcp" = "all" ]; then + min_ss_redir_ports="65535" + max_ss_redir_ports="0" + config_load shadowsocks-rust + config_foreach ss_redir_ports ss_redir $cfgrulesserver + if [ "$min_ss_redir_ports" != "$max_ss_redir_ports" ]; then + all_ss_redir_ports=$min_ss_redir_ports-$max_ss_redir_ports + else + all_ss_redir_ports=$min_ss_redir_ports + fi + local_port_tcp="$all_ss_redir_ports" + if [ "$ss_rules_redir_udp_$redir_udp" = "all" ] || [ "$ss_rules_redir_udp_$redir_udp" = "hi1" ]; then + local_port_udp="$min_ss_redir_ports" + fi + else + eval local_port_tcp="\$ss_rules_redir_tcp_$redir_tcp" + eval local_port_udp="\$ss_rules_redir_udp_$redir_udp" + fi + [ -n "$local_port_tcp" -o -n "$local_port_udp" ] || return 1 remote_servers="$(echo $ss_redir_servers \ | tr ' ' '\n' \ @@ -141,8 +173,8 @@ ss_rules_nft_gen() { local tmp="/tmp/ssrrules" json_init json_add_string o_remote_servers "$remote_servers" - json_add_int o_redir_tcp_port "$local_port_tcp" - json_add_int o_redir_udp_port "$local_port_udp" + json_add_string o_redir_tcp_port "$local_port_tcp" + json_add_string o_redir_udp_port "$local_port_udp" json_add_string o_ifnames "$ifnames" json_add_string o_local_default "$local_default" json_add_string o_src_bypass "$src_ips_bypass" @@ -326,8 +358,8 @@ validate_ss_redir_section() { validate_ss_rules_section() { "${2:-ss_validate}" ss_rules "$1" \ 'disabled:bool:0' \ - 'redir_tcp:uci("shadowsocks-rust", "@ss_redir")' \ - 'redir_udp:uci("shadowsocks-rust", "@ss_redir")' \ + 'redir_tcp:or(uci("shadowsocks-rust", "@ss_redir"),"all")' \ + 'redir_udp:or(uci("shadowsocks-rust", "@ss_redir"),"all")' \ 'src_ips_bypass:or(ipaddr,cidr)' \ 'src_ips_forward:or(ipaddr,cidr)' \ 'src_ips_checkdst:or(ipaddr,cidr)' \ From 398e81f0fed12ac990fb20076d1e0c1f2ad3f747 Mon Sep 17 00:00:00 2001 From: "Ycarus (Yannick Chabanois)" Date: Wed, 24 Jan 2024 15:53:36 +0100 Subject: [PATCH 5/5] Fix multipath command --- mptcp/files/usr/bin/multipath | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/mptcp/files/usr/bin/multipath b/mptcp/files/usr/bin/multipath index 3735748b6..90f648a67 100755 --- a/mptcp/files/usr/bin/multipath +++ b/mptcp/files/usr/bin/multipath @@ -133,25 +133,28 @@ if [ -f /proc/sys/net/mptcp/mptcp_enabled ]; then else # Remove not needed if* interfaces in MPTCP endpoint="$(ip mptcp endpoint show)" - [ -z "$endpoint" ] && exit 0 - oldintfs=$(echo "${endpoint}" | grep "dev if" | awk '{ print $3 }') + [ -n "$endpoint" ] && oldintfs=$(echo "${endpoint}" | grep "dev if" | awk '{ print $3 }') [ -n "$oldintfs" ] && { for oldintf in $oldintfs; do ip mptcp endpoint delete id $oldintf 2>&1 >/dev/null done } endpoint="$(ip mptcp endpoint show)" - [ -z "$endpoint" ] && exit 0 - ID=$(echo "${endpoint}" | sort | grep "dev $DEVICE " | awk '{print $3}') - IFF=$(echo "${endpoint}" | sort | grep -m 1 -E "dev $DEVICE " | awk '{print $4}') + if [ -n "$endpoint" ]; then + ID=$(echo "${endpoint}" | sort | grep "dev $DEVICE " | awk '{print $3}') + IFF=$(echo "${endpoint}" | sort | grep -m 1 -E "dev $DEVICE " | awk '{print $4; exit}') + fi #IP=$(ip a show $DEVICE | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p') if [ -f /usr/bin/jsonfilter ]; then IP=$(ip -j a show $DEVICE scope global | jsonfilter -e '@[0].addr_info[*].local') elif [ -f /usr/bin/jq ]; then IP=$(ip -j a show $DEVICE scope global | jq -r '.[0].addr_info[].local') + else + echo "jsonfilter or jq are required" + exit 1 fi - [ -z "$ID" ] && [ -n "$IP" ] && ID=$(echo "${endpoint}" show | grep "$IP " | awk '{print $3}') - RMID=$(echo "${endpoint}" | grep '::ffff' | awk '{ print $3 }') + [ -z "$ID" ] && [ -n "$IP" ] && [ -n "$endpoint" ] && ID=$(echo "${endpoint}" show | grep "$IP " | awk '{print $3}') + [ -n "$endpoint" ] && RMID=$(echo "${endpoint}" | grep '::ffff' | awk '{ print $3 }') [ -n "$RMID" ] && ip mptcp endpoint delete id $RMID 2>&1 >/dev/null case $TYPE in "off")