2024-07-31 04:30:24 +02:00
|
|
|
#!/usr/bin/env python3
|
2024-07-31 02:48:00 +02:00
|
|
|
|
2024-07-31 04:30:24 +02:00
|
|
|
import subprocess
|
|
|
|
import requests
|
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
import random
|
2024-07-31 02:48:00 +02:00
|
|
|
|
2024-07-31 04:30:24 +02:00
|
|
|
PRIVACY_FRIENDLY_COUNTRIES = ['Sweden', 'Switzerland', 'Germany', 'Finland', 'Netherlands', 'Norway']
|
2024-07-31 02:48:00 +02:00
|
|
|
|
2024-08-03 23:16:10 +02:00
|
|
|
def run_command(command):
|
|
|
|
result = subprocess.run(command, capture_output=True, text=True)
|
2024-07-31 04:30:24 +02:00
|
|
|
if result.returncode != 0:
|
2024-08-03 23:16:10 +02:00
|
|
|
raise Exception(f"Failed to execute command: {' '.join(command)}")
|
|
|
|
return result.stdout
|
2024-07-31 02:48:00 +02:00
|
|
|
|
2024-08-03 23:16:10 +02:00
|
|
|
def get_current_exit_node():
|
|
|
|
status = json.loads(run_command(['tailscale', 'status', '--json']))
|
|
|
|
return status.get('Peer', {}).get('Tailnet', {}).get('ExitNode', {}).get('Name')
|
2024-07-31 02:48:00 +02:00
|
|
|
|
2024-08-03 23:16:10 +02:00
|
|
|
def verify_exit_node(exit_node):
|
2024-07-31 04:30:24 +02:00
|
|
|
response = requests.get('https://am.i.mullvad.net/json')
|
2024-08-03 23:16:10 +02:00
|
|
|
exit_node_hostname = response.json().get('mullvad_exit_ip_hostname')
|
2024-07-31 04:30:24 +02:00
|
|
|
print(f"Current exit node hostname: {exit_node_hostname}")
|
|
|
|
|
|
|
|
exit_node_short = exit_node.split('.')[0]
|
|
|
|
if exit_node_short == exit_node_hostname:
|
|
|
|
print("Exit node set successfully!")
|
|
|
|
else:
|
|
|
|
print("Failed to set exit node!")
|
|
|
|
|
2024-08-03 23:16:10 +02:00
|
|
|
def set_exit_node(exit_node=None):
|
|
|
|
if not exit_node:
|
|
|
|
stdout = run_command(['tailscale', 'exit-node', 'suggest'])
|
|
|
|
exit_node = next((line.split(': ')[1].strip() for line in stdout.splitlines() if 'Suggested exit node' in line), None)
|
|
|
|
|
|
|
|
print(f"Setting exit node: {exit_node}")
|
|
|
|
run_command(['tailscale', 'set', f'--exit-node={exit_node}'])
|
|
|
|
verify_exit_node(exit_node)
|
|
|
|
|
2024-07-31 04:30:24 +02:00
|
|
|
def unset_exit_node():
|
2024-08-03 23:16:10 +02:00
|
|
|
run_command(['tailscale', 'set', '--exit-node='])
|
2024-07-31 04:30:24 +02:00
|
|
|
print("Exit node unset successfully!")
|
|
|
|
|
|
|
|
def get_random_privacy_friendly_exit_node():
|
2024-08-03 23:16:10 +02:00
|
|
|
stdout = run_command(['tailscale', 'exit-node', 'list'])
|
|
|
|
exit_nodes = [parts[1] for parts in (line.split() for line in stdout.splitlines())
|
|
|
|
if len(parts) > 3 and parts[2] in PRIVACY_FRIENDLY_COUNTRIES]
|
2024-07-31 04:30:24 +02:00
|
|
|
|
|
|
|
if not exit_nodes:
|
|
|
|
raise Exception("No privacy-friendly exit nodes available")
|
|
|
|
return random.choice(exit_nodes)
|
|
|
|
|
2024-08-03 23:16:10 +02:00
|
|
|
def main():
|
2024-07-31 04:30:24 +02:00
|
|
|
parser = argparse.ArgumentParser(description='Manage VPN exit nodes.')
|
2024-08-03 23:16:10 +02:00
|
|
|
parser.add_argument('action', nargs='?', default='start', choices=['start', 'stop', 'new', 'shh'],
|
|
|
|
help='Action to perform: start (default), stop, new, or shh')
|
2024-07-31 04:30:24 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.action == 'start':
|
2024-08-03 23:16:10 +02:00
|
|
|
current_exit_node = get_current_exit_node()
|
|
|
|
if current_exit_node:
|
|
|
|
print(f"Already connected to exit node: {current_exit_node}")
|
|
|
|
else:
|
|
|
|
set_exit_node()
|
2024-07-31 04:30:24 +02:00
|
|
|
elif args.action == 'stop':
|
|
|
|
unset_exit_node()
|
|
|
|
elif args.action == 'new':
|
|
|
|
set_exit_node()
|
|
|
|
elif args.action == 'shh':
|
2024-08-03 23:16:10 +02:00
|
|
|
set_exit_node(get_random_privacy_friendly_exit_node())
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|
2024-07-31 02:48:00 +02:00
|
|
|
|