freedombone-app-pihole 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. #iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
  192. iptables -A INPUT -p tcp -m tcp --dport 53 -j ACCEPT
  193. iptables -A INPUT -p udp -m udp --dport 53 -j ACCEPT
  194. function_check save_firewall_settings
  195. save_firewall_settings
  196. OPEN_PORTS+=('DNS 53')
  197. mark_completed $FUNCNAME
  198. }
  199. function configure_interactive_pihole {
  200. while true
  201. do
  202. data=$(tempfile 2>/dev/null)
  203. trap "rm -f $data" 0 1 2 5 15
  204. dialog --backtitle $"Freedombone Control Panel" \
  205. --title $"Ad Blocker" \
  206. --radiolist $"Choose an operation:" 14 70 5 \
  207. 1 $"Edit ads list" off \
  208. 2 $"Edit blacklisted domain names" off \
  209. 3 $"Edit whitelisted domain names" off \
  210. 4 $"Change upstream DNS servers" off \
  211. 5 $"Exit" on 2> $data
  212. sel=$?
  213. case $sel in
  214. 1) exit 1;;
  215. 255) exit 1;;
  216. esac
  217. case $(cat $data) in
  218. 1) editor $PIHOLE_ADLIST
  219. update_pihole_interactive
  220. ;;
  221. 2) editor $PIHOLE_BLACKLIST
  222. update_pihole_interactive
  223. ;;
  224. 3) editor $PIHOLE_WHITELIST
  225. update_pihole_interactive
  226. ;;
  227. 4) pihole_change_upstream_dns
  228. update_pihole_interactive
  229. ;;
  230. 5) break;;
  231. esac
  232. done
  233. }
  234. function install_interactive_pihole {
  235. APP_INSTALLED=1
  236. }
  237. function change_password_pihole {
  238. echo -n ''
  239. }
  240. function reconfigure_pihole {
  241. echo -n ''
  242. }
  243. function upgrade_pihole {
  244. function_check set_repo_commit
  245. set_repo_commit $INSTALL_DIR/pihole "pihole commit" "$PIHOLE_COMMIT" $PIHOLE_REPO
  246. pihole_copy_files
  247. pihole_update
  248. }
  249. function backup_local_pihole {
  250. function_check backup_directory_to_usb
  251. backup_directory_to_usb $piholeDir pihole
  252. }
  253. function restore_local_pihole {
  254. function_check restore_directory_from_usb
  255. restore_directory_from_usb / pihole
  256. }
  257. function backup_remote_pihole {
  258. function_check backup_directory_to_friend
  259. backup_directory_to_friend $piholeDir pihole
  260. }
  261. function restore_remote_pihole {
  262. function_check restore_directory_from_friend
  263. restore_directory_from_friend / pihole
  264. }
  265. function remove_pihole {
  266. apt-get -y remove --purge dnsmasq
  267. if [ ! -d /var/www/pihole ]; then
  268. rm -rf /var/www/pihole
  269. fi
  270. if [ -f /usr/local/bin/gravity.sh ]; then
  271. rm /usr/local/bin/gravity.sh
  272. fi
  273. if [ -f /usr/local/bin/pihole ]; then
  274. rm /usr/local/bin/pihole
  275. fi
  276. if [ -d /opt/pihole ]; then
  277. rm -rf /opt/pihole
  278. fi
  279. if [ -d $piholeDir ]; then
  280. rm -rf $piholeDir
  281. fi
  282. if [ -f /var/log/pihole.log ]; then
  283. rm /var/log/pihole.log
  284. fi
  285. if [ -f /etc/cron.d/pihole ]; then
  286. rm /etc/cron.d/pihole
  287. fi
  288. userdel -r pihole
  289. }
  290. function install_pihole {
  291. apt-get -y install dnsmasq curl
  292. adduser --disabled-login --gecos 'pi-hole' pihole
  293. usermod -a -G www-data pihole
  294. systemctl enable dnsmasq
  295. if [ ! -d $INSTALL_DIR ]; then
  296. mkdir -p $INSTALL_DIR
  297. fi
  298. if [ ! -d $INSTALL_DIR/pihole ]; then
  299. cd $INSTALL_DIR
  300. git_clone $PIHOLE_REPO pihole
  301. if [ ! -d $INSTALL_DIR/pihole ]; then
  302. exit 523925
  303. fi
  304. cd $INSTALL_DIR/pihole
  305. git checkout $PIHOLE_COMMIT -b $PIHOLE_COMMIT
  306. set_completion_param "pihole commit" "$PIHOLE_COMMIT"
  307. fi
  308. if [ ! -d /var/www/pihole/htdocs ]; then
  309. mkdir -p /var/www/pihole/htdocs
  310. fi
  311. # blank file which takes the place of ads
  312. echo '<html>' > /var/www/pihole/htdocs/index.html
  313. echo '<body>' >> /var/www/pihole/htdocs/index.html
  314. echo '</body>' >> /var/www/pihole/htdocs/index.html
  315. echo '</html>' >> /var/www/pihole/htdocs/index.html
  316. if [ ! -f $INSTALL_DIR/pihole/gravity.sh ]; then
  317. exit 26738
  318. fi
  319. cp $INSTALL_DIR/pihole/gravity.sh /usr/local/bin/gravity.sh
  320. chmod 755 /usr/local/bin/gravity.sh
  321. if [ ! -f $INSTALL_DIR/pihole/pihole ]; then
  322. exit 52935
  323. fi
  324. cp $INSTALL_DIR/pihole/pihole /usr/local/bin/pihole
  325. chmod 755 /usr/local/bin/pihole
  326. if [ ! -d $piholeDir ]; then
  327. mkdir $piholeDir
  328. fi
  329. if [ ! -d /opt/pihole ]; then
  330. mkdir -p /opt/pihole
  331. fi
  332. pihole_copy_files
  333. chown -R www-data:www-data /var/www/pihole/htdocs
  334. configure_firewall_for_pihole
  335. pihole_update
  336. APP_INSTALLED=1
  337. }
  338. # NOTE: deliberately no exit 0