Skip to content

Commit

Permalink
Add country code exclusion in connect command
Browse files Browse the repository at this point in the history
  • Loading branch information
Galiley committed Jul 30, 2020
1 parent 494ccd9 commit 2105a0b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
6 changes: 5 additions & 1 deletion USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,16 @@ You can use the `--random` or `-r` flag to connect to a random server:

`protonvpn c -r`

There are several other variables to keep in mind when you want to connect to the “fastest” server. You can connect to the fastest server in a country, the fastest Secure Core server, the fastest P2P-enabled server, or the fastest Tor server.
There are several other variables to keep in mind when you want to connect to the “fastest” server. You can connect to the fastest server in a country, the fastest server outside a country, the fastest Secure Core server, the fastest P2P-enabled server, or the fastest Tor server.

Fastest server in a country (replace UK with the code of the desired country, e.g. `US` for USA, `JP` for Japan, `AU` for Australia, etc.):

`protonvpn c --cc UK`

Fastest server outside a country (replace UK with the code of the desired country, e.g. `US` for USA, `JP` for Japan, `AU` for Australia, etc.):

`protonvpn c --not-cc UK`

Fastest Secure Core server:

`protonvpn c --sc`
Expand Down
12 changes: 11 additions & 1 deletion protonvpn_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ def connect(self):
group.add_argument("-f", "--fastest", help="Connect to the fastest ProtonVPN server.", action="store_true")
group.add_argument("-r", "--random", help="Connect to a random ProtonVPN server.", action="store_true")
group.add_argument("--cc", help="Connect to the specified country code (SE, PT, BR, AR).", metavar="")
group.add_argument(
"--not-cc",
help="Connect to the fastest server outside the specified country code (SE, PT, BR, AR).",
metavar=""
)
group.add_argument("--sc", help="Connect to the fastest Secure-Core server.", action="store_true")
group.add_argument("--p2p", help="Connect to the fastest torrent server.", action="store_true")
group.add_argument("--tor", help="Connect to the fastest Tor server.", action="store_true")
Expand All @@ -138,6 +143,8 @@ def connect(self):
connection.direct(args.servername, protocol)
elif args.cc:
connection.country_f(args.cc, protocol)
elif args.not_cc:
connection.country_f(args.not_cc, protocol, True)
elif args.p2p:
connection.feature_f(self.server_features_dict.get("p2p", None), protocol)
elif args.sc:
Expand Down Expand Up @@ -341,6 +348,9 @@ def print_examples():
"protonvpn connect --cc AU\n"
" Connect to the fastest Australian server\n"
" with the default protocol.\n\n"
"protonvpn connect --not-cc AU\n"
" Connect to the fastest server outside Australia\n"
" with the default protocol.\n\n"
"protonvpn c --p2p -p tcp\n"
" Connect to the fastest torrent server with TCP.\n\n"
"protonvpn c --sc\n"
Expand Down Expand Up @@ -613,7 +623,7 @@ def set_killswitch():
"The Kill Switch will block all network traffic\n"
"if the VPN connection drops unexpectedly.\n"
"\n"
"Please note that the Kill Switch assumes only one network interface being active.\n" # noqa
"Please note that the Kill Switch assumes only one network interface being active.\n" # noqa
"\n"
"1) Enable Kill Switch (Block access to/from LAN)\n"
"2) Enable Kill Switch (Allow access to/from LAN)\n"
Expand Down
40 changes: 25 additions & 15 deletions protonvpn_cli/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def fastest(protocol=None):
openvpn_connect(fastest_server, protocol)


def country_f(country_code, protocol=None):
def country_f(country_code, protocol=None, excluded=False):
"""Connect to the fastest server in a specific country."""
logger.debug("Starting fastest country connect")

Expand All @@ -185,15 +185,25 @@ def country_f(country_code, protocol=None):
# Filter out excluded features and countries
server_pool = []
for server in servers:
if server["Features"] not in excluded_features and server["ExitCountry"] == country_code:
server_pool.append(server)
if server["Features"] not in excluded_features:
if (excluded and server["ExitCountry"] != country_code) or (
not excluded and server["ExitCountry"] == country_code
):
server_pool.append(server)

