forked from awsassets/linux-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_dialog.py
204 lines (171 loc) · 6.49 KB
/
cli_dialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import copy
import sys
from dialog import Dialog
from protonvpn_nm_lib import exceptions
from protonvpn_nm_lib.core.subprocess_wrapper import subprocess
from protonvpn_nm_lib.country_codes import country_codes
from protonvpn_nm_lib.enums import (FeatureEnum, KillswitchStatusEnum,
ProtocolEnum, ServerTierEnum)
from .logger import logger
class ProtonVPNDialog:
def __init__(self, protonvpn):
self.protonvpn = protonvpn
self.SUPPORTED_FEATURES = {
FeatureEnum.NORMAL: "",
FeatureEnum.SECURE_CORE: "Secure-Core",
FeatureEnum.TOR: "Tor",
FeatureEnum.P2P: "P2P",
FeatureEnum.STREAMING: "Streaming",
FeatureEnum.IPv6: "IPv6"
}
self.SERVER_TIERS = {
ServerTierEnum.FREE: "Free",
ServerTierEnum.BASIC: "Basic",
ServerTierEnum.PLUS_VISIONARY: "Plus/Visionary",
ServerTierEnum.PM: "PMTEAM"
}
self.KILLSWITCH_STATUS_TEXT = {
KillswitchStatusEnum.HARD: "Permanent",
KillswitchStatusEnum.SOFT: "On",
KillswitchStatusEnum.DISABLED: "Off",
}
def start(self):
"""Connect to server with a dialog menu.
Args:
server_manager (ServerManager): instance of ServerManager
session (proton.api.Session): the current user session
Returns:
tuple: (servername, protocol)
"""
self.session = self.protonvpn.get_session()
self.servers = self.session.servers
self.country = self.protonvpn.get_country()
self.user_settings = self.protonvpn.get_settings()
self.protonvpn.ensure_connectivity()
countries = self.country.get_dict_with_country_servername(
self.servers, self.session.vpn_tier
)
logger.debug(countries)
# Fist dialog
country = self.display_country(countries)
logger.info("Selected country: \"{}\"".format(country))
# Second dialog
server = self.display_servers(country, countries)
logger.info("Selected server: \"{}\"".format(server))
# Third dialog
protocol = self.display_protocol()
logger.info("Selected protocol: \"{}\"".format(protocol))
subprocess.run(["clear"])
return server, protocol
def display_country(self, countries):
"""Displays a dialog with a list of supported countries.
Args:
countries (dict): {country_code: servername}
server_manager (ServerManager): instance of ServerManager
servers (list): contains server information about each country
Returns:
string: country code (PT, SE, DK, etc)
"""
choices = []
for country in sorted(countries.keys()):
country_code = [
cc
if _country == country
else country
for cc, _country
in country_codes.items()
].pop()
choices.append((country, "{}".format(country_code)))
return self.display_dialog("Choose a country:", choices)
def display_servers(self, country, countries):
"""Displays a dialog with a list of servers.
Args:
countries (dict): {country_code: servername}
country (string): country code (PT, SE, DK, etc)
Returns:
string: servername (PT#8, SE#5, DK#10, etc)
"""
choices = []
country_servers = self.sort_servers(country, countries)
for servername in country_servers:
server = self.protonvpn.config_for_server_with_servername(
servername
)
load = str(int(server.load)).rjust(3, " ")
_features = copy.copy(server.features)
try:
_features.pop(FeatureEnum.NORMAL)
except IndexError:
pass
if len(_features) > 1:
features = ", ".join(
[self.SUPPORTED_FEATURES[feature] for feature in _features]
)
elif len(_features) == 1:
features = self.SUPPORTED_FEATURES[_features[0]]
else:
features = ""
tier = self.SERVER_TIERS[ServerTierEnum(server.tier)]
choices.append(
(
servername, "Load: {0}% | {1} | {2}".format(
load, tier, features
)
)
)
return self.display_dialog("Choose the server to connect:", choices)
def display_protocol(self):
"""Displays a dialog with a list of protocols.
Returns:
string: protocol
"""
return self.display_dialog(
"Choose a protocol:", [
(ProtocolEnum.UDP.value, "Better Speed"),
(ProtocolEnum.TCP.value, "Better Reliability")
]
)
def display_dialog(self, headline, choices, stop=False):
"""Show dialog and process response."""
d = Dialog(dialog="dialog", autowidgetsize=True)
code, tag = d.menu(headline, title="ProtonVPN-CLI", choices=choices)
if code == "ok":
return tag
else:
subprocess.run(["clear"])
print("Canceled.")
sys.exit(1)
def sort_servers(self, country, countries):
country_servers = countries[country]
non_match_tier_servers = {}
match_tier_servers = {}
user_tier = self.session.vpn_tier
for server in country_servers:
logger.debug("Servename: {}".format(server))
try:
_server = self.protonvpn.config_for_server_with_servername(
server
)
except exceptions.EmptyServerListError:
continue
server_tier = _server.tier
if server_tier == user_tier:
match_tier_servers[server] = server_tier
elif (
(server_tier > user_tier or server_tier < user_tier)
and not server_tier == 3
):
non_match_tier_servers[server] = server_tier
sorted_dict = dict(
sorted(
non_match_tier_servers.items(),
key=lambda s: s[1],
reverse=True
)
)
match_tier_servers.update(sorted_dict)
return [
servername
for servername, server_tier
in match_tier_servers.items()
]