You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
4.4 KiB

  1. #!/bin/sh
  2. usage() {
  3. cat <<-EOF
  4. Usage: ss-rules [options]
  5. Valid options are:
  6. -s <server_host> hostname or ip of shadowsocks remote server
  7. -l <local_port> port number of shadowsocks local server
  8. -i <ip_list_file> a file content is bypassed ip list
  9. -a <lan_ips> lan ip of access control, need a prefix to
  10. define access control mode
  11. -b <wan_ips> wan ip of will be bypassed
  12. -w <wan_ips> wan ip of will be forwarded
  13. -e <extra_options> extra options for iptables
  14. -o apply the rules to the OUTPUT chain
  15. -u enable udprelay mode, TPROXY is required
  16. -f flush the rules
  17. EOF
  18. }
  19. loger() {
  20. # 1.alert 2.crit 3.err 4.warn 5.notice 6.info 7.debug
  21. logger -st ss-rules[$$] -p$1 $2
  22. }
  23. ipt_n="iptables -t nat"
  24. ipt_m="iptables -t mangle"
  25. flush_r() {
  26. local IPT
  27. IPT=$(iptables-save -t nat)
  28. eval $(echo "$IPT" | grep "_SS_SPEC_RULE_" | \
  29. sed -e 's/^-A/$ipt_n -D/' -e 's/$/;/')
  30. for chain in $(echo "$IPT" | awk '/^:SS_SPEC/{print $1}'); do
  31. $ipt_n -F ${chain:1} 2>/dev/null && $ipt_n -X ${chain:1}
  32. done
  33. IPT=$(iptables-save -t mangle)
  34. eval $(echo "$IPT" | grep "_SS_SPEC_RULE_" | \
  35. sed -e 's/^-A/$ipt_m -D/' -e 's/$/;/')
  36. for chain in $(echo "$IPT" | awk '/^:SS_SPEC/{print $1}'); do
  37. $ipt_m -F ${chain:1} 2>/dev/null && $ipt_m -X ${chain:1}
  38. done
  39. ip rule del fwmark 0x01/0x01 table 100 2>/dev/null
  40. ip route del local 0.0.0.0/0 dev lo table 100 2>/dev/null
  41. ipset -X ss_spec_lan_ac 2>/dev/null
  42. ipset -X ss_spec_wan_ac 2>/dev/null
  43. return 0
  44. }
  45. ipset_r() {
  46. ipset -! -R <<-EOF || return 1
  47. create ss_spec_wan_ac hash:net
  48. $(echo -e "$IPLIST" | sed -e "s/^/add ss_spec_wan_ac /")
  49. $(for ip in $WAN_FW_IP; do echo "add ss_spec_wan_ac $ip nomatch"; done)
  50. EOF
  51. $ipt_n -N SS_SPEC_WAN_AC && \
  52. $ipt_n -A SS_SPEC_WAN_AC -m set --match-set ss_spec_wan_ac dst -j RETURN && \
  53. $ipt_n -A SS_SPEC_WAN_AC -j SS_SPEC_WAN_FW
  54. return $?
  55. }
  56. fw_rule() {
  57. $ipt_n -N SS_SPEC_WAN_FW && \
  58. $ipt_n -A SS_SPEC_WAN_FW -p tcp \
  59. -j REDIRECT --to-ports $LOCAL_PORT 2>/dev/null || {
  60. loger 3 "Can't redirect, please check the iptables."
  61. exit 1
  62. }
  63. return $?
  64. }
  65. ac_rule() {
  66. local TAG ROUTECHAIN
  67. if [ -n "$LAN_AC_IP" ]; then
  68. if [ "${LAN_AC_IP:0:1}" = "w" ]; then
  69. TAG="nomatch"
  70. else
  71. if [ "${LAN_AC_IP:0:1}" != "b" ]; then
  72. loger 3 "Bad argument \`-a $LAN_AC_IP\`."
  73. return 2
  74. fi
  75. fi
  76. fi
  77. ROUTECHAIN=PREROUTING
  78. if iptables-save -t nat | grep -q "^:zone_lan_prerouting"; then
  79. ROUTECHAIN=zone_lan_prerouting
  80. fi
  81. ipset -! -R <<-EOF || return 1
  82. create ss_spec_lan_ac hash:net
  83. $(for ip in ${LAN_AC_IP:1}; do echo "add ss_spec_lan_ac $ip $TAG"; done)
  84. EOF
  85. $ipt_n -A $ROUTECHAIN -p tcp $EXT_ARGS \
  86. -m set ! --match-set ss_spec_lan_ac src \
  87. -m comment --comment "_SS_SPEC_RULE_" -j SS_SPEC_WAN_AC
  88. if [ "$OUTPUT" = 1 ]; then
  89. $ipt_n -A OUTPUT -p tcp $EXT_ARGS \
  90. -m comment --comment "_SS_SPEC_RULE_" -j SS_SPEC_WAN_AC
  91. fi
  92. return $?
  93. }
  94. tp_rule() {
  95. [ "$TPROXY" = 1 ] || return 0
  96. ip rule add fwmark 0x01/0x01 table 100
  97. ip route add local 0.0.0.0/0 dev lo table 100
  98. $ipt_m -N SS_SPEC_TPROXY
  99. $ipt_m -A SS_SPEC_TPROXY -p udp -m set ! --match-set ss_spec_wan_ac dst \
  100. -j TPROXY --on-port $LOCAL_PORT --tproxy-mark 0x01/0x01
  101. $ipt_m -A PREROUTING -p udp $EXT_ARGS \
  102. -m set ! --match-set ss_spec_lan_ac src \
  103. -m comment --comment "_SS_SPEC_RULE_" -j SS_SPEC_TPROXY
  104. return $?
  105. }
  106. while getopts ":s:l:c:i:e:a:b:w:ouf" arg; do
  107. case $arg in
  108. s)
  109. SERVER=$OPTARG
  110. ;;
  111. l)
  112. LOCAL_PORT=$OPTARG
  113. ;;
  114. i)
  115. IGNORE=$OPTARG
  116. ;;
  117. e)
  118. EXT_ARGS=$OPTARG
  119. ;;
  120. a)
  121. LAN_AC_IP=$OPTARG
  122. ;;
  123. b)
  124. WAN_BP_IP=$(for ip in $OPTARG; do echo $ip; done)
  125. ;;
  126. w)
  127. WAN_FW_IP=$OPTARG
  128. ;;
  129. o)
  130. OUTPUT=1
  131. ;;
  132. u)
  133. TPROXY=1
  134. ;;
  135. f)
  136. flush_r
  137. exit 0
  138. ;;
  139. esac
  140. done
  141. if [ -z "$SERVER" -o -z "$LOCAL_PORT" ]; then
  142. usage
  143. exit 2
  144. fi
  145. SERVER=$(resolveip -t60 $SERVER)
  146. if [ -z "$SERVER" ]; then
  147. loger 3 "Can't resolve the server hostname."
  148. exit 1
  149. fi
  150. if [ -f "$IGNORE" ]; then
  151. IGNORE_IP=$(cat $IGNORE 2>/dev/null)
  152. fi
  153. IPLIST=$(cat <<-EOF | grep -E "^([0-9]{1,3}\.){3}[0-9]{1,3}"
  154. $SERVER
  155. 0.0.0.0/8
  156. 10.0.0.0/8
  157. 100.64.0.0/10
  158. 127.0.0.0/8
  159. 169.254.0.0/16
  160. 172.16.0.0/12
  161. 192.0.0.0/24
  162. 192.0.2.0/24
  163. 192.88.99.0/24
  164. 192.168.0.0/16
  165. 198.18.0.0/15
  166. 198.51.100.0/24
  167. 203.0.113.0/24
  168. 224.0.0.0/4
  169. 240.0.0.0/4
  170. 255.255.255.255
  171. $WAN_BP_IP
  172. $IGNORE_IP
  173. EOF
  174. )
  175. flush_r && fw_rule && ipset_r && ac_rule && tp_rule
  176. exit $?