freedombone-app-pihole 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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='69e3a45083bd311aecdbc3935986dac5ce64caa8'
  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. cp $INSTALL_DIR/pihole/adlists.default $PIHOLE_ADLIST
  52. cp $INSTALL_DIR/pihole/advanced/Scripts/* /opt/$piholeBasename
  53. cp $INSTALL_DIR/pihole/advanced/01-pihole.conf /etc/dnsmasq.d/01-pihole.conf
  54. cp $INSTALL_DIR/pihole/advanced/pihole.cron /etc/cron.d/pihole
  55. cp $INSTALL_DIR/pihole/gravity.sh /opt/$piholeBasename
  56. }
  57. function pihole_change_ipv4 {
  58. new_ipv4="$1"
  59. if [ -f /usr/local/bin/pihole ]; then
  60. setupVars=$piholeDir/setupVars.conf
  61. if [ -f $setupVars ]; then
  62. sed -i "s|IPv4_address=.*|IPv4_address=${new_ipv4}|g" $setupVars
  63. fi
  64. fi
  65. }
  66. function pihole_update {
  67. if [ ! -f /usr/local/bin/gravity.sh ]; then
  68. return
  69. fi
  70. if [ ! -f $HOME/${PROJECT_NAME}-wifi.cfg ]; then
  71. PIHOLE_IFACE=eth0
  72. else
  73. read_config_param WIFI_INTERFACE
  74. PIHOLE_IFACE=$WIFI_INTERFACE
  75. fi
  76. IPv4_address="$(get_ipv4_address)"
  77. IPv6_address="$(get_ipv6_address)"
  78. setupVars=$piholeDir/setupVars.conf
  79. echo "piholeInterface=${PIHOLE_IFACE}" > ${setupVars}
  80. echo "IPv4_address=${IPv4_address}" >> ${setupVars}
  81. echo "IPv6_address=${IPv6_address}" >> ${setupVars}
  82. echo "piholeDNS1=${PIHOLE_DNS1}" >> ${setupVars}
  83. echo "piholeDNS2=${PIHOLE_DNS1}" >> ${setupVars}
  84. echo 'domain-needed' > /etc/dnsmasq.conf
  85. echo 'bogus-priv' >> /etc/dnsmasq.conf
  86. echo 'no-resolv' >> /etc/dnsmasq.conf
  87. echo "server=${PIHOLE_DNS1}" >> /etc/dnsmasq.conf
  88. echo "server=${PIHOLE_DNS2}" >> /etc/dnsmasq.conf
  89. echo "interface=${PIHOLE_IFACE}" >> /etc/dnsmasq.conf
  90. echo 'listen-address=127.0.0.1' >> /etc/dnsmasq.conf
  91. echo 'log-queries' >> /etc/dnsmasq.conf
  92. sed -i "s|@DNS1@|${PIHOLE_DNS1}|g" /etc/dnsmasq.d/01-pihole.conf
  93. sed -i "s|@DNS2@|${PIHOLE_DNS2}|g" /etc/dnsmasq.d/01-pihole.conf
  94. sed -i "s|interface=.*|interface=${PIHOLE_IFACE}|g" /etc/dnsmasq.d/01-pihole.conf
  95. sed -i "s|@IPv4@|${IPv4_address}|g" /etc/dnsmasq.d/01-pihole.conf
  96. if [ ${#IPv6_address} -gt 0 ]; then
  97. sed -i "s|@IPv6@|${IPv6_address}|g" /etc/dnsmasq.d/01-pihole.conf
  98. else
  99. sed -i '/@IPv6@/d' /etc/dnsmasq.d/01-pihole.conf
  100. fi
  101. sed -i "s|@HOSTNAME@|$HOSTNAME|g" /etc/dnsmasq.d/01-pihole.conf
  102. sed -i "s|addn-hosts=.*|addn-hosts=${PIHOLE_ADLIST}|g" /etc/dnsmasq.d/01-pihole.conf
  103. systemctl restart dnsmasq
  104. pihole -g
  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. if [[ $(is_completed $FUNCNAME) == "1" ]]; then
  194. return
  195. fi
  196. firewall_add DNS 53
  197. mark_completed $FUNCNAME
  198. }
  199. function pihole_pause {
  200. pihole disable
  201. dialog --title $"Pause Ad Blocker" \
  202. --msgbox $"Ad blocking is paused" 6 60
  203. }
  204. function pihole_resume {
  205. pihole enable
  206. dialog --title $"Resume Ad Blocker" \
  207. --msgbox $"Ad blocking has resumed" 6 60
  208. }
  209. function configure_interactive_pihole {
  210. while true
  211. do
  212. data=$(tempfile 2>/dev/null)
  213. trap "rm -f $data" 0 1 2 5 15
  214. dialog --backtitle $"Freedombone Control Panel" \
  215. --title $"Ad Blocker" \
  216. --radiolist $"Choose an operation:" 16 70 7 \
  217. 1 $"Edit ads list" off \
  218. 2 $"Edit blacklisted domain names" off \
  219. 3 $"Edit whitelisted domain names" off \
  220. 4 $"Change upstream DNS servers" off \
  221. 5 $"Pause blocker" off \
  222. 6 $"Resume blocker" off \
  223. 7 $"Exit" on 2> $data
  224. sel=$?
  225. case $sel in
  226. 1) exit 1;;
  227. 255) exit 1;;
  228. esac
  229. case $(cat $data) in
  230. 1) editor $PIHOLE_ADLIST
  231. update_pihole_interactive
  232. ;;
  233. 2) editor $PIHOLE_BLACKLIST
  234. update_pihole_interactive
  235. ;;
  236. 3) editor $PIHOLE_WHITELIST
  237. update_pihole_interactive
  238. ;;
  239. 4) pihole_change_upstream_dns
  240. update_pihole_interactive
  241. ;;
  242. 5) pihole_pause
  243. ;;
  244. 6) pihole_resume
  245. ;;
  246. 7) break;;
  247. esac
  248. done
  249. }
  250. function install_interactive_pihole {
  251. APP_INSTALLED=1
  252. }
  253. function change_password_pihole {
  254. echo -n ''
  255. }
  256. function reconfigure_pihole {
  257. echo -n ''
  258. }
  259. function upgrade_pihole {
  260. function_check set_repo_commit
  261. set_repo_commit $INSTALL_DIR/pihole "pihole commit" "$PIHOLE_COMMIT" $PIHOLE_REPO
  262. pihole_copy_files
  263. pihole_update
  264. }
  265. function backup_local_pihole {
  266. function_check backup_directory_to_usb
  267. backup_directory_to_usb $piholeDir pihole
  268. }
  269. function restore_local_pihole {
  270. function_check restore_directory_from_usb
  271. restore_directory_from_usb / pihole
  272. }
  273. function backup_remote_pihole {
  274. function_check backup_directory_to_friend
  275. backup_directory_to_friend $piholeDir pihole
  276. }
  277. function restore_remote_pihole {
  278. function_check restore_directory_from_friend
  279. restore_directory_from_friend / pihole
  280. }
  281. function remove_pihole {
  282. apt-get -yq remove --purge dnsmasq
  283. if [ ! -d /var/www/pihole ]; then
  284. rm -rf /var/www/pihole
  285. fi
  286. if [ -f /usr/local/bin/gravity.sh ]; then
  287. rm /usr/local/bin/gravity.sh
  288. fi
  289. if [ -f /usr/local/bin/pihole ]; then
  290. rm /usr/local/bin/pihole
  291. fi
  292. if [ -d /opt/pihole ]; then
  293. rm -rf /opt/pihole
  294. fi
  295. if [ -d $piholeDir ]; then
  296. rm -rf $piholeDir
  297. fi
  298. if [ -f /var/log/pihole.log ]; then
  299. rm /var/log/pihole.log
  300. fi
  301. if [ -f /etc/cron.d/pihole ]; then
  302. rm /etc/cron.d/pihole
  303. fi
  304. if [ -d $INSTALL_DIR/pihole ]; then
  305. rm -rf $INSTALL_DIR/pihole
  306. fi
  307. firewall_remove 53
  308. userdel -r pihole
  309. }
  310. function install_pihole {
  311. apt-get -yq install dnsmasq curl
  312. adduser --disabled-login --gecos 'pi-hole' pihole
  313. usermod -a -G www-data pihole
  314. systemctl enable dnsmasq
  315. if [ ! -d $INSTALL_DIR ]; then
  316. mkdir -p $INSTALL_DIR
  317. fi
  318. if [ ! -d $INSTALL_DIR/pihole ]; then
  319. cd $INSTALL_DIR
  320. git_clone $PIHOLE_REPO pihole
  321. if [ ! -d $INSTALL_DIR/pihole ]; then
  322. exit 523925
  323. fi
  324. cd $INSTALL_DIR/pihole
  325. git checkout $PIHOLE_COMMIT -b $PIHOLE_COMMIT
  326. set_completion_param "pihole commit" "$PIHOLE_COMMIT"
  327. fi
  328. if [ ! -d /var/www/pihole/htdocs ]; then
  329. mkdir -p /var/www/pihole/htdocs
  330. fi
  331. # blank file which takes the place of ads
  332. echo '<html>' > /var/www/pihole/htdocs/index.html
  333. echo '<body>' >> /var/www/pihole/htdocs/index.html
  334. echo '</body>' >> /var/www/pihole/htdocs/index.html
  335. echo '</html>' >> /var/www/pihole/htdocs/index.html
  336. if [ ! -f $INSTALL_DIR/pihole/gravity.sh ]; then
  337. exit 26738
  338. fi
  339. cp $INSTALL_DIR/pihole/gravity.sh /usr/local/bin/gravity.sh
  340. chmod 755 /usr/local/bin/gravity.sh
  341. if [ ! -f $INSTALL_DIR/pihole/pihole ]; then
  342. exit 52935
  343. fi
  344. cp $INSTALL_DIR/pihole/pihole /usr/local/bin/pihole
  345. chmod 755 /usr/local/bin/pihole
  346. if [ ! -d $piholeDir ]; then
  347. mkdir $piholeDir
  348. fi
  349. if [ ! -d /opt/pihole ]; then
  350. mkdir -p /opt/pihole
  351. fi
  352. pihole_copy_files
  353. chown -R www-data:www-data /var/www/pihole/htdocs
  354. configure_firewall_for_pihole
  355. pihole_update
  356. APP_INSTALLED=1
  357. }
  358. # NOTE: deliberately no exit 0