-
Notifications
You must be signed in to change notification settings - Fork 12
/
setup-cjdns-networking.mustache
456 lines (377 loc) · 11.7 KB
/
setup-cjdns-networking.mustache
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#!/bin/ash
export LC_ALL=en_US.UTF-8
# TODO
# * dns und gateway vom dhcp-server übernehmen, erst setzen, wenn ip erhalten
# vars
remoteaddresses="{{#peerings}}{{ip}} {{/peerings}}{{#missioncontrol}}{{ip}} {{/missioncontrol}}"
internethosts="91.219.237.212 8.8.8.8"
orggatewayfile="/tmp/cjdns_org_gw"
network_profile="$(cat /etc/enigmabox/network-profile)"
if [[ "$network_profile" = "alix" ]]; then
clearnet_interface="eth0"
internal_interfaces="eth1 eth2"
fi
if [[ "$network_profile" = "apu" ]]; then
clearnet_interface="eth2"
internal_interfaces="eth1 eth0"
fi
if [[ "$network_profile" = "raspi" ]]; then
clearnet_interface="eth1"
internal_interfaces="eth0"
fi
ping="ping -c 5 -W 5"
cjdns_request_tries="/tmp/cjdns_request_tries"
netstat_file="/tmp/netstat"
pidfile="/tmp/setup-cjdns-networking.pid"
opmode="{{wlan_opmode}}"
ssid="{{wlan_ssid}}"
wep_pass="{{wlan_pass}}"
security="{{wlan_security}}"
dynamic_output="/tmp/dynamic_output"
{{#if_internet_gateway}}
request_internet="yes"
{{/if_internet_gateway}}
{{^if_internet_gateway}}
request_internet="no"
{{/if_internet_gateway}}
# check if its already running
if [[ "$1" != "startwifi" ]]; then
kill -0 $(cat "$pidfile" 2> /dev/null) 2> /dev/null
if [[ "$?" == "0" ]]; then
echo "script is already running"
exit 0
fi
echo $$ > "$pidfile"
fi
# functions
e() {
echo 1>&2
echo 1>&2
echo "$1" 1>&2
}
startwifi() {
echo "please wait, configuring system..." > "$dynamic_output"
/usr/sbin/cfengine-apply &> /dev/null
echo "done" > "$dynamic_output"
/usr/sbin/setup-cjdns-networking &> "$dynamic_output"
exit 0 #the script should end here
}
dhcp() {
ifconfig "$clearnet_interface" up
if [[ $(ifconfig "$clearnet_interface" | grep 'inet addr' | wc -l) -eq 0 ]]; then
e "dhcp request $clearnet_interface"
udhcpc -i "$clearnet_interface" --now
fi
if [[ $(route -n | grep ^0.0.0.0 | wc -l) -eq 0 ]]; then
e "dhcp request $clearnet_interface"
udhcpc -i "$clearnet_interface" --now
fi
}
start_wpa() {
e "start WPA session"
ifconfig wlan0 down
killall wpa_supplicant
# make sure wpa_supplicant is absent
rm "/var/run/wpa_supplicant/wlan0" 2> /dev/null
sleep 2
ifconfig wlan0 up
wpa_supplicant -i wlan0 -D wext -c /etc/wpa_supplicant/wpa_supplicant.conf -B
sleep 2
}
start_wep() {
{{#if_wlan_pass}}
e "start WEP session"
ifconfig wlan0 up
iwconfig wlan0 essid "$ssid"
iwconfig wlan0 key "d:0:$wep_pass"
{{/if_wlan_pass}}
{{^if_wlan_pass}}
e "connecting to AP"
ifconfig wlan0 up
iwconfig wlan0 essid "$ssid"
{{/if_wlan_pass}}
}
request_cjdns_internet() {
try=$(cat "$cjdns_request_tries" 2> /dev/null)
try=$(($try+1))
echo "$try" > "$cjdns_request_tries"
# try another countryserver on the 2nd try
if [[ "$try" -eq 2 ]]; then
e "switching to an alternative server"
curl http://127.0.0.1:8000/api/v1/set_next_country &> /dev/null
/usr/sbin/cfengine-apply
fi
# request server.json on the 3rd try
if [[ "$try" -eq 3 ]]; then
e "request server.json"
/usr/sbin/request-serverjson
/usr/sbin/cfengine-apply
fi
# request cjdns internet via script on the 4th try
if [[ "$try" -eq 4 ]]; then
e "request cjdns internet"
/usr/sbin/request-internet
fi
# reset counter after 5th try, start from the beginning
if [[ "$try" -ge 5 ]]; then
echo 1 > "$cjdns_request_tries"
fi
}
start_cjdns() {
if [[ "$(/etc/init.d/cjdns status)" != "running" ]]; then
e "starting cjdns"
/etc/init.d/cjdns start
fi
}
restart_cjdns() {
e "restarting cjdns"
/etc/init.d/cjdns restart
}
portforwardings() {
{{#portforwardings}}
start_portforwarding "{{port}}" "{{mac}}" "{{dstport}}"
{{/portforwardings}}
sleep 1
# kill disabled tunnels
for af in $(netstat -tulpen | grep socat | awk '{ print $4 }' | cut -d: -f4 | sort | uniq); do
if [[ $(echo "{{#portforwardings}}{{port}} {{/portforwardings}}" | grep "$af " | wc -l) -eq 0 ]]; then
echo "stopping port forwarding $af"
kill $(netstat -tulpen | grep ":::$af " | awk '{ print $7 }' | cut -d/ -f1) &> /dev/null
kill $(netstat -tulpen | grep ":::$af " | awk '{ print $6 }' | cut -d/ -f1) &> /dev/null
fi
done
}
start_portforwarding() {
port=$1
mac=$2
dstport=$3
# don't start forwarding if ip is not up
[[ $(mactoip "$mac") == "" ]] && return
if [[ "$(netstat -tulpen | grep socat | grep tcp | grep :::$port | wc -l)" -eq 0 ]]; then
echo "starting port forwarding $port TCP"
/usr/bin/socat TCP6-LISTEN:$port,reuseaddr,fork TCP:$(mactoip "$mac"):$dstport &
fi
if [[ "$(netstat -tulpen | grep socat | grep udp | grep :::$port | wc -l)" -eq 0 ]]; then
echo "starting port forwarding $port UDP"
/usr/bin/socat UDP6-LISTEN:$port,reuseaddr,fork UDP:$(mactoip "$mac"):$dstport &
fi
}
mactoip() {
cat /proc/net/arp | grep "$1" | cut -d' ' -f1
}
get_vpn_gateway() {
ifconfig tun0 2> /dev/null | grep "inet addr" | cut -d: -f2 | awk '{ print $1 }'
}
get_original_gateway() {
if [[ -f "$orggatewayfile" ]]; then
org_gw=$(cat "$orggatewayfile")
else
org_gw=$(route -n | grep '^0.0.0.0' | awk '{ print $2 }')
echo "$org_gw" > "$orggatewayfile"
fi
echo "$org_gw"
}
gateway_is_up() {
vpn_gateway=$(get_vpn_gateway)
if [[ "$vpn_gateway" != "" ]]; then
echo true
fi
}
we_have_internet() {
for internethost in $internethosts; do
if [[ "$($ping "$internethost" | grep 'bytes from')" ]]; then
echo true
break
fi
done
}
interface_dhcp_success() {
if [[ "$(ifconfig "$clearnet_interface" | grep 'inet addr' | wc -l)" -gt 0 ]]; then
echo true
fi
}
interface_dhcp_conflict() {
clearnet_range=$(ifconfig "$clearnet_interface" | grep 'inet addr' | cut -d. -f3)
for interface in $internal_interfaces; do
if [[ "$(ifconfig "$interface" | grep 'inet addr' | cut -d. -f3)" == "$clearnet_range" ]]; then
echo true
break
fi
done
}
mtu() {
if [[ "$(ifconfig tun0 2> /dev/null | grep -i mtu | awk '{ print $6 }' | cut -d: -f 2)" -ne 1300 ]]; then
e "setting mtu"
ifconfig tun0 mtu 1300
fi
}
original_gateway() {
original_gateway=$(get_original_gateway)
for remoteaddress in $remoteaddresses; do
if [[ "$(route -n | egrep "$remoteaddress.*?$original_gateway" | wc -l)" -eq 0 ]]; then
e "setting route $remoteaddress via $original_gateway dev $clearnet_interface"
route add "$remoteaddress" gw "$original_gateway" "$clearnet_interface"
fi
done
}
defaultroute() {
vpn_gateway=$(get_vpn_gateway)
if [[ "$(route -n | egrep "0.0.0.0.*?$vpn_gateway" | wc -l)" -eq 0 ]]; then
e "setting defaultroute"
route del default
route add default gw "$vpn_gateway" tun0
fi
if [[ "$(route -A inet6 | egrep '::/0' | wc -l)" -eq 0 ]]; then
e "setting defaultroute ipv6"
route -A inet6 del default
route -A inet6 add default tun0
fi
}
set_network_parameters() {
mtu
portforwardings
original_gateway
defaultroute
(
for i in $(seq 1 60); do
sleep 1
defaultroute
done
) &
}
check_for_internet() {
# check for internet. if only one server with a direct route is pingable,
# we have an internet connection
for remoteaddress in $remoteaddresses; do
if [[ "$($ping "$remoteaddress" | grep 'bytes from')" ]]; then
echo true
break
fi
done
}
set_status() {
key=$1
val=$2
echo "$val" > "$netstat_file-$key"
}
# params
[[ "$1" == "startwifi" ]] && startwifi "$2"
# logic
# ensure dhcpd is running
if [[ $( pidof dhcpd | wc -l ) -eq 0 ]]; then
/etc/init.d/dhcpd restart
fi
# ensure radvd is running
if [[ "$(pidof radvd | wc -l)" -eq 0 ]]; then
/etc/init.d/radvd restart
fi
# setup wifi if available
if [[ -e "/sys/class/net/wlan0" ]]; then
e "wifi detected"
if [[ "$opmode" = "mesh" ]]; then
e "opmode: mesh"
# check if wlan0 has already started
if [[ "$(iwconfig wlan0 | grep 'ESSID' | grep 'cjdns' | wc -l)" -eq 0 \
|| "$(iwconfig wlan0 | grep 'Mode:' | grep 'Ad-Hoc' | wc -l)" -eq 0 ]]; then
e "starting ad-hoc mesh"
ifconfig wlan0 down
iwconfig wlan0 mode ad-hoc
iwconfig wlan0 essid cjdns
ifconfig wlan0 up
restart_cjdns
else
e "ad-hoc mesh is running fine"
fi
fi
if [[ "$opmode" = "client" ]]; then
e "opmode: client"
clearnet_interface=wlan0
# check if wlan0 has already started
if [[ "$(ifconfig wlan0 | grep 'inet addr' | wc -l)" -eq 0 \
|| "$(iwconfig wlan0 | grep 'Access Point: Not-Associated' | wc -l)" -gt 0 ]]; then
if [[ "$security" = "WPA" ]]; then
start_wpa
else
start_wep
fi
else
e "wlan client is running fine"
fi
#TODO: connect to unencrypted wifi
fi
fi
# set MWI status for the phone
if [[ $(/usr/bin/find /box/vmail/mail/Maildir/new/ -type f 2> /dev/null | wc -l) -gt 0 ]]; then
/usr/sbin/asterisk -rx 'sip notify set-mwi 100' &> /dev/null
else
/usr/sbin/asterisk -rx 'sip notify clear-mwi 100' &> /dev/null
fi
if [[ "$(gateway_is_up)" == "true" ]]; then
set_network_parameters
e "checking internet connectivity over cjdns"
if [[ "$(we_have_internet)" == "true" ]]; then
echo "We have internet. Good."
set_status "dhcp" 1
set_status "internet" 1
set_status "cjdns" 1
set_status "cjdns_internet" 1
rm "$cjdns_request_tries" 2> /dev/null
exit
fi
fi
echo "No internet via cjdns. Checking for regular internet connection..."
set_status "dhcp" 0
rm "$netstat_file-dhcp_conflict" 2> /dev/null
set_status "internet" 0
set_status "cjdns" 0
set_status "cjdns_internet" 0
# request dhcp
dhcp
if [[ "$(interface_dhcp_success)" == "true" ]]; then
set_status "dhcp" 1
fi
if [[ "$(interface_dhcp_conflict)" == "true" ]]; then
ifconfig "$clearnet_interface" | grep 'inet addr' | cut -d: -f2 | cut -d' ' -f1 > "$netstat_file-dhcp_conflict"
exit
fi
wehaveinternet="no"
if [[ "$(check_for_internet)" == "true" ]]; then
set_status "internet" 1
wehaveinternet="yes"
fi
if [[ "$wehaveinternet" == "yes" && "$request_internet" == "yes" ]]; then
request_cjdns_internet
restart_cjdns
set_status "cjdns" 1
for i in $(seq 60 -1 1); do
echo "waiting $i seconds for gateway to come up..."
if [[ "$(gateway_is_up)" == "true" ]]; then
e "gateway is up."
set_network_parameters
e "checking internet connectivity over cjdns"
if [[ "$(we_have_internet)" == "true" ]]; then
echo "We have internet. Good."
set_status "dhcp" 1
set_status "internet" 1
set_status "cjdns" 1
set_status "cjdns_internet" 1
rm "$cjdns_request_tries" 2> /dev/null
exit
else
echo "Gateway is up, but no internet. Requesting..."
/usr/sbin/request-internet
exit
fi
fi
sleep 1
done
else
e "no internet via cjdns."
# just ensure that cjdns is running, but DO NOT restart it!
# since local phone calls may be active.
start_cjdns
set_status "cjdns" 1
# 6. Nov. 2018: Possible fix for boxes that won't come up
/usr/sbin/request-serverjson
/usr/sbin/cfengine-apply
fi