-
Notifications
You must be signed in to change notification settings - Fork 7
/
natcfg.sh
221 lines (194 loc) · 7.31 KB
/
natcfg.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
red="\033[31m"
black="\033[0m"
base=/etc/dnat
mkdir $base 2>/dev/null
conf=$base/conf
touch $conf
# wget -qO natcfg.sh http://arloor.com/sh/iptablesUtils/natcfg.sh && bash natcfg.sh
echo -e "${red}用途${black}: 便捷的设置iptables端口转发"
echo -e "${red}注意1${black}: 到域名的转发规则在添加后需要等待2分钟才会生效,且在机器重启后仍然有效"
echo -e "${red}注意2${black}: 到IP的转发规则在重启后会失效,这是iptables的特性"
echo
setupService(){
wget -qO /usr/local/bin/dnat.sh http://arloor.com/sh/iptablesUtils/dnat.sh||{
echo "脚本不存在,请通过github提交issue通知作者"
exit 1
}
echo
cat > /lib/systemd/system/dnat.service <<\EOF
[Unit]
Description=动态设置iptables转发规则
After=network-online.target
Wants=network-online.target
[Service]
WorkingDirectory=/root/
EnvironmentFile=
ExecStart=/bin/bash /usr/local/bin/dnat.sh
Restart=always
RestartSec=30
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable dnat > /dev/null 2>&1
service dnat stop > /dev/null 2>&1
service dnat start > /dev/null 2>&1
}
## 获取本机地址
localIP=$(ip -o -4 addr list | grep -Ev '\s(docker|lo)' | awk '{print $4}' | cut -d/ -f1 | grep -Ev '(^127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^10\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.1[6-9]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.2[0-9]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^172\.3[0-1]{1}[0-9]{0,1}\.[0-9]{1,3}\.[0-9]{1,3}$)|(^192\.168\.[0-9]{1,3}\.[0-9]{1,3}$)')
if [ "${localIP}" = "" ]; then
localIP=$(ip -o -4 addr list | grep -Ev '\s(docker|lo)' | awk '{print $4}' | cut -d/ -f1|head -n 1 )
fi
rmIptablesNat(){
#删除旧的中转规则
local arr1=(`iptables -L PREROUTING -n -t nat --line-number |grep DNAT|grep "dpt:$1 "|sort -r|awk '{print $1,$3,$9}'|tr " " ":"|tr "\n" " "`)
for cell in ${arr1[@]} # cell= 1:tcp:to:8.8.8.8:543
do
local arr2=(`echo $cell|tr ":" " "`) #arr2=(1 tcp to 8.8.8.8 543)
local index=${arr2[0]}
local proto=${arr2[1]}
local targetIP=${arr2[3]}
local targetPort=${arr2[4]}
# echo 清除本机$localport端口到$targetIP:$targetPort的${proto}的PREROUTING转发规则[$index]
iptables -t nat -D PREROUTING $index
# echo ==清除对应的POSTROUTING规则
local toRmIndexs=(`iptables -L POSTROUTING -n -t nat --line-number|grep SNAT|grep $targetIP|grep dpt:$targetPort|grep $proto|awk '{print $1}'|sort -r|tr "\n" " "`)
for cell1 in ${toRmIndexs[@]}
do
iptables -t nat -D POSTROUTING $cell1
done
done
}
addDnat(){
local localport=
local remoteport=
local remotehost=
local valid=
echo -n "本地端口号:" ;read localport
echo -n "远程端口号:" ;read remoteport
# echo $localport $remoteport
# 判断端口是否为数字
echo "$localport"|[ -n "`sed -n '/^[0-9][0-9]*$/p'`" ] && echo $remoteport |[ -n "`sed -n '/^[0-9][0-9]*$/p'`" ]||{
echo -e "${red}本地端口和目标端口请输入数字!!${black}"
return 1;
}
echo -n "目标域名:" ;read remotehost
# 检查输入的不是IP
if [ "$remotehost" = "" -o "$(echo $remotehost |grep -E -o '([0-9]{1,3}[\.]){3}[0-9]{1,3}')" != "" ];then
isip=true
remote=$remotehost
echo -e "${red}请输入一个ddns域名${black}"
return 1
fi
setupService
echo "成功添加转发规则 $localport>$remotehost:$remoteport 大约两分钟后规则会生效"
sed -i "s/^$localport.*/$localport>$remotehost:$remoteport/g" $conf
[ "$(cat $conf|grep "$localport>$remotehost:$remoteport")" = "" ]&&{
cat >> $conf <<LINE
$localport>$remotehost:$remoteport
LINE
}
}
rmDnat(){
local localport=
echo -n "本地端口号:" ;read localport
sed -i "/^$localport>.*/d" $conf
rmIptablesNat $localport
#删除临时文件
rm -f $base/${1}IP
echo "done!"
}
testVars(){
local localport=$1
local remotehost=$2
local remoteport=$3
# 判断端口是否为数字
local valid=
echo "$localport"|[ -n "`sed -n '/^[0-9][0-9]*$/p'`" ] && echo $remoteport |[ -n "`sed -n '/^[0-9][0-9]*$/p'`" ]||{
# echo -e "${red}本地端口和目标端口请输入数字!!${black}";
return 1;
}
# 检查输入的不是IP
if [ "$(echo $remotehost |grep -E -o '([0-9]{1,3}[\.]){3}[0-9]{1,3}')" != "" ];then
local isip=true
local remote=$remotehost
# echo -e "${red}警告:你输入的目标地址是一个ip!${black}"
return 2;
fi
}
lsDnat(){
arr1=(`cat $conf`)
for cell in ${arr1[@]}
do
arr2=(`echo $cell|tr ":" " "|tr ">" " "`) #arr2=16 REJECT 0.0.0.0/0
# 过滤非法的行
[ "${arr2[2]}" != "" -a "${arr2[3]}" = "" ]&& testVars ${arr2[0]} ${arr2[1]} ${arr2[2]}&&{
echo "转发规则: ${arr2[0]}>${arr2[1]}:${arr2[2]}"
}
done
}
addSnat(){
local localport=
local remoteport=
local remotehost=
echo -n "本地端口号:" ;read localport
echo -n "远程端口号:" ;read remoteport
# echo $localport $remoteport
# 判断端口是否为数字
echo "$localport"|[ -n "`sed -n '/^[0-9][0-9]*$/p'`" ] && echo $remoteport |[ -n "`sed -n '/^[0-9][0-9]*$/p'`" ]||{
echo -e "${red}本地端口和目标端口请输入数字!!${black}"
return 1;
}
echo -n "目标IP:" ;read remotehost
# 检查输入的不是IP
if [ "$remotehost" = "" -o "$(echo $remotehost |grep -E -o '([0-9]{1,3}[\.]){3}[0-9]{1,3}')" != "" ];then
rmIptablesNat $localport
## 建立新的中转规则
iptables -t nat -A PREROUTING -p tcp --dport $localport -j DNAT --to-destination $remotehost:$remoteport
iptables -t nat -A PREROUTING -p udp --dport $localport -j DNAT --to-destination $remotehost:$remoteport
iptables -t nat -A POSTROUTING -p tcp -d $remotehost --dport $remoteport -j SNAT --to-source $localIP
iptables -t nat -A POSTROUTING -p udp -d $remotehost --dport $remoteport -j SNAT --to-source $localIP
else
echo 请输入一个IP
return 1
fi
}
rmSnat(){
local localport=
echo -n "本地端口号:" ;read localport
echo "$localport"|[ -n "`sed -n '/^[0-9][0-9]*$/p'`" ] &&rmIptablesNat $localport
}
echo -e "${red}你要做什么呢(请输入数字)?Ctrl+C 退出本脚本${black}"
select todo in 增加到域名的转发 删除到域名的转发 增加到IP的转发 删除到IP的转发 列出所有到域名的转发 查看iptables转发规则
do
case $todo in
增加到域名的转发)
addDnat
#break
;;
删除到域名的转发)
rmDnat
#break
;;
增加到IP的转发)
addSnat
#break
;;
删除到IP的转发)
rmSnat
#break
;;
列出所有到域名的转发)
lsDnat
;;
查看iptables转发规则)
echo "###########################################################"
iptables -L PREROUTING -n -t nat --line-number
iptables -L POSTROUTING -n -t nat --line-number
echo "###########################################################"
;;
*)
echo "如果要退出,请按Ctrl+C"
;;
esac
done