freedombone-app-pihole 11KB

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