Skip to content

Commit

Permalink
fix(Rafficer#367): decode server feature bit flags
Browse files Browse the repository at this point in the history
  • Loading branch information
marcel-engelke committed Mar 24, 2024
1 parent 568dc77 commit 5cfabb6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
8 changes: 3 additions & 5 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 @@ -412,14 +412,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 +439,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
6 changes: 6 additions & 0 deletions protonvpn_cli/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
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",
}
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
11 changes: 9 additions & 2 deletions 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 @@ -115,12 +115,19 @@ def get_servers():
# Sort server IDs by Tier
return [server for server in servers if server["Tier"] <= user_tier and server["Status"] == 1] # noqa


def get_server_value(servername, key, servers):
"""Return the value of a key for a given server."""
value = [server[key] for server in servers if server['Name'] == servername]
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"""
Expand Down

0 comments on commit 5cfabb6

Please sign in to comment.