autovpngate.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #! /usr/bin/env python
  2. # License: CC0
  3. # From: https://gist.github.com/adnan360/f5bf854a9278612e0effedbfa202d6fc
  4. # Run: python autovpngate.py
  5. # For CSV parsing and ovpn file handling
  6. import os
  7. import csv
  8. import base64, string
  9. import subprocess
  10. from subprocess import Popen, PIPE
  11. # Set encoding to utf8 for Python 2
  12. # Python 3 has it set by default
  13. import sys
  14. if sys.version[0] == '2':
  15. reload(sys)
  16. sys.setdefaultencoding("utf-8")
  17. # For regex
  18. import re
  19. try:
  20. # For Python 3.0 and later
  21. from urllib.request import urlopen
  22. except ImportError:
  23. # Fall back to Python 2's urllib2
  24. from urllib2 import urlopen
  25. storagepath = '/tmp/'
  26. # --------------- Functions ---------------
  27. def runme(program):
  28. p = subprocess.Popen([program], shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  29. output = ''
  30. for line in p.stdout.readlines():
  31. #print(line)
  32. output = output+'\n'+line.decode("utf-8")
  33. retval = p.wait()
  34. return output
  35. def deletefile(filename):
  36. if os.path.isfile(filename):
  37. os.remove(filename)
  38. # --------------- Get data from vpngate ---------------
  39. print('downloading latest vpn connection data...')
  40. data = urlopen("http://www.vpngate.net/api/iphone/")
  41. htmlcode = data.read()
  42. print('download finished...')
  43. print('processing...')
  44. deletefile(storagepath+'output.csv')
  45. f = open(storagepath+'output.csv', 'wb')
  46. f.write(htmlcode)
  47. # --------------- Handle CSV and OVPN ---------------
  48. print('updating connections...')
  49. with open(storagepath+'output.csv') as csvfile:
  50. reader = csv.reader(csvfile, delimiter=',')
  51. count = 0
  52. # find previous auto connections
  53. output = runme('sudo /usr/bin/nmcli connection show')
  54. connections = [word for word in output.split() if word.startswith('vpngateauto')]
  55. # remove previous auto connections
  56. for conn in connections:
  57. print('deleting previously created auto connection '+conn+'...')
  58. runme('sudo /usr/bin/nmcli connection delete id '+conn)
  59. for row in reader:
  60. # take first 6 connections
  61. if count < 6:
  62. if len(row) > 13 and row[14] != 'OpenVPN_ConfigData_Base64':
  63. count = count + 1
  64. decoded = base64.b64decode(row[14])
  65. countrycode = row[6]
  66. ovpnname = 'vpngateauto_'+str(count)+'_'+countrycode
  67. deletefile(storagepath+ovpnname+'.ovpn')
  68. with open(storagepath+ovpnname+'.ovpn', 'a') as out:
  69. out.write(decoded.decode('UTF-8') + '\n')
  70. # create connection
  71. print('creating vpngate auto connection '+ovpnname+'...')
  72. runme('sudo /usr/bin/nmcli connection import type openvpn file '+storagepath+ovpnname+'.ovpn')
  73. deletefile(storagepath+ovpnname+'.ovpn')
  74. deletefile(storagepath+'output.csv')
  75. else:
  76. break