forked from arloor/iptablesUtils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
natcfg.sh
308 lines (266 loc) · 9.19 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
red="\033[31m"
black="\033[0m"
base=/etc/dnat
mkdir $base 2>/dev/null
conf=$base/conf
touch $conf
# wget wget --no-check-certificate -qO natcfg.sh http://blog.arloor.com/sh/iptablesUtils/natcfg.sh && bash natcfg.sh
clear
echo "#############################################################"
echo "# Usage: setup iptables nat rules for domian/ip #"
echo "# Website: http://www.arloor.com/ #"
echo "# Author: ARLOOR <[email protected]> #"
echo "# Github: https://github.com/arloor/iptablesUtils #"
echo "#############################################################"
echo
setupService(){
cat > /usr/local/bin/dnat.sh <<"AAAA"
#! /bin/bash
[[ "$EUID" -ne '0' ]] && echo "Error:This script must be run as root!" && exit 1;
base=/etc/dnat
mkdir $base 2>/dev/null
conf=$base/conf
firstAfterBoot=1
lastConfig="/iptables_nat.sh"
lastConfigTmp="/iptables_nat.sh_tmp"
####
echo "正在安装依赖...."
yum install -y bind-utils &> /dev/null
apt install -y dnsutils &> /dev/null
echo "Completed:依赖安装完毕"
echo ""
####
turnOnNat(){
# 开启端口转发
echo "1. 端口转发开启 【成功】"
sed -n '/^net.ipv4.ip_forward=1/'p /etc/sysctl.conf | grep -q "net.ipv4.ip_forward=1"
if [ $? -ne 0 ]; then
echo -e "net.ipv4.ip_forward=1" >> /etc/sysctl.conf && sysctl -p
fi
#开放FORWARD链
echo "2. 开放iptbales中的FORWARD链 【成功】"
arr1=(`iptables -L FORWARD -n --line-number |grep "REJECT"|grep "0.0.0.0/0"|sort -r|awk '{print $1,$2,$5}'|tr " " ":"|tr "\n" " "`) #16:REJECT:0.0.0.0/0 15:REJECT:0.0.0.0/0
for cell in ${arr1[@]}
do
arr2=(`echo $cell|tr ":" " "`) #arr2=16 REJECT 0.0.0.0/0
index=${arr2[0]}
echo 删除禁止FOWARD的规则$index
iptables -D FORWARD $index
done
iptables --policy FORWARD ACCEPT
}
turnOnNat
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;
}
}
dnat(){
[ "$#" = "3" ]&&{
local localport=$1
local remote=$2
local remoteport=$3
cat >> $lastConfigTmp <<EOF
iptables -t nat -A PREROUTING -p tcp --dport $localport -j DNAT --to-destination $remote:$remoteport
iptables -t nat -A PREROUTING -p udp --dport $localport -j DNAT --to-destination $remote:$remoteport
iptables -t nat -A POSTROUTING -p tcp -d $remote --dport $remoteport -j SNAT --to-source $localIP
iptables -t nat -A POSTROUTING -p udp -d $remote --dport $remoteport -j SNAT --to-source $localIP
EOF
}
}
dnatIfNeed(){
[ "$#" = "3" ]&&{
local needNat=0
# 如果已经是ip
if [ "$(echo $2 |grep -E -o '([0-9]{1,3}[\.]){3}[0-9]{1,3}')" != "" ];then
local remote=$2
else
local remote=$(host -t a $2|grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}"|head -1)
fi
if [ "$remote" = "" ];then
echo Warn:解析失败
return 1;
fi
}||{
echo "Error: host命令缺失或传递的参数数量有误"
return 1;
}
echo $remote >$base/${1}IP
dnat $1 $remote $3
}
echo "3. 开始监听域名解析变化"
echo ""
while true ;
do
## 获取本机地址
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
echo "本机网卡IP [$localIP]"
cat > $lastConfigTmp <<EOF
iptables -t nat -F PREROUTING
iptables -t nat -F POSTROUTING
EOF
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]}"
dnatIfNeed ${arr2[0]} ${arr2[1]} ${arr2[2]}
}
done
lastConfigTmpStr=`cat $lastConfigTmp`
lastConfigStr=`cat $lastConfig`
if [ "$firstAfterBoot" = "1" -o "$lastConfigTmpStr" != "$lastConfigStr" ];then
echo '更新iptables规则[DOING]'
source $lastConfigTmp
cat $lastConfigTmp > $lastConfig
echo '更新iptables规则[DONE],新规则如下:'
echo "###########################################################"
iptables -L PREROUTING -n -t nat --line-number
iptables -L POSTROUTING -n -t nat --line-number
echo "###########################################################"
else
echo "iptables规则未变更"
fi
firstAfterBoot=0
echo '' > $lastConfigTmp
sleep 60
echo ''
echo ''
echo ''
done
AAAA
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
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 "目标域名/IP:" ;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
sed -i "s/^$localport.*/$localport>$remotehost:$remoteport/g" $conf
[ "$(cat $conf|grep "$localport>$remotehost:$remoteport")" = "" ]&&{
cat >> $conf <<LINE
$localport>$remotehost:$remoteport
LINE
}
echo "成功添加转发规则 $localport>$remotehost:$remoteport"
setupService
}
rmDnat(){
local localport=
echo -n "本地端口号:" ;read localport
sed -i "/^$localport>.*/d" $conf
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
}
echo -e "${red}你要做什么呢(请输入数字)?Ctrl+C 退出本脚本${black}"
select todo in 增加转发规则 删除转发规则 列出所有转发规则 查看当前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