-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
dowse
executable file
·475 lines (363 loc) · 10.8 KB
/
dowse
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#!/usr/bin/env zsh
#
# Copyright (C) 2012-2016 Dyne.org Foundation
#
# Designed, written and maintained by Denis Roio <[email protected]>
#
# This source code is free software; you can redistribute it
# and/or modify it under the terms of the GNU Public License
# as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later
# version.
#
# This source code is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. Please refer to the GNU Public License for more
# details.
#
# You should have received a copy of the GNU Public License
# along with this source code; if not, write to: Free
# Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
# 02139, USA.
# {{{ GLOBALS
dowse_version=1.0b
dowse_release_date="5/Apr/2016"
# export DOWSE_PREFIX and DOWSE_HOME to override
R=${DOWSE_PREFIX:-/usr/local/dowse}
H=${DOWSE_HOME:-$HOME/.dowse}
E=${DOWSE_CONF:-/etc/dowse}
#####################
source /usr/share/zuper/zuper
[[ -r $R ]] || {
error "Dowse is not installed in: $R"
error "export DOWSE_PREFIX if installed on a different path"
return 1
}
# add all other dowse binaries to the path
path+=($R/bin)
rehash
mkdir -p $H
# For gettext
TEXTDOMAIN=dowse
# path and debugging
vars+=(R H E debug quiet script fun network)
vars+=(last_notice last_act last_func)
vars+=(root sql)
arrs+=(mods)
# network configuration
vars+=(settings address interface hostname wan lan firewall)
vars+=(dowse_uid dowse_gid dowse_net netmask dowse_guests)
# export DOWSE to env with path to the installation
# else start it from inside the dir with source dowse
DEBUG=${DEBUG:-0}
QUIET=${QUIET:-0}
LOG=${LOG:-""}
# pendulum
vars+=(now sqlquery)
maps+=(obj pidmap)
# index of fields in the things database
vars+=(thingidx)
# index of fields in the things database about event related table
vars+=(eventidx)
# configuration parameter table
vars+=(parameteridx)
# dnscrypt-proxy server settings
vars+=(dnscrypt_ipaddr dnscrypt_key dnscrypt_name)
# globals filled by lease parser
arrs+=(host mac ip)
# consul's configuration map
# maps+=(consul_conf consul_watch)
# redis database indexes
maps+=(db)
# dataset indexes
arrs+=(mod)
# daemons exec paths (OS dependent)
maps+=(execmap execrules)
vars+=(execpath)
# default daemons conf and pids
vars+=(pid conf)
zmodload zsh/system
zmodload zsh/mapfile
zmodload zsh/regex
zmodload zsh/sched
zmodload zsh/system
zmodload zsh/net/tcp
source $R/zlibs/modules
source $R/zlibs/processes
source $R/zlibs/iptables
# source $R/zlibs/ebtables
source $R/zlibs/confstore
source $R/zlibs/redis
source $R/zlibs/redis
source $R/zlibs/nmap
# source $R/zlibs/sql
# source $R/daemons/consul
source $R/daemons/dhcpd
# source $R/daemons/squid3
# source $R/daemons/privoxy
# source $R/daemons/polipo
source $R/daemons/mosquitto
source $R/daemons/dnscrypt-proxy
source $R/daemons/netdata
# source $R/daemons/pgld
source $R/daemons/redis
source $R/daemons/redis-volatile
source $R/daemons/seccrond
source $R/daemons/webui2
# source $R/daemons/tinyproxy
# source $R/daemons/tor
# source $R/daemons/node-red
# conclude initialization
source /usr/share/zuper/zuper.init
func "load dataset indexes"
db=()
zkv.load $R/db/database.zkv
func "load paths and execmaps"
execmap=()
source $R/db/execmap.zkv
execrules=()
source $R/db/execrules.zkv
# module fields
mod=(
name
desc
tags
ports
webgui
daemons
authors
version
)
if [[ $dowse_loaded = 1 ]]; then
act "Dowse CLI reloaded ::1::" $dowse_version
RPROMPT=Dowse
else
notice "Dowse CLI ::1:: - local area network rabdomancy" $dowse_version
dowse_loaded=1
RPROMPT=Dowse
fi
[[ $QUIET = 1 ]] || {
cat <<EOF
Copyright (C) 2012-2016 Dyne.org Foundation, License GNU GPL v3+
This is free software: you are free to change and redistribute it
For more informations see http://www.dyne.org/software/dowse
EOF
}
# global file path to settings
settings=${1:-$E/settings}
conf-load
# TODO: using first argument here breaks ~/.zshrc auto-load
act "checking requirements to run dowse..."
[[ "$address" = "" ]] && {
error "Invalid config file"
zdump
return 1
}
mkdir -p $H/log
mkdir -p $H/log/netdata
mkdir -p $H/run
mkdir -p $H/data
_verbose "Dowse command: ::1 commands::" "$*"
_verbose "Caller: uid[::1 uid::], gid[::2 gid::], tty[::3 tty::]." \
$dowse_uid $dowse_gid $TTY
act "daemons running as user and group $dowse_uid:$dowse_gid"
# [[ -z $root ]] || {
# # setup dirs
# chmod go-rwx $H/log
# chown -R $dowse_uid:$dowse_gid $H/log
# chmod go-rwx $R/run
# chown -R $dowse_uid:$dowse_gid $R/run
# }
# func "::1 dir:: directory writable by ::2 uid::" run $dowse_uid
# if touch $R/run/test; then rm -f $R/run/test; else return 1; fi
# func "::1 dir:: directory writable by ::2 uid::" log $dowse_uid
# if touch $H/log/test; then rm -f $H/log/test; else return 1; fi
act "initialise things database"
#dbname="${address}_${wan}_${dowse_net//\//_}"
#db[things]="things_${dbname//./_}"
db[things]="things"
func "$db[things]"
# the global index of things database, path to it
# see: dowse/build/db/thing.idx
thingidx=$R/db/thing.idx
eventidx=$R/db/event.idx
parameteridx=$R/db/parameter.idx
constraintidx=$R/db/constraint.idx
act "all requirements in place"
fn dowse-running
# }}}
# {{{ MAIN
# Here is most of the hard-coded logic of dowse when starting up
# eventually some of this need to interact, acknowledge or even
# be moved into modules, depending from future developments.
# Meanwhile, special care is taken to keep this readable and
# transparent (avoid wrapping when possible), documenting choices
# with references.
dowse-start() {
fn dowse-start
# freq=($R/conf/settings $R/conf/network)
# ckreq || return $?
[[ "`getpid redis-server`" = "" ]] || {
warn "Dowse is already running"
return 0
}
notice "Starting Dowse"
# start the redis daemons (core k/v service)
start redis-server
start redis-volatile
[[ "$firewall" = "yes" ]] && {
# ebtables-stop
iptables-stop
loopback-on
iptables-start
iptables-base-protection
if loopback-only; then
act "dry run on localhost only"
else
loopback-isolate
act "flushing arp cache"
launch ip -s -s neigh flush all
act "set up $interface interface with ip $address"
launch ip addr add ${address}/${dowse_net[(ws:/:)2]} dev $interface
launch ip link set $interface up
[[ "$wan" = "" ]] || {
act "set $wan as default gateway"
launch ip route add default via $wan
}
act "setup snat trasversal"
iptables-snat-on
# make sure the dowse host uses his own dns
[[ -r /etc/resolv.conf.dowse-backup ]] ||
# this is problematic, we cannot sup mv or cat or tee
# so perhaps take another approach: fw rule to redirect
# all traffic to configured dns to our own until is up?
mv /etc/resolv.conf /etc/resolv.conf.dowse-backup
cat <<EOF > /etc/resolv.conf
nameserver $address
domain dowse.it
search dowse.it
EOF
fi
}
notice "Starting all daemons in Dowse"
# launch the dhcp daemon
start dhcpd
# start the dns encrypted tunneling
start dnscrypt-proxy
# [[ "$firewall" = "yes" ]] &&
# start peerguardian (must be at end, after any other iptables setting)
# start pgld
# start the mqtt/websocket hub
start mosquitto
# netdata dashboard for the technical status
start netdata
# nodejs/node-red
# start node-red
start seccrond
start webui2
notice "Dowse succesfully started" # , web interface up on:"
# act "http://${address}:8500/"
}
dowse-stop() {
fn dowse-stop
notice "Stopping all daemons in Dowse"
stop webui2
stop seccrond
stop mosquitto
# for m in `dowse-module-list`; do
# dowse-module-load $m || continue
# dowse-module-stop
# done
# stop nodejs/node-red
# stop node-red
# stop the dashboard
stop netdata
# stop the dns crypto tunnel
stop dnscrypt-proxy
# stop the dhcp server
stop dhcpd
# stop the firewall peerguardian
# stop pgld
# remove the layer 2 firewall rules
# ebtables-stop
# remove the layer 3 firewall rules
iptables-snat-off
iptables-stop
# restore backup if present
# [[ -r /etc/resolv.conf.dowse-backup ]] && {
# mv /etc/resolv.conf.dowse-backup /etc/resolv.conf
# }
stop redis-volatile
stop redis-server
notice "Dowse has stopped running."
}
# dowse-parse-leases() {
# awk '
# BEGIN { c=0 }
# {
# mac[c]=$2
# ip[c]=$3
# if($4=="*") host[c]="anonymous"; else host[c]=$4
# zx c++
# }
# END {
# printf "mac=("; for(i=0;i<c;i++) { printf "%s ", mac[i] }; printf ")\n"
# printf "ip=("; for(i=0;i<c;i++) { printf "%s ", ip[i] }; printf ")\n"
# printf "host=("; for(i=0;i<c;i++) { printf "%s ", host[i] }; printf ")\n"
# }' $H/run/leases
# }
dowse-dump() {
fn dowse-dump
notice "Dumping variables in Redis"
act "Runtime:"
print - "KEYS *" | redis runtime | sort
act "Storage:"
print - "KEYS *" | redis storage | sort
act "Dynamic:"
print - "KEYS *" | redis dynamic | sort
}
dowse-status() {
fn dowse-status $*
req=(sql)
freq=($H/run/things.db)
ckreq || return $?
arg="$1"
if [[ "$arg" = "" ]]; then
minago=`date "+%H:%M:%S" -d '1 day ago'`
now=`date "+%H:%M:%S"`
notice "List things found online"
# for res in "${(f)$(print - "SELECT * from found WHERE strftime('%H:%M:%S', last) between '$minago' and '$now';" | $sql -batch $H/run/things.db)}"; do
for res in "${(f)$(print - "SELECT * from found;" | $sql $db[things])}"; do
[[ "$res" = "" ]] && continue
local -A _obj
_obj=( $(print - $res | sql_idx_parse $R/db/thing.idx) )
notice "$_obj[hostname] (${_obj[os]//_/ })"
act "$_obj[last]"
act "$_obj[macaddr] $_obj[ip4]"
act "$_obj[iface] $_obj[state]"
done
else
act "Search '$arg' among things found"
for res in "${(f)$(sql_search $R/db/thing.idx found "$arg")}"; do
[[ "$res" = "" ]] && continue
local -A _obj
_obj=( $(print - $res | sql_idx_parse $R/db/thing.idx) )
notice "$_obj[hostname] (${_obj[os]//_/ })"
act "$_obj[last]"
act "$_obj[macaddr] $_obj[ip4]"
act "$_obj[iface] $_obj[state]"
done
fi
return 0
}
dowse-npm() {
fn dowse-npm $*
freq=($R/nodejs/node_dir/bin)
ckreq || return $?
NODE_PATH="$R/nodejs/node_dir/lib/node_modules:$R/node-red/node_modules" \
$R/nodejs/node_dir/bin/node \
$R/nodejs/node_dir/lib/node_modules/npm/bin/npm-cli.js $*
return $?
}
# }}}