Skip to content

Instantly share code, notes, and snippets.

@rishubil
Forked from Lazza/README.md
Last active March 3, 2023 22:50

Revisions

  1. rishubil revised this gist Jun 27, 2017. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,12 @@
    #VPNGate for OS X
    # VPNGate for OS X
    This script is a simple script for using to connect vpn server list provided [VPNGate](http://www.vpngate.net/en/) through [OpenVPN](http://openvpn.net/) on OS X environment.

    It inspired by [VPNGate Python script](https://gist.github.com/Lazza/bbc15561b65c16db8ca8) wrtten by Andrea Lazzarotto.

    ##Requirements
    ## Requirements
    If you use the script, [Tunnelblick](https://code.google.com/p/tunnelblick/) needs to be installed first.

    ##Installation
    ## Installation
    Open terminal app and copy & paste the command below.
    ```
    cd ~
    @@ -15,22 +15,22 @@ curl -L -o vpngate.py http://bit.ly/osxvpngatepy

    or [download](https://gist.github.com/rishubil/896bb1b6fbebdcc5ba5a/download#file-vpngate-py), unzip it and copy `vpngate.py` to user home directory.

    ##Quickstart
    ## Quickstart
    Just type `python vpngate.py` in your terminal screen and select country and server you want to connect.
    The connection can be terminated by pressing <kbd>Ctrl</kbd>+<kbd>C</kbd>.

    If you want connect vpn server using default options, just type `python vpngate.py -y`.

    ##Troubleshooting
    ## Troubleshooting

    ###I forgot pressing <kbd>Ctrl</kbd>+<kbd>C</kbd> before quitting my terminal. I can't disconnect vpn connection.
    ### I forgot pressing <kbd>Ctrl</kbd>+<kbd>C</kbd> before quitting my terminal. I can't disconnect vpn connection.
    Open your terminal and type the command below. And please don't forget it anymore.
    ```
    ps aux | grep openvpn | awk '{ print $2 }' | sudo xargs kill -9
    ```

    ##Demo
    ## Demo
    [![YT video](http://img.youtube.com/vi/Le3ixkWkblg/0.jpg)](http://youtu.be/Le3ixkWkblg)

    ##License?
    ## License?
    This script is under GPLv3 license.
  2. rishubil revised this gist Mar 11, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ If you use the script, [Tunnelblick](https://code.google.com/p/tunnelblick/) nee
    Open terminal app and copy & paste the command below.
    ```
    cd ~
    curl -L -o vpngate.py http://bit.ly/vpngatepy
    curl -L -o vpngate.py http://bit.ly/osxvpngatepy
    ```

    or [download](https://gist.github.com/rishubil/896bb1b6fbebdcc5ba5a/download#file-vpngate-py), unzip it and copy `vpngate.py` to user home directory.
  3. rishubil revised this gist Mar 11, 2015. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions vpngate.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,12 @@
    import urllib2, sys, base64, tempfile, subprocess, time

    __author__ = "Heewon Lee and Andrea Lazzarotto"
    __copyright__ = "Copyright 2014+, Heewon Lee and Andrea Lazzarotto"
    __license__ = "GPLv3"
    __version__ = "1.0"
    __maintainer__ = "Heewon Lee"
    __email__ = "rishubil@gmail.com"

    OPENVPN_PATH = "/Applications/Tunnelblick.app/Contents/Resources/openvpn/default"
    VPNGATE_API_URL = "http://www.vpngate.net/api/iphone/"
    DEFAULT_COUNTRY = "JP"
  4. rishubil revised this gist Mar 6, 2015. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -21,6 +21,14 @@ The connection can be terminated by pressing <kbd>Ctrl</kbd>+<kbd>C</kbd>.

    If you want connect vpn server using default options, just type `python vpngate.py -y`.

    ##Troubleshooting

    ###I forgot pressing <kbd>Ctrl</kbd>+<kbd>C</kbd> before quitting my terminal. I can't disconnect vpn connection.
    Open your terminal and type the command below. And please don't forget it anymore.
    ```
    ps aux | grep openvpn | awk '{ print $2 }' | sudo xargs kill -9
    ```

    ##Demo
    [![YT video](http://img.youtube.com/vi/Le3ixkWkblg/0.jpg)](http://youtu.be/Le3ixkWkblg)

  5. rishubil revised this gist Mar 6, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ If you use the script, [Tunnelblick](https://code.google.com/p/tunnelblick/) nee
    Open terminal app and copy & paste the command below.
    ```
    cd ~
    curl -OL http://bit.ly/vpngatepy
    curl -L -o vpngate.py http://bit.ly/vpngatepy
    ```

    or [download](https://gist.github.com/rishubil/896bb1b6fbebdcc5ba5a/download#file-vpngate-py), unzip it and copy `vpngate.py` to user home directory.
  6. rishubil revised this gist Mar 6, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ If you use the script, [Tunnelblick](https://code.google.com/p/tunnelblick/) nee
    Open terminal app and copy & paste the command below.
    ```
    cd ~
    curl -OL https://gist.github.com/rishubil/896bb1b6fbebdcc5ba5a/raw/efca2ea98260072a8b1161cf50227d12d315880d/vpngate.py
    curl -OL http://bit.ly/vpngatepy
    ```

    or [download](https://gist.github.com/rishubil/896bb1b6fbebdcc5ba5a/download#file-vpngate-py), unzip it and copy `vpngate.py` to user home directory.
  7. rishubil revised this gist Mar 6, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ cd ~
    curl -OL https://gist.github.com/rishubil/896bb1b6fbebdcc5ba5a/raw/efca2ea98260072a8b1161cf50227d12d315880d/vpngate.py
    ```

    or [download](https://gist.github.com/rishubil/896bb1b6fbebdcc5ba5a/download#file-vpngate-py) the script and copy it to user home directory.
    or [download](https://gist.github.com/rishubil/896bb1b6fbebdcc5ba5a/download#file-vpngate-py), unzip it and copy `vpngate.py` to user home directory.

    ##Quickstart
    Just type `python vpngate.py` in your terminal screen and select country and server you want to connect.
  8. rishubil revised this gist Mar 6, 2015. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -9,9 +9,12 @@ If you use the script, [Tunnelblick](https://code.google.com/p/tunnelblick/) nee
    ##Installation
    Open terminal app and copy & paste the command below.
    ```
    curl -OL https://gist.github.com/rishubil/896bb1b6fbebdcc5ba5a/raw/392ead72ea538a538e73c1e7c12429c05b7f16e7/vpngate.py
    cd ~
    curl -OL https://gist.github.com/rishubil/896bb1b6fbebdcc5ba5a/raw/efca2ea98260072a8b1161cf50227d12d315880d/vpngate.py
    ```

    or [download](https://gist.github.com/rishubil/896bb1b6fbebdcc5ba5a/download#file-vpngate-py) the script and copy it to user home directory.

    ##Quickstart
    Just type `python vpngate.py` in your terminal screen and select country and server you want to connect.
    The connection can be terminated by pressing <kbd>Ctrl</kbd>+<kbd>C</kbd>.
  9. rishubil revised this gist Mar 5, 2015. 2 changed files with 10 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,8 @@ curl -OL https://gist.github.com/rishubil/896bb1b6fbebdcc5ba5a/raw/392ead72ea538
    Just type `python vpngate.py` in your terminal screen and select country and server you want to connect.
    The connection can be terminated by pressing <kbd>Ctrl</kbd>+<kbd>C</kbd>.

    If you want connect vpn server using default options, just type `python vpngate.py -y`.

    ##Demo
    [![YT video](http://img.youtube.com/vi/Le3ixkWkblg/0.jpg)](http://youtu.be/Le3ixkWkblg)

    8 changes: 8 additions & 0 deletions vpngate.py
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@
    VPNGATE_API_URL = "http://www.vpngate.net/api/iphone/"
    DEFAULT_COUNTRY = "JP"
    DEFAULT_SERVER = 0
    YES = False

    def getServers():
    servers = []
    @@ -56,6 +57,8 @@ def selectCountry(countries):
    short_countries = list(country[0] for country in countries)
    if not default_country in short_countries:
    default_country = short_countries[0]
    if YES:
    selected = default_country
    while not selected:
    try:
    selected = raw_input("[?] Select server's country to connect [%s]: " % (default_country, )).strip().upper()
    @@ -72,6 +75,8 @@ def selectCountry(countries):
    def selectServer(servers):
    selected = -1
    default_server = DEFAULT_SERVER
    if YES:
    selected = default_server
    while selected == -1:
    try:
    selected = raw_input("[?] Select server's number to connect [%d]: " % (default_server, )).strip()
    @@ -108,6 +113,9 @@ def connect(ovpn_path):
    print("[=] Disconnected OpenVPN.")

    if __name__ == "__main__":
    if len(sys.argv) > 1 and sys.argv[1] == "-y":
    YES = True

    servers = []
    try:
    print("[-] Trying to get server's informations...")
  10. rishubil revised this gist Feb 12, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ This script is a simple script for using to connect vpn server list provided [VP
    It inspired by [VPNGate Python script](https://gist.github.com/Lazza/bbc15561b65c16db8ca8) wrtten by Andrea Lazzarotto.

    ##Requirements
    If you use the script, [Tunnelblink](https://code.google.com/p/tunnelblick/) needs to be installed first.
    If you use the script, [Tunnelblick](https://code.google.com/p/tunnelblick/) needs to be installed first.

    ##Installation
    Open terminal app and copy & paste the command below.
  11. rishubil revised this gist Feb 12, 2015. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    #VPNGate for OS X
    This script is a simple script for using to connect vpn server list provided [VPNGate](http://www.vpngate.net/en/) through [OpenVPN](http://openvpn.net/) on OS X environment.

    It inspired at [VPNGate Python script](https://gist.github.com/Lazza/bbc15561b65c16db8ca8) wrtten by Andrea Lazzarotto.
    It inspired by [VPNGate Python script](https://gist.github.com/Lazza/bbc15561b65c16db8ca8) wrtten by Andrea Lazzarotto.

    ##Requirements
    If you use the script, [Tunnelblink](https://code.google.com/p/tunnelblick/) needs to be installed first.
    @@ -16,5 +16,8 @@ curl -OL https://gist.github.com/rishubil/896bb1b6fbebdcc5ba5a/raw/392ead72ea538
    Just type `python vpngate.py` in your terminal screen and select country and server you want to connect.
    The connection can be terminated by pressing <kbd>Ctrl</kbd>+<kbd>C</kbd>.

    ##Demo
    [![YT video](http://img.youtube.com/vi/Le3ixkWkblg/0.jpg)](http://youtu.be/Le3ixkWkblg)

    ##License?
    This script is under GPLv3 license.
  12. rishubil revised this gist Feb 12, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ If you use the script, [Tunnelblink](https://code.google.com/p/tunnelblick/) nee
    ##Installation
    Open terminal app and copy & paste the command below.
    ```
    curl -O https://gist.github.com/rishubil/896bb1b6fbebdcc5ba5a/raw/392ead72ea538a538e73c1e7c12429c05b7f16e7/vpngate.py
    curl -OL https://gist.github.com/rishubil/896bb1b6fbebdcc5ba5a/raw/392ead72ea538a538e73c1e7c12429c05b7f16e7/vpngate.py
    ```

    ##Quickstart
  13. rishubil revised this gist Feb 12, 2015. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,9 @@ If you use the script, [Tunnelblink](https://code.google.com/p/tunnelblick/) nee

    ##Installation
    Open terminal app and copy & paste the command below.
    `curl -O https://gist.github.com/rishubil/896bb1b6fbebdcc5ba5a/raw/392ead72ea538a538e73c1e7c12429c05b7f16e7/vpngate.py`
    ```
    curl -O https://gist.github.com/rishubil/896bb1b6fbebdcc5ba5a/raw/392ead72ea538a538e73c1e7c12429c05b7f16e7/vpngate.py
    ```

    ##Quickstart
    Just type `python vpngate.py` in your terminal screen and select country and server you want to connect.
  14. rishubil revised this gist Feb 12, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,8 @@ It inspired at [VPNGate Python script](https://gist.github.com/Lazza/bbc15561b65
    If you use the script, [Tunnelblink](https://code.google.com/p/tunnelblick/) needs to be installed first.

    ##Installation
    Open terminal app and copy & paste the command below.
    curl -O https://gist.github.com/rishubil/896bb1b6fbebdcc5ba5a/raw/392ead72ea538a538e73c1e7c12429c05b7f16e7/vpngate.py
    Open terminal app and copy & paste the command below.
    `curl -O https://gist.github.com/rishubil/896bb1b6fbebdcc5ba5a/raw/392ead72ea538a538e73c1e7c12429c05b7f16e7/vpngate.py`

    ##Quickstart
    Just type `python vpngate.py` in your terminal screen and select country and server you want to connect.
  15. rishubil revised this gist Feb 12, 2015. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,10 @@ It inspired at [VPNGate Python script](https://gist.github.com/Lazza/bbc15561b65
    ##Requirements
    If you use the script, [Tunnelblink](https://code.google.com/p/tunnelblick/) needs to be installed first.

    ##Installation
    Open terminal app and copy & paste the command below.
    curl -O https://gist.github.com/rishubil/896bb1b6fbebdcc5ba5a/raw/392ead72ea538a538e73c1e7c12429c05b7f16e7/vpngate.py

    ##Quickstart
    Just type `python vpngate.py` in your terminal screen and select country and server you want to connect.
    The connection can be terminated by pressing <kbd>Ctrl</kbd>+<kbd>C</kbd>.
  16. rishubil revised this gist Feb 12, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -10,5 +10,5 @@ If you use the script, [Tunnelblink](https://code.google.com/p/tunnelblick/) nee
    Just type `python vpngate.py` in your terminal screen and select country and server you want to connect.
    The connection can be terminated by pressing <kbd>Ctrl</kbd>+<kbd>C</kbd>.

    ##license?
    ##License?
    This script is under GPLv3 license.
  17. rishubil revised this gist Feb 12, 2015. 1 changed file with 13 additions and 3 deletions.
    16 changes: 13 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,14 @@
    This script is inspired at [VPNGate Python script](https://gist.github.com/Lazza/bbc15561b65c16db8ca8) wrtten by Andrea Lazzarotto.
    #VPNGate for OS X
    This script is a simple script for using to connect vpn server list provided [VPNGate](http://www.vpngate.net/en/) through [OpenVPN](http://openvpn.net/) on OS X environment.

    If you use the script, [Tunnelblink](https://code.google.com/p/tunnelblick/) needs to be installed first.
    The connect can be terminated by pressing <kbd>Ctrl</kbd>+<kbd>C</kbd>.
    It inspired at [VPNGate Python script](https://gist.github.com/Lazza/bbc15561b65c16db8ca8) wrtten by Andrea Lazzarotto.

    ##Requirements
    If you use the script, [Tunnelblink](https://code.google.com/p/tunnelblick/) needs to be installed first.

    ##Quickstart
    Just type `python vpngate.py` in your terminal screen and select country and server you want to connect.
    The connection can be terminated by pressing <kbd>Ctrl</kbd>+<kbd>C</kbd>.

    ##license?
    This script is under GPLv3 license.
  18. rishubil revised this gist Feb 12, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion vpngate.py
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,6 @@ def getServers():
    servers = []
    server_strings = urllib2.urlopen(VPNGATE_API_URL).read()
    for server_string in server_strings.replace("\r", "").split('\n')[2:-2]:
    # pprint(server_string)
    (HostName, IP, Score, Ping, Speed, CountryLong, CountryShort, NumVpnSessions, Uptime, TotalUsers, TotalTraffic, LogType, Operator, Message, OpenVPN_ConfigData_Base64) = server_string.split(',')
    server = {
    'HostName': HostName,
  19. rishubil revised this gist Feb 12, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    This script is inspired at [VPNGate Python script](https://gist.github.com/Lazza/bbc15561b65c16db8ca8) wrtten by Andrea Lazzarotto.

    If you use the script, [Tunnelblink](https://code.google.com/p/tunnelblick/) needs to be installed first.
    If you use the script, [Tunnelblink](https://code.google.com/p/tunnelblick/) needs to be installed first.
    The connect can be terminated by pressing <kbd>Ctrl</kbd>+<kbd>C</kbd>.
  20. rishubil revised this gist Feb 12, 2015. 2 changed files with 143 additions and 143 deletions.
    40 changes: 3 additions & 37 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,38 +1,4 @@
    This script is forked at [VPNGate Python script](https://gist.github.com/Lazza/bbc15561b65c16db8ca8) wrtten by Andrea Lazzarotto for supporting OS X.
    This script is inspired at [VPNGate Python script](https://gist.github.com/Lazza/bbc15561b65c16db8ca8) wrtten by Andrea Lazzarotto.

    If you use the script on OS X environment, [Tunnelblink](https://code.google.com/p/tunnelblick/) needs to be installed.

    Original README below.

    # vpngate.py

    This script allows to use the free VPN service provided by [VPNGate](http://www.vpngate.net/en/) in an easy way. The user just needs to provide the desidered output country, and the script automatically chooses the best server.

    After this step, OpenVPN is launched with the proper configuration. The VPN can be terminated by pressing <kbd>Ctrl</kbd>+<kbd>C</kbd>.

    # Usage

    Run the script by providing the desired output country:

    vpngate.py US

    Both country codes and country names are supported, as listed on the VPNGate website, e.g.:

    vpngate.py "United Kingdom"

    Moreover, the script allows to input countries with **any case** (`Italy`, `italy`, `ItALy` all work) and with **partial names**:
    - `Korea` will work for `Korea Republic Of`
    - `Russia` will work for `Russian Federation`
    - ... and so on

    # Demo

    Here is a short Youtube video showcasing an example usage:

    [![YT video](http://i.imgur.com/WxbOiOT.png)](http://youtu.be/3OFwxkxN_HI)

    # Requirements

    OpenVPN needs to be installed.

    The script should run on any Linux distribution with the Python [Requests](python-requests.org) module installed. The user running the script must be able to run `sudo` commands in order to start `openvpn`.
    If you use the script, [Tunnelblink](https://code.google.com/p/tunnelblick/) needs to be installed first.
    The connect can be terminated by pressing <kbd>Ctrl</kbd>+<kbd>C</kbd>.
    246 changes: 140 additions & 106 deletions vpngate.py
    Original file line number Diff line number Diff line change
    @@ -1,108 +1,142 @@
    #!/usr/bin/env python

    """Pick server and start connection with VPNGate (http://www.vpngate.net/en/)"""

    import requests, os, sys, tempfile, subprocess, base64, time

    __author__ = "Andrea Lazzarotto"
    __copyright__ = "Copyright 2014+, Andrea Lazzarotto"
    __license__ = "GPLv3"
    __version__ = "1.0"
    __maintainer__ = "Andrea Lazzarotto"
    __email__ = "andrea.lazzarotto@gmail.com"

    def which(program):
    import os
    def is_exe(fpath):
    return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

    fpath, fname = os.path.split(program)
    if fpath:
    if is_exe(program):
    return program
    else:
    for path in os.environ["PATH"].split(os.pathsep):
    path = path.strip('"')
    exe_file = os.path.join(path, program)
    if is_exe(exe_file):
    return exe_file

    return None

    if len(sys.argv) != 2:
    print 'usage: ' + sys.argv[0] + ' [country name | country code]'
    exit(1)
    country = sys.argv[1]

    if len(country) == 2:
    i = 6 # short name for country
    elif len(country) > 2:
    i = 5 # long name for country
    else:
    print 'Country is too short!'
    exit(1)

    ovpn_cmd = None
    pf = sys.platform
    if pf == "linux" or pf == "linux2":
    ovpn_cmd = "openvpn"
    elif pf == "darwin":
    ovpn_cmd = "/Applications/Tunnelblick.app/Contents/Resources/openvpn/default"
    elif pf == "win32":
    print "This script does not support Windows."
    exit(1)

    if not which(ovpn_cmd):
    print "Cannot use openvpn"
    exit(1)

    try:
    vpn_data = requests.get('http://www.vpngate.net/api/iphone/').text.replace('\r','')
    servers = [line.split(',') for line in vpn_data.split('\n')]
    labels = servers[1]
    labels[0] = labels[0][1:]
    servers = [s for s in servers[2:] if len(s) > 1]
    except:
    print 'Cannot get VPN servers data'
    exit(1)

    desired = [s for s in servers if country.lower() in s[i].lower()]
    found = len(desired)
    print 'Found ' + str(found) + ' servers for country ' + country
    if found == 0:
    exit(1)

    supported = [s for s in desired if len(s[-1]) > 0]
    print str(len(supported)) + ' of these servers support OpenVPN'
    # We pick the best servers by score
    winner = sorted(supported, key=lambda s: s[2], reverse=True)[0]

    print "\n== Best server =="
    pairs = zip(labels, winner)[:-1]
    for (l, d) in pairs[:4]:
    print l + ': ' + d

    print pairs[4][0] + ': ' + str(float(pairs[4][1]) / 10**6) + ' MBps'
    print "Country: " + pairs[5][1]

    print "\nLaunching VPN..."
    _, path = tempfile.mkstemp()

    f = open(path, 'w')
    f.write(base64.b64decode(winner[-1]))
    f.close()

    x = subprocess.Popen(['sudo', ovpn_cmd, '--config', path])

    try:
    while True:
    time.sleep(600)
    # termination with Ctrl+C
    except:
    import urllib2, sys, base64, tempfile, subprocess, time

    OPENVPN_PATH = "/Applications/Tunnelblick.app/Contents/Resources/openvpn/default"
    VPNGATE_API_URL = "http://www.vpngate.net/api/iphone/"
    DEFAULT_COUNTRY = "JP"
    DEFAULT_SERVER = 0

    def getServers():
    servers = []
    server_strings = urllib2.urlopen(VPNGATE_API_URL).read()
    for server_string in server_strings.replace("\r", "").split('\n')[2:-2]:
    # pprint(server_string)
    (HostName, IP, Score, Ping, Speed, CountryLong, CountryShort, NumVpnSessions, Uptime, TotalUsers, TotalTraffic, LogType, Operator, Message, OpenVPN_ConfigData_Base64) = server_string.split(',')
    server = {
    'HostName': HostName,
    'IP': IP,
    'Score': Score,
    'Ping': Ping,
    'Speed': Speed,
    'CountryLong': CountryLong,
    'CountryShort': CountryShort,
    'NumVpnSessions': NumVpnSessions,
    'Uptime': Uptime,
    'TotalUsers': TotalUsers,
    'TotalTraffic': TotalTraffic,
    'LogType': LogType,
    'Operator': Operator,
    'Message': Message,
    'OpenVPN_ConfigData_Base64': OpenVPN_ConfigData_Base64
    }
    servers.append(server)
    return servers

    def getCountries(server):
    return set((server['CountryShort'], server['CountryLong']) for server in servers)

    def printCountries(countries):
    print(" Connectable countries:")
    newline = False
    for country in countries:
    print(" %-2s) %-25s" % (country[0], country[1])),
    if newline:
    print('\n'),
    newline = not newline
    if newline:
    print('\n'),

    def printServers(servers):
    print(" Connectable Servers:")
    for i in xrange(len(servers)):
    server = servers[i]
    print(" %2d) %-15s [%6.2f Mbps, ping:%3s ms]" % (i, server['IP'], float(server['Speed'])/10**6, server['Ping']))

    def selectCountry(countries):
    selected = ""
    default_country = DEFAULT_COUNTRY
    short_countries = list(country[0] for country in countries)
    if not default_country in short_countries:
    default_country = short_countries[0]
    while not selected:
    try:
    selected = raw_input("[?] Select server's country to connect [%s]: " % (default_country, )).strip().upper()
    except:
    print("[!] Please enter short name of the country.")
    selected = ""
    if selected == "":
    selected = default_country
    elif not selected in short_countries:
    print("[!] Please enter short name of the country.")
    selected = ""
    return selected

    def selectServer(servers):
    selected = -1
    default_server = DEFAULT_SERVER
    while selected == -1:
    try:
    selected = raw_input("[?] Select server's number to connect [%d]: " % (default_server, )).strip()
    except:
    print("[!] Please enter vaild server's number.")
    selected = -1
    if selected == "":
    selected = default_server
    elif not selected.isdigit() or int(selected) >= len(servers):
    print("[!] Please enter vaild server's number.")
    selected = -1
    return servers[int(selected)]

    def saveOvpn(server):
    _, ovpn_path = tempfile.mkstemp()
    ovpn = open(ovpn_path, 'w')
    ovpn.write(base64.b64decode(server["OpenVPN_ConfigData_Base64"]))
    ovpn.close()
    return ovpn_path

    def connect(ovpn_path):
    openvpn_process = subprocess.Popen(['sudo', OPENVPN_PATH, '--config', ovpn_path])
    try:
    while True:
    time.sleep(600)
    # termination with Ctrl+C
    except:
    try:
    openvpn_process.kill()
    except:
    pass
    while openvpn_process.poll() != 0:
    time.sleep(1)
    print("[=] Disconnected OpenVPN.")

    if __name__ == "__main__":
    servers = []
    try:
    x.kill()
    print("[-] Trying to get server's informations...")
    servers = sorted(getServers(), key=lambda server: int(server["Score"]), reverse=True)
    except:
    pass
    while x.poll() != 0:
    time.sleep(1)
    print '\nVPN terminated'
    print("[!] Failed to get server's informations from vpngate.")
    sys.exit(1)

    if not servers:
    print("[!] There is no running server on vpngate.")
    sys.exit(1)

    print("[-] Got server's informations.")

    countries = sorted(getCountries(servers))
    printCountries(countries)
    selected_country = selectCountry(countries)

    print("[-] Gethering %s servers..." % (selected_country, ))

    selected_servers = [server for server in servers if server['CountryShort'] == selected_country]
    printServers(selected_servers)
    selected_server = selectServer(selected_servers)

    print("[-] Generating .ovpn file of %s..." % (selected_server["IP"], ))

    ovpn_path = saveOvpn(selected_server)

    print("[-] Connecting to %s..." % (selected_server["IP"], ))

    connect(ovpn_path)
  21. rishubil revised this gist Jan 31, 2015. 1 changed file with 36 additions and 1 deletion.
    37 changes: 36 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,38 @@
    This script is forked at [VPNGate Python script](https://gist.github.com/Lazza/bbc15561b65c16db8ca8) wrtten by Andrea Lazzarotto for supporting OS X.

    If you use the script on OS X environment, [Tunnelblink](https://code.google.com/p/tunnelblick/) needs to be installed.
    If you use the script on OS X environment, [Tunnelblink](https://code.google.com/p/tunnelblick/) needs to be installed.

    Original README below.

    # vpngate.py

    This script allows to use the free VPN service provided by [VPNGate](http://www.vpngate.net/en/) in an easy way. The user just needs to provide the desidered output country, and the script automatically chooses the best server.

    After this step, OpenVPN is launched with the proper configuration. The VPN can be terminated by pressing <kbd>Ctrl</kbd>+<kbd>C</kbd>.

    # Usage

    Run the script by providing the desired output country:

    vpngate.py US

    Both country codes and country names are supported, as listed on the VPNGate website, e.g.:

    vpngate.py "United Kingdom"

    Moreover, the script allows to input countries with **any case** (`Italy`, `italy`, `ItALy` all work) and with **partial names**:
    - `Korea` will work for `Korea Republic Of`
    - `Russia` will work for `Russian Federation`
    - ... and so on

    # Demo

    Here is a short Youtube video showcasing an example usage:

    [![YT video](http://i.imgur.com/WxbOiOT.png)](http://youtu.be/3OFwxkxN_HI)

    # Requirements

    OpenVPN needs to be installed.

    The script should run on any Linux distribution with the Python [Requests](python-requests.org) module installed. The user running the script must be able to run `sudo` commands in order to start `openvpn`.
  22. rishubil revised this gist Jan 31, 2015. 2 changed files with 46 additions and 44 deletions.
    33 changes: 2 additions & 31 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,32 +1,3 @@
    # vpngate.py
    This script is forked at [VPNGate Python script](https://gist.github.com/Lazza/bbc15561b65c16db8ca8) wrtten by Andrea Lazzarotto for supporting OS X.

    This script allows to use the free VPN service provided by [VPNGate](http://www.vpngate.net/en/) in an easy way. The user just needs to provide the desidered output country, and the script automatically chooses the best server.

    After this step, OpenVPN is launched with the proper configuration. The VPN can be terminated by pressing <kbd>Ctrl</kbd>+<kbd>C</kbd>.

    # Usage

    Run the script by providing the desired output country:

    vpngate.py US

    Both country codes and country names are supported, as listed on the VPNGate website, e.g.:

    vpngate.py "United Kingdom"

    Moreover, the script allows to input countries with **any case** (`Italy`, `italy`, `ItALy` all work) and with **partial names**:
    - `Korea` will work for `Korea Republic Of`
    - `Russia` will work for `Russian Federation`
    - ... and so on

    # Demo

    Here is a short Youtube video showcasing an example usage:

    [![YT video](http://i.imgur.com/WxbOiOT.png)](http://youtu.be/3OFwxkxN_HI)

    # Requirements

    OpenVPN needs to be installed.

    The script should run on any Linux distribution with the Python [Requests](python-requests.org) module installed. The user running the script must be able to run `sudo` commands in order to start `openvpn`.
    If you use the script on OS X environment, [Tunnelblink](https://code.google.com/p/tunnelblick/) needs to be installed.
    57 changes: 44 additions & 13 deletions vpngate.py
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,39 @@
    #!/usr/bin/env python

    """Pick server and start connection with VPNGate (http://www.vpngate.net/en/)"""

    import requests, os, sys, tempfile, subprocess, base64, time

    __author__ = "Andrea Lazzarotto"
    __copyright__ = "Copyright 2014+, Andrea Lazzarotto"
    __license__ = "GPLv3"
    __version__ = "1.0"
    __maintainer__ = "Andrea Lazzarotto"
    __email__ = "andrea.lazzarotto@gmail.com"

    def which(program):
    import os
    def is_exe(fpath):
    return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

    fpath, fname = os.path.split(program)
    if fpath:
    if is_exe(program):
    return program
    else:
    for path in os.environ["PATH"].split(os.pathsep):
    path = path.strip('"')
    exe_file = os.path.join(path, program)
    if is_exe(exe_file):
    return exe_file

    return None

    if len(sys.argv) != 2:
    print 'usage: ' + sys.argv[0] + ' [country name | country code]'
    exit(1)
    country = sys.argv[1]

    if len(country) == 2:
    i = 6 # short name for country
    elif len(country) > 2:
    @@ -25,6 +42,20 @@
    print 'Country is too short!'
    exit(1)

    ovpn_cmd = None
    pf = sys.platform
    if pf == "linux" or pf == "linux2":
    ovpn_cmd = "openvpn"
    elif pf == "darwin":
    ovpn_cmd = "/Applications/Tunnelblick.app/Contents/Resources/openvpn/default"
    elif pf == "win32":
    print "This script does not support Windows."
    exit(1)

    if not which(ovpn_cmd):
    print "Cannot use openvpn"
    exit(1)

    try:
    vpn_data = requests.get('http://www.vpngate.net/api/iphone/').text.replace('\r','')
    servers = [line.split(',') for line in vpn_data.split('\n')]
    @@ -34,35 +65,35 @@
    except:
    print 'Cannot get VPN servers data'
    exit(1)

    desired = [s for s in servers if country.lower() in s[i].lower()]
    found = len(desired)
    print 'Found ' + str(found) + ' servers for country ' + country
    if found == 0:
    exit(1)

    supported = [s for s in desired if len(s[-1]) > 0]
    print str(len(supported)) + ' of these servers support OpenVPN'
    # We pick the best servers by score
    winner = sorted(supported, key=lambda s: s[2], reverse=True)[0]

    print "\n== Best server =="
    pairs = zip(labels, winner)[:-1]
    for (l, d) in pairs[:4]:
    print l + ': ' + d

    print pairs[4][0] + ': ' + str(float(pairs[4][1]) / 10**6) + ' MBps'
    print "Country: " + pairs[5][1]

    print "\nLaunching VPN..."
    _, path = tempfile.mkstemp()

    f = open(path, 'w')
    f.write(base64.b64decode(winner[-1]))
    f.close()

    x = subprocess.Popen(['sudo', 'openvpn', '--config', path])

    x = subprocess.Popen(['sudo', ovpn_cmd, '--config', path])
    try:
    while True:
    time.sleep(600)
  23. @Lazza Lazza revised this gist Aug 31, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions vpngate.py
    Original file line number Diff line number Diff line change
    @@ -23,6 +23,7 @@
    i = 5 # long name for country
    else:
    print 'Country is too short!'
    exit(1)

    try:
    vpn_data = requests.get('http://www.vpngate.net/api/iphone/').text.replace('\r','')
  24. @Lazza Lazza revised this gist Aug 30, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Description
    # vpngate.py

    This script allows to use the free VPN service provided by [VPNGate](http://www.vpngate.net/en/) in an easy way. The user just needs to provide the desidered output country, and the script automatically chooses the best server.

  25. @Lazza Lazza revised this gist Aug 30, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ Moreover, the script allows to input countries with **any case** (`Italy`, `ital

    Here is a short Youtube video showcasing an example usage:

    [![YT video](http://img.youtube.com/vi/3OFwxkxN_HI/0.jpg)](http://youtu.be/3OFwxkxN_HI)
    [![YT video](http://i.imgur.com/WxbOiOT.png)](http://youtu.be/3OFwxkxN_HI)

    # Requirements

  26. @Lazza Lazza revised this gist Aug 30, 2014. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions vpngate.py
    Original file line number Diff line number Diff line change
    @@ -13,8 +13,8 @@


    if len(sys.argv) != 2:
    print 'usage: ' + sys.argv[0] + ' [country name | country code]'
    exit(1)
    print 'usage: ' + sys.argv[0] + ' [country name | country code]'
    exit(1)
    country = sys.argv[1]

    if len(country) == 2:
    @@ -38,7 +38,7 @@
    found = len(desired)
    print 'Found ' + str(found) + ' servers for country ' + country
    if found == 0:
    exit(1)
    exit(1)

    supported = [s for s in desired if len(s[-1]) > 0]
    print str(len(supported)) + ' of these servers support OpenVPN'
    @@ -71,6 +71,6 @@
    x.kill()
    except:
    pass
    while x.poll() != 0:
    time.sleep(1)
    print '\nVPN terminated'
    while x.poll() != 0:
    time.sleep(1)
    print '\nVPN terminated'
  27. @Lazza Lazza created this gist Aug 30, 2014.
    32 changes: 32 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    # Description

    This script allows to use the free VPN service provided by [VPNGate](http://www.vpngate.net/en/) in an easy way. The user just needs to provide the desidered output country, and the script automatically chooses the best server.

    After this step, OpenVPN is launched with the proper configuration. The VPN can be terminated by pressing <kbd>Ctrl</kbd>+<kbd>C</kbd>.

    # Usage

    Run the script by providing the desired output country:

    vpngate.py US

    Both country codes and country names are supported, as listed on the VPNGate website, e.g.:

    vpngate.py "United Kingdom"

    Moreover, the script allows to input countries with **any case** (`Italy`, `italy`, `ItALy` all work) and with **partial names**:
    - `Korea` will work for `Korea Republic Of`
    - `Russia` will work for `Russian Federation`
    - ... and so on

    # Demo

    Here is a short Youtube video showcasing an example usage:

    [![YT video](http://img.youtube.com/vi/3OFwxkxN_HI/0.jpg)](http://youtu.be/3OFwxkxN_HI)

    # Requirements

    OpenVPN needs to be installed.

    The script should run on any Linux distribution with the Python [Requests](python-requests.org) module installed. The user running the script must be able to run `sudo` commands in order to start `openvpn`.
    76 changes: 76 additions & 0 deletions vpngate.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    #!/usr/bin/env python

    """Pick server and start connection with VPNGate (http://www.vpngate.net/en/)"""

    import requests, os, sys, tempfile, subprocess, base64, time

    __author__ = "Andrea Lazzarotto"
    __copyright__ = "Copyright 2014+, Andrea Lazzarotto"
    __license__ = "GPLv3"
    __version__ = "1.0"
    __maintainer__ = "Andrea Lazzarotto"
    __email__ = "andrea.lazzarotto@gmail.com"


    if len(sys.argv) != 2:
    print 'usage: ' + sys.argv[0] + ' [country name | country code]'
    exit(1)
    country = sys.argv[1]

    if len(country) == 2:
    i = 6 # short name for country
    elif len(country) > 2:
    i = 5 # long name for country
    else:
    print 'Country is too short!'

    try:
    vpn_data = requests.get('http://www.vpngate.net/api/iphone/').text.replace('\r','')
    servers = [line.split(',') for line in vpn_data.split('\n')]
    labels = servers[1]
    labels[0] = labels[0][1:]
    servers = [s for s in servers[2:] if len(s) > 1]
    except:
    print 'Cannot get VPN servers data'
    exit(1)

    desired = [s for s in servers if country.lower() in s[i].lower()]
    found = len(desired)
    print 'Found ' + str(found) + ' servers for country ' + country
    if found == 0:
    exit(1)

    supported = [s for s in desired if len(s[-1]) > 0]
    print str(len(supported)) + ' of these servers support OpenVPN'
    # We pick the best servers by score
    winner = sorted(supported, key=lambda s: s[2], reverse=True)[0]

    print "\n== Best server =="
    pairs = zip(labels, winner)[:-1]
    for (l, d) in pairs[:4]:
    print l + ': ' + d

    print pairs[4][0] + ': ' + str(float(pairs[4][1]) / 10**6) + ' MBps'
    print "Country: " + pairs[5][1]

    print "\nLaunching VPN..."
    _, path = tempfile.mkstemp()

    f = open(path, 'w')
    f.write(base64.b64decode(winner[-1]))
    f.close()

    x = subprocess.Popen(['sudo', 'openvpn', '--config', path])

    try:
    while True:
    time.sleep(600)
    # termination with Ctrl+C
    except:
    try:
    x.kill()
    except:
    pass
    while x.poll() != 0:
    time.sleep(1)
    print '\nVPN terminated'