freedombone-app-pihole 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # pi-hole ad blocker
  12. #
  13. # Adapted from instructions at:
  14. # http://jacobsalmela.com/block-millions-ads-network-wide-with-a-raspberry-pi-hole-2-0/#manualsetup
  15. #
  16. # License
  17. # =======
  18. #
  19. # Copyright (C) 2016 Bob Mottram <bob@freedombone.net>
  20. #
  21. # This program is free software: you can redistribute it and/or modify
  22. # it under the terms of the GNU Affero General Public License as published by
  23. # the Free Software Foundation, either version 3 of the License, or
  24. # (at your option) any later version.
  25. #
  26. # This program is distributed in the hope that it will be useful,
  27. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. # GNU Affero General Public License for more details.
  30. #
  31. # You should have received a copy of the GNU Affero General Public License
  32. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  33. VARIANTS='full full-vim adblocker'
  34. IN_DEFAULT_INSTALL=0
  35. SHOW_ON_ABOUT=0
  36. PIHOLE_IFACE=eth0
  37. PIHOLE_DNS1='85.214.73.63'
  38. PIHOLE_DNS2='213.73.91.35'
  39. piholeBasename=pihole
  40. piholeDir=/etc/$piholeBasename
  41. PIHOLE_CUSTOM_ADLIST=$piholeDir/adlists.list
  42. PIHOLE_BLACKLIST=$piholeDir/blacklist.txt
  43. PIHOLE_WHITELIST=$piholeDir/whitelist.txt
  44. PIHOLE_REPO="https://github.com/pi-hole/pi-hole"
  45. PIHOLE_COMMIT='bef0a2fef08b39780610b8885407b855edd3ba49'
  46. pihole_variables=(ONION_ONLY
  47. PIHOLE_IFACE
  48. PIHOLE_DNS1
  49. PIHOLE_DNS2)
  50. function pihole_copy_files {
  51. if [ ! -d /etc/.pihole ]; then
  52. mkdir /etc/.pihole
  53. fi
  54. cp $INSTALL_DIR/pihole/adlists.default /etc/.pihole/adlists.default
  55. cp $INSTALL_DIR/pihole/adlists.default $piholeDir/adlists.default
  56. if [ ! -f $PIHOLE_CUSTOM_ADLIST ]; then
  57. cp $INSTALL_DIR/pihole/adlists.default $PIHOLE_CUSTOM_ADLIST
  58. fi
  59. cp $INSTALL_DIR/pihole/advanced/Scripts/* /opt/$piholeBasename
  60. if [ -f /etc/dnsmasq.d/01-pihole.conf ]; then
  61. rm /etc/dnsmasq.d/01-pihole.conf
  62. fi
  63. cp $INSTALL_DIR/pihole/advanced/pihole.cron /etc/cron.d/pihole
  64. cp $INSTALL_DIR/pihole/gravity.sh /opt/$piholeBasename
  65. chmod +x /opt/pihole/*.sh
  66. }
  67. function pihole_change_ipv4 {
  68. new_ipv4="$1"
  69. if [ -f /usr/local/bin/pihole ]; then
  70. setupVars=$piholeDir/setupVars.conf
  71. if [ -f $setupVars ]; then
  72. sed -i "s|IPv4_address=.*|IPv4_address=${new_ipv4}|g" $setupVars
  73. fi
  74. fi
  75. }
  76. function pihole_update {
  77. if [ ! -f /usr/local/bin/gravity.sh ]; then
  78. return
  79. fi
  80. if [ ! -f $HOME/${PROJECT_NAME}-wifi.cfg ]; then
  81. PIHOLE_IFACE=eth0
  82. else
  83. read_config_param WIFI_INTERFACE
  84. PIHOLE_IFACE=$WIFI_INTERFACE
  85. fi
  86. IPv4_address="$(get_ipv4_address)"
  87. IPv6_address="$(get_ipv6_address)"
  88. setupVars=$piholeDir/setupVars.conf
  89. echo "piholeInterface=${PIHOLE_IFACE}" > ${setupVars}
  90. echo "IPV4_ADDRESS=${IPv4_address}" >> ${setupVars}
  91. if [ ${#IPv6_address} -gt 0 ]; then
  92. echo "IPV6_ADDRESS=${IPv6_address}" >> ${setupVars}
  93. fi
  94. echo "piholeDNS1=${PIHOLE_DNS1}" >> ${setupVars}
  95. echo "piholeDNS2=${PIHOLE_DNS1}" >> ${setupVars}
  96. echo 'domain-needed' > /etc/dnsmasq.conf
  97. echo 'bogus-priv' >> /etc/dnsmasq.conf
  98. echo 'no-resolv' >> /etc/dnsmasq.conf
  99. echo "server=${PIHOLE_DNS1}" >> /etc/dnsmasq.conf
  100. echo "server=${PIHOLE_DNS2}" >> /etc/dnsmasq.conf
  101. echo "interface=${PIHOLE_IFACE}" >> /etc/dnsmasq.conf
  102. echo 'listen-address=127.0.0.1' >> /etc/dnsmasq.conf
  103. pihole -g
  104. systemctl restart dnsmasq
  105. # avoid having the tripwire report pihole updates
  106. if ! grep -q '!/etc/pihole' /etc/tripwire/twpol.txt; then
  107. sed -i '\|/etc\t\t->.*|a\ !/etc/pihole ;' /etc/tripwire/twpol.txt
  108. fi
  109. }
  110. function pihole_change_upstream_dns {
  111. data=$(tempfile 2>/dev/null)
  112. trap "rm -f $data" 0 1 2 5 15
  113. dialog --backtitle $"Ad Blocker Upstream DNS" \
  114. --radiolist $"Pick a domain name service (DNS):" 25 50 16 \
  115. 1 $"Digital Courage" on \
  116. 2 $"German Privacy Foundation 1" off \
  117. 3 $"German Privacy Foundation 2" off \
  118. 4 $"Chaos Computer Club" off \
  119. 5 $"ClaraNet" off \
  120. 6 $"OpenNIC 1" off \
  121. 7 $"OpenNIC 2" off \
  122. 8 $"OpenNIC 3" off \
  123. 9 $"OpenNIC 4" off \
  124. 10 $"OpenNIC 5" off \
  125. 11 $"OpenNIC 6" off \
  126. 12 $"OpenNIC 7" off \
  127. 13 $"PowerNS" off \
  128. 14 $"ValiDOM" off \
  129. 15 $"Freie Unzensierte" off \
  130. 16 $"Google" off 2> $data
  131. sel=$?
  132. case $sel in
  133. 1) exit 1;;
  134. 255) exit 1;;
  135. esac
  136. case $(cat $data) in
  137. 1) PIHOLE_DNS1='85.214.73.63'
  138. PIHOLE_DNS2='213.73.91.35'
  139. ;;
  140. 2) PIHOLE_DNS1='87.118.100.175'
  141. PIHOLE_DNS2='94.75.228.29'
  142. ;;
  143. 3) PIHOLE_DNS1='85.25.251.254'
  144. PIHOLE_DNS2='2.141.58.13'
  145. ;;
  146. 4) PIHOLE_DNS1='213.73.91.35'
  147. PIHOLE_DNS2='85.214.73.63'
  148. ;;
  149. 5) PIHOLE_DNS1='212.82.225.7'
  150. PIHOLE_DNS2='212.82.226.212'
  151. ;;
  152. 6) PIHOLE_DNS1='58.6.115.42'
  153. PIHOLE_DNS2='58.6.115.43'
  154. ;;
  155. 7) PIHOLE_DNS1='119.31.230.42'
  156. PIHOLE_DNS2='200.252.98.162'
  157. ;;
  158. 8) PIHOLE_DNS1='217.79.186.148'
  159. PIHOLE_DNS2='81.89.98.6'
  160. ;;
  161. 9) PIHOLE_DNS1='78.159.101.37'
  162. PIHOLE_DNS2='203.167.220.153'
  163. ;;
  164. 10) PIHOLE_DNS1='82.229.244.191'
  165. PIHOLE_DNS2='82.229.244.191'
  166. ;;
  167. 11) PIHOLE_DNS1='216.87.84.211'
  168. PIHOLE_DNS2='66.244.95.20'
  169. ;;
  170. 12) PIHOLE_DNS1='207.192.69.155'
  171. PIHOLE_DNS2='72.14.189.120'
  172. ;;
  173. 13) PIHOLE_DNS1='194.145.226.26'
  174. PIHOLE_DNS2='77.220.232.44'
  175. ;;
  176. 14) PIHOLE_DNS1='78.46.89.147'
  177. PIHOLE_DNS2='88.198.75.145'
  178. ;;
  179. 15) PIHOLE_DNS1='85.25.149.144'
  180. PIHOLE_DNS2='87.106.37.196'
  181. ;;
  182. 16) PIHOLE_DNS1='8.8.8.8'
  183. PIHOLE_DNS2='4.4.4.4'
  184. ;;
  185. 255) exit 1;;
  186. esac
  187. write_config_param "PIHOLE_DNS1" "$PIHOLE_DNS1"
  188. write_config_param "PIHOLE_DNS2" "$PIHOLE_DNS2"
  189. }
  190. function update_pihole_interactive {
  191. clear
  192. echo $'Updating Ad Blocker Lists'
  193. echo ''
  194. pihole_update
  195. }
  196. function configure_firewall_for_pihole {
  197. firewall_add DNS 53
  198. }
  199. function pihole_pause {
  200. pihole disable
  201. dialog --title $"Pause Ad Blocker" \
  202. --msgbox $"Ad blocking is paused" 6 60
  203. }
  204. function pihole_resume {
  205. pihole enable
  206. dialog --title $"Resume Ad Blocker" \
  207. --msgbox $"Ad blocking has resumed" 6 60
  208. }
  209. function configure_interactive_pihole {
  210. while true
  211. do
  212. data=$(tempfile 2>/dev/null)
  213. trap "rm -f $data" 0 1 2 5 15
  214. dialog --backtitle $"Freedombone Control Panel" \
  215. --title $"Ad Blocker" \
  216. --radiolist $"Choose an operation:" 16 70 7 \
  217. 1 $"Edit ads list" off \
  218. 2 $"Edit blacklisted domain names" off \
  219. 3 $"Edit whitelisted domain names" off \
  220. 4 $"Change upstream DNS servers" off \
  221. 5 $"Pause blocker" off \
  222. 6 $"Resume blocker" off \
  223. 7 $"Exit" on 2> $data
  224. sel=$?
  225. case $sel in
  226. 1) exit 1;;
  227. 255) exit 1;;
  228. esac
  229. case $(cat $data) in
  230. 1) editor $PIHOLE_CUSTOM_ADLIST
  231. update_pihole_interactive
  232. ;;
  233. 2) editor $PIHOLE_BLACKLIST
  234. update_pihole_interactive
  235. ;;
  236. 3) editor $PIHOLE_WHITELIST
  237. update_pihole_interactive
  238. ;;
  239. 4) pihole_change_upstream_dns
  240. update_pihole_interactive
  241. ;;
  242. 5) pihole_pause
  243. ;;
  244. 6) pihole_resume
  245. ;;
  246. 7) break;;
  247. esac
  248. done
  249. }
  250. function install_interactive_pihole {
  251. APP_INSTALLED=1
  252. }
  253. function reconfigure_pihole {
  254. echo -n ''
  255. }
  256. function upgrade_pihole {
  257. function_check set_repo_commit
  258. set_repo_commit $INSTALL_DIR/pihole "pihole commit" "$PIHOLE_COMMIT" $PIHOLE_REPO
  259. pihole_copy_files
  260. pihole_update
  261. }
  262. function backup_local_pihole {
  263. function_check backup_directory_to_usb
  264. backup_directory_to_usb $piholeDir pihole
  265. }
  266. function restore_local_pihole {
  267. function_check restore_directory_from_usb
  268. restore_directory_from_usb / pihole
  269. }
  270. function backup_remote_pihole {
  271. function_check backup_directory_to_friend
  272. backup_directory_to_friend $piholeDir pihole
  273. }
  274. function restore_remote_pihole {
  275. function_check restore_directory_from_friend
  276. restore_directory_from_friend / pihole
  277. }
  278. function remove_pihole {
  279. apt-get -yq remove --purge dnsmasq
  280. if [ ! -d /var/www/pihole ]; then
  281. rm -rf /var/www/pihole
  282. fi
  283. if [ -f /usr/local/bin/gravity.sh ]; then
  284. rm /usr/local/bin/gravity.sh
  285. fi
  286. if [ -f /usr/local/bin/pihole ]; then
  287. rm /usr/local/bin/pihole
  288. fi
  289. if [ -d /opt/pihole ]; then
  290. rm -rf /opt/pihole
  291. fi
  292. if [ -d $piholeDir ]; then
  293. rm -rf $piholeDir
  294. fi
  295. if [ -d /etc/.pihole ]; then
  296. rm -rf /etc/.pihole
  297. fi
  298. if [ -f /var/log/pihole.log ]; then
  299. rm /var/log/pihole.log
  300. fi
  301. if [ -f /etc/cron.d/pihole ]; then
  302. rm /etc/cron.d/pihole
  303. fi
  304. if [ -d $INSTALL_DIR/pihole ]; then
  305. rm -rf $INSTALL_DIR/pihole
  306. fi
  307. firewall_remove 53
  308. userdel -r pihole
  309. }
  310. function install_pihole {
  311. apt-get -yq install dnsmasq curl
  312. adduser --disabled-login --gecos 'pi-hole' pihole
  313. chmod 600 /etc/shadow
  314. chmod 600 /etc/gshadow
  315. usermod -a -G www-data pihole
  316. chmod 0000 /etc/shadow
  317. chmod 0000 /etc/gshadow
  318. systemctl enable dnsmasq
  319. if [ ! -d $INSTALL_DIR ]; then
  320. mkdir -p $INSTALL_DIR
  321. fi
  322. if [ ! -d $INSTALL_DIR/pihole ]; then
  323. cd $INSTALL_DIR
  324. git_clone $PIHOLE_REPO pihole
  325. if [ ! -d $INSTALL_DIR/pihole ]; then
  326. exit 523925
  327. fi
  328. cd $INSTALL_DIR/pihole
  329. git checkout $PIHOLE_COMMIT -b $PIHOLE_COMMIT
  330. set_completion_param "pihole commit" "$PIHOLE_COMMIT"
  331. fi
  332. if [ ! -d /var/www/pihole/htdocs ]; then
  333. mkdir -p /var/www/pihole/htdocs
  334. fi
  335. # blank file which takes the place of ads
  336. echo '<html>' > /var/www/pihole/htdocs/index.html
  337. echo '<body>' >> /var/www/pihole/htdocs/index.html
  338. echo '</body>' >> /var/www/pihole/htdocs/index.html
  339. echo '</html>' >> /var/www/pihole/htdocs/index.html
  340. if [ ! -f $INSTALL_DIR/pihole/gravity.sh ]; then
  341. exit 26738
  342. fi
  343. cp $INSTALL_DIR/pihole/gravity.sh /usr/local/bin/gravity.sh
  344. chmod 755 /usr/local/bin/gravity.sh
  345. if [ ! -f $INSTALL_DIR/pihole/pihole ]; then
  346. exit 52935
  347. fi
  348. cp $INSTALL_DIR/pihole/pihole /usr/local/bin/pihole
  349. chmod 755 /usr/local/bin/pihole
  350. if [ ! -d $piholeDir ]; then
  351. mkdir $piholeDir
  352. fi
  353. if [ ! -d /opt/pihole ]; then
  354. mkdir -p /opt/pihole
  355. fi
  356. pihole_copy_files
  357. chown -R www-data:www-data /var/www/pihole/htdocs
  358. configure_firewall_for_pihole
  359. pihole_update
  360. APP_INSTALLED=1
  361. }
  362. # NOTE: deliberately no exit 0