-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhot
executable file
·142 lines (127 loc) · 2.44 KB
/
hot
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
#!/bin/bash
UP="up"
DOWN="down"
ACTION=${ACTION:=$UP}
WAN=${WAN:=eth0}
LAN=${LAN:=wlan0}
LAN_IPADDR=172.16.0.1
DHCP_START=${DHCP_START:=100}
DHCP_END=${DHCP_END:=200}
DHCP_LEASE=${DHCP_LEASE:=3600}
print_usage() {
cat - <<-EOF
hot -o <wan interface> -i <lan interface> -u -d -l -w
defaults are "lan up eth0"
eg: hot . down == hot lan down eth0
EOF
}
am_root(){
if [[ `whoami` != "root" ]] ;then
echo "Must be root to execute!"
exit 1
fi
}
iptables_rules_up(){
/usr/sbin/iptables -t nat -A POSTROUTING -o $LAN -j MASQUERADE || return 1
/usr/sbin/iptables -A FORWARD -i $LAN -o $WAN -j ACCEPT || return 1
return 0
}
iptables_rules_down(){
/usr/sbin/iptables -t nat -D POSTROUTING -o $LAN -j MASQUERADE || return 1
/usr/sbin/iptables -D FORWARD -i $LAN -o $WAN -j ACCEPT || return 1
return 0
}
_forwarding(){
if [[ $ACTION == $UP ]] ;then
val=1
else
val=0
fi
# ensure ipv4 forwarding set
sysctl net.ipv4.ip_forward=$val || return 1
# ensure ipv6 forwarding set
sysctl -w net.ipv6.conf.all.forwarding=$val || return 1
return 0
}
_iface(){
if [[ $ACTION == $UP ]] ;then
echo -n "loading interface ..."
else
echo -n "putting interfaces down ..."
fi
if ( ifconfig $LAN $ACTION ) && ( ifconfig $WAN $ACTION ) ;then
echo "they is $ACTION now"
else
echo "failed to $ACTION interfaces"
return 1
fi
return 0
}
_dnsmasq(){
if [[ $ACTION == $UP ]] ;then
echo -n "running dnsmasq ..."
dnsmasq --interface=$LAN --dhcp-range="$DHCP_START,$DHCP_END,$DHCP_LEASE" || ( echo "failed to start dnsmasq" && return 1 )
else
echo -n "running dnsmasq ..."
killall -w dnsmasq || ( echo "failed to kill dnsmasq" && return 1 )
fi
return 0
}
_iptables(){
if [[ $ACTION == $UP ]] ;then
echo -n "loading iptables rules ..."
iptables_rules_up || ( echo "failed to set iptables rules" && return 1 )
else
echo -n "removing iptables rules ..."
iptables_rules_down || ( echo "failed to remove iptables rules" && return 1 )
fi
return 0
}
hot(){
#am_root
_forwarding
_iface
_dnsmasq
_iptables
}
# handle args
handle_args(){
index=0
while getopts "hi:o:udlw" OPTION ; do
case $OPTION in
h)
print_usage
return 1
;;
i)
LAN=$OPTARG
;;
o)
WAN=$OPTARG
;;
u)
ACTION=$UP
;;
d)
ACTION=$DOWN
;;
l)
LAN=$LAN
WAN=$WAN
;;
w)
lan_orig=$LAN
LAN=$WAN
WAN=$lan_orig
;;
*)
print_usage
return 1
;;
esac
done
return 0
}
if handle_args ;then
hot
fi