This repository has been archived by the owner on Apr 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
funceble
executable file
·1557 lines (1371 loc) · 58.1 KB
/
funceble
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
# _______ _ _______ _______ ______ _ _______
# ( ____ \|\ /|( ( /|( ____ \( ____ \( ___ \ ( \ ( ____ \
# | ( \/| ) ( || \ ( || ( \/| ( \/| ( ) )| ( | ( \/
# | (__ | | | || \ | || | | (__ | (__/ / | | | (__
# | __) | | | || (\ \) || | | __) | __ ( | | | __)
# | ( | | | || | \ || | | ( | ( \ \ | | | (
# | ) | (___) || ) \ || (____/\| (____/\| )___) )| (____/\| (____/\
# |/ (_______)|/ )_)(_______/(_______/|/ \___/ (_______/(_______/
# Written by: @Funilrys, Nissar Chababy <contact at funilrys dot com>
# Github : https://github.com/funilrys/funceble
################################ Contributors ##################################
# - @mitchellkrogza, Mitchell Krog <mitchellkrog@gmail.com>
# - badreferslist: used along the construction of this script :)
# @Source: https://git.io/vQPz8
# - Idea of implementation of a timeout for whois
# @Source: https://git.io/vQPz0
# - @PromoFaux, Adam Warner <adamw@rner.email> with Pi-Hole <https://pi-hole.net>
# - Their blocking list is just awesome to test this script :)
# - @WaLLy3K <github@firebog.net>
# - Idea of implementation of execution time
# @Source: https://git.io/vQPzg
################################################################################
################################# Temporary ####################################&&
# Path to temporary file
whoIs="/var/tmp/whois.$$"
################################################################################
################################### Files ######################################
# IANA Root Zone Database file.
# The file is a dump of the extensions this script can accept.
# Source: https://www.iana.org/domains/root/db
ianaDB="iana-domains-db"
################################################################################
############################### Text Format ###################################
# Red Color
red=$(tput setaf 1)
# White Color
white=$(tput setaf 7)
# Cyan Color
cyan=$(tput setaf 6)
# Magenta Color
magenta=$(tput setaf 5)
# Bold
bold=$(tput bold)
# Disable formating
normal=$(tput sgr0)
################################################################################
################################## Status ######################################
# Valid domain status
validStatus="ACTIVE"
# Invalid domain status
invalidStatus="INVALID"
# Down domain status
errorStatus="INACTIVE"
################################################################################
################################## Outputs #####################################
# If noFiles=true, We don't any results files ==> Not applied to hosts file
noFiles=false
# Generate debug file under log
# DO NOT USE THIS IF YOU DON'T KNOW HOW TO DEBUG FROM OUTPUTTED FILE
debugUnknown=false
# Defaut name for hosts file
hostsDefault='hosts'
# Output directory.
# DO NOT FORGET '/' AT THE END
outputDir='%%currentDir%%/output/'
# hosts directory
hostsDir=${outputDir}hosts/
# ACTIVE hosts directory
activeHostsDir=${hostsDir}ACTIVE/
# INACTIVE hosts directory
inactiveHostsDir=${hostsDir}INACTIVE/
# INVALID hosts directory
invalidHostsDir=${hostsDir}INVALID/
# Output of log
logOutput=${outputDir}'logs/'
# Whois output
whoisLogOutput=${logOutput}whois/
# Date output
dateLogOutput=${logOutput}dateFormat/
# Percentages output
percentageLogOutput=${logOutput}percentage/
# Output of execution Time log
executionLog=${logOutput}execution.log
# Ouput of no referer log
noRefererLogOutput=${logOutput}noReferer/
# Output of inactive domain
outputInactiveHost=${inactiveHostsDir}${hostsDefault}
# Output of active domain
outputActiveHost=${activeHostsDir}${hostsDefault}
# Output of invalid domain
outputInvalidHost=${invalidHostsDir}${hostsDefault}
# Output of unified results
outputUnifiedResult=${outputDir}/result.txt
# Output of valid results
outputValidResult=${outputDir}splited/active.txt
# Output of invalid results
outputInvalidResult=${outputDir}splited/invalid.txt
# Output of error results
outputErrorResult=${outputDir}splited/inactive.txt
# Output of persentage log
outputPercentageLog=${percentageLogOutput}percentage.txt
################################################################################
############################## Default Values ##################################
# IP for host file
customIP='0.0.0.0'
# Current Date
currentDate=$(date)
# Date if we append it to file
fileNameDate=_${currentDate// /_}
# Activate/Desactivate the generation of hosts file
generateHosts=false
# Output less data
outputLess=false
# Output unified file
outputUnified=true
# Output logs
outputLogs=true
# If set to true, we don't print anything
quiet=false
# If set to false, we generate only one file with the results
splitFiles=false
# IANA whois server. The following is there to give use the whois
# host we have to call to get a (valid) response.
# Note: Whois Server are different for each extension.
whoisServer="whois.iana.org"
# Regex to match
regexCurrentDir='%%currentDir%%'
# Show/hide execution time
showExecutionTime=false
# Link to script
linkScript="https://github.com/funilrys/funceble"
# Show/hide percentage
showPercentage=true
# Number of domain tested
numberOfTested=0
# Number of active
numberOfActive=0
# Number of domain which returns inactive
numberOfInactive=0
# Number of domain which returns invalid
numberOfInvalid=0
# Default seconds before timeout
secondsBeforeTimeout=30
# Version number
versionNumber='1.4.0'
################################################################################
# _______ _ _______ __________________ _______ _ _______
# ( ____ \|\ /|( ( /|( ____ \\__ __/\__ __/( ___ )( ( /|( ____ \
# | ( \/| ) ( || \ ( || ( \/ ) ( ) ( | ( ) || \ ( || ( \/
# | (__ | | | || \ | || | | | | | | | | || \ | || (_____
# | __) | | | || (\ \) || | | | | | | | | || (\ \) |(_____ )
# | ( | | | || | \ || | | | | | | | | || | \ | ) |
# | ) | (___) || ) \ || (____/\ | | ___) (___| (___) || ) \ |/\____) |
# |/ (_______)|/ )_)(_______/ )_( \_______/(_______)|/ )_)\_______)
############################### Before Header ##################################
# Used to print before header data.
# @CalledBy getReferer
################################################################################
beforeHeader(){
# We get the file we're gonna print into
local file="${1}"
if [[ ! -f "${file}" ]]
then
printf "# File generated with %s\n" "${linkScript}" > ${file}
printf "# Date of generation: %s\n" "${currentDate}" >> ${file}
printf "\n" >> ${file}
fi
}
############################### Print Header ###################################
# Used to print header if quiet=false and in files
# @CalledBy getReferer
################################################################################
printHeader()
{
# type <string>
local type=${1}
if [[ ${type} == 'Invalid' ]]
then
# Generation of header for invalid.txt
printf "\n%-100s %-17s %-10s %-8s\n" "Domain" "Status" "Source" "Analyse Date"
echo "---------------------------------------------------------------------------------------------------- ----------- ---------- ----------------------------"
elif [[ ${type} == 'Error' ]]
then
# Generation of header for error.txt
printf "\n%-100s %-35s %-17s %10s %-20s\n" "Domain" "WHOIS Server" "Status" "Source" "Analyse Date"
echo "---------------------------------------------------------------------------------------------------- ----------------------------------- ----------- ---------- ----------------------------"
elif [[ ${type} == 'Valid' ]]
then
# Generation of header for valid.txt
printf "\n%-100s %-17s %-17s %-10s %-20s\n" "Domain" "Status" "Expiration Date" "Source" "Analyse Date"
echo "---------------------------------------------------------------------------------------------------- ----------- ----------------- ---------- -------------------------------"
elif [[ ${type} == 'Generic' ]]
then
# Generation of header
printf "\n%-100s %-11s %-17s %-10s %-20s\n" "Domain" "Status" "Expiration Date" "Source" "Analyse Date"
echo "---------------------------------------------------------------------------------------------------- ----------- ----------------- ---------- -------------------------------"
elif [[ ${type} == 'Less' ]]
then
# Generation of header
printf "\n%-100s %-11s %-10s\n" "Domain" "Status" "Source"
echo "---------------------------------------------------------------------------------------------------- ----------- ----------"
elif [[ ${type} == 'Percentage' ]]
then
# Generation of header
printf "\n%-11s %-12s %-12s\n" "Status" "Percentage" "Numbers"
echo "----------- ------------ -------------"
fi
}
################################# Prints #######################################
# Come with printHeader(). In charge to print data
#
# @CalledBy getReferer
################################################################################
prints()
{
# type <string>
local type=${1}
local dateAn=$(date)
if [[ ${type} == 'Invalid' ]]
then
# Print data for invalid.txt
printf "%-100s %-17s %-8s %-8s\n" "${2}" "${invalidStatus}" "${3}" "${dateAn}"
elif [[ ${type} == 'Error' ]]
then
# Print data for error.txt
printf "%-100s %-35s %-17s %8s %-20s\n" "${2}" "${3}" "${errorStatus}" "${4}" "${dateAn}"
elif [[ ${type} == 'Valid' ]]
then
# Print data for valid.txt
printf "%-100s %-17s %-17s %-8s %-20s\n" "${2}" "${validStatus}" "${3}" "${4}" "${dateAn}"
elif [[ ${type} == 'Generic' ]]
then
# Print data
printf "%-100s %-11s %-17s %-10s %-20s\n" "${2}" "${3}" "${4}" "${5}" "${dateAn}"
elif [[ ${type} == 'FullHosts' ]]
then
# Print host file
printf "%s %s\n" "${customIP}" "${2}"
elif [[ ${type} == 'Less' ]]
then
# Print data
printf "%-100s %-11s %-10s\n" "${2}" "${3}" "${4}"
elif [[ ${type} == 'Percentage' ]]
then
# We calcul the results
percentage 'result' 'act'
percentage 'result' 'ina'
percentage 'result' 'inv'
# We print data
printf "%-11s %-12s %-12s\n" "ACTIVE" "${percentageOfActive}%" "${numberOfActive}"
printf "%-11s %-12s %-12s\n" "INACTIVE" "${percentageOfInactive}%" "${numberOfInactive}"
printf "%-11s %-12s %-12s\n" "INVALID" "${percentageOfInvalid}%" "${numberOfInvalid}"
fi
}
################################### Timeout ####################################
# In charge to initiate a timeout for commands
#
# @CalledBy timeout, getExpirationDate
################################################################################
timeout (){
# type <int|string>
time=${1}
# Create the temporary file
touch ${whoIs}
# start the command in a subshell in order to avoid problem with pipes
command="/bin/bash -c \"${2}\""
expect -c "set echo \"-noecho\"; set timeout ${time}; spawn -noecho ${command}; expect timeout { exit 1 } eof { exit 0 }"
}
############################# Generate Hosts File ##############################
# In charge to print data into hosts file
#
# @CalledBy generateInvalidFile,generateErrorFile,generateValidFile
################################################################################
generateHostsFile()
{
# type <string>
local type=${1}
if [[ ${generateHosts} == true ]]
then
if [[ ${type} == 'inactive' ]]
then
# We print / create the inactive file
beforeHeader ${outputInactiveHost}
prints 'FullHosts' "${domain}" >> ${outputInactiveHost}
elif [[ ${type} == 'active' ]]
then
# We print / create active file
beforeHeader ${outputActiveHost}
prints 'FullHosts' "${domain}" >> ${outputActiveHost}
elif [[ ${type} == 'invalid' ]]
then
# We print / create invalid file
((numberOfInvalid=${numberOfInvalid}+1))
beforeHeader ${outputInvalidHost}
prints 'FullHosts' "${domain}" >> ${outputInvalidHost}
fi
fi
}
########################### Generate Unified File ##############################
# In charge to print data into result.txt
#
# @CalledBy generateInvalidFile,generateErrorFile,generateValidFile
################################################################################
generateUnifiedFile()
{
# type <string>
local type=${1}
# status <string>
local status=${2}
# expdate <string>
local expdate=${3}
if [[ ${outputUnified} == true ]]
then
if [[ -f ${outputUnifiedResult} ]]
then
if [[ ${outputLess} == true ]]
then
# We save the domain and his data to the unified file
prints "Less" "${domain}" "${status}" "${type}" >> ${outputUnifiedResult}
else
# We save the domain and his data to the unified file
prints "Generic" "${domain}" "${status}" "${expdate}" "${type}" >> ${outputUnifiedResult}
fi
else
if [[ ${outputLess} == true ]]
then
# We create header into and in between, create the file
beforeHeader ${outputUnifiedResult}
printHeader "Less" >> ${outputUnifiedResult}
# We save the domain and his data to the unified file
prints "Less" "${domain}" "${status}" "${type}" >> ${outputUnifiedResult}
else
# We create header into and in between, create the file
beforeHeader ${outputUnifiedResult}
printHeader "Generic" >> ${outputUnifiedResult}
# We save the domain and his data to the unified file
prints "Generic" "${domain}" "${status}" "${expdate}" "${type}" >> ${outputUnifiedResult}
fi
fi
fi
}
############################### Generate Invalid File ##########################
# In charge to print data into invalid.txt
#
# @CalledBy errorHandle
################################################################################
generateInvalidFile()
{
# type <string>
local type=${1}
# We generate the host file
generateHostsFile 'invalid'
percentage 'counter' 'inv'
if [[ ${noFiles} == false && ${splitFiles} == true ]]
then
if [[ -f ${outputInvalidResult} ]]
then
if [[ ${outputLess} == true ]]
then
# We save the domain and his data
prints "Less" "${domain}" "${invalidStatus}" "${type}" >> ${outputInvalidResult}
else
# We save the domain and his data
prints "Invalid" "${domain}" "${type}" >> ${outputInvalidResult}
fi
else
if [[ ${outputLess} == true ]]
then
# We create header into and in between, create the file
beforeHeader ${outputInvalidResult}
printHeader "Less" >> ${outputInvalidResult}
# We save the domain and his data
prints "${domain}" "${invalidStatus}" "${type}" >> ${outputInvalidResult}
else
# We create header into and in between, create the file
beforeHeader ${outputInvalidResult}
printHeader 'Invalid' >> ${outputInvalidResult}
# We save the domain and his data
prints "Invalid" "${domain}" "${type}" >> ${outputInvalidResult}
fi
fi
elif [[ ${noFiles} == false ]]
then
# We generate the unified file
generateUnifiedFile "${type}" "${invalidStatus}" "Unknown"
fi
}
################################# Generate Error File ##########################
# In charge to print data into error.txt
#
# @CalledBy errorHandle
################################################################################
generateErrorFile()
{
# type <string>
local type=${1}
# We generate the host file
generateHostsFile 'inactive'
percentage 'counter' 'ina'
if [[ ${noFiles} == false && ${splitFiles} == true ]]
then
if [[ ${referer} == '' ]]
then
# We assign "Not Found" to prevent empty column in files
referer='Not Found'
fi
if [[ -f ${outputErrorResult} ]]
then
# We save the domain and his data
prints "Error" "${domain}" "${referer}" "${type}" >> ${outputErrorResult}
else
# We create header into and in between, create the file
beforeHeader ${outputErrorResult}
printHeader "Error" >> ${outputErrorResult}
# We save the domain and his data
prints "Error" "${domain}" "${referer}" "${type}" >> ${outputErrorResult}
fi
elif [[ ${noFiles} == false ]]
then
# We generate the unified files
generateUnifiedFile "${type}" "${errorStatus}" "Unknown"
fi
}
############################ Generate Valid File ###############################
# In charge to print data into error.txt
#
# @CalledBy getReferer, errorHandle
################################################################################
generateValidFile(){
# We get the expiration date
# exp <string>
local exp=${1}
# type <string>
local type=${2}
# If empty, we change the data of ${exp}
if [[ ${exp} == '' ]]
then
exp='Unknown'
fi
# If ${quiet} != false, we don't print anything
if [[ ${quiet} == false ]]
then
if [[ ${outputLess} == true ]]
then
# We save domain and its data
prints "Less" "${domain}" "${validStatus}" "${type}"
else
# We print domain and its data
prints "Generic" "${domain}" "${validStatus}" "${exp}" "${type}"
fi
fi
# we generate the host file
generateHostsFile 'active'
percentage 'counter' 'act'
# We execute this only if ${noFiles} == false
if [[ ${noFiles} == false && ${splitFiles} == true ]]
then
if [[ -f ${outputValidResult} ]]
then
if [[ ${outputLess} == true ]]
then
# We save domain and its data
prints "Less" "${domain}" "${validStatus}" "${type}" >> ${outputValidResult}
else
# We save domain and its data
prints "Valid" "${domain}" "${exp}" "${type}" >> ${outputValidResult}
fi
else
if [[ ${outputLess} == true ]]
then
# We create a file and add header
beforeHeader ${outputValidResult}
printHeader "Less" >> ${outputValidResult}
# We add domain and its data to file
prints "Less" "${domain}" "${validStatus}" "${type}" >> ${outputValidResult}
else
# We create a file and add header
beforeHeader ${outputValidResult}
printHeader 'Valid' >> ${outputValidResult}
# We add domain and its data to file
prints "Valid" "${domain}" "${exp}" "${type}" >> ${outputValidResult}
fi
fi
elif [[ ${noFiles} == false ]]
then
# We generate the unified file
generateUnifiedFile "${type}" "${validStatus}" "${exp}"
fi
}
############################## Error Handle ####################################
# Used to return issue status and issue url
#
# @CalledBy getReferer
################################################################################
errorHandle()
{
# We get/save the type
# type <string>
local type=${1}
# We create a variable to catch the case that domain don't pass lslookup
local issueStatus=''
# We set the current type
sourceType="NSLOOKUP"
# We save output of nslookup
local lookup=$(nslookup ${domain})
regexNxDomain='NXDOMAIN'
if [[ ${lookup} =~ ${regexNxDomain} ]]
then
# If domain is down
issueStatus='Unknown'
elif [[ ${issueStatus} == '' ]]
then
# If domain is up, We generate the valid/unified file
generateValidFile "Unknown" "${sourceType}"
return 1
fi
if [[ ${type} == 'Invalid' && ${issueStatus} == 'Unknown' ]]
then
# We assign the status
local status="${invalidStatus}"
# We generate the invalid/unified file
generateInvalidFile "${sourceType}"
elif [[ ${issueStatus} == 'Unknown' && ${type} == 'Error' || ${type} != 'Invalid' || ${type} != 'Valid' ]]
then
# We assign the status
local status="${errorStatus}"
# We generate the error/unified file
generateErrorFile "${sourceType}"
fi
# If quiet != false, we don't print anything
if [[ ${quiet} == false ]]
then
if [[ ${outputLess} == true ]]
then
# We print data to screen
prints "Less" "${domain}" "${status}" "${sourceType}"
else
# We print data to screen
prints "Generic" "${domain}" "${status}" "Unknown" "${sourceType}"
fi
fi
}
############################## No Referers Log #################################
# Used to generate the logs
#
# @CalledBy getReferer
################################################################################
noRefererLog(){
if [[ ${outputLogs} == true ]]
then
# We save the output of the captured date into log directory
printf "=%.0s" {1..100} >> ${noRefererLogOutput}.${domainExtension}
printf "\nNo referer found for: %s domains\n" "${domainExtension}" >> ${noRefererLogOutput}.${domainExtension}
printf "Tested domain: %s\n" "${initialDomain}" >> ${noRefererLogOutput}.${domainExtension}
printf "=%.0s" {1..100} >> ${noRefererLogOutput}.${domainExtension}
printf "\n" >> ${noRefererLogOutput}.${domainExtension}
fi
}
############################### Get Referer ####################################
# Used to generate the report issue url
#
# @CalledBy getExpirationDate
################################################################################
getReferer(){
# initialDomain <string>
initialDomain=$(echo ${1} |awk '{print tolower($0)}')
# We get the extension of the given domain
domainExtension=${initialDomain##*.}
# We get the list of allowed extensions
validExtension=$(cat ${ianaDB})
# We check if the given domain extension is in our database file
if [[ ${validExtension} =~ ${domainExtension} ]]
then
# We initiate this variable in order to know if ${domainExtension} pass
# The cases
notSupported=true
# Special cases domains which don't have their whois server
# registered to IANA.
# cf. https://github.com/funilrys/funceble/issues?q=is%3Aissue+Not+Found+is%3Aclosed
case ${domainExtension} in
ga)
# We can now use the domain
domain=${initialDomain}
# We assign the domain WHOIS referer server
referer="whois.my.ga"
# The Following is used to stop the execution of getExpirationDate
# in case the domain is not valid.
notSupported=false
;;
za)
# We can now use the domain
domain=${initialDomain}
# We assign the domain WHOIS referer server
referer="whois.registry.net.za"
# The Following is used to stop the execution of getExpirationDate
# in case the domain is not valid.
notSupported=false
;;
bz)
# We can now use the domain
domain=${initialDomain}
# We assign the domain WHOIS referer server
referer="whois.afilias-grs.info"
# The Following is used to stop the execution of getExpirationDate
# in case the domain is not valid.
notSupported=false
;;
esac
if [[ ${notSupported} == true ]]
then
# We get the host to call.
# If extension have a host assigned, it's written if format
# refer: whois.hello.world for example
referer=$(whois -h "${whoisServer}" "${initialDomain}" | awk '/refer/{print $NF}')
# regex to pass to have a valid referer
regexWhois='(.*)whois(.*)'
# We check if the format is valid
if [[ ${referer} =~ ${regexWhois} ]]
then
# We can now use the domain
domain=${initialDomain}
# The Following is used to stop the execution of getExpirationDate
# in case the domain is not valid.
notSupported=false
else
# We empty the referer variable
referer=''
# We log the issue
noRefererLog
# We handle error & check if server is up
errorHandle 'Error'
fi
fi
else
# We handle error & check if server is up
errorHandle 'Invalid'
fi
}
################################## Whois Log ###################################
# Used to generate the logs
#
# @CalledBy getExpirationDate
################################################################################
whoisLog(){
if [[ ${outputLogs} == true ]]
then
# We save the output of whois into log directory
printf "=%.0s" {1..100} >> ${whoisLogOutput}${referer}
printf "\n" >> ${whoisLogOutput}${referer}
cat ${whoIs} >> ${whoisLogOutput}${referer}
printf "\n" >> ${whoisLogOutput}${referer}
printf "=%.0s" {1..100} >> ${whoisLogOutput}${referer}
printf "\n" >> ${whoisLogOutput}${referer}
fi
}
############################## Date Format Log #################################
# Used to generate the logs
#
# @CalledBy formatDate
################################################################################
dateFormatlog(){
if [[ ${outputLogs} == true ]]
then
# We save the output of the captured date into log directory
printf "=%.0s" {1..100} >> ${dateLogOutput}${referer}
printf "\nExpiration Date:%s\n" "${expirationDate}" >> ${dateLogOutput}${referer}
printf "=%.0s" {1..100} >> ${dateLogOutput}${referer}
printf "\n" >> ${dateLogOutput}${referer}
fi
}
####################### Get (Domain) Expiration Date ###########################
# Used to get the expiration date of a given domain.
#
# @CalledBy None
################################################################################
getExpirationDate(){
# domain <string>
# Domain given by user
domain=${1}
# We get the whois server referenced for the given domain
getReferer ${domain}
# Ensure that it's supported and not empty
if [[ ${notSupported} == false && ${domain} != "" && ${referer} != "" ]]
then
# We call the whois server and save the result into ${whoIs}
# cf. Default Values Section
timeout ${secondsBeforeTimeout} "whois -h ${referer} ${domain} > ${whoIs}"
# We get the content of the previously saved file
local content=$(cat ${whoIs})
# Here's a list of words the ${content} have to match to get the
# expiration date from the whois record of domain
local toMatch=('expire:' 'expire on:' 'Expiry Date:' 'free-date' 'expires:' 'Expiration date:' 'Expiry date:' 'Expire Date:' 'renewal date:' 'Expires:' 'validity:' 'Expiration Date :' 'Expiry :' 'expires at:' 'domain_datebilleduntil:' 'Data de expiração \/ Expiration Date \(dd\/mm\/yyyy\):' 'Fecha de expiración \(Expiration date\):' '\[Expires on\]' 'Record expires on' 'status: OK-UNTIL' 'renewal:' 'expires............:' 'expire-date:' 'Exp date:' 'Valid-date' 'Expires On:' 'Fecha de vencimiento:' 'Expiration:.........' 'Fecha de Vencimiento:' 'Registry Expiry Date:' 'Expires on..............:' 'Expiration Time:' 'Expiration Date:' 'Expired:' "Date d'expiration:")
for i in ${!toMatch[*]}
do
if [[ ${content} =~ ${toMatch[${i}]} ]]
then
# We get the expiration date
if [[ ${toMatch[${i}]} == '\[Expires on\]' ]]
then
expirationDate=$(cat ${whoIs} | awk "/${toMatch[${i}]}/{print $NC}" | sed "s/\] /]:/")
elif [[ ${toMatch[${i}]} == 'Record expires on' ]]
then
expirationDate=$(cat ${whoIs} | awk "/${toMatch[${i}]}/{print $NC}" | sed "s/on/on:/" | sed "s/\s(YYYY-MM-DD)//")
else
expirationDate=$(cat ${whoIs} | awk "/${toMatch[${i}]}/{print $NC}")
fi
expirationDate=${expirationDate#*:}
expirationDate=$(echo -e "${expirationDate}" | sed -e 's/^[[:space:]]*//')
# Regex to pass in order to be reconized as a date
regexNumber='[0-9]'
if [[ ${expirationDate} =~ ${regexNumber} ]]
then
# If it contain numbers, we format it
formatDate
# We set the current type
sourceType="WHOIS"
# We generate valid/unified file
generateValidFile "${expirationDate}" "${sourceType}"
return 1
elif [[ ${expirationDate} == '' && ${debugUnknown} == true ]]
then
# In case expiration Date is empty we save the record so we
# can debug it.
# In case this happen please report issue to
# https://github.com/funilrys/funceble/issues/new
# So it can be fixed for everyone who use this script
#
# You can find the logs under output/logs/whois/
# or with the command `ls output/logs/whois/*`
whoisLog
fi
elif [[ ${i} == $(( ${#toMatch[@]} - 1)) ]]
then
if [[ ${debugUnknown} == true ]]
then
# generate whois log file
whoisLog
fi
# We handle error & check if server is up
errorHandle 'Error'
fi
done
fi
}
################################ Date to Lower #################################
# Used to convert date to lower case
#
# @CalledBy formatDate
################################################################################
dateToLower(){
expirationDate=$(echo "${expirationDate}" | awk '{print tolower($0)}')
}
############################# Get Month In English #############################
# Used to convert month from numeric to alphabetic
#
# @CalledBy formatDate
################################################################################
convertOrShortenMonth(){
local numericMonth=${1}
case ${numericMonth} in
1|01|January)
month='jan'
;;
2|02|February)
month='feb'
;;
3|03|March)
month='mar'
;;
4|04|April)
month='apr'
;;
5|05|May)
month='may'
;;
6|06|June)
month='jun'
;;
7|07|July)
month='jul'
;;
8|08|August)
month='aug'
;;
9|09|September)
month='sep'
;;
10|October)
month='oct'
;;
11|November)
month='nov'
;;
12|December)
month='dec'
;;
*)
month='0'
;;
esac
}
############################# Convert 1 to 2 Digits ##########################
# Used to convert one digit to two Digits numbers
# e.g -> 1 ==> 01
#
# @CalledBy formatDate
################################################################################
convert1To2digits(){
local number="${1}"
local monthOrDay="${2}"
local validNumber=('1' '2' '3' '4' '5' '6' '7' '8' '9')
local null='0'
for i in ${!validNumber[*]}
do
if [[ ${number} == ${validNumber[${i}]} ]]
then
case "${monthOrDay}" in
month)
month=${null}${number}
break
;;
day)
day=${null}${number}
break
;;
esac
fi
done
}
################################ Format Date ###################################
# Used to format date. We have to work with multiple date format from WHOIS
# records so this function is in charge to create an unified format.
#
# @CalledBy getExpirationDate
################################################################################
formatDate()
{
# Date in format: 02-jan-2017
local regex1='[0-9]{2}-[a-z]{3}-[0-9]{4}'
# Date in format: 02.01.2017 // Month: jan
local regex2='[0-9]{2}\.[0-9]{2}\.[0-9]{4}$'
# Date in format: 02/01/2017 // Month: jan
local regex3='[0-9]{2}\/[0-9]{2}\/[0-9]{4}'
# Date in format: 2017-01-02 // Month: jan
local regex4='[0-9]{4}-[0-9]{2}-[0-9]{2}$'
# Date in format: 2017.01.02 // Month: jan
local regex5='[0-9]{4}\.[0-9]{2}\.[0-9]{2}$'
# Date in format: 2017/01/02 // Month: jan
local regex6='[0-9]{4}\/[0-9]{2}\/[0-9]{2}$'
# Date in format: 2017.01.02 15:00:00
local regex7='[0-9]{4}\.[0-9]{2}\.[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}'
# Date in format: 20170102 15:00:00 // Month: jan
local regex8='[0-9]{8}\s[0-9]{2}:[0-9]{2}:[0-9]{2}'
# Date in format: 2017-01-02 15:00:00 // Month: jan
local regex9='[0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}'
# Date in format: 02.01.2017 15:00:00 // Month: jan
local regex10='[0-9]{2}\.[0-9]{2}\.[0-9]{4}\s[0-9]{2}:[0-9]{2}:[0-9]{2}'
# Date in format: 02-Jan-2017 15:00:00 UTC
local regex11='[0-9]{2}-[A-Z]{1}[a-z]{2}-[0-9]{4}\s[0-9]{2}:[0-9]{2}:[0-9]{2}\s[A-Z]{1}.*'
# Date in format: 2017/01/02 01:00:00 (+0900) // Month: jan
local regex12='[0-9]{4}\/[0-9]{2}\/[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}\s\(.*\)'
# Date in format: 2017/01/02 01:00:00 // Month: jan
local regex13='[0-9]{4}\/[0-9]{2}\/[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}$'
# Date in format: Mon Jan 02 15:00:00 GMT 2017
local regex14='[a-zA-Z]{3}\s[a-zA-Z]{3}\s[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}\s[A-Z]{3}\s[0-9]{4}'
# Date in format: Mon Jan 02 2017
local regex15='[a-zA-Z]{3}\s[a-zA-Z]{3}\s[0-9]{2}\s[0-9]{4}'
# Date in format: 2017-01-02T15:00:00 // Month: jan
local regex16='[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$'
# Date in format: 2017-01-02T15:00:00Z // Month: jan
local regex17='[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}[A-Z].*'
# Date in format: 2017-01-02T15:00:00+0200 // Month: jan
local regex18='[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}[+-][0-9]{4}'
# Date in format: 2017-01-02T15:00:00+0200.622265+03:00 // Month: jan
local regex19='[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9].*[+-][0-9]{2}:[0-9]{2}'