-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
184 lines (158 loc) · 5.76 KB
/
main.py
File metadata and controls
184 lines (158 loc) · 5.76 KB
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
from src.Client import *
from src.Monitoring import Monitoring
from src.Server import *
from src.exchanges.HTTP import *
from src.vpns.NoVPN import *
from src.vpns.Rosenpass import *
from src.vpns.WireGuard import *
from src.vpns.OpenVPN import *
from src.vpns.OpenVPNstatic import *
class HandleInput:
"""
Handles the inputs given.
"""
valid_inputs = False
def __init__(
self, role, vpn_option, exchange_type, operation, iterations, directory, auto
) -> None:
"""
Sets all values to the given inputs and checks the values.
:param role: role of the host
:param vpn_option: VPN type to be used
:param exchange_type: Exchange type to be used
:param operation: Operation to be executed
:param iterations: number of iterations of exchanges
:param directory: directory to save the keys (only for keysend option)
:param auto: activates monitoring in automatic mode
"""
self.role = role
self.vpn_option = vpn_option
self.exchange_type = exchange_type
self.operation = operation
self.iterations = iterations
self.directory = directory
self.auto = auto
if self.__check_values():
self.valid_inputs = True
def execute(self) -> bool:
"""
If inputs are valid, creates the server or client instance with the given parameters and executes the given
operation.
:return: True for success, False otherwise
"""
if not self.valid_inputs:
helpers.messages.print_err("Inputs are not valid. Please start again.")
return False
self.__create_instance()
if self.operation == "keygen":
self.__handle_keygen()
elif self.operation == "keysend":
self.__handle_keysend()
elif self.operation == "exchange":
self.__handle_exchange()
return True
def __check_values(self) -> bool:
"""
Checks if the given inputs are in the defined scope. Returns False otherwise.
:return: True for success, False otherwise
"""
if self.role not in ("server", "client"):
helpers.messages.print_err(
"Invalid ROLE argument. Has to be server|client."
)
return False
if self.vpn_option not in ("novpn", "rosenpass", "wg", "openvpn", "openvpnstatic"):
helpers.messages.print_err(
"Invalid VPN_OPTION argument. Has to be novpn|rosenpass|wg|openvpn|openvpnstatic."
)
return False
if not self.exchange_type == "http":
helpers.messages.print_err(
"Invalid EXCHANGE_TYPE argument. Has to be http."
)
return False
if self.operation not in ("keygen", "keysend", "exchange"):
helpers.messages.print_err(
"Invalid OPERATION argument. Has to be keygen|keysend|exchange."
)
return False
if self.iterations < 1:
helpers.messages.print_err("Invalid ITERATIONS option. Has to be positive.")
return False
return True
def __create_instance(self) -> None:
"""
Creates the server or client instance with the given parameters.
"""
# create Exchange instance
exchange = None
if self.exchange_type == "http":
exchange = HTTP
# create VPN instance
vpn = None
if self.vpn_option == "novpn":
vpn = NoVPN
elif self.vpn_option == "rosenpass":
vpn = Rosenpass
elif self.vpn_option == "wg":
vpn = WireGuard
elif self.vpn_option == "openvpn":
vpn = OpenVPN
elif self.vpn_option == "openvpnstatic":
vpn = OpenVPNstatic
if self.role == "server":
self.instance = Server(exchange, vpn)
elif self.role == "client":
self.instance = Client(exchange, vpn)
def __handle_keygen(self) -> None:
"""
Handles the key generation.
"""
self.instance.keygen()
def __handle_keysend(self) -> None:
"""
Handles the key sending.
"""
self.instance.keysend(self.directory)
def __handle_exchange(self) -> None:
"""
Starts the monitor, executes the exchange and stops the monitor.
"""
monitor = Monitoring(self.role, self.vpn_option)
monitor.start(auto=self.auto)
# start test
self.instance.run(self.iterations, monitor)
# end test
monitor.stop()
@click.command()
@click.option("-i", "--iterations", type=int, default=1, help="number of iterations")
@click.option(
"-d",
"--directory",
type=str,
help="directory to save the keys (only for keysend option)",
default="~",
)
@click.option("--auto", help="monitor in auto mode", is_flag=True)
@click.argument("role", type=str)
@click.argument("vpn_option", type=str)
@click.argument("exchange_type", type=str)
@click.argument("operation", type=str)
def cli(role, vpn_option, exchange_type, operation, iterations, directory, auto):
"""
Calls the handler with the given CLI inputs.
:param role: role of the host
:param vpn_option: VPN type to be used
:param exchange_type: Exchange type to be used
:param operation: Operation to be executed
:param iterations: number of iterations of exchanges
:param directory: directory to save the keys (only for keysend option)
:param auto: activates monitoring in automatic mode
"""
handler = HandleInput(
role, vpn_option, exchange_type, operation, iterations, directory, auto
)
if not handler.execute():
messages.print_err("Execution failed.")
if __name__ == "__main__":
cli()