freedombone-utils-network 4.4KB

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