github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/bin/package/config/darwin/update-resolv-conf (about)

     1  #!/bin/bash
     2  
     3  # Mac name-resolution updater based on @cl's script here:
     4  # https://blog.netnerds.net/2011/10/openvpn-update-client-dns-on-mac-os-x-using-from-the-command-line/
     5  # Openvpn envar parsing taken from the script in debian's openvpn package.
     6  # Smushed together and improved by @andrewgdotcom.
     7  
     8  # Parses DHCP options from openvpn to update resolv.conf
     9  # To use set as 'up' and 'down' script in your openvpn *.conf:
    10  # up /etc/openvpn/update-resolv-conf
    11  # down /etc/openvpn/update-resolv-conf
    12  
    13  [ "$script_type" ] || exit 0
    14  [ "$dev" ] || exit 0
    15  
    16  NMSRVRS=()
    17  SRCHS=()
    18  adapters=()
    19  
    20  NETWORKSETUP=/usr/sbin/networksetup
    21  
    22  # Set bash delimeter to be line break (temporarily)
    23  IFSSAVE=$IFS
    24  IFS=$'\n'
    25  # Get adapter list
    26  for i in `$NETWORKSETUP -listallnetworkservices | grep -v denotes`; do
    27      adapters=(${adapters[@]} "$i")
    28  done
    29  IFS=$IFSSAVE
    30  
    31  split_into_parts()
    32  {
    33      part1="$1"
    34      part2="$2"
    35      part3="$3"
    36  }
    37  
    38  update_all_dns()
    39  {
    40      for adapter in "${adapters[@]}"
    41      do
    42          echo updating dns for $adapter
    43          # set dns server to the vpn dns server
    44          if [[ "${SRCHS[@]}" ]]; then
    45              if [ ! -f "/tmp/myst.$adapter.searchdomains" ]; then
    46                  $NETWORKSETUP -getsearchdomains "$adapter" | grep -v "There aren't any" > "/tmp/myst.$adapter.searchdomains"
    47              fi
    48              $NETWORKSETUP -setsearchdomains "$adapter" "${SRCHS[@]}"
    49          fi
    50          if [[ "${NMSRVRS[@]}" ]]; then
    51              if [ ! -f "/tmp/myst.$adapter.dnsservers" ]; then
    52                  $NETWORKSETUP -getdnsservers "$adapter" | grep -v "There aren't any" > "/tmp/myst.$adapter.dnsservers"
    53              fi
    54              $NETWORKSETUP -setdnsservers "$adapter" "${NMSRVRS[@]}"
    55          fi
    56      done
    57  }
    58  
    59  clear_all_dns()
    60  {
    61      for adapter in "${adapters[@]}"
    62      do
    63          echo updating dns for $adapter
    64  
    65          dnsservers=""
    66          searchdomains=""
    67          dnsservers=`echo $(cat "/tmp/myst.$adapter.dnsservers")`
    68          searchdomains=`echo $(cat "/tmp/myst.$adapter.searchdomains")`
    69  
    70          if [ -z "$dnsservers" ]; then
    71              dnsservers="empty"
    72          fi
    73          if [ -z "$searchdomains" ]; then
    74              searchdomains="empty"
    75          fi
    76  
    77          if [ -f "/tmp/myst.$adapter.dnsservers" ]; then
    78              $NETWORKSETUP -setdnsservers "$adapter" $dnsservers
    79              rm "/tmp/myst.$adapter.dnsservers"
    80          fi
    81          if [ -f "/tmp/myst.$adapter.searchdomains" ]; then
    82              $NETWORKSETUP -setsearchdomains "$adapter" $searchdomains
    83              rm "/tmp/myst.$adapter.searchdomains"
    84          fi
    85      done
    86  }
    87  
    88  case "$script_type" in
    89      up)
    90          for optionvarname in ${!foreign_option_*} ; do
    91              option="${!optionvarname}"
    92              echo "$option"
    93              split_into_parts $option
    94              if [ "$part1" = "dhcp-option" ] ; then
    95                  if [ "$part2" = "DNS" ] ; then
    96                      NMSRVRS=(${NMSRVRS[@]} $part3)
    97                      elif [ "$part2" = "DOMAIN" ] ; then
    98                      SRCHS=(${SRCHS[@]} $part3)
    99                  fi
   100              fi
   101          done
   102          update_all_dns
   103      ;;
   104      down)
   105          clear_all_dns
   106      ;;
   107  esac