Skip to content

Commit

Permalink
feat(vpn): adds option to populate vpn peers in hosts file
Browse files Browse the repository at this point in the history
  • Loading branch information
pallabpain committed Oct 9, 2024
1 parent ea0524f commit ca83f5b
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 27 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ python-benedict = ">=0.33.2"
etcd3gw = ">=2.4.0"
graphviz = ">=0.20.3"
python-magic = ">=0.4.27"
python-hosts = "*"

[requires]
python_version = "3"
40 changes: 24 additions & 16 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions riocli/auth/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
)
from riocli.constants import Colors, Symbols
from riocli.utils.context import get_root_context
from riocli.vpn.util import cleanup_hosts_file

LOGIN_SUCCESS = click.style('{} Logged in successfully!'.format(Symbols.SUCCESS), fg=Colors.GREEN)

Expand Down Expand Up @@ -125,4 +126,10 @@ def login(

ctx.obj.save()

try:
cleanup_hosts_file()
except Exception as e:
click.secho(f'{Symbols.WARNING} Failed to '
f'clean up hosts file: {str(e)}', fg=Colors.YELLOW)

click.echo(LOGIN_SUCCESS)
8 changes: 7 additions & 1 deletion riocli/organization/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from riocli.constants import Colors
from riocli.project.util import name_to_organization_guid
from riocli.utils.context import get_root_context
from riocli.vpn.util import cleanup_hosts_file


@click.command(
Expand Down Expand Up @@ -71,5 +72,10 @@ def select_organization(
"Please set your project with `rio project select PROJECT_NAME`".format(organization_name),
fg=Colors.GREEN)


ctx.obj.save()

try:
cleanup_hosts_file()
except Exception as e:
click.secho(f'{Symbols.WARNING} Failed to '
f'clean up hosts file: {str(e)}', fg=Colors.YELLOW)
7 changes: 7 additions & 0 deletions riocli/project/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from riocli.constants import Colors, Symbols
from riocli.project.util import name_to_guid
from riocli.utils.context import get_root_context
from riocli.vpn.util import cleanup_hosts_file


@click.command(
Expand All @@ -42,6 +43,12 @@ def select_project(
ctx.obj.data['project_name'] = project_name
ctx.obj.save()

try:
cleanup_hosts_file()
except Exception as e:
click.secho(f'{Symbols.WARNING} Failed to '
f'clean up hosts file: {str(e)}', fg=Colors.YELLOW)

click.secho('{} Project {} ({}) is selected!'.format(
Symbols.SUCCESS,
project_name,
Expand Down
24 changes: 18 additions & 6 deletions riocli/vpn/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@


from datetime import timedelta

import click
from click_help_colors import HelpColorsCommand
from yaspin.api import Yaspin
Expand All @@ -29,11 +30,12 @@
priviledged_command,
stop_tailscale,
install_vpn_tools,
is_vpn_enabled_in_project
is_vpn_enabled_in_project,
update_hosts_file
)

_TAILSCALE_CMD_FORMAT = 'tailscale up --auth-key={} --login-server={} --reset --force-reauth ' \
'--accept-routes --accept-dns --advertise-tags={} --timeout=30s'
'--accept-routes --accept-dns --advertise-tags={} --timeout=30s'


@click.command(
Expand All @@ -42,9 +44,13 @@
help_headers_color=Colors.YELLOW,
help_options_color=Colors.GREEN,
)
@click.option('--update-hosts', 'update_hosts', is_flag=True,
default=False, help='Update hosts file with VPN peers to allow '
'access to them by hostname. Run with `sudo` '
'when you enable this flag.')
@click.pass_context
@with_spinner(text="Connecting...")
def connect(ctx: click.Context, spinner: Yaspin):
def connect(ctx: click.Context, update_hosts: bool, spinner: Yaspin):
"""
Connect to the current project's VPN network
"""
Expand Down Expand Up @@ -91,7 +97,15 @@ def connect(ctx: click.Context, spinner: Yaspin):
Symbols.ERROR), fg=Colors.RED)
raise SystemExit(1)

spinner.green.text = 'You are now connected to the project\'s VPN'
if update_hosts and is_tailscale_up():
spinner.text = 'Updating hosts file...'
try:
update_hosts_file()
spinner.write(click.style(f'{Symbols.SUCCESS} Hosts file updated', fg=Colors.CYAN))
except Exception as e:
spinner.write(click.style(f'Failed to update hosts: {str(e)}', fg=Colors.RED))

spinner.text = click.style('You are now connected to the project\'s VPN', fg=Colors.GREEN)
spinner.green.ok(Symbols.SUCCESS)
except click.exceptions.Abort as e:
spinner.red.text = 'Aborted!'
Expand Down Expand Up @@ -120,5 +134,3 @@ def start_tailscale(ctx: click.Context, spinner: Yaspin) -> bool:
return False

return True


11 changes: 10 additions & 1 deletion riocli/vpn/disconnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
from riocli.vpn.util import (
install_vpn_tools,
is_tailscale_up,
stop_tailscale
stop_tailscale,
cleanup_hosts_file,
)


Expand All @@ -44,10 +45,18 @@ def disconnect(ctx: click.Context):
fg=Colors.RED)
raise SystemExit(1)

try:
cleanup_hosts_file()
except Exception as e:
click.secho(f'{Symbols.WARNING} Could not clean '
f'up hosts file: {str(e)}', fg=Colors.YELLOW)

click.secho(
'{} You have been disconnected from the project\'s VPN'.format(
Symbols.SUCCESS),
fg=Colors.GREEN)
except Exception as e:
click.secho(str(e), fg=Colors.RED)
raise SystemExit(1) from e


Loading

0 comments on commit ca83f5b

Please sign in to comment.