freedombone-app-pihole 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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='69e3a45083bd311aecdbc3935986dac5ce64caa8'
  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. }
  106. function pihole_change_upstream_dns {
  107. data=$(tempfile 2>/dev/null)
  108. trap "rm -f $data" 0 1 2 5 15
  109. dialog --backtitle $"Ad Blocker Upstream DNS" \
  110. --radiolist $"Pick a domain name service (DNS):" 25 50 16 \
  111. 1 $"Digital Courage" on \
  112. 2 $"German Privacy Foundation 1" off \
  113. 3 $"German Privacy Foundation 2" off \
  114. 4 $"Chaos Computer Club" off \
  115. 5 $"ClaraNet" off \
  116. 6 $"OpenNIC 1" off \
  117. 7 $"OpenNIC 2" off \
  118. 8 $"OpenNIC 3" off \
  119. 9 $"OpenNIC 4" off \
  120. 10 $"OpenNIC 5" off \
  121. 11 $"OpenNIC 6" off \
  122. 12 $"OpenNIC 7" off \
  123. 13 $"PowerNS" off \
  124. 14 $"ValiDOM" off \
  125. 15 $"Freie Unzensierte" off \
  126. 16 $"Google" off 2> $data
  127. sel=$?
  128. case $sel in
  129. 1) exit 1;;
  130. 255) exit 1;;
  131. esac
  132. case $(cat $data) in
  133. 1) PIHOLE_DNS1='85.214.73.63'
  134. PIHOLE_DNS2='213.73.91.35'
  135. ;;
  136. 2) PIHOLE_DNS1='87.118.100.175'
  137. PIHOLE_DNS2='94.75.228.29'
  138. ;;
  139. 3) PIHOLE_DNS1='85.25.251.254'
  140. PIHOLE_DNS2='2.141.58.13'
  141. ;;
  142. 4) PIHOLE_DNS1='213.73.91.35'
  143. PIHOLE_DNS2='85.214.73.63'
  144. ;;
  145. 5) PIHOLE_DNS1='212.82.225.7'
  146. PIHOLE_DNS2='212.82.226.212'
  147. ;;
  148. 6) PIHOLE_DNS1='58.6.115.42'
  149. PIHOLE_DNS2='58.6.115.43'
  150. ;;
  151. 7) PIHOLE_DNS1='119.31.230.42'
  152. PIHOLE_DNS2='200.252.98.162'
  153. ;;
  154. 8) PIHOLE_DNS1='217.79.186.148'
  155. PIHOLE_DNS2='81.89.98.6'
  156. ;;
  157. 9) PIHOLE_DNS1='78.159.101.37'
  158. PIHOLE_DNS2='203.167.220.153'
  159. ;;
  160. 10) PIHOLE_DNS1='82.229.244.191'
  161. PIHOLE_DNS2='82.229.244.191'
  162. ;;
  163. 11) PIHOLE_DNS1='216.87.84.211'
  164. PIHOLE_DNS2='66.244.95.20'
  165. ;;
  166. 12) PIHOLE_DNS1='207.192.69.155'
  167. PIHOLE_DNS2='72.14.189.120'
  168. ;;
  169. 13) PIHOLE_DNS1='194.145.226.26'
  170. PIHOLE_DNS2='77.220.232.44'
  171. ;;
  172. 14) PIHOLE_DNS1='78.46.89.147'
  173. PIHOLE_DNS2='88.198.75.145'
  174. ;;
  175. 15) PIHOLE_DNS1='85.25.149.144'
  176. PIHOLE_DNS2='87.106.37.196'
  177. ;;
  178. 16) PIHOLE_DNS1='8.8.8.8'
  179. PIHOLE_DNS2='4.4.4.4'
  180. ;;
  181. 255) exit 1;;
  182. esac
  183. write_config_param "PIHOLE_DNS1" "$PIHOLE_DNS1"
  184. write_config_param "PIHOLE_DNS2" "$PIHOLE_DNS2"
  185. }
  186. function update_pihole_interactive {
  187. clear
  188. echo $'Updating Ad Blocker Lists'
  189. echo ''
  190. pihole_update
  191. }
  192. function configure_firewall_for_pihole {
  193. firewall_add DNS 53
  194. }
  195. function pihole_pause {
  196. pihole disable
  197. dialog --title $"Pause Ad Blocker" \
  198. --msgbox $"Ad blocking is paused" 6 60
  199. }
  200. function pihole_resume {
  201. pihole enable
  202. dialog --title $"Resume Ad Blocker" \
  203. --msgbox $"Ad blocking has resumed" 6 60
  204. }
  205. function configure_interactive_pihole {
  206. while true
  207. do
  208. data=$(tempfile 2>/dev/null)
  209. trap "rm -f $data" 0 1 2 5 15
  210. dialog --backtitle $"Freedombone Control Panel" \
  211. --title $"Ad Blocker" \
  212. --radiolist $"Choose an operation:" 16 70 7 \
  213. 1 $"Edit ads list" off \
  214. 2 $"Edit blacklisted domain names" off \
  215. 3 $"Edit whitelisted domain names" off \
  216. 4 $"Change upstream DNS servers" off \
  217. 5 $"Pause blocker" off \
  218. 6 $"Resume blocker" off \
  219. 7 $"Exit" on 2> $data
  220. sel=$?
  221. case $sel in
  222. 1) exit 1;;
  223. 255) exit 1;;
  224. esac
  225. case $(cat $data) in
  226. 1) editor $PIHOLE_CUSTOM_ADLIST
  227. update_pihole_interactive
  228. ;;
  229. 2) editor $PIHOLE_BLACKLIST
  230. update_pihole_interactive
  231. ;;
  232. 3) editor $PIHOLE_WHITELIST
  233. update_pihole_interactive
  234. ;;
  235. 4) pihole_change_upstream_dns
  236. update_pihole_interactive
  237. ;;
  238. 5) pihole_pause
  239. ;;
  240. 6) pihole_resume
  241. ;;
  242. 7) break;;
  243. esac
  244. done
  245. }
  246. function install_interactive_pihole {
  247. APP_INSTALLED=1
  248. }
  249. function reconfigure_pihole {
  250. echo -n ''
  251. }
  252. function upgrade_pihole {
  253. function_check set_repo_commit
  254. set_repo_commit $INSTALL_DIR/pihole "pihole commit" "$PIHOLE_COMMIT" $PIHOLE_REPO
  255. pihole_copy_files
  256. pihole_update
  257. }
  258. function backup_local_pihole {
  259. function_check backup_directory_to_usb
  260. backup_directory_to_usb $piholeDir pihole
  261. }
  262. function restore_local_pihole {
  263. function_check restore_directory_from_usb
  264. restore_directory_from_usb / pihole
  265. }
  266. function backup_remote_pihole {
  267. function_check backup_directory_to_friend
  268. backup_directory_to_friend $piholeDir pihole
  269. }
  270. function restore_remote_pihole {
  271. function_check restore_directory_from_friend
  272. restore_directory_from_friend / pihole
  273. }
  274. function remove_pihole {
  275. apt-get -yq remove --purge dnsmasq
  276. if [ ! -d /var/www/pihole ]; then
  277. rm -rf /var/www/pihole
  278. fi
  279. if [ -f /usr/local/bin/gravity.sh ]; then
  280. rm /usr/local/bin/gravity.sh
  281. fi
  282. if [ -f /usr/local/bin/pihole ]; then
  283. rm /usr/local/bin/pihole
  284. fi
  285. if [ -d /opt/pihole ]; then
  286. rm -rf /opt/pihole
  287. fi
  288. if [ -d $piholeDir ]; then
  289. rm -rf $piholeDir
  290. fi
  291. if [ -d /etc/.pihole ]; then
  292. rm -rf /etc/.pihole
  293. fi
  294. if [ -f /var/log/pihole.log ]; then
  295. rm /var/log/pihole.log
  296. fi
  297. if [ -f /etc/cron.d/pihole ]; then
  298. rm /etc/cron.d/pihole
  299. fi
  300. if [ -d $INSTALL_DIR/pihole ]; then
  301. rm -rf $INSTALL_DIR/pihole
  302. fi
  303. firewall_remove 53
  304. userdel -r pihole
  305. }
  306. function install_pihole {
  307. apt-get -yq install dnsmasq curl
  308. adduser --disabled-login --gecos 'pi-hole' pihole
  309. usermod -a -G www-data pihole
  310. systemctl enable dnsmasq
  311. if [ ! -d $INSTALL_DIR ]; then
  312. mkdir -p $INSTALL_DIR
  313. fi
  314. if [ ! -d $INSTALL_DIR/pihole ]; then
  315. cd $INSTALL_DIR
  316. git_clone $PIHOLE_REPO pihole
  317. if [ ! -d $INSTALL_DIR/pihole ]; then
  318. exit 523925
  319. fi
  320. cd $INSTALL_DIR/pihole
  321. git checkout $PIHOLE_COMMIT -b $PIHOLE_COMMIT
  322. set_completion_param "pihole commit" "$PIHOLE_COMMIT"
  323. fi
  324. if [ ! -d /var/www/pihole/htdocs ]; then
  325. mkdir -p /var/www/pihole/htdocs
  326. fi
  327. # blank file which takes the place of ads
  328. echo '<html>' > /var/www/pihole/htdocs/index.html
  329. echo '<body>' >> /var/www/pihole/htdocs/index.html
  330. echo '</body>' >> /var/www/pihole/htdocs/index.html
  331. echo '</html>' >> /var/www/pihole/htdocs/index.html
  332. if [ ! -f $INSTALL_DIR/pihole/gravity.sh ]; then
  333. exit 26738
  334. fi
  335. cp $INSTALL_DIR/pihole/gravity.sh /usr/local/bin/gravity.sh
  336. chmod 755 /usr/local/bin/gravity.sh
  337. if [ ! -f $INSTALL_DIR/pihole/pihole ]; then
  338. exit 52935
  339. fi
  340. cp $INSTALL_DIR/pihole/pihole /usr/local/bin/pihole
  341. chmod 755 /usr/local/bin/pihole
  342. if [ ! -d $piholeDir ]; then
  343. mkdir $piholeDir
  344. fi
  345. if [ ! -d /opt/pihole ]; then
  346. mkdir -p /opt/pihole
  347. fi
  348. pihole_copy_files
  349. chown -R www-data:www-data /var/www/pihole/htdocs
  350. configure_firewall_for_pihole
  351. pihole_update
  352. APP_INSTALLED=1
  353. }
  354. # NOTE: deliberately no exit 0