freedombone-utils-dns 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # DNS functions
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. # DNS
  29. NAMESERVER1='91.239.100.100'
  30. NAMESERVER2='89.233.43.71'
  31. NAMESERVER3='87.118.100.175'
  32. NAMESERVER4='94.75.228.29'
  33. NAMESERVER5='85.25.251.254'
  34. NAMESERVER6='2.141.58.13'
  35. # parameters used when adding a new domain
  36. DDNS_PROVIDER="default@freedns.afraid.org"
  37. DDNS_USERNAME=
  38. DDNS_PASSWORD=
  39. INADYN_REPO="https://github.com/bashrc/inadyn"
  40. INADYN_COMMIT='fadbe17f520d337dfb8d69ee4bf1fcaa23fce0d6'
  41. # web site used to obtain the external IP address of the system
  42. GET_IP_ADDRESS_URL="checkip.two-dns.de"
  43. # other possible services to obtain the external IP address
  44. EXTERNAL_IP_SERVICES=( \
  45. 'https://check.torproject.org/' \
  46. 'https://www.whatsmydns.net/whats-my-ip-address.html' \
  47. 'https://www.privateinternetaccess.com/pages/whats-my-ip/' \
  48. 'http://checkip.two-dns.de' \
  49. 'http://ip.dnsexit.com' \
  50. 'http://ifconfig.me/ip' \
  51. 'http://ipecho.net/plain' \
  52. 'http://checkip.dyndns.org/plain' \
  53. 'http://ipogre.com/linux.php' \
  54. 'http://whatismyipaddress.com/' \
  55. 'http://ip.my-proxy.com/' \
  56. 'http://websiteipaddress.com/WhatIsMyIp' \
  57. 'http://getmyipaddress.org/' \
  58. 'http://www.my-ip-address.net/' \
  59. 'http://myexternalip.com/raw' \
  60. 'http://www.canyouseeme.org/' \
  61. 'http://www.trackip.net/' \
  62. 'http://icanhazip.com/' \
  63. 'http://www.iplocation.net/' \
  64. 'http://www.howtofindmyipaddress.com/' \
  65. 'http://www.ipchicken.com/' \
  66. 'http://whatsmyip.net/' \
  67. 'http://www.ip-adress.com/' \
  68. 'http://checkmyip.com/' \
  69. 'http://www.tracemyip.org/' \
  70. 'http://checkmyip.net/' \
  71. 'http://www.lawrencegoetz.com/programs/ipinfo/' \
  72. 'http://www.findmyip.co/' \
  73. 'http://ip-lookup.net/' \
  74. 'http://www.dslreports.com/whois' \
  75. 'http://www.mon-ip.com/en/my-ip/' \
  76. 'http://www.myip.ru' \
  77. 'http://ipgoat.com/' \
  78. 'http://www.myipnumber.com/my-ip-address.asp' \
  79. 'http://www.whatsmyipaddress.net/' \
  80. 'http://formyip.com/' \
  81. 'http://www.displaymyip.com/' \
  82. 'http://www.bobborst.com/tools/whatsmyip/' \
  83. 'http://www.geoiptool.com/' \
  84. 'http://checkip.dyndns.com/' \
  85. 'http://myexternalip.com/' \
  86. 'http://www.ip-adress.eu/' \
  87. 'http://www.infosniper.net/' \
  88. 'http://wtfismyip.com/' \
  89. 'http://ipinfo.io/' \
  90. 'http://httpbin.org/ip')
  91. function create_freedns_updater {
  92. if [[ $ONION_ONLY != "no" ]]; then
  93. return
  94. fi
  95. # currently inadyn doesn't work as expected with freeDNS, so this is a workaround
  96. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  97. return
  98. fi
  99. if [[ $DDNS_PROVIDER != *"freedns"* ]]; then
  100. return
  101. fi
  102. if [[ $SYSTEM_TYPE == "mesh"* ]]; then
  103. return
  104. fi
  105. # remove any legacy command
  106. if [ -f /usr/bin/dynamicdns ]; then
  107. rm /usr/bin/dynamicdns
  108. fi
  109. if grep -q "dynamicdns" /etc/crontab; then
  110. sed -i '/dynamicdns/d' /etc/crontab
  111. fi
  112. # add the update command to cron
  113. if ! grep -q "/usr/local/bin/freedombone-freedns" /etc/crontab; then
  114. function_check cron_add_mins
  115. cron_add_mins 3 '/usr/local/bin/freedombone-freedns'
  116. systemctl restart cron
  117. fi
  118. mark_completed "${FUNCNAME[0]}"
  119. }
  120. function add_ddns_domain {
  121. if [ ! "$1" ]; then
  122. echo $'ddns domain not specified'
  123. exit 5638
  124. fi
  125. CURRENT_DDNS_DOMAIN="$1"
  126. if [[ $ONION_ONLY != "no" ]]; then
  127. return
  128. fi
  129. if [[ "$DDNS_PROVIDER" == 'none' ]]; then
  130. return
  131. fi
  132. if [ ! -f /etc/inadyn.conf ]; then
  133. echo $'Unable to find inadyn configuration file /etc/inadyn.conf'
  134. exit 5745
  135. fi
  136. if ! grep -q "$DDNS_PROVIDER" /etc/inadyn.conf; then
  137. { echo '';
  138. echo "system $DDNS_PROVIDER";
  139. echo ' ssl';
  140. echo " checkip-url $GET_IP_ADDRESS_URL /"; } >> /etc/inadyn.conf
  141. if [ $DDNS_USERNAME ]; then
  142. echo " username $DDNS_USERNAME" >> /etc/inadyn.conf
  143. fi
  144. if [ $DDNS_PASSWORD ]; then
  145. echo " password $DDNS_PASSWORD" >> /etc/inadyn.conf
  146. fi
  147. fi
  148. if ! grep -q "$CURRENT_DDNS_DOMAIN" /etc/inadyn.conf; then
  149. echo " alias $CURRENT_DDNS_DOMAIN" >> /etc/inadyn.conf
  150. fi
  151. chmod 600 /etc/inadyn.conf
  152. systemctl restart inadyn
  153. systemctl daemon-reload
  154. }
  155. function remove_ddns_domain {
  156. if [ ! "$1" ]; then
  157. echo $'ddns domain not specified'
  158. exit 5638
  159. fi
  160. CURRENT_DDNS_DOMAIN="$1"
  161. if [[ $ONION_ONLY != "no" ]]; then
  162. return
  163. fi
  164. if [[ "$DDNS_PROVIDER" == 'none' ]]; then
  165. return
  166. fi
  167. if [ ! -f /etc/inadyn.conf ]; then
  168. echo $'Unable to find inadyn configuration file /etc/inadyn.conf'
  169. exit 5745
  170. fi
  171. if grep -q "$CURRENT_DDNS_DOMAIN" /etc/inadyn.conf; then
  172. systemctl stop inadyn
  173. sed -i "/alias $CURRENT_DDNS_DOMAIN/d" /etc/inadyn.conf
  174. systemctl start inadyn
  175. systemctl daemon-reload
  176. fi
  177. }
  178. function configure_dns {
  179. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  180. return
  181. fi
  182. apt-get -yq install resolvconf
  183. resolvconf=/etc/resolvconf/resolv.conf.d/head
  184. # allow changes to resolv.conf
  185. chattr -i $resolvconf
  186. { echo 'domain localdomain';
  187. echo 'search localdomain';
  188. echo "nameserver $NAMESERVER1";
  189. echo "nameserver $NAMESERVER2";
  190. echo "nameserver $NAMESERVER3";
  191. echo "nameserver $NAMESERVER4";
  192. echo "nameserver $NAMESERVER5";
  193. echo "nameserver $NAMESERVER6"; } > $resolvconf
  194. # prevent resolv.conf from changing
  195. resolvconf -u
  196. mark_completed "${FUNCNAME[0]}"
  197. }
  198. function set_hostname {
  199. DEFAULT_DOMAIN_NAME="$1"
  200. echo "$DEFAULT_DOMAIN_NAME" > /etc/hostname
  201. hostname "$DEFAULT_DOMAIN_NAME"
  202. echo "$DEFAULT_DOMAIN_NAME" > /etc/mailname
  203. if grep -q "127.0.1.1" /etc/hosts; then
  204. sed -i "s/127.0.1.1.*/127.0.1.1 $DEFAULT_DOMAIN_NAME/g" /etc/hosts
  205. else
  206. echo "127.0.1.1 $DEFAULT_DOMAIN_NAME" >> /etc/hosts
  207. fi
  208. }
  209. function set_your_domain_name {
  210. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  211. return
  212. fi
  213. function_check set_hostname
  214. set_hostname "$DEFAULT_DOMAIN_NAME"
  215. mark_completed "${FUNCNAME[0]}"
  216. }
  217. function configure_firewall_for_dns {
  218. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  219. return
  220. fi
  221. if [[ $INSTALLED_WITHIN_DOCKER == "yes" ]]; then
  222. # docker does its own firewalling
  223. return
  224. fi
  225. iptables -A INPUT -p udp -m udp --dport 1024:65535 --sport 53 -j ACCEPT
  226. function_check save_firewall_settings
  227. save_firewall_settings
  228. mark_completed "${FUNCNAME[0]}"
  229. }
  230. # NOTE: deliberately no exit 0