Plain is headed towards 1.0! Subscribe for development updates →

plain.tunnel

 1import getpass
 2import random
 3import string
 4
 5import click
 6
 7from .client import TunnelClient
 8
 9
10@click.command()
11@click.argument("destination")
12@click.option(
13    "--subdomain",
14    help="The subdomain to use for the tunnel.",
15    envvar="PLAIN_TUNNEL_SUBDOMAIN",
16)
17@click.option(
18    "--tunnel-host", envvar="PLAIN_TUNNEL_HOST", hidden=True, default="plaintunnel.com"
19)
20@click.option("--debug", "log_level", flag_value="DEBUG", help="Enable debug logging.")
21@click.option(
22    "--quiet", "log_level", flag_value="WARNING", help="Only log warnings and errors."
23)
24def cli(destination, subdomain, tunnel_host, log_level):
25    if not destination.startswith("http://") and not destination.startswith("https://"):
26        destination = f"https://{destination}"
27
28    if not log_level:
29        log_level = "INFO"
30
31    if not subdomain:
32        # Generate a subdomain using the system username + 7 random characters
33        random_chars = "".join(random.choices(string.ascii_lowercase, k=7))
34        subdomain = f"{getpass.getuser()}-{random_chars}"
35
36    tunnel = TunnelClient(
37        destination_url=destination,
38        subdomain=subdomain,
39        tunnel_host=tunnel_host,
40        log_level=log_level,
41    )
42    click.secho(f"Tunneling {tunnel.tunnel_http_url} -> {destination}", bold=True)
43    tunnel.run()