-
Notifications
You must be signed in to change notification settings - Fork 31
/
forward.sh
executable file
·124 lines (116 loc) · 2.56 KB
/
forward.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
#!/bin/sh
# Simple script to enable / disable IP forwarding
case "${OSTYPE}" in
darwin*)
WAN_IF="en0"
LAN_IF=""
;;
linux*)
WAN_IF="eth0"
LAN_IF="eth1"
;;
*)
WAN_IF=""
LAN_IF=""
;;
esac
# Show usage information
usage()
{
NAME=`basename $0`
cat <<EOT
$NAME [options] <on|off>
Enable or disable IP forwarding
-h Print this help message
-i INTERFACE WAN interface name (default: ${WAN_IF})
-j INTERFACE LAN interface name (default: ${LAN_IF})
EOT
}
ENABLE=0
# Parse the command line
while [ $# -ge 0 ]; do
case "$1" in
-h)
usage
exit 0
;;
-i)
shift
WAN_IF=$1
;;
-j)
shift
LAN_IF=$1
;;
-*)
usage
echo "Unsupported option: $1"
exit 1
;;
on)
ENABLE=1
;;
off)
ENABLE=0
;;
'')
break
;;
*)
usage
echo "Unsupported command: $1"
exit 1
;;
esac
shift
done
if [ -z "${WAN_IF}" ]; then
echo "Undefined WAN interface" >&2
exit 1
fi
if [ -z "${LAN_IF}" ]; then
echo "Undefined LAN interface" >&2
exit 1
fi
UID=`id -u`
if [ ${UID} -ne 0 ]; then
echo "Superuser privileges are required (use sudo)" >&2
exit 1
fi
case "${OSTYPE}" in
darwin*)
if [ ${ENABLE} -eq 1 ]; then
echo "Enabling IP forwarding through interface ${WAN_IF}"
sysctl -w net.inet.ip.forwarding=1
pfctl -F all -f /etc/pf.conf
conf=`mktemp`
echo "nat on ${WAN_IF} from ${LAN_IF}:network to any -> (${WAN_IF})" > \
"${conf}"
pfctl -e -f "${conf}"
rm "${conf}"
else
echo "Disabling IP forwarding"
pfctl -F all -f /etc/pf.conf
sysctl -w net.inet.ip.forwarding=0
fi
;;
linux*)
if [ -z "${LAN_IF}" ]; then
echo "Unknown LAN interface" >&2
exit 1
fi
if [ ${ENABLE} -eq 1 ]; then
echo "Enabling IP forwarding through interface $WAN_IF"
iptables -t nat -A POSTROUTING -o ${WAN_IF} -j MASQUERADE
iptables -A FORWARD -i ${LAN_IF} -j ACCEPT
echo 1 > /proc/sys/net/ipv4/ip_forward
else
echo "Disabling IP forwarding"
echo 0 > /proc/sys/net/ipv4/ip_forward
fi
;;
*)
echo "Forward mode for OS '${OSTYPE}' is not supported yet" >&2
exit 1
;;
esac