freedombone-utils-network 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Network functions
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
  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. # If the system is on an IPv6 network
  31. IPV6_NETWORK='2001:470:26:307'
  32. # Destinations used to get the local IP address of this system
  33. # Google ipv6 DNS 2001:4860:4860::8888
  34. # OpenDNS ipv6 DNS 2620:0:ccc::2
  35. IPV4_ADDRESS_TEST_DESTINATION='85.214.73.63'
  36. IPV6_ADDRESS_TEST_DESTINATION='2620:0:ccc::2'
  37. EXTERNAL_IP_LOOKUP_URL='ifcfg.me'
  38. # The static IP address of the system within the local network
  39. # By default the IP address is dynamic within your LAN
  40. LOCAL_NETWORK_STATIC_IP_ADDRESS=
  41. # IP address of the router (gateway)
  42. ROUTER_IP_ADDRESS="192.168.1.254"
  43. MESH_INSTALL_DIR=/var/lib
  44. function get_app_icann_address {
  45. app_name="$1"
  46. if grep -q "${app_name} domain" "$COMPLETION_FILE"; then
  47. grep "${app_name} domain" "${COMPLETION_FILE}" | head -n 1 | awk -F ':' '{print $2}'
  48. return
  49. else
  50. app_name_upper="$(echo "$app_name" | tr '[:lower:]' '[:upper:]')_DOMAIN_NAME"
  51. if [ "$app_name_upper" ]; then
  52. param_value=$(grep "${app_name_upper}=" "$CONFIGURATION_FILE" | head -n 1 | awk -F '=' '{print $2}')
  53. if [ "${param_value}" ]; then
  54. echo "${param_value}"
  55. return
  56. fi
  57. fi
  58. fi
  59. echo "${DEFAULT_DOMAIN_NAME}"
  60. }
  61. function install_static_network {
  62. if [[ $(is_completed "${FUNCNAME[0]}") == "1" ]]; then
  63. return
  64. fi
  65. if [[ $INSTALLING_ON_BBB == "yes" ]]; then
  66. return
  67. fi
  68. if [ ! $LOCAL_NETWORK_STATIC_IP_ADDRESS ]; then
  69. return
  70. fi
  71. echo '# This file describes the network interfaces available on your system' > /etc/network/interfaces
  72. echo '# and how to activate them. For more information, see interfaces(5).' >> /etc/network/interfaces
  73. echo 'source /etc/network/interfaces.d/*' >> /etc/network/interfaces
  74. { echo 'auto eth0';
  75. echo 'iface eth0 inet static';
  76. echo " address $LOCAL_NETWORK_STATIC_IP_ADDRESS";
  77. echo ' netmask 255.255.255.0';
  78. echo " gateway $ROUTER_IP_ADDRESS"; } > /etc/network/interfaces.d/static
  79. mark_completed "${FUNCNAME[0]}"
  80. }
  81. function get_external_ipv4_address {
  82. nslookup . $EXTERNAL_IP_LOOKUP_URL | grep Address | tail -n 1 | awk -F ' ' '{print $2}'
  83. }
  84. function get_ipv4_address {
  85. IPv4dev=$(ip route get $IPV4_ADDRESS_TEST_DESTINATION | awk '{for(i=1;i<=NF;i++)if($i~/dev/)print $(i+1)}')
  86. ip -o -f inet addr show dev "$IPv4dev" | awk '{print $4}' | awk 'END {print}' | awk -F '/' '{print $1}'
  87. }
  88. function get_ipv6_address {
  89. retval=$(ip -6 route get $IPV6_ADDRESS_TEST_DESTINATION 2> /dev/null)
  90. echo "$retval" | awk -F " " '{ for(i=1;i<=NF;i++) if ($i == "src") print $(i+1) }'
  91. }
  92. function update_external_ip {
  93. ip_update_script=/usr/bin/externalipupdate
  94. { echo '#!/bin/bash';
  95. echo "existing_ip=\$(cat $CONFIGURATION_FILE | grep \"EXTERNAL_IPV4_ADDRESS=\" | head -n 1 | awk -F '=' '{print \$2}')'";
  96. echo "curr_ip=\$(nslookup . $EXTERNAL_IP_LOOKUP_URL | grep Address | tail -n 1 | awk -F ' ' '{print \$2}')";
  97. echo "if [[ \"\$curr_ip\" != \"\$existing_ip\" ]]; then";
  98. echo " sed -i \"s|EXTERNAL_IPV4_ADDRESS=.*|EXTERNAL_IPV4_ADDRESS=\${curr_ip}|g\" $CONFIGURATION_FILE";
  99. echo " echo \"\$(date)\" >> ~/${PROJECT_NAME}-external-ip-changes.txt";
  100. echo 'fi'; } > $ip_update_script
  101. cron_add_mins 10 $ip_update_script
  102. }
  103. # NOTE: deliberately no exit 0