1#!/bin/sh
2# /usr/bin/spoofer
3
4. /usr/share/spoofer/spoofer-lib.sh
5
6spoofer_init_config || exit $?
7
8enableTLS=$(      uci_get spoofer general enableTLS 1)
9enableIPv4=$(     uci_get spoofer general enableIPv4 1)
10enableIPv6=$(     uci_get spoofer general enableIPv6 1)
11sharePublic=$(    uci_get spoofer general sharePublic 1)
12shareRemedy=$(    uci_get spoofer general shareRemedy 1)
13keepResults=$(    uci_get spoofer general keepResults 60)
14keepLogs=$(       uci_get spoofer general keepLogs 2)
15standaloneMode=$( uci_get spoofer debug standaloneMode 0)
16pretendMode=$(    uci_get spoofer debug pretendMode 0)
17useDevServer=$(   uci_get spoofer debug useDevServer 0)
18
19PROBER="/usr/bin/spoofer-prober"
20OPTIONS="-1 --cafile /usr/share/spoofer/gd_bundle.crt --cafile /usr/share/spoofer/letsencrypt_bundle.pem.txt"
21test "$enableTLS" = "0" && OPTIONS="$OPTIONS --no-tls"
22test "$enableIPv4" = "1" && OPTIONS="$OPTIONS -4"
23test "$enableIPv6" = "1" && OPTIONS="$OPTIONS -6"
24test "$standaloneMode" = "1" && OPTIONS="$OPTIONS -S"
25test "$pretendMode" = "1" && OPTIONS="$OPTIONS -P"
26test "$useDevServer" = "1" && OPTIONS="$OPTIONS -T"
27OPTIONS="$OPTIONS -s${sharePublic} -r${shareRemedy}"
28
29case "$*" in
30    --postinst)
31	# after spoofer_init_config
32	exit 0
33	;;
34    --version)
35	$PROBER --version
36	exit 0
37	;;
38    -?|-h|--help)
39	cat <<EOF
40"$0 [options]" will run spoofer-prober with options according to
41spoofer settings in uci, and record summary results.  With current settings,
42the command line options will be:
43    $PROBER $OPTIONS [options]
44
45EOF
46	$PROBER --help
47	exit 0
48	;;
49esac
50
51trap 'uci revert spoofer' EXIT
52NOW=$(date +'%s')
53LOGFILE=$LOGDIR/spoofer-prober-$(date -d@$NOW +"%Y%m%d-%H%M%S").txt
54
55# delete expired log files and references to them
56keeplogs $(($keepLogs - 1))
57
58# create new result record in uci
59uci set spoofer.$NOW=result &&
60uci reorder spoofer.$NOW=0 && # move new record to beginning of list
61uci_set spoofer $NOW start "$NOW" &&
62uci_set spoofer $NOW log "$LOGFILE" &&
63uci_commit spoofer || exit $?
64
65# run the prober
66$PROBER $OPTIONS "$@" >$LOGFILE
67STATUS=$?
68
69# summarize results from log into uci
70SUMMARYFILE=$LOGDIR/spoofer.summary
71awk -- '
72    function SET(name, value) { print "set spoofer.'$NOW'." name "=\"" value "\""; }
73    function ADD(name, value) { print "add_list spoofer.'$NOW'." name "=\"" value "\""; }
74    /^>> +standaloneMode$/ { flags = flags "S"; }
75    /^>> +pretendMode$/    { flags = flags "P"; }
76    /^>> +useDevServer$/   { flags = flags "T"; }
77    /^# ClientMessage / { v=0; }
78    /^# ServerMessage \(IPv4\):$/ { v=4; }
79    /^# ServerMessage \(IPv6\):$/ { v=6; }
80    /^# +clientip: / { if (v) { SET("ipv" v, v flags); SET("clientip" v, $3); } }
81    /^# IPv4 Result Summary:$/ { v=4; }
82    /^# IPv6 Result Summary:$/ { v=6; }
83    /^>> +ASN: / { SET("ASN" v, $NF); }
84    /^>> +Spoofed private addresses, outbound: /   { split($0, a, ": +"); SET("privaddr" v, a[2]); }
85    /^>> +Spoofed routable addresses, outbound: /  { split($0, a, ": +"); SET("routable" v, a[2]); }
86    /^>> +Spoofed private addresses, inbound: /  { split($0, a, ": +"); SET("inprivaddr" v, a[2]); }
87    /^>> +Spoofed internal addresses, inbound: / { split($0, a, ": +"); SET("ininternal" v, a[2]); }
88    /^Your test results:$/ { footer=1; }
89    /^ +https?:.*\/report/ { if (footer) SET("report", $NF); }
90    /^[*][*][*].*([Ee]rror|[Ww]arning|[Nn]otice):/ { ADD("message", $0); }
91' <$LOGFILE >$SUMMARYFILE || exit $?
92
93uci batch <$SUMMARYFILE || exit $?
94rm $SUMMARYFILE
95
96# keep only the first (most recent) $keepResults results
97if test "$keepResults" -gt 0; then
98    while uci -q show spoofer.@result[$keepResults] >/dev/null; do
99	uci delete spoofer.@result[-1]
100    done
101fi
102
103uci_commit spoofer || exit $?
104
105# if there's a spoofer crontab, reschedule it (in case this script was called
106# outside of the cronjob)
107if crontab_l | egrep "$PROBERTAG" >/dev/null; then
108    /etc/init.d/spoofer start
109fi
110
111trap - EXIT
112exit $STATUS
113