| 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 |
01cdf2e |
Dawsh |
2y ago |
def get_link(username: str = typer.Option(..., *utils.FLAGS["username"], prompt=True)): |
| 23 |
a47eb4c |
Dawsh |
2y ago |
""" |
| 24 |
a47eb4c |
Dawsh |
2y ago |
Prints the given user's subscription link. |
| 25 |
a47eb4c |
Dawsh |
2y ago |
|
| 26 |
a47eb4c |
Dawsh |
2y ago |
NOTE: This command needs `XRAY_SUBSCRIPTION_URL_PREFIX` environment variable to be set |
| 27 |
a47eb4c |
Dawsh |
2y ago |
in order to work correctly. |
| 28 |
a47eb4c |
Dawsh |
2y ago |
""" |
| 29 |
a47eb4c |
Dawsh |
2y ago |
with GetDB() as db: |
| 30 |
a47eb4c |
Dawsh |
2y ago |
user: UserResponse = UserResponse.from_orm(utils.get_user(db, username)) |
| 31 |
a47eb4c |
Dawsh |
2y ago |
print(user.subscription_url) |
| 32 |
a47eb4c |
Dawsh |
2y ago |
|
| 33 |
a47eb4c |
Dawsh |
2y ago |
|
| 34 |
a47eb4c |
Dawsh |
2y ago |
@app.command(name="get-config") |
| 35 |
a47eb4c |
Dawsh |
2y ago |
def get_config( |
| 36 |
a47eb4c |
Dawsh |
2y ago |
username: str = typer.Option(..., *utils.FLAGS["username"], prompt=True), |
| 37 |
01cdf2e |
Dawsh |
2y ago |
config_format: ConfigFormat = typer.Option( |
| 38 |
01cdf2e |
Dawsh |
2y ago |
..., *utils.FLAGS["format"], prompt=True |
| 39 |
01cdf2e |
Dawsh |
2y ago |
), |
| 40 |
a47eb4c |
Dawsh |
2y ago |
output_file: Optional[str] = typer.Option( |
| 41 |
01cdf2e |
Dawsh |
2y ago |
None, |
| 42 |
01cdf2e |
Dawsh |
2y ago |
*utils.FLAGS["output_file"], |
| 43 |
01cdf2e |
Dawsh |
2y ago |
help="Writes the generated config in the file if provided", |
| 44 |
a47eb4c |
Dawsh |
2y ago |
), |
| 45 |
a47eb4c |
Dawsh |
2y ago |
as_base64: bool = typer.Option( |
| 46 |
01cdf2e |
Dawsh |
2y ago |
False, |
| 47 |
01cdf2e |
Dawsh |
2y ago |
"--base64", |
| 48 |
01cdf2e |
Dawsh |
2y ago |
is_flag=True, |
| 49 |
01cdf2e |
Dawsh |
2y ago |
help="Encodes output in base64 format if present", |
| 50 |
01cdf2e |
Dawsh |
2y ago |
), |
| 51 |
a47eb4c |
Dawsh |
2y ago |
): |
| 52 |
a47eb4c |
Dawsh |
2y ago |
""" |
| 53 |
a47eb4c |
Dawsh |
2y ago |
Generates a subscription config. |
| 54 |
a47eb4c |
Dawsh |
2y ago |
|
| 55 |
a47eb4c |
Dawsh |
2y ago |
Generates a subscription config for the given user in the given format. |
| 56 |
a47eb4c |
Dawsh |
2y ago |
|
| 57 |
a47eb4c |
Dawsh |
2y ago |
The output will be written in the output file when the `output-file` is present, |
| 58 |
a47eb4c |
Dawsh |
2y ago |
otherwise will be shown in the terminal. |
| 59 |
a47eb4c |
Dawsh |
2y ago |
""" |
| 60 |
a47eb4c |
Dawsh |
2y ago |
with GetDB() as db: |
| 61 |
a47eb4c |
Dawsh |
2y ago |
user: UserResponse = UserResponse.from_orm(utils.get_user(db, username)) |
| 62 |
a47eb4c |
Dawsh |
2y ago |
conf: str = generate_subscription( |
| 63 |
a47eb4c |
Dawsh |
2y ago |
user=user, config_format=config_format.name, as_base64=as_base64 |
| 64 |
a47eb4c |
Dawsh |
2y ago |
) |
| 65 |
a47eb4c |
Dawsh |
2y ago |
|
| 66 |
a47eb4c |
Dawsh |
2y ago |
if output_file: |
| 67 |
a47eb4c |
Dawsh |
2y ago |
with open(output_file, "w") as out_file: |
| 68 |
a47eb4c |
Dawsh |
2y ago |
out_file.write(conf) |
| 69 |
a47eb4c |
Dawsh |
2y ago |
|
| 70 |
a47eb4c |
Dawsh |
2y ago |
utils.success( |
| 71 |
a47eb4c |
Dawsh |
2y ago |
f'{username}\'s configuration in "{config_format.value}" format' |
| 72 |
a47eb4c |
Dawsh |
2y ago |
f' successfully save to "{output_file}".' |
| 73 |
a47eb4c |
Dawsh |
2y ago |
) |
| 74 |
a47eb4c |
Dawsh |
2y ago |
else: |
| 75 |
a47eb4c |
Dawsh |
2y ago |
utils.success( |
| 76 |
01cdf2e |
Dawsh |
2y ago |
"No output file specified." |
| 77 |
a47eb4c |
Dawsh |
2y ago |
f' using pager for {username}\'s config in "{config_format}" format.', |
| 78 |
01cdf2e |
Dawsh |
2y ago |
auto_exit=False, |
| 79 |
a47eb4c |
Dawsh |
2y ago |
) |
| 80 |
a47eb4c |
Dawsh |
2y ago |
utils.paginate(conf) |