-
Notifications
You must be signed in to change notification settings - Fork 2
/
establish_connection.py
61 lines (54 loc) · 3.07 KB
/
establish_connection.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
import signal
import os
import subprocess
import threading
from pathlib import Path
from subprocess import PIPE
class EstablishConnection:
def __init__(self, sudo_pass):
self.network_interfaces_list = os.listdir("/sys/class/net")
self.default_gateway = '192.168.0.1'
self.ipvanish_dns = ['198.18.0.1', '198.18.0.2']
self.sudo_password = sudo_pass
self.main_directory = str(Path(__file__).parent)
self.configuration_files_path = str(Path(__file__).parent) + "/configs"
self.pid = None
def open_vpn_connection(self):
connect = ['sudo', '-S', 'openvpn', '--config', self.configuration_files_path + "/conn.ovpn"]
disable_ipv6 = ['sudo', '-S', 'sysctl', 'net.ipv6.conf.all.disable_ipv6=1']
run_ipv6 = subprocess.Popen(disable_ipv6, shell=False, stdin=PIPE, encoding='utf-8')
run = subprocess.Popen(connect, shell=False, stdin=PIPE, encoding='utf-8')
with open(self.main_directory + "/pid", 'w') as process_id:
process_id.write(str(run.pid))
process_id.close()
run_ipv6.communicate(input=self.main_directory)
run.communicate(input=self.main_directory)
def set_options(self):
for interface in self.network_interfaces_list:
commands_as_list_of_lists = [['sudo', '-S', 'resolvectl', 'default-route', interface, 'false'],
['sudo', '-S', 'resolvectl', 'llmnr', interface, 'false'],
['sudo', '-S', 'resolvectl', 'mdns', interface, 'false'],
['sudo', '-S', 'resolvectl', 'dns', interface, self.ipvanish_dns[0],
self.ipvanish_dns[1]]]
for command in commands_as_list_of_lists:
if "tun0" not in interface and interface != "lo":
self.launch_command(command)
tunnel_options = [['sudo', '-S', 'resolvectl', 'dns', 'tun0', self.ipvanish_dns[0], self.ipvanish_dns[1]],
['sudo', '-S', 'resolvectl', 'default-route', 'tun0', 'true']]
for t_opt in tunnel_options:
self.launch_command(t_opt)
def start_connection(self):
threading.Thread(target=self.open_vpn_connection, args=()).start()
def launch_command(self, command):
subprocess.Popen(command, shell=False,
stdin=PIPE, encoding='utf-8').communicate(input=self.main_directory)
def disconnect(self):
pid = open(self.main_directory + "/pid", 'r').readline()
os.kill(int(pid), signal.SIGINT)
os.remove(self.main_directory + "/pid")
for interface in self.network_interfaces_list:
commands_as_list_of_lists = [['sudo', '-S', 'sysctl', 'net.ipv6.conf.all.disable_ipv6=0'],
['sudo', '-S', 'resolvectl', 'default-route', interface, 'true'],
['sudo', '-S', 'resolvectl', 'dns', interface, self.default_gateway]]
for command in commands_as_list_of_lists:
self.launch_command(command)