forked from Gozargah/Marzban
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscription.py
75 lines (61 loc) · 2.32 KB
/
subscription.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
import typer
from enum import Enum
from typing import Optional
from rich.console import Console
from app.db import GetDB
from app.models.user import UserResponse
from app.utils.share import generate_subscription
from . import utils
app = typer.Typer(no_args_is_help=True)
console = Console()
class ConfigFormat(str, Enum):
v2ray = "v2ray"
clash = "clash"
@app.command(name="get-link")
def get_link(
username: str = typer.Option(..., *utils.FLAGS["username"], prompt=True)
):
"""
Prints the given user's subscription link.
NOTE: This command needs `XRAY_SUBSCRIPTION_URL_PREFIX` environment variable to be set
in order to work correctly.
"""
with GetDB() as db:
user: UserResponse = UserResponse.from_orm(utils.get_user(db, username))
print(user.subscription_url)
@app.command(name="get-config")
def get_config(
username: str = typer.Option(..., *utils.FLAGS["username"], prompt=True),
config_format: ConfigFormat = typer.Option(..., *utils.FLAGS["format"], prompt=True),
output_file: Optional[str] = typer.Option(
None, *utils.FLAGS["output_file"], help="Writes the generated config in the file if provided"
),
as_base64: bool = typer.Option(
False, "--base64", is_flag=True, help="Encodes output in base64 format if present"
)
):
"""
Generates a subscription config.
Generates a subscription config for the given user in the given format.
The output will be written in the output file when the `output-file` is present,
otherwise will be shown in the terminal.
"""
with GetDB() as db:
user: UserResponse = UserResponse.from_orm(utils.get_user(db, username))
conf: str = generate_subscription(
user=user, config_format=config_format.name, as_base64=as_base64
)
if output_file:
with open(output_file, "w") as out_file:
out_file.write(conf)
utils.success(
f'{username}\'s configuration in "{config_format.value}" format'
f' successfully save to "{output_file}".'
)
else:
utils.success(
'No output file specified.'
f' using pager for {username}\'s config in "{config_format}" format.',
auto_exit=False
)
utils.paginate(conf)