-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathipfg-ng
executable file
·707 lines (665 loc) · 18.8 KB
/
ipfg-ng
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
#!/usr/bin/env bash
# ipfg-ng
# original version: v1.2.2
# IPFS public gateway checker (extended cli version)
# original repository: https://github.com/ipfs/public-gateway-checker
# this script copyright 2017 by Joss Brown (pseud.): https://github.com/JayBrown
# this script copyright 2021 by MDrights : https://github.com/mdrights/csobot
# license: MIT
# 2021.08.31 Use http response code as the criteria for judging the availability of a gateway; add IPNS support. etc.
LANG=en_US.UTF-8
export PATH=/usr/local/bin:$PATH
export HISTIGNORE='*ipfs config show*'
CURL="curl --dns-servers 114.114.114.114 "
VERSION="2.0.0"
DATE="08-2021"
UPDV="2.0.0"
RL_PATH="https://raw.githubusercontent.com/ipfs/public-gateway-checker/master/gateways.json"
WB_PATH="https://raw.githubusercontent.com/ipfs/public-gateway-checker/master/lastpubver"
# REPOV="https://raw.githubusercontent.com/JayBrown/Tools/master/ipfg/ipfg"
#TESTHASH="ipfs/QmbXekWeorBfgRfvNFiLUrUWGWjo2coQcU3rfsAtqBoB7C"
TESTHASH="ipns/k2k4r8mjf4sdgf6oh5337x4pv5aj8lb4b9px3wh5g6rpchvqztpdwfc0"
echoerr() {
echo "$1" 1>&2
}
usage() {
echo "ipfg $VERSION ($DATE)"
echo "IPFS public gateway checker (extended cli version)"
echo ""
echo "Running 'ipfg' without an option will check the online status of all available IPFS gateways."
echo ""
echo "OPTIONS:"
echo -e "\t-a | --add <URL>\n\t\tAdd specified gateway URLs to local list"
echo ""
echo -e "\t-c | --compare\n\t\tCheck remote gateway list for changes incl. auto-backup"
echo ""
echo -e "\t-d | --delete [<URL> | all]\n\t\tDelete specified or all gateway URLs from local list"
echo ""
echo -e "\t-h | --hash\n\t\tPrint current and saved IPFS hashes incl. auto-backup"
echo ""
echo -e "\t-H | --help\n\t\tThis help page"
echo ""
echo -e "\t-l | --list [raw] [all | local | remote]\n\t\tDisplay listed gateways as domains or raw URLs"
echo ""
echo -e "\t-L | --local [raw]\n\t\tOnly check gateways on local list"
echo ""
echo -e "\t-M | --manual <URL>\n\t\tCheck only the specified URLs"
echo ""
echo -e "\t-R | --remote [raw]\n\t\tOnly check gateways on remote list"
echo ""
echo -e "\t-s | --save [all | hash | url]\n\t\tManually save remote data to local backup files"
echo ""
echo -e "\t-u | --upload | --cache [local | remote] <URL>\n\t\tCache an IPFS object from the local or a remote node on a gateway node"
echo ""
echo -e "\t-V | --version\n\t\tPrint version number (incl. update check)"
echo ""
echo -e "\t-w | --web\n\t\tOpen web-based version of the IPFS public gateway checker"
echo ""
echo "Adding 'raw' to a gateway check will print the full URL instead of the gateway domain."
echo "When using multiple options, only the final option will be recognized."
echo "Caching will only be attempted on one gateway at a time."
echo ""
echo "GATEWAY URL FORMAT:"
echo -e "\thttp[s]://<domain>.<tld>/ipfs/:hash"
echo ""
echo "EXAMPLES:"
echo -e "\tipfg"
echo -e "\tipfg -M https://ipfs.io/ipfs/:hash"
echo -e "\tipfg -l remote"
echo -e "\tipfg -R raw"
echo -e "\tipfg -a https://mygateway.com/ipfs/:hash"
echo -e "\tipfg -l raw local"
echo -e "\tipfg -L"
echo -e "\tipfg raw"
echo -e "\tipfg -d https://mygateway.com/ipfs/:hash"
echo -e "\tipfg -u local https://ipfs.io/ipfs/\"\$(echo 'This is an ipfg cache run.' | ipfs add -Q)\""
echo ""
echo "Copyright 2017 by Joss Brown (pseud.): https://github.com/JayBrown (License: MIT)"
echo ""
echo "See also the web-based IPFS gateway checker: https://github.com/ipfs/public-gateway-checker"
}
backup-remote() {
if ! $INITIAL ; then
echoerr "Creating backup of remote gateway list. Please wait..."
fi
RGATEWAYS=$(${CURL} --silent "$RL_PATH" | sed -e '$ d' -e '1,1d' -e '/^$/d' -e 's/\"//g' -e 's/,$//g')
if [[ $RGATEWAYS == "" ]] ; then
echoerr "Error: no access to remote list. Please try again later."
else
rm -f "$IPFG_DIR/remote-backup-old" 2>/dev/null \
&& mv "$RLB_PATH" "$IPFG_DIR/remote-backup-old" 2>/dev/null
while read -r GATEWAY
do
GATEWAY=$(echo "$GATEWAY" | xargs)
echo "$GATEWAY" >> "$RLB_PATH"
done < <(echo "$RGATEWAYS")
echoerr "Done."
fi
}
backup-hash() {
if ! $INITIAL ; then
echoerr "Creating backup of current IPFS hash. Please wait..."
fi
WEB_HASH=$(${CURL} --silent "$WB_PATH")
if [[ $WEB_HASH == "" ]] ; then
echoerr "Error: no access to current IPFS hash. Please try again later."
exit 1
else
rm -f "$IPFG_DIR/hash-old" 2>/dev/null \
&& mv "$HASH_PATH" "$IPFG_DIR/hash-old" 2>/dev/null
echo "$WEB_HASH" > "$HASH_PATH"
echoerr "Done."
fi
}
RED='\033[0;31m'
BLUE='\033[0;34m'
GREEN='\033[0;32m'
NC='\033[0m'
INITIAL=false
IPFG_DIR="$HOME/.ipfg"
mkdir -p "$IPFG_DIR"
RLB_PATH="$IPFG_DIR/remote-backup"
LL_PATH="$IPFG_DIR/local"
HASH_PATH="$IPFG_DIR/hash"
if ! [[ -f "$LL_PATH" ]] ; then
touch "$LL_PATH"
fi
if ! [[ -f "$RLB_PATH" ]] ; then
INITIAL=true
echoerr "Creating initial backup of remote gateway list. Please wait..."
backup-remote
INITIAL=false
fi
if ! [[ -f "$HASH_PATH" ]] ; then
INITIAL=true
echoerr "Creating initial backup of current IPFS hash. Please wait..."
backup-hash
INITIAL=false
fi
ADD=false
COMPARE=false
DELETE=false
ADDED=false
REMOVED=false
LIST=false
RAW=false
LIST_REMOTE=false
LIST_LOCAL=false
MANUAL=false
REMOTE=false
LOCAL=false
WPRINT=true
LPRINT=true
CACHE=false
CLOCAL=false
CREMOTE=true
if [[ "$@" == "" ]] ; then
REMOTE=true
LOCAL=true
else
if [[ $1 != "-"* ]] ; then
if [[ $1 == "raw" ]] ; then
RAW=true
REMOTE=true
LOCAL=true
else
echoerr "Invalid option: $1"
echoerr ""
usage
exit 1
fi
else
while :; do
case $1 in
-a|--add)
ADD=true
DELETE=false
;;
-c|--compare)
COMPARE=true
;;
-d|--delete)
DELETE=true
ADD=false
;;
-h|--hash|--hashes)
WEB_HASH=$(${CURL} --silent "$WB_PATH")
if [[ $WEB_HASH == "" ]] ; then
WEB_HASH="n/a"
WPRINT=false
fi
LOCAL_HASH=$(cat "$HASH_PATH" 2>/dev/null | sed -e '/^$/d')
if [[ $LOCAL_HASH == "" ]] ; then
LOCAL_HASH="n/a"
LPRINT=false
fi
if ! $WPRINT && ! $LPRINT ; then
echoerr "No hashes available. Please try again later."
exit 1
else
echo -e "IPFS hash (current):\t$WEB_HASH"
echo -e "IPFS hash (backup):\t$LOCAL_HASH"
if [[ $LOCAL_HASH != $WEB_HASH ]] ; then
echoerr "Attention: a new web version was published."
backup-hash
else
echoerr "No recent changes."
fi
fi
exit 0
;;
-H|-\?|--help|--Help)
usage
exit 0
;;
-l|--list)
LIST=true
shift
if [[ $1 == "raw" ]] ; then
RAW=true
shift
fi
if [[ $1 == "remote" ]] ; then
LIST_REMOTE=true
LIST_LOCAL=false
elif [[ $1 == "local" ]] ; then
LIST_REMOTE=false
LIST_LOCAL=true
else
LIST_REMOTE=true
LIST_LOCAL=true
fi
;;
-L|--local)
LOCAL=true
MANUAL=false
shift
if [[ $1 == "raw" ]] ; then
RAW=true
fi
;;
-M|--manual)
MANUAL=true
LOCAL=false
REMOTE=false
;;
-R|--remote)
REMOTE=true
MANUAL=false
shift
if [[ $1 == "raw" ]] ; then
RAW=true
fi
;;
-s|--save)
shift
if [[ $1 == "url" ]] ; then
backup-remote
elif [[ $1 == "hash" ]] ; then
backup-hash
else
backup-remote && backup-hash
fi
exit 0
;;
-u|--upload|--cache)
CACHE=true
if [[ $2 == "http"* ]] ; then
if [[ $2 != *"/ipfs/"* ]] ; then
echoerr "Error: Not a valid IPFS gateway URL."
exit 1
fi
else
shift
if ! [[ $1 =~ ^(local|remote)$ ]] ; then
echoerr "Error: wrong argument: $1"
exit 1
else
if [[ $1 == "local" ]] ; then
CLOCAL=true
CREMOTE=false
fi
if [[ $2 != "http"* ]] || [[ $2 != *"/ipfs/"* ]] ; then
echoerr "Error: Not a valid IPFS gateway URL: $2"
exit 1
fi
fi
fi
;;
-V|--version)
echo "ipfg $VERSION $DATE"
echoerr "Checking for updates. Please wait..."
RVERS=$(${CURL} --silent "$REPOV" | grep "UPDV=" | awk -F\" '{print $2}' | head -1)
[[ $RVERS == "" ]] && RVERS="1.0"
if (( $(bc -l <<< "$RVERS > $UPDV") )) ; then
echo "New update v$RVERS available at: $REPOV"
else
echoerr "Your version is up-to-date."
fi
exit 0
;;
-w|--web)
WEB_HASH=$(${CURL} --silent "$WB_PATH")
if [[ $WEB_HASH == "" ]] ; then
WEB_HASH=$(cat "$HASH_PATH" 2>/dev/null | sed -e '/^$/d')
if [[ $WEB_HASH == "" ]] ; then
echoerr "Opening fallback web version of IPFS public gateway checker..."
open "https://ipfs.github.io/public-gateway-checker/"
exit 0
fi
else
LOCAL_HASH=$(cat "$HASH_PATH" 2>/dev/null | sed -e '/^$/d')
if [[ $LOCAL_HASH != "" ]] && [[ $WEB_HASH != $LOCAL_HASH ]] ; then
echoerr "New hash detected: $WEB_HASH"
backup-hash
fi
fi
if [[ $(ipfs 2>/dev/null) == "" ]] ; then
echoerr "Opening web version of IPFS public gateway checker on IPFS gateway..."
open "https://ipfs.io/ipfs/$WEB_HASH"
else
if [[ $(ps aux | grep "ipfs daemon" | grep -v "grep.*ipfs daemon" 2>/dev/null) == "" ]] ; then
echoerr "Opening web version of IPFS public gateway checker on IPFS gateway..."
open "https://ipfs.io/ipfs/$WEB_HASH"
else
IPFS_PORT=$(ipfs config show | grep "Gateway" | head -1 | rev | awk -F/ '{print $1}' | rev | sed -e 's/,//' -e 's/\"//')
echoerr "Opening web version of IPFS public gateway checker on localhost..."
open "http://localhost:$IPFS_PORT/ipfs/$WEB_HASH"
fi
fi
exit 0
;;
--)
shift
break
;;
-?*)
echoerr "Invalid option: $1"
echoerr ""
usage
exit 1
;;
*)
break
esac
shift
done
fi
fi
if $ADD ; then
if [[ "$@" == "" ]] ; then
echoerr "Error: no gateway URL specified."
exit 1
else
LOCAL_LIST=$(cat "$LL_PATH" | sed -e '/^$/d')
for ADD_URL in "$@"
do
if ! [[ $ADD_URL =~ ^("http://"|"https://") ]] ; then
echoerr "Error: wrong domain format: $ADD_URL"
echoerr "Format must be: http[s]://<domain>.<tld>/ipfs/:hash"
continue
elif [[ $ADD_URL != *"/ipfs/:hash" ]] ; then
echoerr "Error: wrong URL format: $ADD_URL"
echoerr "Format must be: http[s]://<domain>.<tld>/ipfs/:hash"
continue
else
if [[ $(echo "$LOCAL_LIST" | grep ^$ADD_URL$) != "" ]] ; then
echoerr "Already exists: $ADD_URL"
continue
else
echo "$ADD_URL" >> "$LL_PATH"
echo "Added: $ADD_URL"
fi
fi
done
fi
exit 0
fi
if $COMPARE ; then
RGATEWAYS=$(${CURL} --silent "$RL_PATH" | sed -e '$ d' -e '1,1d' -e '/^$/d' -e 's/\"//g' -e 's/,$//g' | sort)
if [[ $RGATEWAYS == "" ]] ; then
echoerr "Error: no access to remote list. Please try again later."
exit 1
else
REMOTELIST=$(while read -r GWURL
do
GWURL=$(echo "$GWURL" | xargs)
echo "$GWURL"
done < <(echo "$RGATEWAYS")
)
fi
SAVELIST=$(cat "$RLB_PATH" | sed -e '/^$/d' | sort)
if [[ $SAVELIST == "" ]] ; then
echoerr "Error: no local backup available."
exit 1
fi
NEWGWS=$(comm -23 <(echo "$REMOTELIST") <(echo "$SAVELIST"))
if [[ $NEWGWS == "" ]] ; then
ADDED=false
else
ADDED=true
fi
DELGWS=$(comm -13 <(echo "$REMOTELIST") <(echo "$SAVELIST"))
if [[ $DELGWS == "" ]] ; then
REMOVED=false
else
REMOVED=true
fi
if ! $ADDED && ! $REMOVED ; then
echoerr "No changes to remote gateway list."
exit 0
else
echoerr "Changes to remote gateway list:"
if $ADDED ; then
while read -r NEWGW
do
echo -e "${GREEN}ADDED${NC}\t$NEWGW"
done < <(echo "$NEWGWS")
fi
if $REMOVED ; then
while read -r DELGW
do
echo -e "${RED}REMOVED${NC}\t$DELGW"
done < <(echo "$DELGWS")
fi
backup-remote
fi
exit 0
fi
if $DELETE ; then
LOCAL_LIST=$(cat "$LL_PATH" | sed -e '/^$/d')
if [[ $LOCAL_LIST == "" ]] ; then
echoerr "Local gateway list is already empty."
exit 0
fi
if [[ "$@" == "" ]] ; then
echoerr "Error: no gateway URL specified."
exit 1
elif [[ "$@" == "all" ]] ; then
echo -e "Do you really want to delete all local gateway URL entries? (y/n)"
read -n 1 -s DELYN
if [[ $DELYN == "y" ]] ; then
LOCAL_LIST=$(cat "$LL_PATH" | sed -e '/^$/d')
echo "" > "$LL_PATH"
while read -r DEL_URL
do
echo "Deleted: $DEL_URL"
done < <(echo "$LOCAL_LIST")
else
echoerr "Canceled."
fi
else
for DEL_URL in "$@"
do
LOCAL_LIST=$(cat "$LL_PATH" | sed -e '/^$/d')
if [[ ! $DEL_URL =~ ^("http://"|"https://") ]] ; then
echoerr "Error: wrong domain format: $DEL_URL"
echoerr "Format must be: http[s]://<domain>.<tld>/ipfs/:hash"
continue
elif [[ $DEL_URL != *"/ipfs/:hash" ]] ; then
echoerr "Error: wrong URL format: $DEL_URL"
echoerr "Format must be: http[s]://<domain>.<tld>/ipfs/:hash"
continue
elif [[ $(echo "$LOCAL_LIST" | grep "^$DEL_URL$") == "" ]] ; then
echoerr "Error: not on the local gateway list: $DEL_URL"
continue
else
NEW_LIST=$(echo "$LOCAL_LIST" | grep -v "^$DEL_URL$")
echo "$NEW_LIST" > "$LL_PATH"
echo "Deleted: $DEL_URL"
fi
done
fi
exit 0
fi
if $LIST ; then
if $LIST_REMOTE ; then
RGATEWAYS=$(${CURL} --silent "$RL_PATH" | sed -e '$ d' -e '1,1d' -e '/^$/d' -e 's/\"//g' -e 's/,$//g')
if ! $RAW ; then
RGATEWAYS=$(echo "$RGATEWAYS" | awk -F/ '{print $1"//"$3}')
fi
if [[ $RGATEWAYS == "" ]] ; then
echoerr "Error: remote gateway list is not accessible."
echoerr "Checking for backup list..."
RGATEWAYS=$(cat "$RLB_PATH" | awk -F/ '{print $1"//"$3}')
fi
if [[ $RGATEWAYS == "" ]] ; then
echoerr "Error: no backup list available."
LIST_REMOTE=false
else
RGATEWAYS=$(while read -r GATEWAY
do
GATEWAY=$(echo "$GATEWAY" | xargs)
echo "$GATEWAY"
done < <(echo "$RGATEWAYS")
)
fi
fi
if $LIST_LOCAL ; then
LGATEWAYS=$(cat "$LL_PATH" | sed -e '/^$/d')
if [[ $LGATEWAYS == "" ]] ; then
LIST_LOCAL=false
else
if ! $RAW ; then
LGATEWAYS=$(echo "$LGATEWAYS" | awk -F/ '{print $1"//"$3}')
fi
fi
fi
if $LIST_REMOTE && $LIST_LOCAL ; then
echoerr "*** REMOTE ***"
echo "$RGATEWAYS"
echoerr "*** LOCAL ***"
echo "$LGATEWAYS"
elif $LIST_REMOTE && ! $LIST_LOCAL ; then
echo "$RGATEWAYS"
elif ! $LIST_REMOTE && $LIST_LOCAL ; then
echo "$LGATEWAYS"
fi
exit 0
fi
if $CACHE ; then
if $CLOCAL ; then
if [[ $(ipfs 2>/dev/null) == "" ]] ; then
echoerr "Error: ipfs is either not installed or not in your \$PATH"
exit 1
fi
if [[ $(ps aux | grep "ipfs daemon" | grep -v "grep.*ipfs daemon" 2>/dev/null) == "" ]] ; then
echoerr "Error: IPFS daemon is not running"
exit 1
fi
fi
CDOMAIN=$(echo "$1" | awk -F/ '{print $1"//"$3}')
TEMPNAME=$(basename "$1")
TESTURL=$(echo "$1" | sed "s/$TEMPNAME$/$TESTHASH/")
if [[ $(${CURL} --silent "$TESTURL") != "Hello from IPFS Gateway Checker" ]] ; then
RESP_CODE=$(${CURL} -o /dev/null --silent --head --write-out "%{http_code}\n" $TESTURL)
if [[ $RESP_CODE =~ ^(301|303|307|308)$ ]] ; then
RD_CDOMAIN=$(${CURL} -w "%{url_effective}\n" -I -L -s -S "$CDOMAIN" -o /dev/null | sed 's-/*$--')
RD_TESTURL="$RD_CDOMAIN/ipfs/$TESTHASH"
if [[ $(${CURL} --silent "$RD_TESTURL") != "Hello from IPFS Gateway Checker" ]] ; then
echoerr "Error: gateway on $RD_CDOMAIN [$RESP_CODE] is currently offline."
exit 1
else
CDOMAIN="$RD_CDOMAIN"
CACHE_URL="$RD_CDOMAIN/ipfs/$TEMPNAME"
fi
else
echoerr "Error: gateway on $CDOMAIN is currently offline [$RESP_CODE]"
exit 1
fi
else
CACHE_URL="$1"
fi
echoerr "Please wait... attempting to cache $TEMPNAME on $CDOMAIN"
echoerr "Connect timeout: 60 seconds"
echoerr ""
${CURL} -o /dev/null --connect-timeout 60 "$CACHE_URL"
echoerr ""
echoerr "Done."
exit 0
fi
if $MANUAL ; then
if [[ "$@" == "" ]] ; then
echoerr "Error: no gateway URL specified."
exit 1
fi
GATEWAYS=$(for MANGW in "$@"
do
if ! [[ $MANGW =~ ^("http://"|"https://") ]] ; then
echoerr "Error: wrong domain format: $MANGW"
echoerr "Format must be: http[s]://<domain>.<tld>/ipfs/:hash"
continue
elif [[ $MANGW != *"/ipfs/:hash" ]] ; then
echoerr "Error: wrong URL format: $MANGW"
echoerr "Format must be: http[s]://<domain>.<tld>/ipfs/:hash"
continue
else
echo "$MANGW"
fi
done
)
if [[ $GATEWAYS == "" ]] ; then
echoerr "Error: no valid gateway URLs specified."
exit 1
fi
else
if $REMOTE ; then
RGATEWAYS=$(${CURL} --silent "$RL_PATH" | sed -e '$ d' -e '1,1d' -e 's/\"//g' -e 's/,$//g' -e '/^$/d')
if [[ $RGATEWAYS == "" ]] ; then
echoerr "Error: remote gateway list is not accessible."
echoerr "Checking for backup list..."
RGATEWAYS=$(cat "$RLB_PATH")
fi
if [[ $RGATEWAYS == "" ]] ; then
echoerr "Error: no backup list available."
REMOTE=false
fi
fi
if $LOCAL ; then
LGATEWAYS=$(cat "$LL_PATH" | sed -e '/^$/d')
if [[ $LGATEWAYS == "" ]] ; then
LOCAL=false
if ! $REMOTE ; then
echoerr "No gateway URLs available."
fi
fi
fi
if ! $REMOTE && ! $LOCAL ; then
exit 0
fi
if $REMOTE && $LOCAL ; then
GATEWAYS="$RGATEWAYS
$LGATEWAYS"
elif $REMOTE && ! $LOCAL ; then
GATEWAYS="$RGATEWAYS"
elif ! $REMOTE && $LOCAL ; then
GATEWAYS="$LGATEWAYS"
fi
fi
############ The Main Action #############
GWNUM=$(echo "$GATEWAYS" | wc -l | xargs)
if [[ $GWNUM -gt 1 ]] ; then
echoerr "*** Public IPFS Gateways ***"
echoerr "TESTHASH: $TESTHASH"
echoerr "CURL: $CURL"
fi
while read -r GATEWAY
do
GATEWAY=$(echo "$GATEWAY" | xargs)
TESTURL=$(echo "$GATEWAY" | sed -e "s-ipfs/:hash-$TESTHASH-")
if $RAW ; then
DOMAIN="$TESTURL"
else
DOMAIN=$(echo "$GATEWAY" | awk -F/ '{print $1"//"$3}')
fi
# Not using this method.
#if [[ $(${CURL} --silent "$TESTURL") == "Hello from IPFS Gateway Checker" ]] ; then
#| echo -e "${GREEN}Online${NC}\t$DOMAIN"
#else
RESP_CODE=$(${CURL} -o /dev/null -L --silent --head --write-out "%{http_code}\n" $TESTURL)
if [[ $RESP_CODE =~ ^(301|302|303|307|308)$ ]] ; then
DOMAIN=$(echo "$GATEWAY" | awk -F/ '{print $1"//"$3}')
RD_DOMAIN=$(${CURL} -w "%{url_effective}\n" -I -L -s -S "$DOMAIN" -o /dev/null | sed 's-/*$--')
RD_TESTURL="$RD_DOMAIN/$TESTHASH"
if $RAW ; then
RD_DOMAIN="$RD_TESTURL"
fi
# Not using this method.
# if [[ $(${CURL} --silent "$RD_TESTURL") == "Hello from IPFS Gateway Checker" ]] ; then
RD_RESP_CODE=$(${CURL} -o /dev/null -L --silent --head --write-out "%{http_code}\n" $RD_TESTURL)
if [[ $RD_RESP_CODE =~ ^(0|3|4|5)[0-9]+$ ]] ; then
echo -e "${RED}Offline${NC}\t$DOMAIN\t[$RD_RESP_CODE: $RD_DOMAIN]"
else
echo -e "${BLUE}Online${NC}\t$DOMAIN\t[$RD_RESP_CODE: $RD_DOMAIN]"
fi
elif [[ $RESP_CODE =~ ^(0|4|5)[0-9]+$ ]] ; then
echo -e "${RED}Offline${NC}\t$DOMAIN\t[$RESP_CODE]"
else
echo -e "${GREEN}Online${NC}\t$DOMAIN\t[$RESP_CODE]"
fi
#fi
done < <(echo "$GATEWAYS")
if [[ $GWNUM -gt 1 ]] ; then
echoerr "Done: $GWNUM gateways checked."
fi
exit 0