2 export LC_ALL=en_US.UTF-8
5 # * dns und gateway vom dhcp-server übernehmen, erst setzen, wenn ip erhalten
9 remoteaddresses="{{#peerings}}{{ip}} {{/peerings}}{{#missioncontrol}}{{ip}} {{/missioncontrol}}"
11 orggatewayfile="/tmp/cjdns_org_gw"
13 network_profile="$(cat /etc/enigmabox/network-profile)"
14 [[ "$network_profile" = "alix" ]] && clearnet_interface="eth0"
15 [[ "$network_profile" = "apu" ]] && clearnet_interface="eth2"
16 [[ "$network_profile" = "raspi" ]] && clearnet_interface="eth1"
19 cjdns_request_tries="/tmp/cjdns_request_tries"
20 netstat_file="/tmp/netstat"
21 pidfile="/tmp/setup-cjdns-networking.pid"
22 opmode="{{wlan_opmode}}"
24 wep_pass="{{wlan_pass}}"
25 security="{{wlan_security}}"
26 dynamic_output="/tmp/dynamic_output"
28 {{#if_internet_gateway}}
29 request_internet="yes"
30 {{/if_internet_gateway}}
32 {{^if_internet_gateway}}
34 {{/if_internet_gateway}}
38 # check if its already running
39 if [[ "$1" != "startwifi" ]]; then
40 kill -0 $(cat "$pidfile" 2> /dev/null) 2> /dev/null
41 if [[ "$?" == "0" ]]; then
42 echo "script is already running"
60 echo "please wait, configuring system..." > "$dynamic_output"
61 /usr/sbin/cfengine-apply &> /dev/null
62 echo "done" > "$dynamic_output"
64 /usr/sbin/setup-cjdns-networking &> "$dynamic_output"
66 exit 0 #the script should end here
70 ifconfig "$clearnet_interface" up
72 if [[ $( route -n | grep ^0.0.0.0 | wc -l ) -eq 0 ]]; then
73 e "dhcp request $clearnet_interface"
74 udhcpc -i "$clearnet_interface" --now
82 killall wpa_supplicant
84 # make sure wpa_supplicant is absent
85 rm "/var/run/wpa_supplicant/wlan0" 2> /dev/null
90 wpa_supplicant -i wlan0 -D wext -c /etc/wpa_supplicant/wpa_supplicant.conf -B
99 iwconfig wlan0 essid "$ssid"
100 iwconfig wlan0 key "d:0:$wep_pass"
105 iwconfig wlan0 essid "$ssid"
109 request_cjdns_internet() {
110 try=$(cat "$cjdns_request_tries" 2> /dev/null)
112 echo "$try" > "$cjdns_request_tries"
114 # try another countryserver after the 2nd try
115 if [[ "$try" -ge 2 ]]; then
116 e "switching to an alternative server"
117 curl http://127.0.0.1:8000/api/v1/set_next_country &> /dev/null
118 /usr/sbin/cfengine-apply
121 # request cjdns internet via script after the 3rd try
122 if [[ "$try" -ge 3 ]]; then
123 e "request cjdns internet"
124 /usr/sbin/request-internet
129 if [[ "$(/etc/init.d/cjdns status)" != "running" ]]; then
131 /etc/init.d/cjdns start
137 /etc/init.d/cjdns restart
141 ifconfig tun0 2> /dev/null | grep "inet addr" | cut -d: -f2 | awk '{ print $1 }'
144 get_original_gateway() {
145 if [[ -f "$orggatewayfile" ]]; then
146 org_gw=$(cat "$orggatewayfile")
148 org_gw=$(route -n | grep '^0.0.0.0' | awk '{ print $2 }')
149 echo "$org_gw" > "$orggatewayfile"
155 vpn_gateway=$(get_vpn_gateway)
156 if [[ "$vpn_gateway" != "" ]]; then
161 interface_dhcp_success() {
162 if [[ "$(ifconfig "$clearnet_interface" | grep 'inet addr' | wc -l)" -gt 0 ]]; then
168 if [[ "$(ifconfig tun0 2> /dev/null | grep -i mtu | awk '{ print $6 }' | cut -d: -f 2)" -ne 1300 ]]; then
170 ifconfig tun0 mtu 1300
175 original_gateway=$(get_original_gateway)
176 for remoteaddress in $remoteaddresses; do
177 if [[ "$(route -n | egrep "$remoteaddress.*?$original_gateway" | wc -l)" -eq 0 ]]; then
178 e "setting route $remoteaddress via $original_gateway dev $clearnet_interface"
179 route add "$remoteaddress" gw "$original_gateway" "$clearnet_interface"
185 original_gateway=$(get_original_gateway)
186 vpn_gateway=$(get_vpn_gateway)
187 if [[ "$(route -n | egrep "0.0.0.0.*?$vpn_gateway" | wc -l)" -eq 0 ]]; then
188 e "setting defaultroute"
190 route add default gw "$vpn_gateway" tun0
194 set_network_parameters() {
200 check_for_internet() {
201 # check for internet. if only one server with a direct route is pingable,
202 # we have an internet connection
203 for remoteaddress in $remoteaddresses; do
204 if [[ "$($ping "$remoteaddress" | grep 'bytes from')" ]]; then
214 echo "$val" > "$netstat_file-$key"
220 [[ "$1" == "startwifi" ]] && startwifi "$2"
226 # ensure dhcpd is running
227 if [[ $( pidof dhcpd | wc -l ) -eq 0 ]]; then
228 /etc/init.d/dhcpd restart
231 # ensure radvd is running
232 if [[ "$(pidof radvd | wc -l)" -eq 0 ]]; then
233 /etc/init.d/radvd restart
236 # setup wifi if available
237 if [[ -e "/sys/class/net/wlan0" ]]; then
240 if [[ "$opmode" = "mesh" ]]; then
243 # check if wlan0 has already started
244 if [[ "$(iwconfig wlan0 | grep 'ESSID' | grep 'cjdns' | wc -l)" -eq 0 \
245 || "$(iwconfig wlan0 | grep 'Mode:' | grep 'Ad-Hoc' | wc -l)" -eq 0 ]]; then
246 e "starting ad-hoc mesh"
248 iwconfig wlan0 mode ad-hoc
249 iwconfig wlan0 essid cjdns
253 e "ad-hoc mesh is running fine"
257 if [[ "$opmode" = "client" ]]; then
260 clearnet_interface=wlan0
262 # check if wlan0 has already started
263 if [[ "$(ifconfig wlan0 | grep 'inet addr' | wc -l)" -eq 0 \
264 || "$(iwconfig wlan0 | grep 'Access Point: Not-Associated' | wc -l)" -gt 0 ]]; then
265 if [[ "$security" = "WPA" ]]; then
271 e "wlan client is running fine"
273 #TODO: connect to unencrypted wifi
277 if [[ "$(gateway_is_up)" == "true" ]]; then
278 set_network_parameters
279 e "checking internet connectivity over cjdns"
280 if [[ "$($ping 8.8.8.8 | grep 'bytes from')" ]]; then
281 echo "We have internet. Good."
283 set_status "internet" 1
285 set_status "cjdns_internet" 1
286 rm "$cjdns_request_tries" 2> /dev/null
291 echo "No internet via cjdns. Checking for regular internet connection..."
293 set_status "internet" 0
295 set_status "cjdns_internet" 0
300 if [[ "$(interface_dhcp_success)" == "true" ]]; then
305 if [[ "$(check_for_internet)" == "true" ]]; then
306 set_status "internet" 1
310 if [[ "$wehaveinternet" == "yes" && "$request_internet" == "yes" ]]; then
311 request_cjdns_internet
314 for i in $(seq 60 -1 1); do
315 echo "waiting $i seconds for gateway to come up..."
316 if [[ "$(gateway_is_up)" == "true" ]]; then
318 set_network_parameters
319 e "checking internet connectivity over cjdns"
320 if [[ "$($ping 8.8.8.8 | grep 'bytes from')" ]]; then
321 echo "We have internet. Good."
323 set_status "internet" 1
325 set_status "cjdns_internet" 1
326 rm "$cjdns_request_tries" 2> /dev/null
329 echo "Gateway is up, but no internet. Requesting..."
330 /usr/sbin/request-internet
337 e "no internet via cjdns."
338 # just ensure that cjdns is running, but DO NOT restart it!
339 # since local phone calls may be active.