forked from eblot/pybootd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforward.sh
executable file
·116 lines (109 loc) · 2.39 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
#!/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 "Unknown WAN interface" >&2
exit 1
fi
UID=`id -u`
if [ ${UID} -ne 0 ]; then
echo "Superuser privileges are required" >&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
natd -interface ${WAN_IF}
ipfw add divert natd ip from any to any via ${WAN_IF}
else
echo "Disabling IP forwarding"
ipfw delete `sudo ipfw show | grep divert | cut -d' ' -f1`
killall natd
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