Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new and refactor server features #1

Merged
merged 4 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 11 additions & 24 deletions protonvpn_cli/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# protonvpn-cli Functions
from .logger import logger
from .utils import (
check_init, pull_server_data, is_connected,
check_init, get_server_features, pull_server_data, is_connected,
get_servers, get_server_value, get_config_value,
set_config_value, get_ip_info, get_country_name,
get_fastest_server, check_update, get_default_nic,
Expand Down Expand Up @@ -57,12 +57,6 @@ def show_dialog(headline, choices, stop=False):

pull_server_data()

features = {
1: "Secure-Core",
2: "Tor",
4: "P2P",
8: "Streaming",
}
server_tiers = {0: "F", 1: "B", 2: "P"}

servers = get_servers()
Expand All @@ -80,11 +74,10 @@ def show_dialog(headline, choices, stop=False):
for country in sorted(countries.keys()):
country_features = []
for server in countries[country]:
feat = int(get_server_value(server, "Features", servers))
for bit_flag in features:
if (feat & bit_flag) != 0:
if not features[bit_flag] in country_features:
country_features.append(features[bit_flag])
server_features = get_server_features(server, servers)
for feat in server_features:
if not feat in country_features:
country_features.append(feat)

if len(country_features) == 0:
country_features.append("Normal")
Expand All @@ -107,21 +100,17 @@ def show_dialog(headline, choices, stop=False):
get_server_value(servername, "Load", servers)
).rjust(3, " ")

servers_features = []
feat = int(get_server_value(servername, 'Features', servers))
for bit_flag in features:
if (feat & bit_flag) != 0:
servers_features.append(features[bit_flag])
server_features = get_server_features(servername, servers)

if len(servers_features) == 0:
servers_features.append("Normal")
if len(server_features) == 0:
server_features.append("Normal")

tier = server_tiers[
get_server_value(servername, "Tier", servers)
]

choices.append((servername, "Load: {0}% | {1} | {2}".format(
load, tier, ", ".join(servers_features)
load, tier, ", ".join(server_features)
)))

server_result = show_dialog("Choose the server to connect:", choices)
Expand Down Expand Up @@ -412,14 +401,12 @@ def status():
ip, isp = get_ip_info()

# Collect Information
all_features = {0: "Normal", 1: "Secure-Core", 2: "Tor", 4: "P2P"}

logger.debug("Collecting status information")
country_code = get_server_value(connected_server, "ExitCountry", servers)
country = get_country_name(country_code)
city = get_server_value(connected_server, "City", servers)
load = get_server_value(connected_server, "Load", servers)
feature = get_server_value(connected_server, "Features", servers)
features = get_server_features(connected_server, servers)
last_connection = get_config_value("metadata", "connected_time")
connection_time = time.time() - int(last_connection)

Expand All @@ -441,7 +428,7 @@ def status():
+ "Time: {0}\n".format(connection_time)
+ "IP: {0}\n".format(ip)
+ "Server: {0}\n".format(connected_server)
+ "Features: {0}\n".format(all_features[feature])
+ "Features: {0}\n".format(', '.join(features))
+ "Protocol: {0}\n".format(connected_protocol.upper())
+ "Kill Switch: {0}\n".format(killswitch_status)
+ "Country: {0}\n".format(country)
Expand Down
7 changes: 7 additions & 0 deletions protonvpn_cli/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
CONFIG_DIR = os.path.join(os.path.expanduser("~{0}".format(USER)), ".pvpn-cli")
CONFIG_FILE = os.path.join(CONFIG_DIR, "pvpn-cli.cfg")
SERVER_INFO_FILE = os.path.join(CONFIG_DIR, "serverinfo.json")
SERVER_FEATURES = {
1: "Secure-Core",
2: "Tor",
4: "P2P",
8: "Streaming",
16: "IPv6"
}
SPLIT_TUNNEL_FILE = os.path.join(CONFIG_DIR, "split_tunnel.txt")
OVPN_FILE = os.path.join(CONFIG_DIR, "connect.ovpn")
PASSFILE = os.path.join(CONFIG_DIR, "pvpnpass")
Expand Down
12 changes: 11 additions & 1 deletion protonvpn_cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .logger import logger
# Constants
from .constants import (
USER, CONFIG_FILE, SERVER_INFO_FILE, SPLIT_TUNNEL_FILE,
USER, CONFIG_FILE, SERVER_INFO_FILE, SERVER_FEATURES, SPLIT_TUNNEL_FILE,
VERSION, OVPN_FILE, CLIENT_SUFFIX
)
import distro
Expand Down Expand Up @@ -122,6 +122,16 @@ def get_server_value(servername, key, servers):
return value[0]


def get_server_features(servername, servers):
"""Decode server feature bit flags and return feature strings in list"""
feat = int(get_server_value(servername, "Features", servers))
server_features = []
for bit_flag in SERVER_FEATURES:
if (feat & bit_flag):
server_features.append(SERVER_FEATURES[bit_flag])
return server_features


def get_config_value(group, key):
"""Return specific value from CONFIG_FILE as string"""
config = configparser.ConfigParser()
Expand Down