-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhostflash
executable file
·2151 lines (1640 loc) · 57.6 KB
/
hostflash
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 -euo pipefail
# set -x
IFS=$'\n\t'
###
# Host Flash™ 4.0.1
#
# Downloads hosts file rules from various sources.
# Installs hosts file rules into /etc/hosts
# Integrates Host Flash™ hosts file rules with rules that already exist in /etc/hosts
#
# For OS: Linux (Debian)
# Tested With: Ubuntu flavours
#
# Lead Author: Lee Hodson<https://github.com/vr51//>
# Contributor: skeletonkey<https://github.com/skeletonkey/>
# Donate: https://paypal.me/vr51
# Website: https://host-flash.com<https://host-flash.com>
# This Release: 9th Oct 2022
# First Written: 18th Oct. 2015
# First Release: 2nd Nov. 2015
#
# Copyright Host Flash™ https://host-flash.com<https://host-flash.com>
# License: GPL3
#
# Programmer: Lee Hodson<journalxtra.com>, VR51<vr51.com>
#
# Use of this program is at your own risk
#
# INSTALLS OR UPDATES
#
# Use Host Flash™ to block access to websites (hosts), ad servers, malicious websites and time wasting websites.
# Use Host Flash™ to manage your hosts file
#
# TO RUN:
#
# Ensure the script is executable:
#
# Right-click > properties > Executable
#
# OR
#
# chmod u+x hostflash
#
# Launch by clicking the script file or by typing `bash ./hostflash` at the command line.
#
# Host Flash™ will attempt to preserve existing host rules.
#
###
##
# NEW in 4
# Auto-detect source file type. No need to flag the file type in custom lists. The now obsolete flags were: zip, p7, raw and direct.
# New directory structure
# Permanent Directories
# ~/hostflash/backups
# ~/hostflash/DOCS
# ~/hostflash/logs
# Temp. Directories. These are deleted when a new hosts file is built (unless DEBUG is set to '1', in which case they are left in place)
# ~/src/tmp/download
# ~/src/tmp/unprocessed
# ~/src/tmp/processed
# Config Directory restructure
# ~/.configs/hostflash
# Files are no longer dot files.
# ~/.configs/hostflash/lists # To be used later
# ~/.configs/hostflash/menus # To be used later
# ~/.configs/hostflash/plugins # To be used later
#
# Replaced hosts files are now zipped to the backup directory. Restore option to be added later.
# Hosts file summary info is now written to the history log when a host file is built.
# __log now logs three message types: log, hist and debug. The __debug will eventually be rolled into __log.
# __log with 'debug' flag causes cURL to log to the debug file the head fetched with source file downloads.
#
##
##
# TO DO / DEV NOTES
# Please feel free to work through this list if you wish to help the project.
#
#
#
#
# line 1583 fix restore. should point to backup directory and unzip into tmp directory then test for hosts before moving it to /etc/hosts
##
#
# Modularisation
# Host Flash is meant to be a single file downloadable script.
# I (Lee) might convert the script to an installer that chops itself up to extract modules.
# This would enable Host Flash to accept plugins, facilitate user customizations and facilitate HF's future development.
# TBC...
#
# Use one file per list.
# Download Free Mode. Place source files into directory. HF builds from source files.
# Rebuild source file process method
#
# If internet is available (Check exit status of latest Git commit check: $networked should = 0 if the net is available)
# Download DOCs
# Offer HF script updates
# Show error if internet is unavailable
#
# versions: add deb installer
#
# En/disable Automatic list update
# En/disable Automatic script update
#
# CLI: disable HF, enable HF, add rule, remove rule, update lists, set options
#
# List Build Options
# Build pure list. No whitelisting
# Check whitelists (extracted, found, not found) # To be integrated from standalone file.
# Rebuild whitelists after check
# Build filtered list. Whitelisting done
# Install after build
# Do not install after build
# Compact local whitelists and local blacklists (all lower, sort -u)
# View/Edit local lists (nano, pico, cat)
#
# Feature: search hosts file for a domain or list of domains (grep without regex)
# Offer to disable if found
# Offer to add to custom whitelist
#
# Switchlist
# Temporarily disable and enable whitelist hosts
# Options
# Check .hfremoved list # Script written and tested ready for insertion
# Move items from .hfremoved to post processing blacklist
# Move items from .hfremoved to Switch List
#
# Move more actions into functions.
# Tidy up
# Add input checking/sanitisation
# Make messages and menus clearer
# Menu + View all HF configs
#
# Summary info on main screen / maybe footer for all screens
# Add more useful info
# Include link to host-flash.com
#
# Info about each list
# Last update time
#
# Go through functions to state the output format
#
# Rebuild menu system based on SaLi text mode only
###
##
# NOTES
#
# Hosts File Build Process
#
# The host file work is done in $HOME/src/hostflash/tmp. This eventually builds hosts.txt
# The final build of hosts.txt is moved to $HOME/src/hostflash/
# __enable looks for $HOME/src/hostflash/hosts.txt which it moves to /etc/hosts
#
# Whitelist Processing
#
# Whitelist wild is processed twice:
# As a normal whitelist
# As a wild whitelist
# A period is added before the first character of each host name processed
##
##
# CONFIGS ARE BELOW HERE - should be no reason to manually change these
#
# Any of these can be overruled by adding each variable along with its new value into $HOME/.config/hostflash/.hfrc
# Adding config changes to $HOME/.config/hostflash/.hfrc will protect changes from script version updates.
##
# IMPORTANT: Do not Use trailing slashes in URLs, URIs or directory paths
package='Host Flash™' # Name of software installed by this installer
version='4.0.1 - 9th Oct 2022'
debug='1'
## Clear screen
function __clear() {
if test $debug -eq '0' ; then
clear
fi
}
## Source loader
# Load source files
# This facilitates modularisation
function __flying_sourcer() {
SourcerSection="$1"
if source "${SourcerSection}"
then
source "${SourcerSection}"
else
. "${SourcerSection}"
fi
}
# Clear arrays
unset app conf dir file in list log message rem menu select mode essentialpackages credits flush listc listcc listsc
# Declare associative arrays
declare -A app conf dir file list log message rem
# Declare regular arrays and types
declare -a menu # Menu options are set within __menu_main()
declare -a select # Menu options count
declare -a mode # Used for notices
declare -a essentialpackages # Packages to install to help the build process
declare -a credits # Hosts file credits
declare -a flush # DNS cache flush commands
declare -i listc listcc listsc # For list repository counts
# Remote Hosts
rem[gith]='https://github.com/VR51/host-flash' # Github home URL for this package.
rem[gitp]='https://github.com/VR51/host-flash.git' # Software source package git address
rem[tld]='https://data.iana.org/TLD/tlds-alpha-by-domain.txt' # Official list of valid TLDs
# Directories
# These directories are created by dir[@] dump. Be careful when adding directories to this list
#
# Top level directories
dir[hfh]="$HOME/hostflash" # Host Flash Home directory
dir[src]="$HOME/src/hostflash" # Directory where source hosts files are stored locally
dir[conf]="$HOME/.config/hostflash" # Location of custom configuration files.
# Sub level directories
## These are permanent doc and log directories
dir[docs]="${dir[hfh]}/DOC" # Location of program documents. # To be written
dir[logs]="${dir[hfh]}/logs" # Location of host file logs.
dir[bck]="${dir[hfh]}/backups" # Location of host file backups.
## These are config directories
dir[lists]="${dir[conf]}/lists" # Modular lists
dir[menus]="${dir[conf]}/menus" # Modular menus
dir[plugs]="${dir[conf]}/plugins" # Modular plugins
## These are wiped each time a hosts file is built unless DEBUG is set to '1'.
dir[tmp]="${dir[src]}/tmp" # Directory where temporary files are stored locally
dir[tmpd]="${dir[tmp]}/downloads" # Directory where source hosts files downloaded and unpacked before being moved
dir[tmpu]="${dir[tmp]}/unprocessed" # Directory where unprocessed source hosts files are stored
dir[tmpp]="${dir[tmp]}/processed" # Directory where processed source hosts files are stored. This allows us to check events.
# Files
file[hosts]=''
# System hosts file location. Where it is copied from and where it is installed to.
# See next code block, it replaces this current section. This section is here for reference or in case there is need to manually set the path.
#
# file[hosts]='/etc/hosts' # Location of system hosts file in Unix, Unix-like and POSIX systems.
# file[hosts]='/private/etc/hosts' # iOS, Apple Mac
# file[hosts]='/system/etc/hosts' # Android
# file[hosts]='%SystemRoot%\System32\drivers\etc\hosts' # Windows NT, 2000, XP,[5] 2003, Vista, 2008, 7, 2012, 8, 10
# Custom Hosts File location
# file[hosts]='/enter/path/to/file/called/hosts' # Add the path to your system's hosts file then uncomment (remove the #) and save to activate.
__clear
if test ! "${file[hosts]}"; then
if test -f "/system/etc/hosts"; then
file[hosts]='/system/etc/hosts'
elif test -f "/private/etc/hosts"; then
file[hosts]='/private/etc/hosts'
elif test -f "/etc/hosts"; then
file[hosts]='/etc/hosts'
else
echo "Unable to detect the location of this system's hosts file."
echo "Please edit the hostflash program at around line 175 to set a custom value for ${bold}file[hosts]${normal} to point to your system's hosts file"
echo "and uncomment the line edited."
echo "Alternatively, add ${bold}file[hosts]='/path/to/your/hosts'${normal} to ${bold}$HOME/.config/hostflash/.hfrc${normal} if the change should survive script updates."
echo "Visit ${bold}${rem[gith]}${normal} if you want us to add your host file's location to the automatic detection list."
echo "Press any key to exit $package"
read a
exit 1
fi
fi
# HF Config Files
# These are created by file[@] dump. Be careful when adding files to this list
file[conf]="${dir[conf]}/.hfrc" # Location of user's custom settings for Host Flash™.
file[confwl]="${dir[conf]}/.hfwlrc" # Location of the user's custom whitelist.
file[confwlw]="${dir[conf]}/.hfwlwrc" # Location of the user's custom whitelist-wild.
file[confbl]="${dir[conf]}/.hfblrc" # Location of the user's custom blacklist.
file[confcl]="${dir[conf]}/.hfclrc" # Location of the user's custom hosts lists.
file[confr]="${dir[conf]}/.hfremoved" # Hosts removed from hosts list during hosts build/update.
file[conftld]="${dir[conf]}/.hftld" # Location of valid TLD list.
# Log files
file[hist]="${dir[logs]}/history.txt" # Location of update history.
file[log]="${dir[logs]}/log.txt" # Location of the general process log file.
file[debug]="${dir[logs]}/debug.txt" # Location of debug logging file.
# Next files to be used in a later version of HF
file[confpost]="${dir[conf]}/.hfpost" # Location of the post processing blacklist (block hosts that are wild needlessly whitelisted)
file[confsl]="${dir[conf]}/.hfslrc" # Location of the user's hosts switch list.
# App Launcher Settings
# These are specific to the application/task manager installer (yet to be implemented)
app[icon]="${dir[src]}/data" # Directory where the application icon will be located.
app[bin]='/usr/bin' # Binary installation path. Where should the compiled binary be installed to? Exact path. # Not required for this installer.
app[icon]="hostflash.svg" # Icon name
app[type]='Application' #
app[cats]='Utilities' # Application launcher categories
app[binary]="hostflash" # Name of the binary file that is this program.
# Detected settings
user=$(whoami) # Current User
group=$(id -g -n $user) # Current user's primary group
###
# DECLARE LIST ARRAYS
###
# Declare custom list associative arrays. If they are not declared we cannot use them.
# Count Lists
listc=$(grep -E '\[status[[:digit:]]+]=' "$0" | wc -l) # Count lists in hostflash script
if test $(grep -En '\[status[[:digit:]]+]=' "${file[confcl]}")
then
listcc=$(grep -E '\[status[[:digit:]]+]=' "${file[confcl]}" | wc -l) # Count custom lists in .hfclrc
else
listcc=0
fi
let listsc=listc+listcc
##
# LISTS
#
# These are set with
#
# list[status]='Enabled / Disabled' # + or - for TRUE or FALSE
# list[info]='List Domain' # The name of the list
# list[src]='Download URL' # From where the package will be retrieved
# list[file]='Download Package' # Name of package retrieved
# list[target]='Package Target' # Name of usable file in a retrieved zip package / Name of file created on retrieval if FLAG is raw
# list[ip]='0' # 0 or 1 for false or true. Add loopback IP address to downloaded list. This is required when a hosts list consists of domain names with no IP prefix. Not required for whitelists, in fact should never be set to '1' for whitelists.
#
# Changes to list[status] are stored in .hfrc
# Custom lists will be added to .hfclrc
# Framework in place: conf etc... Needs menu section to be added.
##
# TO DO: Change to list[NUM TYPE]
# https://hosts-file.net/?s=Download <-- DEAD. This is now a Malwarebytes browser addon.
# Files from here now mirrored and updated at https://github.com/fredprod/host-file.net-backup
# & https://github.com/blackskye-sx/host-file.net-backup
list[status1]='+'
list[info1]='hosts-file.net - Ad Servers (Liberal)'
list[src1]='https://raw.githubusercontent.com/blackskye-sx/host-file.net-backup/master'
list[file1]='ad_servers.txt'
list[target1]='ad_servers.txt'
list[ip1]='0'
list[status2]='+'
list[info2]='hosts-file.net - EMD (Liberal)'
list[src2]='https://raw.githubusercontent.com/blackskye-sx/host-file.net-backup/master'
list[file2]='emd.txt'
list[target2]='emd.txt'
list[ip2]='0'
list[status3]='+'
list[info3]='hosts-file.net - EXP (Liberal)'
list[src3]='https://raw.githubusercontent.com/blackskye-sx/host-file.net-backup/master'
list[file3]='exp.txt'
list[target3]='exp.txt'
list[ip3]='0'
list[status4]='+'
list[info4]='hosts-file.net - FSA (Liberal)'
list[src4]='https://raw.githubusercontent.com/blackskye-sx/host-file.net-backup/master'
list[file4]='fsa.txt'
list[target4]='fsa.txt'
list[ip4]='0'
list[status5]='+'
list[info5]='hosts-file.net - GRM (Liberal)'
list[src5]='https://raw.githubusercontent.com/blackskye-sx/host-file.net-backup/master'
list[file5]='grm.txt'
list[target5]='grm.txt'
list[ip5]='0'
list[status6]='+'
list[info6]='hosts-file.net - HFS (Liberal)'
list[src6]='https://raw.githubusercontent.com/blackskye-sx/host-file.net-backup/master'
list[file6]='hfs.txt'
list[target6]='hfs.txt'
list[ip6]='0'
list[status7]='+'
list[info7]='hosts-file.net - HJK (Liberal)'
list[src7]='https://raw.githubusercontent.com/blackskye-sx/host-file.net-backup/master'
list[file7]='hjk.txt'
list[target7]='hjk.txt'
list[ip7]='0'
list[status8]='+'
list[info8]='hosts-file.net - MMT (Liberal)'
list[src8]='https://raw.githubusercontent.com/blackskye-sx/host-file.net-backup/master'
list[file8]='mmt.txt'
list[target8]='mmt.txt'
list[ip8]='0'
list[status9]='+'
list[info9]='hosts-file.net - PHA (Liberal)'
list[src9]='https://raw.githubusercontent.com/blackskye-sx/host-file.net-backup/master'
list[file9]='pha.txt'
list[target9]='pha.txt'
list[ip9]='0'
list[status10]='+'
list[info10]='hosts-file.net - PSH (Liberal)'
list[src10]='https://raw.githubusercontent.com/blackskye-sx/host-file.net-backup/master'
list[file10]='psh.txt'
list[target10]='psh.txt'
list[ip10]='0'
list[status11]='+'
list[info11]='hosts-file.net - PUP (Liberal)'
list[src11]='https://raw.githubusercontent.com/blackskye-sx/host-file.net-backup/master'
list[file11]='pup.txt'
list[target11]='pup.txt'
list[ip11]='0'
list[status12]='+'
list[info12]='hosts-file.net - WRZ (Liberal)'
list[src12]='https://raw.githubusercontent.com/blackskye-sx/host-file.net-backup/master'
list[file12]='wrz.txt'
list[target12]='wrz.txt'
list[ip12]='0'
list[status13]='+'
list[info13]='mvps.org (Liberal: Blocks mostly adware, spyware, malware and trackers. Big list)'
list[src13]='https://winhelp2002.mvps.org'
list[file13]='hosts.zip'
list[target13]='HOSTS'
list[ip13]='0'
list[status14]='+'
list[info14]='free.fr-Trackers (Moderate: Blocks trackers)'
list[src14]='http://rlwpx.free.fr/WPFF'
list[file14]='htrc.7z'
list[target14]='Hosts.trc'
list[ip14]='0'
list[status15]='+'
list[info15]='free.fr-Ad-Servers (Moderate: Blocks ad servers)'
list[src15]='http://rlwpx.free.fr/WPFF'
list[file15]='hpub.7z'
list[target15]='Hosts.pub'
list[ip15]='0'
list[status16]='+'
list[info16]='free.fr-Malware (Moderate: Blocks malware domains)'
list[src16]='http://rlwpx.free.fr/WPFF'
list[file16]='hrsk.7z'
list[target16]='Hosts.rsk'
list[ip16]='0'
list[status17]='+'
list[info17]='free.fr-Adult (Moderate: Blocks adult domains. Coincidentally blocks forums and some social sites)'
list[src17]='http://rlwpx.free.fr/WPFF'
list[file17]='hsex.7z'
list[target17]='Hosts.sex'
list[ip17]='0'
list[status18]='+'
list[info18]='free.fr-Misc (Moderate: Blocks miscellaneous domains)'
list[src18]='http://rlwpx.free.fr/WPFF'
list[file18]='hmis.7z'
list[target18]='Hosts.mis'
list[ip18]='0'
list[status19]='+'
list[info19]='someonewhocares.org (Liberal: Blocks mostly adware, spyware, malware and trackers. Small list)'
list[src19]='https://someonewhocares.org/hosts'
list[file19]='hosts'
list[target19]='hosts.txt'
list[ip19]='0'
list[status20]='+'
list[info20]='malwaredomainlist.com (Liberal: Blocks mostly malware. Small list)'
list[src20]='https://www.malwaredomainlist.com/hostslist'
list[file20]='hosts.txt'
list[target20]='hosts.txt'
list[ip20]='0'
list[status21]='+'
list[info21]='KADhosts (Liberal: Blocks malware hosts)'
list[src21]='https://raw.githubusercontent.com/azet12/KADhosts/master/KADhosts.txt'
list[file21]=''
list[target21]='hosts.txt'
list[ip21]='0'
list[status22]='+'
list[info22]='yoyo.org (Liberal: Blocks adservers)'
list[src22]='https://pgl.yoyo.org/as/serverlist.php?showintro=0;hostformat=hosts'
list[file22]=''
list[target22]='hosts.txt'
list[ip22]='0'
list[status23]='+'
list[info23]='Mitchell Krog (Liberal: Blocks spam bot hosts)'
list[src23]='https://raw.githubusercontent.com/mitchellkrogza/Badd-Boyz-Hosts/master/hosts'
list[file23]=''
list[target23]='hosts.txt'
list[ip23]='0'
list[status24]='+'
list[info24]='CoinBlockerLists (Liberal: Blocks cryptominers)'
list[src24]='https://zerodot1.gitlab.io/CoinBlockerLists/hosts_browser'
list[file24]=''
list[target24]='hosts.txt'
list[ip24]='0'
list[status25]='+'
list[info25]='Developer Dan Ads and Trackers Safe List (Liberal: Blocks ads and trackers)'
list[src25]='https://www.github.developerdan.com/hosts/lists'
list[file25]='ads-and-tracking-extended.txt'
list[target25]='hosts.txt'
list[ip25]='0'
list[status26]='+'
list[info26]='Developer Dan Ads and Trackers Aggressive List (Moderate: Could cause browsing issues)'
list[src26]='https://www.github.developerdan.com/hosts/lists'
list[file26]='tracking-aggressive-extended.txt'
list[target26]='hosts.txt'
list[ip26]='0'
# Alternative address: https://raw.githubusercontent.com/AdAway/adaway.github.io/master/hosts.txt
list[status27]='+'
list[info27]='AdAway (Liberal: Blocks ad networks)'
list[src27]='https://adaway.org'
list[file27]='hosts.txt'
list[target27]='hosts.txt'
list[ip27]='0'
list[status28]='+'
list[info28]='hostsfile.org (Very Strict: Regular blocks + porn, gambling and gaming sites)\n'
list[src28]='https://www.hostsfile.org/Downloads'
list[file28]='hosts.txt'
list[target28]='hosts.txt'
list[ip28]='0'
# Others
list[status29]='+'
list[info29]='Community Whitelist (Host Flash™ whitelist. Unblocks a limited set of sites blocked by the other blacklists)'
list[src29]='https://gist.githubusercontent.com/VR51/7eaace2b6778ea508996/raw/'
list[file29]='remw'
list[target29]='whitelist.txt'
list[ip29]='0'
list[status30]='+'
list[info30]='Community Whitelist Wild (Host Flash™ wild whitelist. Unblocks a limited set of sites blocked by the other blacklists)'
list[src30]='https://gist.githubusercontent.com/VR51/9798c78337fe2f7ad589/raw/'
list[file30]='remww'
list[target30]='whitelist-wild.txt'
list[ip30]='0'
list[status31]='+'
list[info31]='Community Blacklist (Host Flash™ blacklist. Blocks a limited set of sites not blocked by other lists)'
list[src31]='https://gist.githubusercontent.com/VR51/ef3b90b1be2693a44f27/raw/'
list[file31]='remb'
list[target31]='hosts.txt'
list[ip31]='1'
list[status32]='+'
list[info32]='Local Whitelist (Your own custom whitelist)'
list[src32]='localhost'
list[file32]="${file[confwl]}"
list[target32]='whitelist.txt'
list[ip32]='0'
list[status33]='+'
list[info33]='Local Whitelist Wild (Your own custom wild whitelist)'
list[src33]='localhost'
list[file33]="${file[confwlw]}"
list[target33]='whitelist-wild.txt'
list[ip33]='0'
list[status34]='+'
list[info34]='Local blacklist (Your own custom blacklist)'
list[src34]='localhost'
list[file34]="${file[confbl]}"
list[target34]='hosts.txt'
list[ip34]='1'
# GENERAL
essentialpackages=( zip unzip 7z curl sed git )
conf[ess]=0 # Essentials # Install build essential software. 0 = Not done, 1 = Done
conf[upv]=0 # Update Host Flash™ program.
#conf[locv]=$($binary -v) # Installed package version
conf[inst]=$( ls -l "$0" | cut -d' ' -f 6-8) # $package installation date
conf[locv]="$version installed ${conf[inst]}"
conf[gitv]=$(curl -v --silent "${rem[gith]}/commit/master" --stderr - | grep '<relative-time datetime' | sed -E 's#.+">(.+)<.+#\1#')
networked="$?" # Are we connected to the net? We are if $networked = 0.
conf[hosts]=$( ls -l "${file[hosts]}" | cut -d' ' -f5 ) # Hosts file size
conf[hostt]=$( ls -l "${file[hosts]}" | cut -d' ' -f 6-8 ) # Hosts file timestamp
conf[hostc]=$( wc -l "${file[hosts]}" | cut -d' ' -f1 ) # Hosts file line count
conf[ip]='0.0.0.0' # Redirect IP address
conf[mode]='1' # Build Only or Build and Install 0 or 1
timestamp="$(date +"%Y-%m-%d-%H-%M-%S")"
# LOGS
# Partially utilised. Usage to be expanded
# Log host file history, general log messages and debug messages
function __log() {
# @1 = File to store log
# @2 = Message to store with log
file=$1
message=$2
now="$(date +"%Y-%m-%d %H:%M:%S")"
case "$file" in
hist)
if test "$message" ; then echo "$message" >> "${file[hist]}"; fi
echo "$now: Line Count: ${conf[hostc]} | Bytes: ${conf[hosts]} | Timestamp: ${conf[hostt]} | Location: ${file[hosts]}" >> "${file[hist]}"
;;
log)
echo "$now: $message" >> "${file[log]}"
;;
debug)
echo "$now: $message" >> "${file[debug]}"
;;
esac
}
## END CONFIGS
## BEGIN
# Other settings
bold=$(tput bold)
normal=$(tput sgr0)
# Locate where we are
filepath="$( echo $PWD )"
# A Little precaution
cd "$filepath"
# Make/Remove directories and files
function __download_tlds() {
if test $(find "${file[conftld]}" -mmin +1440) || test ! -f "${file[conftld]}" # If exists but is older than 1440 minutes
then
curl -e -L "${rem[tld]}" "${rem[tld]}" -o "${file[conftld]}"
sed -e 's/\(.*\)/\L\1/' "${file[conftld]}" > "${file[conftld]}tmp"
grep -vE '^#' "${file[conftld]}tmp" > "${file[conftld]}"
# Next lines allow the file to be passed to Grep as a string of ORs
#grep -vE '^#' "${file[conftld]}tmp" | tr '[:space:]' '|' > "${file[conftld]}"
#sed -i 's/[|]$//' "${file[conftld]}" # Remove superfluous | from end of file
#rm "${file[conftld]}tmp"
fi
}
# __download_tlds # Get TLD list # Not yet used.
case "${debug}" in
0)
if test -d "${dir[tmp]}"; then
rm -r "${dir[tmp]}" # Clean the tmp directory but not if we are debugging.
fi
;;
esac
# Make directories and files that do not yet exist
mkdir -p "${dir[@]}" # Make directories
touch -a "${file[@]}" # Make config files
# INSTALL RC SETTINGS
function __repaint() {
# Loads configs then clears the screen
# Whatever calls this should reload the screen for the new configs to be reflected on the screen
# @1 = type: soft, hard, conf, ret
# soft just refreshes the screen
# hard reloads the script in the current shell
# conf must be followed by a config file to load. Loads default conf then extra conf.
# 'no argument' loads the default config file represented by ${file[conf]}
# @2..@n = config file(s) to reload
#
# ret executes the function stated in $2
#
# We use $in instead of $@
__clear
in=( $@ )
case ${in[0]} in
soft) # Reset variables
unset
__repaint
;;
hard) # Restart this script
exec bash "$0"
;;
conf) # Include basic conf and any extras listed in $@
for i in ${in[@]:1}; do
__flying_sourcer "$i"
done
__flying_sourcer "${file[conf]}"
;;
ret)
$2
;;
*) # Include basic conf only
__flying_sourcer "${file[conf]}"
;;
esac
}
# Include custom lists
__repaint conf "${file[confcl]}"
##
# Actions
##
function __run() {
# Check for terminal then run else just run program
tty -s
if test "$?" -ne 0 ; then
__launch
else
__menu_main "${1:-}" # This will be changed when CLI added
fi
}
function __hosts_update() {
# Read the file of hosts and remove any line listed in the input whitelist file
# The results are logged in a file in the config directory.
# @1 = type: (file) norm or wild, or (string) rem or remw
# @2 = file to remove data from
# @3 = file containing lines of data to remove from @2 or single line of text to remove from @2
f2="$2"
f3="$3"
ip=$(echo "${conf[ip]}" | sed 's/\./[.]/g') # Convert periods to character group (just commenting out sometimes failed)
case $1 in
norm)
# We tried a method that removed the while loop. It was overzealous so reverted back to while loop.
echo -ne "\n###\n# NON-WILD WHITELIST\n###\n\n" >> "${file[confr]}"
while read LINE ; do
LINE=$(echo $LINE | sed 's/\./[.]/g')
echo -ne "\n# $LINE\n\n" >> "${file[confr]}"
grep -i "^${ip}[[:blank:]]${LINE}"'$' "${f2}" >> "${file[confr]}" # List of hosts removed during whitelisting
grep -vi "^${ip}[[:blank:]]${LINE}"'$' "${f2}" > "${f2}.tmp" # This method removes whitelisted hostnames
mv -f "${f2}.tmp" "${f2}"
done < "${f3}"
;;
wild)
# Process wild whitelist to replace .TLD with all possible TLD values
# Testing - will eventually allow for .TLD in wild whitelist to represent any valid TLD.
# Disabled for now because it causes grep to crash while whitelisting -- too many arguments.
#while read LINE ; do
# sed -r "s/^(.*)[.]TLD/\1.${LINE}/g" "$3" >> "${dir[tmp]}/wlwtmp" # Replace 'TLD' with list of possible TLDs
# echo '' >> "${dir[tmp]}/wlwtmp"
#done < "${file[conftld]}"
#grep -v "[.]TLD" "$3" >> "${dir[tmp]}/wlwtmp" # Add none .TLD$ lines back into the wild list
echo -ne "\n###\n# WILD WHITELIST PASS 2 - WILD\n###\n\n" >> "${file[confr]}"
while read LINE ; do
LINE=$(echo $LINE | sed 's/\./[.]/g') # Replace periods with character group match to prevent periods being interpreted as 'any character'
echo -ne "\n# $LINE\n\n" >> "${file[confr]}"
grep -i "^${ip}[[:blank:]].*[.]${LINE}" "${f2}" >> "${file[confr]}" # List of hosts removed during whitelisting
grep -vi "^${ip}[[:blank:]].*[.]${LINE}" "${f2}" > "${f2}.tmp" # This method removes whitelisted hostnames
mv -f "${f2}.tmp" "${f2}"
done < "${f3}"
;;
linen) # Not yet used
data=$3
data=$(echo $data | sed 's/\./[.]/g') # Replace periods with character group to prevent them being interpreted as 'any character'
# sed -r -i "s/${ip}[[:blank:]]$3.*?$//g" "$2" # Remove exact match
grep -i "^${ip}[[:blank:]]${data}"'$' "$2" >> "${file[confr]}" # List of hosts removed during whitelisting
grep -vi "^${ip}[[:blank:]]${data}"'$' "$2" > "$2.tmp" # This method removes whitelisted hostnames
mv -f "$2.tmp" "$2"
;;
linew) # Not yet used
data=$3
data=$(echo $data | sed 's/\./[.]/g') # Replace periods with character group to prevent them being interpreted as 'any character'
# sed -r -i "s/${ip}[[:blank:]].*\.$3.*?$//g" "$2" # Remove any line with a match
grep -i "^${ip}[[:blank:]].*[.]${data}" "$2" >> "${file[confr]}" # List of hosts removed during whitelisting
grep -vi "^${ip}[[:blank:]].*[.]${data}" "$2" > "$2.tmp" # This method removes whitelisted hostnames
mv -f "$2.tmp" "$2"
;;
esac
}
function __conf_update() {
# Add or remove a single host from the .hf*rc config files
# ! Considered merging this function with __hosts_update. Opted to keep the two separate. It simplifies input sanitization (to come later).
# @1 = type: add, rem, remw, or remn (remove by line number)
# @2 = data to add/remove
# @3 = file to add/remove @1 to/from
d=$2
f=$3
case $1 in
add)
grep "${d}" "${f}"
if test $? -eq 1
then
echo "${d}" >> "${f}"
fi
;;
rem)
sed -r -i "s/${d}.*?$//g" "${f}" # Remove exact match
sed -i '/^$/d' "${f}" # Remove empty lines
;;
remw)
sed -r -i "s/.*?${d}.*?$//g" "${f}" # Remove any line with a match
sed -i '/^$/d' "${f}" # Remove empty lines
;;
remn)
if [ $d != '' ]; then
sed -i "${d}d" "${f}" # Remove the line number $d from the file
sed -i '/^$/d' "${f}" # Remove empty lines
fi
;;
esac
}
function __conf_delete() {
__repaint conf "${file[confcl]}"
# Delete specified file
# @1 = File to delete
# @2 = File's friendly name
f[1]=${1}
f[2]=${2}
printf "${bold}DELETE CONFIG FILES${normal}"
printf "\n\nReset chosen configuration file\n\n"
# Display menu
unset menu
menu[1]="Proceed: Reset ${f[2]} by deleting file ${f[1]}"
menu[2]="Return: Do not reset ${f[2]}"
m=1
while [ $m -le ${#menu[@]} ] ; do
printf "$m) ${menu[$m]}\n"
let m=m+1
done
printf "\nChoose an option then press Enter:\n"
read a
while true ; do
case $a in
1)
if test -f "${f[1]}"; then
rm "${f[1]}"
printf "${f[2]} reset.\n"
printf "\nPress any key to continue\n"
touch "${f[1]}"
read something
else
printf "${f[2]} custom settings not yet created. Nothing to do. Nothing done.\n"
printf "\nPress any key to continue\n"
read something
fi
__menu_reset_confs
;;
2)
__menu_reset_confs
;;
*)
__repaint
;;
esac
done
}
function __remove_custom_list_entry_menu() {
filename=$1
description=$2
printf "Custom $description is located at $filename\n\n"
__display_file $filename
printf "\nType the line number to remove then press Enter. Press 0 to exit:\n\n"
read a
case $a in
0)
__repaint '__menu_custom_list_management' # Effectively 'Return here'
;;
*)
__conf_update 'remn' "$a" "$filename"
__repaint '__menu_custom_list_management' # Effectively 'Return here'
;;
esac
}
function __display_file() {
filename=$1
n=1
while read LINE; do
printf "$n) $LINE\n"
let n=n+1
done < $filename
}
function __status() {
# Change status of list: On or Off.
# @1 = host list to change status of
n=$1
if grep "list\[status${n}]='-'" "${file[conf]}"
then
sed -i "/list\[status${n}]=/d" "${file[conf]}"
echo -e "list[status${n}]='+'" >> "${file[conf]}"
else
sed -i "/list\[status${n}]=/d" "${file[conf]}"
echo -e "list[status${n}]='-'" >> "${file[conf]}"
fi
}
function __build() {
# Build the hosts file: download lists, clean them, merge them, create new hosts file
# Download lists. Merge them into new hosts file
n=1
until [ $n -eq 0 ]; do
status="${list[status${n}]}"
info="${list[info${n}]}"
src="${list[src${n}]}"
file="${list[file${n}]}"
target="${list[target${n}]}"
ip="${list[ip${n}]}"
if test "$status" = '+' # The list is enabled
then
case "$src" in
localhost)
case "$target" in
whitelist-wild.txt)
cat "$file" >> "${dir[tmpu]}/whitelist-wild.txt"
;;