freedombone-app-pihole 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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='2ceeac41fe8e493f9040b54a7c82f1183ecf5566'
  46. pihole_variables=(ONION_ONLY
  47. PIHOLE_IFACE
  48. PIHOLE_DNS1
  49. PIHOLE_DNS2)
  50. function logging_on_pihole {
  51. echo -n ''
  52. }
  53. function logging_off_pihole {
  54. echo -n ''
  55. }
  56. function pihole_copy_files {
  57. if [ ! -d /etc/.pihole ]; then
  58. mkdir /etc/.pihole
  59. fi
  60. cp $INSTALL_DIR/pihole/adlists.default /etc/.pihole/adlists.default
  61. cp $INSTALL_DIR/pihole/adlists.default $piholeDir/adlists.default
  62. if [ ! -f $PIHOLE_CUSTOM_ADLIST ]; then
  63. cp $INSTALL_DIR/pihole/adlists.default $PIHOLE_CUSTOM_ADLIST
  64. fi
  65. cp $INSTALL_DIR/pihole/advanced/Scripts/* /opt/$piholeBasename
  66. if [ -f /etc/dnsmasq.d/01-pihole.conf ]; then
  67. rm /etc/dnsmasq.d/01-pihole.conf
  68. fi
  69. cp $INSTALL_DIR/pihole/advanced/pihole.cron /etc/cron.d/pihole
  70. cp $INSTALL_DIR/pihole/gravity.sh /opt/$piholeBasename
  71. chmod +x /opt/pihole/*.sh
  72. }
  73. function pihole_change_ipv4 {
  74. new_ipv4="$1"
  75. if [ -f /usr/local/bin/pihole ]; then
  76. setupVars=$piholeDir/setupVars.conf
  77. if [ -f $setupVars ]; then
  78. sed -i "s|IPv4_address=.*|IPv4_address=${new_ipv4}|g" $setupVars
  79. fi
  80. fi
  81. }
  82. function pihole_update {
  83. if [ ! -f /usr/local/bin/gravity.sh ]; then
  84. return
  85. fi
  86. if [ ! -f $HOME/${PROJECT_NAME}-wifi.cfg ]; then
  87. PIHOLE_IFACE=eth0
  88. else
  89. read_config_param WIFI_INTERFACE
  90. PIHOLE_IFACE=$WIFI_INTERFACE
  91. fi
  92. IPv4_address="$(get_ipv4_address)"
  93. IPv6_address="$(get_ipv6_address)"
  94. setupVars=$piholeDir/setupVars.conf
  95. echo "piholeInterface=${PIHOLE_IFACE}" > ${setupVars}
  96. echo "IPV4_ADDRESS=${IPv4_address}" >> ${setupVars}
  97. if [ ${#IPv6_address} -gt 0 ]; then
  98. echo "IPV6_ADDRESS=${IPv6_address}" >> ${setupVars}
  99. fi
  100. echo "piholeDNS1=${PIHOLE_DNS1}" >> ${setupVars}
  101. echo "piholeDNS2=${PIHOLE_DNS1}" >> ${setupVars}
  102. echo 'domain-needed' > /etc/dnsmasq.conf
  103. echo 'bogus-priv' >> /etc/dnsmasq.conf
  104. echo 'no-resolv' >> /etc/dnsmasq.conf
  105. echo "server=${PIHOLE_DNS1}" >> /etc/dnsmasq.conf
  106. echo "server=${PIHOLE_DNS2}" >> /etc/dnsmasq.conf
  107. echo "interface=${PIHOLE_IFACE}" >> /etc/dnsmasq.conf
  108. echo 'listen-address=127.0.0.1' >> /etc/dnsmasq.conf
  109. pihole -g
  110. systemctl restart dnsmasq
  111. # avoid having the tripwire report pihole updates
  112. if ! grep -q '!/etc/pihole' /etc/tripwire/twpol.txt; then
  113. sed -i '\|/etc\t\t->.*|a\ !/etc/pihole ;' /etc/tripwire/twpol.txt
  114. fi
  115. }
  116. function pihole_change_upstream_dns {
  117. data=$(tempfile 2>/dev/null)
  118. trap "rm -f $data" 0 1 2 5 15
  119. dialog --backtitle $"Ad Blocker Upstream DNS" \
  120. --radiolist $"Pick a domain name service (DNS):" 25 50 16 \
  121. 1 $"Digital Courage" on \
  122. 2 $"German Privacy Foundation 1" off \
  123. 3 $"German Privacy Foundation 2" off \
  124. 4 $"Chaos Computer Club" off \
  125. 5 $"ClaraNet" off \
  126. 6 $"OpenNIC 1" off \
  127. 7 $"OpenNIC 2" off \
  128. 8 $"OpenNIC 3" off \
  129. 9 $"OpenNIC 4" off \
  130. 10 $"OpenNIC 5" off \
  131. 11 $"OpenNIC 6" off \
  132. 12 $"OpenNIC 7" off \
  133. 13 $"PowerNS" off \
  134. 14 $"ValiDOM" off \
  135. 15 $"Freie Unzensierte" off \
  136. 16 $"Google" off 2> $data
  137. sel=$?
  138. case $sel in
  139. 1) exit 1;;
  140. 255) exit 1;;
  141. esac
  142. case $(cat $data) in
  143. 1) PIHOLE_DNS1='85.214.73.63'
  144. PIHOLE_DNS2='213.73.91.35'
  145. ;;
  146. 2) PIHOLE_DNS1='87.118.100.175'
  147. PIHOLE_DNS2='94.75.228.29'
  148. ;;
  149. 3) PIHOLE_DNS1='85.25.251.254'
  150. PIHOLE_DNS2='2.141.58.13'
  151. ;;
  152. 4) PIHOLE_DNS1='213.73.91.35'
  153. PIHOLE_DNS2='85.214.73.63'
  154. ;;
  155. 5) PIHOLE_DNS1='212.82.225.7'
  156. PIHOLE_DNS2='212.82.226.212'
  157. ;;
  158. 6) PIHOLE_DNS1='58.6.115.42'
  159. PIHOLE_DNS2='58.6.115.43'
  160. ;;
  161. 7) PIHOLE_DNS1='119.31.230.42'
  162. PIHOLE_DNS2='200.252.98.162'
  163. ;;
  164. 8) PIHOLE_DNS1='217.79.186.148'
  165. PIHOLE_DNS2='81.89.98.6'
  166. ;;
  167. 9) PIHOLE_DNS1='78.159.101.37'
  168. PIHOLE_DNS2='203.167.220.153'
  169. ;;
  170. 10) PIHOLE_DNS1='82.229.244.191'
  171. PIHOLE_DNS2='82.229.244.191'
  172. ;;
  173. 11) PIHOLE_DNS1='216.87.84.211'
  174. PIHOLE_DNS2='66.244.95.20'
  175. ;;
  176. 12) PIHOLE_DNS1='207.192.69.155'
  177. PIHOLE_DNS2='72.14.189.120'
  178. ;;
  179. 13) PIHOLE_DNS1='194.145.226.26'
  180. PIHOLE_DNS2='77.220.232.44'
  181. ;;
  182. 14) PIHOLE_DNS1='78.46.89.147'
  183. PIHOLE_DNS2='88.198.75.145'
  184. ;;
  185. 15) PIHOLE_DNS1='85.25.149.144'
  186. PIHOLE_DNS2='87.106.37.196'
  187. ;;
  188. 16) PIHOLE_DNS1='8.8.8.8'
  189. PIHOLE_DNS2='4.4.4.4'
  190. ;;
  191. 255) exit 1;;
  192. esac
  193. write_config_param "PIHOLE_DNS1" "$PIHOLE_DNS1"
  194. write_config_param "PIHOLE_DNS2" "$PIHOLE_DNS2"
  195. }
  196. function update_pihole_interactive {
  197. clear
  198. echo $'Updating Ad Blocker Lists'
  199. echo ''
  200. pihole_update
  201. }
  202. function configure_firewall_for_pihole {
  203. firewall_add DNS 53
  204. }
  205. function pihole_pause {
  206. pihole disable
  207. dialog --title $"Pause Ad Blocker" \
  208. --msgbox $"Ad blocking is paused" 6 60
  209. }
  210. function pihole_resume {
  211. pihole enable
  212. dialog --title $"Resume Ad Blocker" \
  213. --msgbox $"Ad blocking has resumed" 6 60
  214. }
  215. function configure_interactive_pihole {
  216. while true
  217. do
  218. data=$(tempfile 2>/dev/null)
  219. trap "rm -f $data" 0 1 2 5 15
  220. dialog --backtitle $"Freedombone Control Panel" \
  221. --title $"Ad Blocker" \
  222. --radiolist $"Choose an operation:" 16 70 7 \
  223. 1 $"Edit ads list" off \
  224. 2 $"Edit blacklisted domain names" off \
  225. 3 $"Edit whitelisted domain names" off \
  226. 4 $"Change upstream DNS servers" off \
  227. 5 $"Pause blocker" off \
  228. 6 $"Resume blocker" off \
  229. 7 $"Exit" on 2> $data
  230. sel=$?
  231. case $sel in
  232. 1) exit 1;;
  233. 255) exit 1;;
  234. esac
  235. case $(cat $data) in
  236. 1) editor $PIHOLE_CUSTOM_ADLIST
  237. update_pihole_interactive
  238. ;;
  239. 2) editor $PIHOLE_BLACKLIST
  240. update_pihole_interactive
  241. ;;
  242. 3) editor $PIHOLE_WHITELIST
  243. update_pihole_interactive
  244. ;;
  245. 4) pihole_change_upstream_dns
  246. update_pihole_interactive
  247. ;;
  248. 5) pihole_pause
  249. ;;
  250. 6) pihole_resume
  251. ;;
  252. 7) break;;
  253. esac
  254. done
  255. }
  256. function install_interactive_pihole {
  257. APP_INSTALLED=1
  258. }
  259. function reconfigure_pihole {
  260. echo -n ''
  261. }
  262. function upgrade_pihole {
  263. CURR_PIHOLE_COMMIT=$(get_completion_param "pihole commit")
  264. if [[ "$CURR_PIHOLE_COMMIT" == "$PIHOLE_COMMIT" ]]; then
  265. return
  266. fi
  267. function_check set_repo_commit
  268. set_repo_commit $INSTALL_DIR/pihole "pihole commit" "$PIHOLE_COMMIT" $PIHOLE_REPO
  269. pihole_copy_files
  270. pihole_update
  271. }
  272. function backup_local_pihole {
  273. function_check backup_directory_to_usb
  274. backup_directory_to_usb $piholeDir pihole
  275. }
  276. function restore_local_pihole {
  277. function_check restore_directory_from_usb
  278. restore_directory_from_usb / pihole
  279. }
  280. function backup_remote_pihole {
  281. function_check backup_directory_to_friend
  282. backup_directory_to_friend $piholeDir pihole
  283. }
  284. function restore_remote_pihole {
  285. function_check restore_directory_from_friend
  286. restore_directory_from_friend / pihole
  287. }
  288. function remove_pihole {
  289. apt-get -yq remove --purge dnsmasq
  290. if [ ! -d /var/www/pihole ]; then
  291. rm -rf /var/www/pihole
  292. fi
  293. if [ -f /usr/local/bin/gravity.sh ]; then
  294. rm /usr/local/bin/gravity.sh
  295. fi
  296. if [ -f /usr/local/bin/pihole ]; then
  297. rm /usr/local/bin/pihole
  298. fi
  299. if [ -d /opt/pihole ]; then
  300. rm -rf /opt/pihole
  301. fi
  302. if [ -d $piholeDir ]; then
  303. rm -rf $piholeDir
  304. fi
  305. if [ -d /etc/.pihole ]; then
  306. rm -rf /etc/.pihole
  307. fi
  308. if [ -f /var/log/pihole.log ]; then
  309. rm /var/log/pihole.log
  310. fi
  311. if [ -f /etc/cron.d/pihole ]; then
  312. rm /etc/cron.d/pihole
  313. fi
  314. if [ -d $INSTALL_DIR/pihole ]; then
  315. rm -rf $INSTALL_DIR/pihole
  316. fi
  317. firewall_remove 53
  318. userdel -r pihole
  319. }
  320. function install_pihole {
  321. apt-get -yq install dnsmasq curl
  322. adduser --disabled-login --gecos 'pi-hole' pihole
  323. if [ ! -d /home/pihole ]; then
  324. echo $"/home/pihole directory not created"
  325. exit 538929
  326. fi
  327. chmod 600 /etc/shadow
  328. chmod 600 /etc/gshadow
  329. usermod -a -G www-data pihole
  330. chmod 0000 /etc/shadow
  331. chmod 0000 /etc/gshadow
  332. systemctl enable dnsmasq
  333. if [ ! -d $INSTALL_DIR ]; then
  334. mkdir -p $INSTALL_DIR
  335. fi
  336. if [ ! -d $INSTALL_DIR/pihole ]; then
  337. cd $INSTALL_DIR
  338. if [ -d /repos/pihole ]; then
  339. mkdir pihole
  340. cp -r -p /repos/pihole/. pihole
  341. cd pihole
  342. git pull
  343. else
  344. git_clone $PIHOLE_REPO pihole
  345. fi
  346. if [ ! -d $INSTALL_DIR/pihole ]; then
  347. exit 523925
  348. fi
  349. cd $INSTALL_DIR/pihole
  350. git checkout $PIHOLE_COMMIT -b $PIHOLE_COMMIT
  351. set_completion_param "pihole commit" "$PIHOLE_COMMIT"
  352. fi
  353. if [ ! -d /var/www/pihole/htdocs ]; then
  354. mkdir -p /var/www/pihole/htdocs
  355. fi
  356. # blank file which takes the place of ads
  357. echo '<html>' > /var/www/pihole/htdocs/index.html
  358. echo '<body>' >> /var/www/pihole/htdocs/index.html
  359. echo '</body>' >> /var/www/pihole/htdocs/index.html
  360. echo '</html>' >> /var/www/pihole/htdocs/index.html
  361. if [ ! -f $INSTALL_DIR/pihole/gravity.sh ]; then
  362. exit 26738
  363. fi
  364. cp $INSTALL_DIR/pihole/gravity.sh /usr/local/bin/gravity.sh
  365. chmod 755 /usr/local/bin/gravity.sh
  366. if [ ! -f $INSTALL_DIR/pihole/pihole ]; then
  367. exit 52935
  368. fi
  369. cp $INSTALL_DIR/pihole/pihole /usr/local/bin/pihole
  370. chmod 755 /usr/local/bin/pihole
  371. if [ ! -d $piholeDir ]; then
  372. mkdir $piholeDir
  373. fi
  374. if [ ! -d /opt/pihole ]; then
  375. mkdir -p /opt/pihole
  376. fi
  377. pihole_copy_files
  378. chown -R www-data:www-data /var/www/pihole/htdocs
  379. configure_firewall_for_pihole
  380. pihole_update
  381. APP_INSTALLED=1
  382. }
  383. # NOTE: deliberately no exit 0