]> git.donarmstrong.com Git - bin.git/blob - iodine-jigger
add reset usb bus command
[bin.git] / iodine-jigger
1 #! /bin/bash
2
3 ### Script to set up an iodine tunnel route traffic through it
4 ###
5 ### Copyright 2008 Barak A. Pearlmutter <bap@debian.org>
6 ###
7 ### License: MIT
8 ###
9 ### Permission to use, copy, modify, and distribute this software for
10 ### any purpose with or without fee is hereby granted, provided that
11 ### the above copyright notice and this permission notice appear in
12 ### all copies.
13 ###
14 ### THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
15 ### WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
16 ### WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
17 ### AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
18 ### CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
19 ### LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
20 ### NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 ### CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23 ## Cause script to bail immediately on failed command
24 set -e
25
26 ## Options for user to set.
27
28 ## Minimal customization: put the two lines
29 ##  subdomain=your.tunnel.sub.domain
30 ##  passed=password_for_that_tunnel
31 ## in the file /etc/default/iodine-client.
32
33 echo "${iodine_client_rc:=/etc/default/iodine-client}" > /dev/null
34
35 if [ -r ${iodine_client_rc} ]; then
36     . ${iodine_client_rc}
37 else
38     echo WARNING: Cannot read ${iodine_client_rc}
39 fi
40
41 if [ -z ${subdomain} ]; then
42     read -p "DNS tunnel DNS subdomain: " subdomain
43 fi
44
45 if [ -z ${subdomain} ]; then
46     echo ERROR: Must set subdomain.
47     exit 1
48 fi
49
50 if [ -z ${passwd} ]; then
51     read -p "Password for DNS tunnel over ${subdomain}: " passwd
52 fi
53
54 ## This is a host name used for testing DNS and for pinging
55 echo "${testhost:=slashdot.org}"                > /dev/null
56
57 ## Set if local network should be taken down and then up
58 echo "${bounce_localnet:=true}"                 > /dev/null
59
60 ## Set for testing network availability via ping at various points
61 echo "${test_ping_localnet:=true}"              > /dev/null
62 echo "${test_ping_tunnel:=true}"                > /dev/null
63 echo "${test_ping_final:=true}"                 > /dev/null
64
65 ## Set if the script cannot find and then incorrectly guesses the
66 ## local network router
67 echo "${default_router}"                        > /dev/null
68
69 ## Set if script uses the wrong hardware interface
70 echo "{interface}"                              > /dev/null
71
72 ## Set if the script should continue even if a command fails.
73 ## Used to test script when running as non-root.
74 if [ $(whoami) = root ]; then
75     echo "${continue_on_error:=false}"          > /dev/null
76 else
77     echo "${continue_on_error:=true}"           > /dev/null
78 fi
79
80 ## DEBIAN PACKAGES TO INSTALL: these are needed to run this script
81 ##  iodine (for /usr/sbin/iodine)
82 ##  iproute (for /bin/ip)
83 ##  ipcalc (for /usr/bin/ipcalc)
84 ##  dnsutils (for /usr/bin/dig)
85 ##  fping (for /usr/bin/fping)
86
87 ## The default tunnel MTU is 1024.
88 ## If local DNS server restricts to 512 byte packets then do this:
89 # ifconfig ${d} mtu 220
90
91 ## TO DO
92 ## - avoid double ping when DNS server and local router are the same
93 ## - option to not kill existing iodine DNS tunnels, in case there
94 ##   are meant to be more than one
95 ## - sanify check whether default_router is on local network
96
97 echo ==== Creating IP-over-DNS tunnel over local network connection...
98
99
100 ## Find a network interface
101
102 if [ -z ${interface} ]; then
103     interface=$(tail --lines=+3 /proc/net/wireless \
104         | head -1 | tr -d : | awk '{print $1}')
105 fi
106
107 if [ -z ${interface} ]; then
108     interface=$(ifconfig -a | egrep '^[^ ].*encap:Ethernet' \
109         | head -1 | awk '{print $1}')
110 fi
111
112 if [ -z ${interface} ]; then
113     echo ERROR: No network interface found.
114     exit 1
115 fi
116
117 echo ==== Local network interface: ${interface}
118
119 ## Down any existing DNS tunnel (wish there were "approved" way to do this)
120
121 echo ==== Killing existing DNS tunnels...
122 if killall --quiet --wait --verbose --signal HUP iodine; then
123     sleep 2
124 fi
125
126 ## Stabilize local network
127
128 if ${bounce_localnet}; then
129     echo ==== Bouncing local network connection...
130     ifdown --force ${interface} || true
131     ifup ${interface} || ${continue_on_error}
132 fi
133
134 ## Fetch some information about the local network
135
136 addr=$(ip -4 addr show dev ${interface} scope global \
137     | tail -1 | awk '{print $2}')
138 prefix_len=$(echo ${addr} | sed 'sX^.*/XX')
139 local_net=$(ipcalc --nobinary ${addr} | awk '$1=="Network:" {print $2}')
140
141 echo ==== Local address: ${addr}
142 echo ==== Local network: ${local_net}
143
144 router=$(ip -4 route list dev ${interface} \
145     | awk '$1=="default" {print $3}' | head -1)
146 if [ -z ${router} ]; then
147     ## This can happen when the default local route is already deleted
148     if [ -z ${default_router} ]; then
149         echo WARNING: no default route, guessing local router IP address.
150         ## Minimum address on local net is usually right
151         router=$(ipcalc --nobinary ${addr} | awk '$1=="HostMin:" {print $2}')
152     else
153         echo WARNING: no default route, using configured default router.
154         ## But sometimes need to hardwire...
155         router=${default_router}
156     fi
157 fi
158
159 echo ==== Local network router: ${router}
160
161 ## Test DNS service
162
163 testhost_ip=$(dig +short -t A -q ${testhost})
164 if [ -z ${testhost_ip} ]; then
165     echo WARNING: Failure on DNS lookup of ${testhost}.
166 fi
167
168 ## fetch DNS servers
169
170 nameservers=$(awk '$1=="nameserver" {print $2}' /etc/resolv.conf)
171 if [ -n "${nameservers}" ]; then
172     echo ==== DNS servers: ${nameservers}
173 else
174     echo ERROR: No DNS servers found.
175     exit 1
176 fi
177
178 ## Test if local network is up
179
180 if ${test_ping_localnet}; then
181     echo ==== Ping test of  local network router and DNS servers...
182     fping -C1 ${router} ${nameservers} \
183         || echo WARNING: Ping test failed.
184 fi
185
186 ## Add point-to-point routes for any non-local DNS servers
187
188 for n in ${nameservers}; do
189     n_net=$(ipcalc --nobinary ${n}/${prefix_len} | awk '$1=="Network:" {print $2}')
190     if [ "${n_net}" != "${local_net}" ]; then
191         echo ==== Adding point-to-point route for DNS server ${n}
192         ip -4 route add ${n}/32 via ${router} || ${continue_on_error}
193     fi
194 done
195
196 ## Bring up DNS tunnel
197
198 echo ==== Creating IP-over-DNS tunnel...
199 iodine -P ${passwd} ${subdomain} || ${continue_on_error}
200
201 ## Find DNS tunnel interface
202
203 tunnel_interface=$(ifconfig -a | egrep '^dns' | awk '{print $1}' | head -1)
204 if [ -z "${tunnel_interface}" ]; then
205     echo WARNING: Cannot find DNS tunnel interface, using default.
206     tunnel_interface=dns0
207 fi
208 echo ==== DNS tunnel interface: ${tunnel_interface}
209
210 ## Figure out router at other end of tunnel, assuming router uses final octet .1
211 ## (There should be some way to get this information out of iodine, since
212 ## it *prints* it as it sets up the tunnel, so it does know it.)
213
214 tunnel_remote=$(ip -4 address show dev ${tunnel_interface} \
215     | awk '$1=="inet" {print gensub("[.][0-9]*/.*", ".1", 1, $2)}' | head -1)
216
217 if [ -z ${tunnel_remote} ]; then
218     echo ERROR: Cannot find DNS tunnel remote endpoint.
219     ${continue_on_error}
220     ## set something random if debugging
221     echo WARNING: Confabulating DNS tunnel remote endpoint.
222     tunnel_remote=192.168.253.1
223 fi
224
225 echo ==== DNS tunnel remote endpoint: ${tunnel_remote}
226
227 if ${test_ping_tunnel}; then
228     echo ==== Ping test of local router, nameserver, and DNS tunnel...
229     fping -C1 ${router} ${nameservers} ${tunnel_remote} \
230         || echo WARNING: Ping test failed.
231 fi
232
233 ## Modify routing table to send trafic via DNS tunnel
234
235 echo ==== Setting default route through DNS tunnel...
236
237 ## Remove default route via local router
238 ip -4 route del default via ${router} || ${continue_on_error}
239 ## Add default via tunnel
240 ip -4 route add default via ${tunnel_remote} || ${continue_on_error}
241
242 ## Test if all is well
243
244 if ${test_ping_final}; then
245     echo ==== Ping test of local router, nameserver, DNS tunnel, external test host...
246     fping -C1 ${router} ${nameservers} ${tunnel_remote} ${testhost_ip:-${testhost}} \
247         || echo WARNING: Ping test failed.
248 fi