-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtek_survey.sh
executable file
·2636 lines (2394 loc) · 89.9 KB
/
tek_survey.sh
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# set -x
# script to grab TEKs for various places, and stash 'em
# For a cronjob set HOME as needed in the crontab
# and then the rest should be ok, but you can override
# any of these you want
x=${HOME:='/home/stephen'}
x=${DOCROOT:='/var/www/tact/tek-counts/'}
x=${TOP:="$HOME/code/tek_transparency"}
x=${DATADIR:="$TOP/data"}
x=${ARCHIVE:="$DATADIR/all-zips"}
x=${DAILIES:="$DATADIR/dailies"}
x=${DAILIES2:="$DATADIR/dailies2"}
TEK_DECODE="$TOP/tek_file_decode.py"
TEK_TIMES="$TOP/tek_times.sh"
TEK_REPORT="$TOP/tek_report.sh"
DE_CFG_DECODE="$TOP/de_tek_cfg_decode.py"
CURL="/usr/bin/curl -s"
UNZIP="/usr/bin/unzip"
# The services here could generally be trusted to not provide (or
# cause us to derive) "bad" file names (e.g. "/etc/passwd" or
# "../../tek_survey.sh") that might do damage. However, now that
# a number of those services are being decommissioned, we should
# consider what'd happen if a domain name were in future snagged
# by a bad actor, or were just re-used for something that caused
# us a problem. So we'll sanitise all file names we create down
# to just alphanumerics plus "-", "_" and "." which seems to be all we
# need for the real services.
# We should call this anytime we create a file based on a string
# we've downloaded from a service.
function sanitise_filename()
{
fname=$1
echo ${fname//[^a-zA-Z0-9_\-.]/}
}
# same for what we want to be a decimal number string
function sanitise_decimal()
{
num=$1
echo ${num//[^0-9]/}
}
function whenisitagain()
{
date -u +%Y%m%d-%H%M%S
}
NOW=$(whenisitagain)
# For the log
echo "======================"
echo "======================"
echo "======================"
echo "Running $0 at $NOW"
mkdir -p $DATADIR/$NOW
if [ ! -d $DATADIR/$NOW ]
then
echo "Failed to create $DATADIR/$NOW"
# maybe try get data in /tmp
cd /tmp
else
cd $DATADIR/$NOW
fi
# Ireland
# used to notify us that something went wrong
IE_CANARY="$ARCHIVE/ie-canary"
IE_BASE="https://app.covidtracker.ie/api"
IE_CONFIG="$IE_BASE/settings/"
IE_RTFILE="$HOME/ie-refreshToken.txt"
$CURL --output ie-cfg.json -L $IE_CONFIG
echo "======================"
echo ".ie TEKs"
if [ ! -f $IE_RTFILE ]
then
if [ ! -f $IE_CANARY ]
then
echo "<p>Skipping .ie because refreshToken access failed at $NOW.</p>" >$IE_CANARY
fi
echo "Skipping .ie because refreshToken access failed at $NOW.</p>"
else
refreshToken=`cat $IE_RTFILE`
tok_json=`$CURL -s -L $IE_BASE/refresh -H "Authorization: Bearer $refreshToken" -d "{}"`
if [[ "$?" != 0 ]]
then
if [ ! -f $IE_CANARY ]
then
echo "<p>Skipping .ie because refreshToken use failed at $NOW.</p>" >$IE_CANARY
fi
echo "Skipping .ie because refreshToken use failed at $NOW."
else
newtoken=`echo $tok_json | awk -F: '{print $2}' | sed -e 's/"//g' | sed -e 's/}//'`
if [[ "$newtoken" == "" ]]
then
echo "No sign of an authToken, sorry - Skipping .ie"
else
# get stats
$CURL -s -L "$IE_BASE/stats" -o ie-stats.json -H "Authorization: Bearer $newtoken"
index_str=`$CURL -s -L "$IE_BASE/exposures/?since=0&limit=1000" -H "Authorization: Bearer $newtoken"`
echo "Irish index string: $index_str"
iefiles=""
for row in $(echo "${index_str}" | jq -r '.[] | @base64');
do
_jq() {
echo ${row} | base64 --decode | jq -r ${1}
}
iefiles="$iefiles $(_jq '.path')"
done
for iefile in $iefiles
do
echo "Getting $iefile"
iebname_raw=`basename $iefile`
iebname=$(sanitise_filename $iebname_raw)
$CURL -s -L "$IE_BASE/data/$iefile" --output ie-$iebname -H "Authorization: Bearer $newtoken"
if [[ $? == 0 ]]
then
# we should be good now, so remove canary
rm -f $IE_CANARY
if [ ! -f $ARCHIVE/ie-$iebname ]
then
cp ie-$iebname $ARCHIVE
fi
# try unzip and decode
#if [[ "$DODECODE" == "yes" ]]
#then
#$UNZIP "ie-$iebname" >/dev/null 2>&1
#if [[ $? == 0 ]]
#then
#tderr=`mktemp /tmp/tderrXXXX`
#$TEK_DECODE >/dev/null 2>$tderr
#new_keys=$?
#total_keys=$((total_keys+new_keys))
#tderrsize=`stat -c%s $tderr`
#if [[ "$tderrsize" != '0' ]]
#then
#echo "tek-decode error processing ie-$iebname"
#fi
#rm -f $tderr
#fi
#rm -f export.bin export.sig
#chunks_down=$((chunks_down+1))
#fi
else
echo "Error fetching ie-$iebname"
fi
done
fi
fi
fi
# Northern Ireland
# Same setup as Ireland app-wise
# NI is a region of the UK, so we'll use the prefix "ukni-"
NI_BASE="https://app.stopcovidni.hscni.net/api"
NI_CONFIG="$NI_BASE/settings/"
# Northern Ireland
# used to notify us that something went wrong
NI_CANARY="$ARCHIVE/ukni-canary"
NI_RTFILE="$HOME/ukni-refreshToken.txt"
$CURL --output ukni-cfg.json -L $NI_CONFIG
echo "======================"
echo "Northern Ireland TEKs"
if [ ! -f $NI_RTFILE ]
then
if [ ! -f $NI_CANARY ]
then
echo "<p>Skipping Northern Ireland because refreshToken access failed at $NOW.</p>" >$NI_CANARY
fi
echo "Skipping Northern Ireland because refreshToken access failed at $NOW.</p>"
else
refreshToken=`cat $NI_RTFILE`
tok_json=`$CURL -s -L $NI_BASE/refresh -H "Authorization: Bearer $refreshToken" -d "{}"`
if [[ "$?" != 0 ]]
then
if [ ! -f $NI_CANARY ]
then
echo "<p>Skipping Northern Ireland because refreshToken use failed at $NOW.</p>" >$NI_CANARY
fi
echo "Skipping Northern Ireland because refreshToken use failed at $NOW."
else
newtoken=`echo $tok_json | awk -F: '{print $2}' | sed -e 's/"//g' | sed -e 's/}//'`
if [[ "$newtoken" == "" ]]
then
echo "No sign of an authToken, sorry - Skipping Northern Ireland"
else
# grab stats
$CURL -s -L "$NI_BASE/stats" -o ukni-stats.json -H "Authorization: Bearer $newtoken"
index_str=`$CURL -s -L "$NI_BASE/exposures/?since=0&limit=1000" -H "Authorization: Bearer $newtoken"`
echo "Northern Irish index string: $index_str"
nifiles=""
for row in $(echo "${index_str}" | jq -r '.[] | @base64');
do
check401=`echo ${row} | base64 --decode`
if [[ "$check401" == "401" ]]
then
echo "401 detected in JSON answer - oops"
break
fi
_jq() {
echo ${row} | base64 --decode | jq -r ${1}
}
nifiles="$nifiles $(_jq '.path')"
done
for nifile in $nifiles
do
echo "Getting $nifile"
nibname_raw=`basename $nifile`
nibname=$(sanitise_filename $nibname_raw)
$CURL -s -L "$NI_BASE/data/$nifile" --output ukni-$nibname -H "Authorization: Bearer $newtoken"
if [[ $? == 0 ]]
then
# we should be good now, so remove canary
rm -f $NI_CANARY
if [ ! -f $ARCHIVE/ukni-$nibname ]
then
cp ukni-$nibname $ARCHIVE
fi
# try unzip and decode
#if [[ "$DODECODE" == "yes" ]]
#then
#$UNZIP "ukni-$nibname" >/dev/null 2>&1
#if [[ $? == 0 ]]
#then
#$TEK_DECODE >/dev/null
#new_keys=$?
#total_keys=$((total_keys+new_keys))
#fi
#rm -f export.bin export.sig
#chunks_down=$((chunks_down+1))
#fi
else
echo "Error fetching ukni-$nibname"
fi
done
fi
fi
fi
# italy
echo "======================"
echo ".it TEKs"
IT_BASE="https://get.immuni.gov.it/v1/keys"
IT_INDEX="$IT_BASE/index"
IT_CONFIG="https://get.immuni.gov.it/v1/settings?platform=android&build=1"
index_str=`$CURL -L $IT_INDEX`
raw_bottom_chunk_no=`echo $index_str | awk '{print $2}' | sed -e 's/,//'`
raw_top_chunk_no=`echo $index_str | awk '{print $4}' | sed -e 's/}//'`
bottom_chunk_no=$(sanitise_decimal $raw_bottom_chunk_no)
top_chunk_no=$(sanitise_decimal $raw_top_chunk_no)
echo "Bottom: $bottom_chunk_no, Top: $top_chunk_no"
if [[ "$bottom_chunk_no" != "" && "$top_chunk_no" != "" && $((top_chunk_no > bottom_chunk_no)) ]]
then
total_keys=0
chunks_down=0
chunk_no=$bottom_chunk_no
while [ $chunk_no -le $top_chunk_no ]
do
echo "Getting it-$chunk_no.zip"
$CURL -L "$IT_BASE/{$chunk_no}" --output it-$chunk_no.zip
if [[ $? == 0 ]]
then
if [ ! -f $ARCHIVE/it-$chunk_no.zip ]
then
cp it-$chunk_no.zip $ARCHIVE
fi
# try unzip and decode
#if [[ "$DODECODE" == "yes" ]]
#then
#$UNZIP "it-$chunk_no.zip" >/dev/null 2>&1
#if [[ $? == 0 ]]
#then
#$TEK_DECODE >/dev/null
#new_keys=$?
#total_keys=$((total_keys+new_keys))
#fi
#rm -f export.bin export.sig
#fi
else
echo "Error fetching it-$chunk_no.zip"
fi
chunk_no=$((chunk_no+1))
chunks_down=$((chunks_down+1))
done
else
echo "Skipping Italy TEKS due to malformed chunk nos"
fi
$CURL -L $IT_CONFIG --output it-cfg.json
echo "======================"
echo ".de TEKs"
# Germany
DE_BASE="https://svc90.main.px.t-online.de/version/v1/diagnosis-keys/country/DE"
DE_INDEX="$DE_BASE/date"
# .de index format is like: ["2020-06-23","2020-06-24"]
index_str=`$CURL -f -L $DE_INDEX`
echo "German index string: $index_str"
if [[ "$index_str" != "" ]]
then
dedates=`echo $index_str \
| sed -e 's/\[//' \
| sed -e 's/]//' \
| sed -e 's/"//g' \
| sed -e 's/,/ /g' `
for dedate in $dedates
do
sane_dedate=$(sanitise_filename $dedate)
$CURL -L "$DE_BASE/date/$dedate" --output de-$sane_dedate.zip
if [[ $? == 0 ]]
then
echo "Got de-$sane_dedate.zip"
if [ ! -f $ARCHIVE/de-$sane_dedate.zip ]
then
cp de-$sane_dedate.zip $ARCHIVE
fi
# try unzip and decode
#if [[ "$DODECODE" == "yes" ]]
#then
#$UNZIP "de-$sane_dedate.zip" >/dev/null 2>&1
#if [[ $? == 0 ]]
#then
#$TEK_DECODE >/dev/null
#new_keys=$?
#total_keys=$((total_keys+new_keys))
#fi
#rm -f export.bin export.sig
#chunks_down=$((chunks_down+1))
#fi
else
echo "Error fetching de-$sane_dedate.zip"
fi
# Now check for hourly zips - it's ok that we have dups as we
# will use "sort|uniq" before counting and it's nice to have
# all the zips even if we have >1 copy
hours_str=`$CURL -L "$DE_BASE/date/$dedate/hour"`
dehours=`echo $hours_str \
| sed -e 's/\[//' \
| sed -e 's/]//' \
| sed -e 's/"//g' \
| sed -e 's/,/ /g' `
if [[ "$dehours" != "" ]]
then
echo ".de on $dedate has hours: $dehours"
fi
for dehour in $dehours
do
sane_dehour=$(sanitise_filename $dehour)
$CURL -L "$DE_BASE/date/$dedate/hour/$dehour" --output de-$sane_dedate-$sane_dehour.zip
if [[ $? == 0 ]]
then
echo "Got de-$sane_dedate-$sane_dehour.zip"
if [ ! -f $ARCHIVE/de-$sane_dedate-$sane_dehour.zip ]
then
cp de-$sane_dedate-$sane_dehour.zip $ARCHIVE
fi
# try unzip and decode
#if [[ "$DODECODE" == "yes" ]]
#then
#$UNZIP "de-$sane_dedate-$sane_dehour.zip" >/dev/null 2>&1
#if [[ $? == 0 ]]
#then
#$TEK_DECODE >/dev/null
#new_keys=$?
#total_keys=$((total_keys+new_keys))
#fi
#rm -f export.bin export.sig
#chunks_down=$((chunks_down+1))
#fi
else
echo "Error fetching de-$sane_dedate-$sane_dehour.zip"
fi
done
done
fi
# lastly, check for today in case there are hourlies already
# that are not in the date index
# when is today? that's in UTC, so I may be a TZ off for an hour
# or two
today=`date -u +%Y-%m-%d`
echo "today: Checking .de for today's hourlies $today"
# Now check for hourly zips - it's ok that we have dups as we
# will use "sort|uniq" before counting and it's nice to have
# all the zips even if we have >1 copy
hours_str=`$CURL -f -L "$DE_BASE/date/$today/hour"`
dehours=""
if [[ "$hours_str" != "" ]]
then
dehours=`echo $hours_str \
| sed -e 's/\[//' \
| sed -e 's/]//' \
| sed -e 's/"//g' \
| sed -e 's/,/ /g' `
echo "today: Checking .de for today's hourlies dehours: $dehours"
if [[ "$dehours" != "" ]]
then
echo ".de on $today has hours: $dehours"
fi
fi
for dehour in $dehours
do
sane_dehour=$(sanitise_filename $dehour)
$CURL -L "$DE_BASE/date/$today/hour/$dehour" --output de-$today-$sane_dehour.zip
if [[ $? == 0 ]]
then
echo "Got de-$today-$sane_dehour.zip"
if [ ! -f $ARCHIVE/de-$today-$sane_dehour.zip ]
then
cp de-$today-$sane_dehour.zip $ARCHIVE
fi
# try unzip and decode
#if [[ "$DODECODE" == "yes" ]]
#then
#$UNZIP "de-$today-$sane_dehour.zip" >/dev/null 2>&1
#if [[ $? == 0 ]]
#then
#$TEK_DECODE >/dev/null
#new_keys=$?
#total_keys=$((total_keys+new_keys))
#fi
#rm -f export.bin export.sig
#chunks_down=$((chunks_down+1))
#fi
else
echo "Error fetching de-$today-$sane_dehour.zip"
fi
done
DE_CONFIG="https://svc90.main.px.t-online.de/version/v1/configuration/country/DE/app_config"
$CURL -f -L $DE_CONFIG --output de-cfg.zip
# not that interesting to decode these each time now
#if [ -f de-cfg.zip ]
#then
#$UNZIP de-cfg.zip
#if [[ $? == 0 ]]
#then
#echo ".de config:"
#$DE_CFG_DECODE
#rm -f export.bin export.sig
#fi
#fi
DE_KEYS="https://github.com/micb25/dka/raw/master/data_CWA/diagnosis_keys_statistics.csv"
$CURL -L $DE_KEYS --output de-keys.csv
echo "======================"
echo ".ch TEKs"
# Switzerland
# Apparently the .ch scheme is to use the base URL below with a filename
# of the milliseconds version of time_t for midnight on the day concerned
# (What? Baroque? Nah - just normal nerds:-)
# so https://www.pt.bfs.admin.ch/v1/gaen/exposed/1592611200000 works for
# june 20
CH_BASE="https://www.pt.bfs.admin.ch/v1/gaen/exposed"
now=`date +%s`
today_midnight="`date -d "00:00:00Z" +%s`000"
# Swiss cgs & stats
# The GAEN config
CH_CONFIG="https://www.pt-a.bfs.admin.ch/v1/config?appversion=1&osversion=ios&buildnr=1"
# The count of active users
CH_ACTIVES="https://www.bfs.admin.ch/bfsstatic/dam/assets/orderNr:ds-q-14.01-SwissCovidApp-01.2/master"
# The count of covidcodes uploaded
CH_CODES="https://www.bfs.admin.ch/bfsstatic/dam/assets/orderNr:ds-q-14.01-SwissCovidApp-03/master"
# Same but weekly - we don't get this now, just still here 'cause I accidentally grabbed it for
# a day or two;-)
CH_WEEKLY_CODES="https://www.bfs.admin.ch/bfsstatic/dam/assets/orderNr:ds-q-14.01-SwissCovidApp-04/master"
# it turns out (personal communication) that the .ch scheme is to change
# the content of files but re-use the file name. I think that means that
# files that are less than 14 days old may be updated, with newly uploaded
# TEKs, so for each run, we'll download all 14 files.
# (Feck it, we'll go for 15:-)
# one day in milliseconds
day=$((60*60*24*1000))
for fno in {0..15}
do
echo "Doing .ch file $fno"
midnight=$((today_midnight-fno*day))
# CH started returning ascii errors on 2022-06-04
chhttpcode=`$CURL -L "$CH_BASE/$midnight" -s -w "%{http_code}" -o ch-$midnight.txt`
if [[ "$http_code" != "200" ]]
then
echo "CH error http response code: $http_code for ch-$midnight"
continue;
fi
$CURL -L "$CH_BASE/$midnight" --output ch-$midnight.zip
if [[ $? == 0 ]]
then
# we do see zero sized files from .ch sometimes
# which is odd but whatever (could be their f/w
# doing that but what'd be the effect on the
# app?)
if [ ! -s ch-$midnight.zip ]
then
echo "Empty or non-existent downloaded Swiss file: ch-$midnight.zip ($fno)"
else
if [ ! -f $ARCHIVE/ch-$midnight.zip ]
then
echo "New .ch file $fno ch-$midnight"
cp ch-$midnight.zip $ARCHIVE
elif ((`stat -c%s "ch-$midnight.zip"`>`stat -c%s "$ARCHIVE/ch-$midnight.zip"`));then
# if the new one is bigger than archived, then archive new one
echo "Updated/bigger .ch file $fno ch-$midnight"
cp ch-$midnight.zip $ARCHIVE
fi
# try unzip and decode
#if [[ "$DODECODE" == "yes" ]]
#then
#$UNZIP "ch-$midnight.zip" >/dev/null 2>&1
#if [[ $? == 0 ]]
#then
#$TEK_DECODE >/dev/null
#new_keys=$?
#total_keys=$((total_keys+new_keys))
#fi
#rm -f export.bin export.sig
#chunks_down=$((chunks_down+1))
#fi
fi
else
echo "Error fetching ch-$midnight.zip (file $fno)"
fi
# don't appear to be too keen:-)
sleep 1
done
$CURL -L $CH_CONFIG --output ch-cfg.json
$CURL -L $CH_ACTIVES --output ch-actives.json
$CURL -L $CH_CODES --output ch-codes.json
# This URL has the number of active users of the app
ACTIVES_URL="https://www.bfs.admin.ch/bfsstatic/dam/assets/orderNr:ds-q-14.01-SwissCovidApp-01/master"
$CURL -L $ACTIVES_URL -o ch-actives.json
# This URL contains text that says how many people uploaded codes (presumably after a positive test)
# Very oddly, the TLS server cert for that uses some oddball CA unknown to curl or wget
# (that's CN=QuoVadis Global SSL ICA G3,O=QuoVadis Limited,C=BM) so we'll ignore cert checks
# just this once;-)
CODES_URL="https://www.experimental.bfs.admin.ch/expstat/en/home/innovative-methods/swisscovid-app-monitoring.html"
$CURL --insecure -L $CODES_URL -o ch-codes.html
echo "======================"
echo ".pl TEKs"
# Poland
# yes - we end up with two slashes between hostname and path for some reason!
PL_BASE="https://exp.safesafe.app/"
PL_CONFIG="dunno; get later"
plzips=`$CURL -L "$PL_BASE/index.txt" | sed -e 's/\///g'`
plhttpcode=`$CURL -L "$PL_BASE/index.txt" -s -w "%{http_code}" -o pl-index.txt`
if [[ "$plhttpcode" != "200" ]]
then
echo "PL index failure, HTTP response $plhttpcode"
else
for plzip in $plzips
do
echo "Getting $plzip"
sane_plzip=$(sanitise_filename $plzip)
$CURL -L "$PL_BASE/$plzip" --output pl-$sane_plzip
if [[ $? == 0 ]]
then
if [ ! -s pl-$sane_plzip ]
then
echo "Empty or non-existent Polish file: pl-$sane_plzip"
else
if [ ! -f $ARCHIVE/pl-$sane_plzip ]
then
cp pl-$sane_plzip $ARCHIVE
fi
# try unzip and decode
#if [[ "$DODECODE" == "yes" ]]
#then
#$UNZIP "pl-$sane_plzip" >/dev/null 2>&1
#if [[ $? == 0 ]]
#then
#$TEK_DECODE >/dev/null
#new_keys=$?
#total_keys=$((total_keys+new_keys))
#fi
#rm -f export.bin export.sig
#chunks_down=$((chunks_down+1))
#fi
fi
else
echo "Error fetching pl-$sane_plzip"
fi
done
fi
# PL config, still don't have a good URL but...
# to get config needs a post to firebaseremoteconfig.googleapis.com with a ton of ids/authentication. response is
# {
# "appName": "pl.gov.mc.protegosafe",
# "entries": {
# "diagnosisKeyDownloadConfiguration": "{\"timeoutMobileSeconds\":120,\"timeoutWifiSeconds\":60,\"retryCount\":2}",
# "exposureConfiguration": "{\"minimumRiskScore\":4,\"attenuationScores\":[2,5,6,7,8,8,8,8],\"attenuationWeigh\":50,\"daysSinceLastExposureScores\":[7,8,8,8,8,8,8,8],\"daysSinceLastExposureWeight\":50,\"durationScores\":[0,5,6,7,8,8,8,8],\"durationWeight\":50,\"transmissionRiskScores\":[8,8,8,8,8,8,8,8],\"transmissionRiskWeight\":50,\"durationAtAttenuationThresholds\":[48,58]}",
# "provideDiagnosisKeysWorkerConfiguration": "{\"repeatIntervalInMinutes\":360,\"backoffDelayInMinutes\":10}",
# "riskLevelConfiguration": "{\"maxNoRiskScore\":0,\"maxLowRiskScore\":1499,\"maxMiddleRiskScore\":2999}"
# },
# "state": "UPDATE"
# }
echo "======================"
echo ".dk TEKs"
# Denmark
DK_HOST="app.smittedtop.dk"
DK_BASE="https://$DK_HOST/API/v1/diagnostickeys"
DK_CONFIG="$DK_BASE/exposureconfiguration"
try_dk="yes"
# The DK_HOST for now gives NXDOMAIN so check for that
nxd=`dig $DK_HOST | grep status | grep NXDOMAIN`
if [[ "$nxd" != "" ]]
then
echo "$DK_HOST gives NXDOMAIN now"
try_dk="no"
else
# the DK config needs a weird authorization header
$CURL -o dk-cfg.json -D - -L $DK_CONFIG -H "Authorization_Mobile: 68iXQyxZOy"
dkcfg_res=$?
if [[ "$dkcfg_res" != "0" ]]
then
# since there's a version number in the DK_BASE that'll
# presumably change sometime so check to see if that or
# some other failure happened
# my guess is I might notice this easier than the
# absence of the config file
echo "Failed to get DK config - curl returned $dkcfg_res"
# we only do the zip files if the above worked - in fact they've
# turned this one off now (20220403)
try_dk="no"
fi
fi
if [[ "$try_dk" == "no" ]]
then
echo "No sign of dk-cfg so won't go for TEKs"
else
# For DK, we grab $DK_Base/<date>.0.zip and there's an HTTP header
# ("FinalForTheDay: False") in the response to us if we still need
# to get $DK_BASE/<date>.1.zip
# we'll do that for the last 14 days then archive any zips that are
# new or bigger than before
oneday=$((60*60*24))
end_time_t=`date -d 00:00 +%s`
start_time_t=$((`date -d 00:00 +%s`-14*oneday))
the_time_t=$start_time_t
while [ $the_time_t -lt $end_time_t ]
do
the_time_t=$((the_time_t+oneday))
the_day=`date -d @$the_time_t +%Y-%m-%d`
echo "Doing $the_day"
more_to_come="True"
dk_chunk=0
while [[ "$more_to_come" != "" ]]
do
the_zip_name="$the_day:$dk_chunk.zip"
# colons in file names is a bad plan for some OSes
the_local_zip_name="dk-$the_day.$dk_chunk.zip"
echo "Fetching $the_zip_name"
response_headers=`$CURL -o $the_local_zip_name -D - -L "$DK_BASE/$the_zip_name" -H "Authorization_Mobile: 68iXQyxZOy"`
dkzip_res=$?
if [[ "$dkzip_res" == "0" ]]
then
if [ ! -s $the_local_zip_name ]
then
# for june 28th we seem to get an endless stream of zero-sized non-final files
echo "Got empty file for $the_zip_name"
more_to_come=""
else
echo "Got $the_zip_name"
echo "RH: $response_headers"
more_to_come=`echo $response_headers | grep "FinalForTheDay: False"`
if [[ "$more_to_come" != "" ]]
then
# check in case of a 404 - for today's :0 file we do get FinalForTheDay: False
# but then a 404 for chunk :1 - I guess they're not sure that the :0 is is
# final 'till the day's over, but that's a bit iccky
more_to_come=`echo $response_headers | grep "HTTP/1.1 404 Not Found"`
fi
dk_chunk=$((dk_chunk+1))
if [ ! -f $ARCHIVE/$the_local_zip_name ]
then
echo "New .dk file $the_local_zip_name"
cp $the_local_zip_name $ARCHIVE
elif ((`stat -c%s "$the_local_zip_name"`>`stat -c%s "$ARCHIVE/$the_local_zip_name"`));then
# if the new one is bigger than archived, then archive new one
echo "Updated/bigger .dk file $the_local_zip_name"
cp $the_local_zip_name $ARCHIVE
fi
# try unzip and decode
#if [[ "$DODECODE" == "yes" ]]
#then
#$UNZIP "$the_local_zip_name" >/dev/null 2>&1
#if [[ $? == 0 ]]
#then
#$TEK_DECODE >/dev/null
#new_keys=$?
#total_keys=$((total_keys+new_keys))
#fi
#rm -f export.bin export.sig
#chunks_down=$((chunks_down+1))
#fi
fi
else
echo "Didn't get a $the_zip_name"
more_to_come=""
fi
# let's not be too insistent
sleep 1
done
done
fi
echo "======================"
echo ".at Teks"
AT_SOURCE="https://github.com/austrianredcross/stopp-corona-android"
AT_CONFIG="https://app.prod-rca-coronaapp-fd.net/Rest/v8/configuration"
AT_BASE="https://cdn.prod-rca-coronaapp-fd.net/"
AT_INDEX="$AT_BASE/exposures/at/index.json"
$CURL -L $AT_CONFIG \
-H "authorizationkey: 64165cfc5a984bb09e185b6258392ecb" \
-H "x-appid: at.roteskreuz.stopcorona" \
-o at.config.json
$CURL -L $AT_INDEX \
-H "authorizationkey: 64165cfc5a984bb09e185b6258392ecb" \
-H "x-appid: at.roteskreuz.stopcorona" \
-o at.index.json
if [ -f at.index.json ]
then
zipnames=`cat at.index.json | sed -e 's/\["/\n/g' | sed -e 's/"\].*//g' | grep exposure`
for zipname in $zipnames
do
echo "Fetching .at $zipname"
zipurl=https://cdn.prod-rca-coronaapp-fd.net/$zipname
the_zip_name=`basename $zipname`
the_local_zip_name=$(sanitise_filename "at-$the_zip_name")
$CURL -L $zipurl \
-H "authorizationkey: 64165cfc5a984bb09e185b6258392ecb" \
-H "x-appid: at.roteskreuz.stopcorona" \
-o $the_local_zip_name
atzip_res=$?
if [[ "$atzip_res" == "0" ]]
then
if [ ! -s $the_local_zip_name ]
then
echo "Got empty file for $the_zip_name"
more_to_come=""
else
echo "Got $the_zip_name"
at_chunk=$((at_chunk+1))
if [ ! -f $ARCHIVE/$the_local_zip_name ]
then
echo "New .at file $the_local_zip_name"
cp $the_local_zip_name $ARCHIVE
elif ((`stat -c%s "$the_local_zip_name"`>`stat -c%s "$ARCHIVE/$the_local_zip_name"`));then
# if the new one is bigger than archived, then archive new one
echo "Updated/bigger .at file $the_local_zip_name"
cp $the_local_zip_name $ARCHIVE
fi
# try unzip and decode
#if [[ "$DODECODE" == "yes" ]]
#then
#$UNZIP "$the_local_zip_name" >/dev/null 2>&1
#if [[ $? == 0 ]]
#then
#$TEK_DECODE >/dev/null
#new_keys=$?
#total_keys=$((total_keys+new_keys))
#fi
#rm -f export.bin export.sig
#chunks_down=$((chunks_down+1))
#fi
fi
else
echo "Didn't get a $the_zip_name"
more_to_come=""
fi
# let's not be too insistent
sleep 1
done
fi
echo "======================"
echo ".lv Teks"
# Latvia
LV_BASE="https://apturicovid-files.spkc.gov.lv"
LV_CONFIG="$LV_BASE/exposure_configurations/v1/android.json"
LV_INDEX="$LV_BASE/dkfs/v1/index.txt"
$CURL -o lv-cfg.json -L "$LV_CONFIG"
if [[ "$?" != 0 ]]
then
echo "Error grabbing .lv config: $LV_CONFIG"
fi
response_headers=`$CURL -D - -o lv-index.txt -L "$LV_INDEX" -i`
if [[ "$?" == 0 ]]
then
clzero=`echo $response_headers | grep -c "Content-Length: 0"`
if [[ "$clzero" == "1" ]]
then
echo "no .lv TEKs at $NOW"
else
urls2get=`cat lv-index.txt | grep https`
for theurl in $urls2get
do
the_zip_name=$theurl
the_local_zip_name=$(sanitise_filename "lv-`basename $theurl`")
$CURL -L $theurl -o $the_local_zip_name
lvzip_res=$?
if [[ "$lvzip_res" == "0" ]]
then
if [ ! -s $the_local_zip_name ]
then
echo "Got empty file for $the_zip_name"
else
echo "Got $the_zip_name"
lv_chunk=$((lv_chunk+1))
if [ ! -f $ARCHIVE/$the_local_zip_name ]
then
echo "New .lv file $the_local_zip_name"
cp $the_local_zip_name $ARCHIVE
elif ((`stat -c%s "$the_local_zip_name"`>`stat -c%s "$ARCHIVE/$the_local_zip_name"`));then
# if the new one is bigger than archived, then archive new one
echo "Updated/bigger .lv file $the_local_zip_name"
cp $the_local_zip_name $ARCHIVE
fi
# try unzip and decode
#if [[ "$DODECODE" == "yes" ]]
#then
#$UNZIP "$the_local_zip_name" >/dev/null 2>&1
#if [[ $? == 0 ]]
#then
#$TEK_DECODE >/dev/null
#new_keys=$?
#total_keys=$((total_keys+new_keys))
#fi
#rm -f export.bin export.sig
#chunks_down=$((chunks_down+1))
#fi
fi
else
echo "Didn't get a $the_zip_name"
fi
# let's not be too insistent
sleep 1
done
fi
fi
echo "======================"
echo ".es TEKs"
# Spain
# a friend sent me the apk for the spanish app that's being tested just now (in
# the canaries i think). to get the config settings use GET
# https://dqarr2dc0prei.cloudfront.net/configuration/settings to get TEKs use GET
# https://dqarr2dc0prei.cloudfront.net/dp3t/v1/gaen/exposed/1594512000000 (which
# responds with 204 No Content) and GET
# https://dqarr2dc0prei.cloudfront.net/dp3t/v1/gaen/exposed/1594425600000 which
# gives a TEK file (signed as a demo i think)
# Seems to be same as .ch scheme, see the comments there
# (Yeah, we should make a function, can do it later, but
# we should also start to use a real DB maybe so TBD)
ES_BASE="https://radarcovid.covid19.gob.es/dp3t/v1/gaen/exposed"
now=`date +%s`
today_midnight="`date -d "00:00:00Z" +%s`000"
# one day in milliseconds
day=$((60*60*24*1000))
for fno in {0..14}
do
echo "Doing .es file $fno"
midnight=$((today_midnight-fno*day))
$CURL -L "$ES_BASE/$midnight" --output es-$midnight.zip
if [[ $? == 0 ]]
then
# spain turned off on 2022-10-09 (or so) so we'll check if we're
# really getting a zip or not (since they turned off we get some
# HTML with a 5xx message)
iszip=`file es-$midnight.zip | grep Zip`
if [[ "$iszip" == "" ]]
then
echo "Spain fail: not a zip es-$midnight.zip"
continue
fi
# we do see zero sized files from .es sometimes
# which is odd but whatever (could be their f/w
# doing that but what'd be the effect on the
# app?)
if [ ! -s es-$midnight.zip ]
then
echo "Empty or non-existent downloaded Spanish file: es-$midnight.zip ($fno)"
else
if [ ! -f $ARCHIVE/es-$midnight.zip ]
then
echo "New .es file $fno es-$midnight"
cp es-$midnight.zip $ARCHIVE
elif ((`stat -c%s "es-$midnight.zip"`>`stat -c%s "$ARCHIVE/es-$midnight.zip"`));then
# if the new one is bigger than archived, then archive new one
echo "Updated/bigger .es file $fno es-$midnight"
cp es-$midnight.zip $ARCHIVE
fi
# try unzip and decode
#if [[ "$DODECODE" == "yes" ]]
#then
#$UNZIP "es-$midnight.zip" >/dev/null 2>&1
#if [[ $? == 0 ]]
#then
#$TEK_DECODE >/dev/null
#new_keys=$?
#total_keys=$((total_keys+new_keys))
#fi
#rm -f export.bin export.sig
#chunks_down=$((chunks_down+1))
#fi
fi
else
echo "curl - error downloading es-$midnight.zip (file $fno)"
fi
# don't appear to be too keen:-)
sleep 1
done
ES_CONFIG="https://radarcovid.covid19.gob.es/configuration/settings"
ES_LOCALES="https://radarcovid.covid19.gob.es/configuration/masterData/locales?locale=es-ES"
ES_CCAA="https://radarcovid.covid19.gob.es/configuration/masterData/ccaa?locale=es-ES&additionalInfo=true"
$CURL -L $ES_CONFIG --output es-cfg.json
$CURL -L $ES_LOCALES --output es-locales.json
$CURL -L $ES_CCAA --output es-ccaa.json
#echo ".es config:"
#cat es-cfg.json
echo "======================"
echo "US Virginia TEKs"