openvpn-config-validation.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/bin/bash
  2. # if network interface docker0 is present then we are running in host mode and thus must exit
  3. if [[ ! -z "$(ifconfig | grep docker0 || true)" ]]; then
  4. echo "[OpenVPN] docker network type detected as 'Host', this will cause major issues, please stop the container and switch back to 'Bridge'. Exiting..."
  5. rm -rf /etc/services.d/openvpn && exit 1
  6. fi
  7. VPN_PROVIDER="${OPENVPN_PROVIDER,,}"
  8. echo VPN_PROVIDER is $VPN_PROVIDER
  9. # Remove the extension so we allow the user to specify or not specify the .ovpn extension
  10. VPN_CONFIG="$(echo ${OPENVPN_CONFIG} | sed 's/\b.ovpn\b//g')"
  11. # Get the directory where the providers config files sit
  12. if [[ "${VPN_PROVIDER}" == "custom" ]]; then
  13. VPN_PROVIDER_CONFIGS="/config/openvpn"
  14. # If no file has been specified or the file specified does not exist in the custom directory then find the first file in that directory
  15. if [[ ! -f "${VPN_PROVIDER_CONFIGS}/${VPN_CONFIG}.ovpn" ]]; then
  16. # Remove mac os files before we try find the first
  17. rm -rf /config/openvpn/._*.ovpn
  18. VPN_CONFIG=$(basename -- $(find ${VPN_PROVIDER_CONFIGS} -maxdepth 1 -name "*.ovpn" -print -quit) | sed 's/\b.ovpn\b//g')
  19. fi
  20. # If the file exists then lets clean it and link our credentials files
  21. if [[ -f "${VPN_PROVIDER_CONFIGS}/${VPN_CONFIG}.ovpn" ]]; then
  22. /etc/scripts/openvpn-config-clean.sh "${VPN_PROVIDER_CONFIGS}/${VPN_CONFIG}.ovpn"
  23. sed -i "s/auth-user-pass.*/auth-user-pass \/config\/openvpn\/custom-openvpn-credentials.txt/g" "${VPN_PROVIDER_CONFIGS}/${VPN_CONFIG}.ovpn"
  24. fi
  25. else
  26. # Set the pia config file to tcp directory if the protocol is tcp
  27. if [[ "${VPN_PROVIDER}" == "pia" && "${OPENVPN_PROTOCOL,,}" == "tcp" ]]; then
  28. VPN_PROVIDER_CONFIGS="/etc/openvpn/${VPN_PROVIDER}/tcp"
  29. else
  30. VPN_PROVIDER_CONFIGS="/etc/openvpn/${VPN_PROVIDER}"
  31. # allow for a config file without the _udp (protocol) to be used
  32. if [[ ! -f "${VPN_PROVIDER_CONFIGS}/${VPN_CONFIG}.ovpn" ]] && [[ -f "${VPN_PROVIDER_CONFIGS}/${VPN_CONFIG}_${OPENVPN_PROTOCOL,,}.ovpn" ]]; then
  33. VPN_CONFIG="${VPN_CONFIG}_${OPENVPN_PROTOCOL,,}"
  34. fi
  35. fi
  36. fi
  37. # Exit out if the provider config directory does not exist
  38. if [[ ! -d "${VPN_PROVIDER_CONFIGS}" ]]; then
  39. echo "[OpenVPN] Could not find provider: ${OPENVPN_PROVIDER}. Exiting..." | ts '%Y-%m-%d %H:%M:%S'
  40. rm -rf /etc/services.d/openvpn && exit 1
  41. fi
  42. if [[ "${VPN_PROVIDER}" == "surfshark" ]] && [[ ! -f "${VPN_PROVIDER_CONFIGS}/${VPN_CONFIG}.ovpn" ]]; then
  43. # This allows us to use a list similar to PIA list file and try and match it to surfsharks config file. Allows for a somewhat quick transition from PIA to Surfshark
  44. SURFSHARK_CLUSTERS_URL='https://my.surfshark.com/vpn/api/v1/server/clusters'
  45. LOCATIONS=$(curl -s "${SURFSHARK_CLUSTERS_URL}" | jq -r '.[] | [.countryCode,.location,":",.location,":",.country,":",.connectionName] | @sh' | tr -d "'")
  46. readarray -t LOCATIONS_ARRAY <<< "${LOCATIONS//,/$'\n'}"
  47. ## Try match the config file to SURSHARKS config files
  48. for i in "${!LOCATIONS_ARRAY[@]}"; do
  49. IFS=: read -r -a VALUES <<< "${LOCATIONS_ARRAY[${i}]}"
  50. CODE_AND_LOCATION=$(echo ${VALUES[0]} | sed 's/ *$//g')
  51. LOCATION=$(echo ${VALUES[1]} | sed 's/ *$//g')
  52. COUNTRY=$(echo ${VALUES[2]} | sed 's/ *$//g')
  53. FILE=$(echo ${VALUES[3]} | sed 's/ *$//g')
  54. if [[ "${CODE_AND_LOCATION}" == "${VPN_CONFIG}" ]] || [[ "${COUNTRY} ${LOCATION}" == "${VPN_CONFIG}" ]] || [[ "${COUNTRY}" == "${VPN_CONFIG}" ]]; then
  55. VPN_CONFIG="$(echo "${FILE}" | sed -e 's/\<.ovpn\>//g')_${OPENVPN_PROTOCOL,,}"
  56. if [[ "${DEBUG}" == "true" ]]; then
  57. echo "[OpenVPN] ${VPN_CONFIG} found using the surfshark API" | ts '%Y-%m-%d %H:%M:%S'
  58. fi
  59. fi
  60. done
  61. fi
  62. if [[ -f "${VPN_PROVIDER_CONFIGS}/${VPN_CONFIG}.ovpn" ]]; then
  63. echo "[OpenVPN] config file ${VPN_CONFIG}.ovpn found" | ts '%Y-%m-%d %H:%M:%S'
  64. VPN_CONFIG="${VPN_PROVIDER_CONFIGS}/${VPN_CONFIG}.ovpn"
  65. else
  66. echo "[OpenVPN] supplied config ${VPN_PROVIDER_CONFIGS}/${VPN_CONFIG}.ovpn could not be found. Exiting..." | ts '%Y-%m-%d %H:%M:%S'
  67. rm -rf /etc/services.d/openvpn && exit 1
  68. fi