gitscope
https://dev.kronoz.win/r/marzneshin.git
cli/subscription.py · v0.5.1
Source
1 a47eb4c Dawsh 2y ago import typer
2 a47eb4c Dawsh 2y ago from enum import Enum
3 a47eb4c Dawsh 2y ago from typing import Optional
4 a47eb4c Dawsh 2y ago from rich.console import Console
5 a47eb4c Dawsh 2y ago
6 a47eb4c Dawsh 2y ago from app.db import GetDB
7 a47eb4c Dawsh 2y ago from app.models.user import UserResponse
8 a47eb4c Dawsh 2y ago from app.utils.share import generate_subscription
9 a47eb4c Dawsh 2y ago
10 a47eb4c Dawsh 2y ago from . import utils
11 a47eb4c Dawsh 2y ago
12 a47eb4c Dawsh 2y ago app = typer.Typer(no_args_is_help=True)
13 a47eb4c Dawsh 2y ago console = Console()
14 a47eb4c Dawsh 2y ago
15 a47eb4c Dawsh 2y ago
16 a47eb4c Dawsh 2y ago class ConfigFormat(str, Enum):
17 a47eb4c Dawsh 2y ago v2ray = "v2ray"
18 a47eb4c Dawsh 2y ago clash = "clash"
19 a47eb4c Dawsh 2y ago
20 a47eb4c Dawsh 2y ago
21 a47eb4c Dawsh 2y ago @app.command(name="get-link")
22 f4fecee Dawsh 2y ago def get_link(
23 f4fecee Dawsh 2y ago username: str = typer.Option(..., *utils.FLAGS["username"], prompt=True)
24 f4fecee Dawsh 2y ago ):
25 a47eb4c Dawsh 2y ago """
26 a47eb4c Dawsh 2y ago Prints the given user's subscription link.
27 a47eb4c Dawsh 2y ago
28 a47eb4c Dawsh 2y ago NOTE: This command needs `XRAY_SUBSCRIPTION_URL_PREFIX` environment variable to be set
29 a47eb4c Dawsh 2y ago in order to work correctly.
30 a47eb4c Dawsh 2y ago """
31 a47eb4c Dawsh 2y ago with GetDB() as db:
32 f4fecee Dawsh 2y ago user: UserResponse = UserResponse.from_orm(
33 f4fecee Dawsh 2y ago utils.get_user(db, username)
34 f4fecee Dawsh 2y ago )
35 a47eb4c Dawsh 2y ago print(user.subscription_url)
36 a47eb4c Dawsh 2y ago
37 a47eb4c Dawsh 2y ago
38 a47eb4c Dawsh 2y ago @app.command(name="get-config")
39 a47eb4c Dawsh 2y ago def get_config(
40 a47eb4c Dawsh 2y ago username: str = typer.Option(..., *utils.FLAGS["username"], prompt=True),
41 01cdf2e Dawsh 2y ago config_format: ConfigFormat = typer.Option(
42 01cdf2e Dawsh 2y ago ..., *utils.FLAGS["format"], prompt=True
43 01cdf2e Dawsh 2y ago ),
44 a47eb4c Dawsh 2y ago output_file: Optional[str] = typer.Option(
45 01cdf2e Dawsh 2y ago None,
46 01cdf2e Dawsh 2y ago *utils.FLAGS["output_file"],
47 01cdf2e Dawsh 2y ago help="Writes the generated config in the file if provided",
48 a47eb4c Dawsh 2y ago ),
49 a47eb4c Dawsh 2y ago as_base64: bool = typer.Option(
50 01cdf2e Dawsh 2y ago False,
51 01cdf2e Dawsh 2y ago "--base64",
52 01cdf2e Dawsh 2y ago is_flag=True,
53 01cdf2e Dawsh 2y ago help="Encodes output in base64 format if present",
54 01cdf2e Dawsh 2y ago ),
55 a47eb4c Dawsh 2y ago ):
56 a47eb4c Dawsh 2y ago """
57 a47eb4c Dawsh 2y ago Generates a subscription config.
58 a47eb4c Dawsh 2y ago
59 a47eb4c Dawsh 2y ago Generates a subscription config for the given user in the given format.
60 a47eb4c Dawsh 2y ago
61 a47eb4c Dawsh 2y ago The output will be written in the output file when the `output-file` is present,
62 a47eb4c Dawsh 2y ago otherwise will be shown in the terminal.
63 a47eb4c Dawsh 2y ago """
64 a47eb4c Dawsh 2y ago with GetDB() as db:
65 f4fecee Dawsh 2y ago user: UserResponse = UserResponse.from_orm(
66 f4fecee Dawsh 2y ago utils.get_user(db, username)
67 f4fecee Dawsh 2y ago )
68 a47eb4c Dawsh 2y ago conf: str = generate_subscription(
69 a47eb4c Dawsh 2y ago user=user, config_format=config_format.name, as_base64=as_base64
70 a47eb4c Dawsh 2y ago )
71 a47eb4c Dawsh 2y ago
72 a47eb4c Dawsh 2y ago if output_file:
73 a47eb4c Dawsh 2y ago with open(output_file, "w") as out_file:
74 a47eb4c Dawsh 2y ago out_file.write(conf)
75 a47eb4c Dawsh 2y ago
76 a47eb4c Dawsh 2y ago utils.success(
77 a47eb4c Dawsh 2y ago f'{username}\'s configuration in "{config_format.value}" format'
78 a47eb4c Dawsh 2y ago f' successfully save to "{output_file}".'
79 a47eb4c Dawsh 2y ago )
80 a47eb4c Dawsh 2y ago else:
81 a47eb4c Dawsh 2y ago utils.success(
82 01cdf2e Dawsh 2y ago "No output file specified."
83 a47eb4c Dawsh 2y ago f' using pager for {username}\'s config in "{config_format}" format.',
84 01cdf2e Dawsh 2y ago auto_exit=False,
85 a47eb4c Dawsh 2y ago )
86 a47eb4c Dawsh 2y ago utils.paginate(conf)