freedombone-utils-dns 7.2KB

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