Feat: url correcter util
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2025-01-20 13:44:54 +03:00
parent 0fa538d079
commit c13eb62770
5 changed files with 25 additions and 3 deletions

View File

@@ -5,5 +5,6 @@ POSTGRES_PASSWORD=postgres
POSTGRES_DB=postgres
POSTGRES_HOST=postgres
POSTGRES_PORT=5432
DOMAIN_NAME=localhost
COMPOSE_PROJECT_NAME=nwxraybot

View File

@@ -1,3 +1,4 @@
from nwxraybot.bot import NwXrayBot
from nwxraybot.config import Settings
from nwxraybot.utils import get_code, get_subscription_info
from nwxraybot.utils import (get_code, get_correct_user_url,
get_subscription_info)

View File

@@ -10,6 +10,7 @@ class Settings(BaseSettings):
postgres_db: str = Field('db', env="POSTGRES_DB")
postgres_host: str = Field('localhost', env="POSTGRES_HOST")
postgres_port: int = Field(5432, env="POSTGRES_PORT")
domain_name: str = Field('localhost', env="DOMAIN_NAME")
@property
def postgres_url(self) -> str:

View File

@@ -16,7 +16,7 @@ from nwxraybot.fsm import BroadcastStates
from nwxraybot.meta import Handler
from nwxraybot.middlewares import AdminMiddleware
from nwxraybot.models import User
from nwxraybot.utils import get_subscription_info
from nwxraybot.utils import get_correct_user_url
class AdminHandler(Handler):
@@ -45,13 +45,15 @@ class AdminHandler(Handler):
await message.reply('Вы ввели команду в неверном формате. Вводите в формате:\n``` /adduser name vless://.... 01.01.1970 00:00```', parse_mode=ParseMode.MARKDOWN)
return
user_dict = match.groupdict()
url = user_dict['url']
url = get_correct_user_url(url)
date = None
if user_dict['date']:
date = datetime.strptime(f"{user_dict['date']} {
user_dict['time']}", "%d.%m.%Y %H:%M")
code = get_code()
new_user = User(
name=user_dict['name'], url=user_dict['url'], time=date, code=code)
name=user_dict['name'], url=url, time=date, code=code)
new_user.save()
await message.answer(f'Пользователь создан. Вот его ссылка для доступа:\n`https://t.me/nwproxybot?start={code}`', parse_mode=ParseMode.MARKDOWN)

View File

@@ -2,9 +2,13 @@ import logging
from datetime import datetime
from secrets import token_urlsafe
from typing import Optional
from urllib.parse import parse_qs, urlencode, urlparse, urlunparse
from nwxraybot import Settings
from nwxraybot.models import User
config = Settings()
def get_subscription_info(telegram_id: str) -> str:
user: User = User.select().where(User.telegram_id == telegram_id).first()
@@ -20,3 +24,16 @@ def get_subscription_info(telegram_id: str) -> str:
def get_code(length: int = 10) -> str:
return token_urlsafe(length)[:length]
def get_correct_user_url(url: str) -> str:
parsed_url = urlparse(url)
query = parse_qs(parsed_url.query)
query['fp'] = 'chrome'
query['alpn'] = 'h2,h3'
query['packetEncoding'] = 'xudp'
query['security'] = 'tls'
new_query = urlencode(query, doseq=True)
new_url = urlunparse((parsed_url.scheme, parsed_url.netloc.replace("127.0.0.1:1234", f"{
config.domain_name}:443"), parsed_url.path, parsed_url.params, new_query, parsed_url.fragment))
return new_url