freedombone-app-pihole 11KB

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