This repository was archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtunnelport.sh
executable file
·91 lines (82 loc) · 2.06 KB
/
tunnelport.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
#!/bin/bash
if [ -z $2 ]; then
echo "call: $0 cid port [hostport]"
echo "eg: $0 3 80"
echo "to drop a port use negative number: $0 3 -80"
echo "to specify a port directly: $0 120 6667 6667"
exit
fi
SCRIPTSPATH=`dirname ${BASH_SOURCE[0]}`
source $SCRIPTSPATH/lib.sh
interface=$(getOutwardInterface)
echo "outward interface:" $interface
HostIP=$(getIPOfInterface $interface)
echo "outward IP address:" $HostIP
interfaceBridge=$(getBridgeInterface)
echo "bridge interface:" $interfaceBridge
bridgeAddress=$(getIPOfInterface $interfaceBridge)
echo "bridge address:" $bridgeAddress
cid=$1
guestip=${bridgeAddress:0: -2}.$cid
echo "guestip: "$guestip
if [ -z $guestip ]
then
exit -1
fi
port=$2
remove=0
# remove existing routes
if [ $port -lt 0 ]
then
port=$((-1 * $port))
remove=1
fi
if [ -z $3 ]
then
firstdigit=${port:0:1}
hostport=$(($firstdigit * 1000 + $cid))
else
hostport=$3
fi
# Ubuntu
rules=/etc/iptables.rules
if [ ! -f $rules ]
then
# Fedora
rules=/etc/sysconfig/iptables
fi
# first save the iptables
if [ -f /etc/network/if-post-down.d/iptablessave ]
then
/etc/network/if-post-down.d/iptablessave
else
iptables-save > $rules
fi
if [ $remove -eq 1 ]
then
if [ ! -z "`cat $rules | grep "to-destination ${guestip}:${port}"`" ]
then
iptables -t nat -D PREROUTING -p tcp -d ${HostIP} --dport $hostport -i ${interface} -j DNAT --to-destination ${guestip}:$port
echo "dropping rule ${HostIP}:$hostport => ${guestip}:${port}"
fi
else
if [ ! -z "`cat $rules | grep "PREROUTING" | grep "dport $hostport "`" ]
then
echo "there is already a mapping for port " $hostport
else
iptables -t nat -A PREROUTING -p tcp -d ${HostIP} --dport $hostport -i ${interface} -j DNAT --to-destination ${guestip}:$port
echo "forwarding ${HostIP}:$hostport => ${guestip}:${port}"
fi
fi
if [ -f /etc/network/if-post-down.d/iptablessave ]
then
/etc/network/if-post-down.d/iptablessave
else
iptables-save > $rules
systemctl status firewalld > /dev/null
if [ $? -eq 0 ]
then
# firewalld is running
firewall-cmd --runtime-to-permanent
fi
fi