if len(server_pool) == 0:
print(
"[!] No Server in country {0} found\n".format(country_code)
+ "[!] Please choose a valid country"
)
logger.debug("No server in country {0}".format(country_code))
if not excluded:
print(
"[!] No Server in country {0} found\n".format(country_code)
+ "[!] Please choose a valid country"
)
logger.debug("No server in country {0}".format(country_code))
else:
print(
"[!] No Server found outside country {0}\n".format(country_code)
+ "[!] Please choose a valid country"
)
logger.debug("No server outside country {0}".format(country_code))
sys.exit(1)

fastest_server = get_fastest_server(server_pool)
Expand Down Expand Up @@ -713,7 +723,7 @@ def manage_ipv6(mode):
ipv6_addr = lines[1].strip()

ipv6_info = subprocess.run(
"ip addr show dev {0} | grep '\<inet6.*global\>'".format(default_nic), # noqa
"ip addr show dev {0} | grep '\<inet6.*global\>'".format(default_nic), # noqa
shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE
)

Expand Down Expand Up @@ -831,10 +841,10 @@ def manage_killswitch(mode, proto=None, port=None):
"iptables -A INPUT -i lo -j ACCEPT",
"iptables -A OUTPUT -o {0} -j ACCEPT".format(device),
"iptables -A INPUT -i {0} -j ACCEPT".format(device),
"iptables -A OUTPUT -o {0} -m state --state ESTABLISHED,RELATED -j ACCEPT".format(device), # noqa
"iptables -A INPUT -i {0} -m state --state ESTABLISHED,RELATED -j ACCEPT".format(device), # noqa
"iptables -A OUTPUT -p {0} -m {1} --dport {2} -j ACCEPT".format(proto.lower(), proto.lower(), port), # noqa
"iptables -A INPUT -p {0} -m {1} --sport {2} -j ACCEPT".format(proto.lower(), proto.lower(), port), # noqa
"iptables -A OUTPUT -o {0} -m state --state ESTABLISHED,RELATED -j ACCEPT".format(device), # noqa
"iptables -A INPUT -i {0} -m state --state ESTABLISHED,RELATED -j ACCEPT".format(device), # noqa
"iptables -A OUTPUT -p {0} -m {1} --dport {2} -j ACCEPT".format(proto.lower(), proto.lower(), port), # noqa
"iptables -A INPUT -p {0} -m {1} --sport {2} -j ACCEPT".format(proto.lower(), proto.lower(), port), # noqa
]

if int(get_config_value("USER", "killswitch")) == 2:
Expand All @@ -847,8 +857,8 @@ def manage_killswitch(mode, proto=None, port=None):
local_network = local_network.stdout.decode().strip().split()[1]

exclude_lan_commands = [
"iptables -A OUTPUT -o {0} -d {1} -j ACCEPT".format(default_nic, local_network), # noqa
"iptables -A INPUT -i {0} -s {1} -j ACCEPT".format(default_nic, local_network), # noqa
"iptables -A OUTPUT -o {0} -d {1} -j ACCEPT".format(default_nic, local_network), # noqa
"iptables -A INPUT -i {0} -s {1} -j ACCEPT".format(default_nic, local_network), # noqa
]

for lan_command in exclude_lan_commands:
Expand Down
2 changes: 2 additions & 0 deletions protonvpn_cli/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
protonvpn (c | connect) [<servername>] [-p <protocol>]
protonvpn (c | connect) [-f | --fastest] [-p <protocol>]
protonvpn (c | connect) [--cc <code>] [-p <protocol>]
protonvpn (c | connect) [--not-cc <code>] [-p <protocol>]
protonvpn (c | connect) [--sc] [-p <protocol>]
protonvpn (c | connect) [--p2p] [-p <protocol>]
protonvpn (c | connect) [--tor] [-p <protocol>]
Expand All @@ -44,6 +45,7 @@
-f, --fastest Select the fastest ProtonVPN server.
-r, --random Select a random ProtonVPN server.
--cc CODE Determine the country for fastest connect.
--not-cc CODE Determine the country to exclude for fastest connect.
--sc Connect to the fastest Secure-Core server.
--p2p Connect to the fastest torrent server.
--tor Connect to the fastest Tor server.
Expand Down

0 comments on commit 2105a0b

Please sign in to comment.