-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpn.py
229 lines (185 loc) · 8.04 KB
/
vpn.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import os
import time
import json
import typer
import github
import atexit
import shutil
import tailscale
import subprocess
app = typer.Typer()
CONFIG_FILE = "config.json"
PWD = os.path.dirname(os.path.abspath(__file__))
def read_config():
if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, "r") as f:
try:
return json.load(f)
except:
return {}
return {}
def write_config(config_data):
with open(CONFIG_FILE, "w") as f:
json.dump(config_data, f, indent=4)
def check_config(config):
parameters_needed = [
"repo",
"action_name",
"tailscale_auth_token",
"tailscale_auth_token_id",
"tailscale_api_token",
"github_token",
"safe_mode"
]
config_valid = True
for parameter in parameters_needed:
config_valid = config_valid and parameter in config
return config_valid
def print_error(s):
typer.echo(typer.style(s, fg=typer.colors.RED))
def print_info(s):
typer.echo(typer.style(s, fg=typer.colors.GREEN))
def validate_yes_no(value: str) -> bool:
"""Validate user input as 'y' or 'n' and convert to boolean."""
if value.lower() in ['y', 'yes']:
return True
elif value.lower() in ['n', 'no']:
return False
else:
raise typer.BadParameter("Please enter 'y' for yes or 'n' for no.")
def unplug_tailscale():
print_info("[+] Unplugging tailscale exit node")
command = [f"{PWD}/tailscale_down.sh"]
_, stderr = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True).communicate()
if stderr:
print_error(f"[-] Failed to execute `{' '.join(command)}`")
print_error(stderr)
def start_vpn(github_client, tailscale_client):
github_client.trigger_vpn()
print_info("[+] Waiting for tailscale to setup...")
if not github_client.wait_tail_scale_setup():
print_error("[-] Tailscale not setup after 60 seconds")
exit(1)
print_info("[+] Tailscale is up")
time.sleep(1)
vpn_ip = tailscale_client.get_vpn_ip()
print_info(f"[+] VPN ip : {vpn_ip}")
return vpn_ip
def connect_vpn(vpn_ip):
command = [f"{PWD}/tailscale_up.sh", vpn_ip]
_, stderr = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True).communicate()
if stderr:
print_error(f"[-] Failed to execute `{' '.join(command)}`")
print_error(stderr)
exit()
print_info("[+] Connected!")
def ping(host, ping_count=3, timeout=10):
command = ["ping", f"{host}", "-c", f"{ping_count}", "-w", f"{timeout}"]
output = subprocess.Popen(command, stdout=subprocess.PIPE, encoding="utf-8")
for line in output.stdout:
if "received" in line:
packet_received = line.split(",")[1].split(" ")[1]
return int(packet_received) > 0
return False
def is_sudo():
return not os.geteuid()
def is_tailscale():
return shutil.which("tailscale") is not None
@app.command()
def setup():
"""Setup the various variables needed to work with poor-vpn"""
config = read_config()
repo = typer.prompt(f"Enter the repo [{config.get('repo', 'sarapuce/poor-vpn')}]", default=config.get("repo", "sarapuce/poor-vpn"), show_default=False)
action_name = typer.prompt(f"Enter the action file name [{config.get('action_name', 'vpn.yaml')}]", default=config.get("action_name", "vpn.yaml"), show_default=False)
github_token = typer.prompt(f"Enter your github token [{8 * bool(len(config.get('github_token', ''))) * '*'}]", default=config.get("github_token", ""), show_default=False, hide_input=True)
tailscale_api_token = typer.prompt(f"Enter your tailscale api token [{8 * bool(len(config.get('tailscale_api_token', ''))) * '*'}]", default=config.get("tailscale_api_token", ""), show_default=False, hide_input=True)
reset_auth = typer.prompt("Do you want to reset tailscale auth token? (y/N)", default="n", show_default=False) in ["y", "Y", "yes", "Yes"]
safe_mode = typer.prompt("Do you want to enable safe mode? (Y/n)", default="y", show_default=False) not in ["n", "N", "no", "No"]
config["repo"] = repo
config["action_name"] = action_name
config["github_token"] = github_token
config["tailscale_api_token"] = tailscale_api_token
config["safe_mode"] = safe_mode
write_config(config)
g = github.github(github_token, repo, action_name)
print_info("[+] Connected to Github!")
t = tailscale.tailscale(tailscale_api_token)
print_info("[+] Connected to Tailscale!")
check_acl = t.check_acl()
if check_acl:
print_error(f"[-] {check_acl}")
exit()
if reset_auth and config.get("tailscale_auth_token_id", ""):
t.delete_key(config["tailscale_auth_token_id"])
config["tailscale_auth_token_id"] = ""
if not config.get("tailscale_auth_token_id", "") or not t.verify_auth_keys(config["tailscale_auth_token_id"]):
key = t.create_auth_key()
config["tailscale_auth_token"] = key["key"]
config["tailscale_auth_token_id"] = key["id"]
write_config(config)
print_info("[+] Generated auth token for tailscale...")
g.set_tailscale_secret(config["tailscale_auth_token"])
print_info("[+] Configuration saved!")
@app.command()
def connect():
if not is_sudo():
print_error("[-] To use tailscale, this script must be executed as root")
exit()
if not is_tailscale():
print_error("[-] To use tailscale, tailscale must be installed and in the path of the root user")
exit()
config = read_config()
if not check_config(config):
print_error("[-] Please start vpn.py setup to configure the application")
exit(1)
print_info("[+] Config loaded!")
g = github.github(config["github_token"], config["repo"], config["action_name"])
print_info("[+] Connected to Github!")
t = tailscale.tailscale(config["tailscale_api_token"])
print_info("[+] Connected to Tailscale!")
if not t.verify_auth_keys(config["tailscale_auth_token_id"]):
print_error("Tailscale auth key is invalid, please generate it with python3 vpn.py setup")
vpn_deleted = t.kill_vpns()
if vpn_deleted == -1:
print_error("[-] Can't delete some workflow, are they still running ?")
if vpn_deleted:
print_info(f"[+] Removed {vpn_deleted} old VPN{'s' * bool(vpn_deleted - 1)}")
if g.delete_runs():
print_info("[+] Deleted all old workflow runs")
atexit.register(g.stop_runs)
vpn_ip = start_vpn(g, t)
if not config["safe_mode"]:
atexit.register(unplug_tailscale)
connect_vpn(vpn_ip)
top = time.time()
while(True):
if not ping(vpn_ip):
if config["safe_mode"]:
print_info("[+] Safe mode activated, not disabling tailscale.")
exit(1)
unplug_tailscale()
if not ping("google.com"):
print_error("[-] Can't connect to google.com without VPN, your internet connexion is probably down")
else:
if not ping(vpn_ip): # Catch the case where the connexion get back during the ping to google
print_info("[+] VPN seems down, reconnecting...")
vpn_ip = start_vpn(g, t)
connect_vpn(vpn_ip)
if not config["safe_mode"] and time.time() - top > 6900:
print_info("[+] Creating a new VPN to avoid timeout")
vpn_ip = start_vpn(g, t)
connect_vpn(vpn_ip)
top = time.time()
time.sleep(5)
@app.command()
def test():
config = read_config()
if not check_config(config):
print_error("[-] Please start vpn.py setup to configure the application")
exit(1)
g = github.github(config["github_token"], config["repo"], config["action_name"])
print_info("[+] Connected to Github!")
print(g.get_run_ids())
print(g.get_run_ids(include_completed=False))
if __name__ == "__main__":
app()