fetch.sh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env bash
  2. cd "$(dirname $0)" && source "inc/common.sh"
  3. if [ -z "$(command -v base64)" ]; then
  4. echo 'This script needs base64 binary. Please install base64 package. Exiting...'; exit 2953
  5. fi
  6. if [ -z "$(command -v openvpn)" ]; then
  7. echo 'This script needs openvpn. Please install openvpn package. Exiting...'; exit 2831
  8. fi
  9. WRITE_OUTPUT=0
  10. if [ -f "$CSVOUTPUT" ]; then
  11. is_file_stale "$CSVOUTPUT"
  12. if [ "$?" -gt 0 ]; then
  13. if $auto_update_when_stale; then
  14. WRITE_OUTPUT=1
  15. else
  16. read -e -p "CSV data is older than $stale_threshold_hours hour(s). Do you want to download the latest version? [Y/n] " choice
  17. if [[ ! "$choice" == [Nn]* ]]; then # ! means default is Y
  18. WRITE_OUTPUT=1
  19. fi
  20. fi
  21. else
  22. read -e -p "CSV data is already there. Do you want to download the latest version? [y/N] " choice
  23. if [[ "$choice" == [Yy]* ]]; then
  24. WRITE_OUTPUT=1
  25. fi
  26. fi
  27. else
  28. WRITE_OUTPUT=1
  29. fi
  30. if [ "$WRITE_OUTPUT" -gt 0 ]; then
  31. echo '== Downloading latest VPN connections data...'
  32. if [ -x "$(command -v curl)" ]; then
  33. curl -L 'http://www.vpngate.net/api/iphone/' -o "$CSVOUTPUT"
  34. elif [ -x "$(command -v wget)" ]; then
  35. wget 'http://www.vpngate.net/api/iphone/' -o "$CSVOUTPUT"
  36. else
  37. echo 'curl or wget is required. Exiting...'; exit 9437
  38. fi
  39. fi
  40. # Trim the vpn csv data
  41. # First 2 lines are just data type, column name etc. so we use $csv_offset to get rid of them.
  42. # Columns:
  43. # 01. #HostName
  44. # 02. IP
  45. # 03. Score
  46. # 04. Ping
  47. # 05. Speed
  48. # 06. CountryLong
  49. # 07. CountryShort
  50. # 08. NumVpnSessions
  51. # 09. Uptime
  52. # 10. TotalUsers
  53. # 11. TotalTraffic
  54. # 12. LogType
  55. # 13. Operator
  56. # 14. Message
  57. # 15. OpenVPN_ConfigData_Base64
  58. csv_text=$(head -n $(( $ovpn_count + $csv_offset )) "$CSVOUTPUT" | tail -n -$ovpn_count)
  59. rm -rf "$DATADIR_OVPNS"
  60. mkdir -p "$DATADIR_OVPNS"
  61. count=0
  62. echo "$csv_text" | while read -r line ; do
  63. ((count++))
  64. # CountryShort|Score|Ping|Speed
  65. echo "$line" | awk -F',' -v count="$count" '{ print count ".\t" $7 "\t" $3 "\t" $4 "ms \t" $5/1000/1000 "mbps" }' >> "$DATADIR_OVPNS/ovpns.list"
  66. if [ ! -z "$(base64 --help | grep 'i, --ignore-garbage')" ]; then # GNU+Linux
  67. ignore_error_option='i'
  68. else # BSD etc.
  69. ignore_error_option='n'
  70. fi
  71. echo "$line" | awk -F',' '{ print $15 }' | base64 -d${ignore_error_option} > "$DATADIR_OVPNS/$count.ovpn"
  72. done