* Улучшена генерация кода, пояснения в некоторых участках, очистка номера телефона через регулярные выражения :>

* Именовать переменные snake_case стоит везде, даже если ты достаешь заголовки в такомСтиле

if not object использовать предпочтительнее, т.к. он обрабатывает более широкие случаи, когда достать данные не получилось
This commit is contained in:
relyay 2026-03-10 21:59:44 +03:00 committed by GitHub
parent 917db80460
commit 4d82f55b79
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 40 additions and 27 deletions

View File

@ -1,12 +1,19 @@
import hashlib, secrets, random, time, logging, json # PEP-8 по приколу сделан >_< import hashlib
import secrets
import time
import logging
import json
import re import re
from common.static import Static from common.static import Static
from common.tools import Tools from common.tools import Tools
from tamtam_tcp.proto import Proto from tamtam_tcp.proto import Proto
from tamtam_tcp.models import * from tamtam_tcp.models import *
class Processors: class Processors:
def __init__(self, db_pool=None, clients={}, send_event=None): def __init__(self, db_pool=None, clients=None, send_event=None):
if clients is None:
clients = {} # Более правильная логика
self.static = Static() self.static = Static()
self.proto = Proto() self.proto = Proto()
self.tools = Tools() self.tools = Tools()
@ -45,8 +52,8 @@ class Processors:
return None, None return None, None
# Получаем данные из пакета # Получаем данные из пакета
deviceType = payload.get("userAgent").get("deviceType") device_type = payload.get("userAgent").get("deviceType")
deviceName = payload.get("userAgent").get("deviceName") device_name = payload.get("userAgent").get("deviceName")
# Данные пакета # Данные пакета
payload = { payload = {
@ -65,7 +72,7 @@ class Processors:
# Отправляем # Отправляем
await self._send(writer, packet) await self._send(writer, packet)
return deviceType, deviceName return device_type, device_name
async def process_request_code(self, payload, seq, writer): async def process_request_code(self, payload, seq, writer):
"""Обработчик запроса кода""" """Обработчик запроса кода"""
@ -77,10 +84,10 @@ class Processors:
return return
# Извлекаем телефон из пакета # Извлекаем телефон из пакета
phone = re.sub(r'\D', '', payload.get("phone", "")) # Не хардкодим, через регулярки phone = re.sub(r'\D', '', payload.get("phone", "")) # Не хардкодим, через регулярки
# Генерируем токен с кодом # Генерируем токен с кодом
code = f"{secrets.randbelow(1_000_000):06d}" # Старая версия ненадежна, могла отбросить ведущие нули или вообще интерпритировать как систему счисления с основанием 8 code = f"{secrets.randbelow(1_000_000):06d}" # Старая версия ненадежна, могла отбросить ведущие нули или вообще интерпритировать как систему счисления с основанием 8
token = secrets.token_urlsafe(128) token = secrets.token_urlsafe(128)
# Хешируем # Хешируем
@ -96,12 +103,14 @@ class Processors:
await cursor.execute("SELECT * FROM users WHERE phone = %s", (phone,)) await cursor.execute("SELECT * FROM users WHERE phone = %s", (phone,))
user = await cursor.fetchone() user = await cursor.fetchone()
if user is None: if not user:
await self._send_error(seq, self.proto.REQUEST_CODE, self.error_types.USER_NOT_FOUND, writer) await self._send_error(seq, self.proto.REQUEST_CODE, self.error_types.USER_NOT_FOUND, writer)
return return
# Сохраняем токен # Сохраняем токен
await cursor.execute("INSERT INTO auth_tokens (phone, token_hash, code_hash, expires, state) VALUES (%s, %s, %s, %s, %s)", (phone, token_hash, code_hash, expires, "started",)) await cursor.execute(
"INSERT INTO auth_tokens (phone, token_hash, code_hash, expires, state) VALUES (%s, %s, %s, %s, %s)",
(phone, token_hash, code_hash, expires, "started",))
# Данные пакета # Данные пакета
payload = { payload = {
@ -144,10 +153,11 @@ class Processors:
async with self.db_pool.acquire() as conn: async with self.db_pool.acquire() as conn:
async with conn.cursor() as cursor: async with conn.cursor() as cursor:
# Ищем токен # Ищем токен
await cursor.execute("SELECT * FROM auth_tokens WHERE token_hash = %s AND expires > UNIX_TIMESTAMP()", (hashed_token,)) await cursor.execute("SELECT * FROM auth_tokens WHERE token_hash = %s AND expires > UNIX_TIMESTAMP()",
(hashed_token,))
stored_token = await cursor.fetchone() stored_token = await cursor.fetchone()
if stored_token is None: if not stored_token:
await self._send_error(seq, self.proto.VERIFY_CODE, self.error_types.CODE_EXPIRED, writer) await self._send_error(seq, self.proto.VERIFY_CODE, self.error_types.CODE_EXPIRED, writer)
return return
@ -161,7 +171,8 @@ class Processors:
account = await cursor.fetchone() account = await cursor.fetchone()
# Обновляем состояние токена # Обновляем состояние токена
await cursor.execute("UPDATE auth_tokens set state = %s WHERE token_hash = %s", ("verified", hashed_token,)) await cursor.execute("UPDATE auth_tokens set state = %s WHERE token_hash = %s",
("verified", hashed_token,))
# # Создаем сессию # # Создаем сессию
# await cursor.execute( # await cursor.execute(
@ -171,9 +182,9 @@ class Processors:
# Генерируем профиль # Генерируем профиль
# Аватарка с биографией # Аватарка с биографией
photoId = None if not account.get("avatar_id") else int(account.get("avatar_id")) photo_id = int(account["avatar_id"]) if account.get("avatar_id") else None
avatar_url = None if not photoId else self.config.avatar_base_url + photoId avatar_url = f"{self.config.avatar_base_url}{photo_id}" if photo_id else None
description = None if not account.get("description") else account.get("description") description = account.get("description")
# Собираем данные пакета # Собираем данные пакета
payload = { payload = {
@ -181,7 +192,7 @@ class Processors:
id=account.get("id"), id=account.get("id"),
phone=int(account.get("phone")), phone=int(account.get("phone")),
avatarUrl=avatar_url, avatarUrl=avatar_url,
photoId=photoId, photoId=photo_id,
updateTime=int(account.get("updatetime")), updateTime=int(account.get("updatetime")),
firstName=account.get("firstname"), firstName=account.get("firstname"),
lastName=account.get("lastname"), lastName=account.get("lastname"),
@ -235,7 +246,8 @@ class Processors:
async with self.db_pool.acquire() as conn: async with self.db_pool.acquire() as conn:
async with conn.cursor() as cursor: async with conn.cursor() as cursor:
# Ищем токен # Ищем токен
await cursor.execute("SELECT * FROM auth_tokens WHERE token_hash = %s AND expires > UNIX_TIMESTAMP()", (hashed_token,)) await cursor.execute("SELECT * FROM auth_tokens WHERE token_hash = %s AND expires > UNIX_TIMESTAMP()",
(hashed_token,))
stored_token = await cursor.fetchone() stored_token = await cursor.fetchone()
if stored_token is None: if stored_token is None:
@ -256,12 +268,13 @@ class Processors:
# Создаем сессию # Создаем сессию
await cursor.execute( await cursor.execute(
"INSERT INTO tokens (phone, token_hash, device_type, device_name, location, time) VALUES (%s, %s, %s, %s, %s, %s)", "INSERT INTO tokens (phone, token_hash, device_type, device_name, location, time) VALUES (%s, %s, %s, %s, %s, %s)",
(stored_token.get("phone"), hashed_login, deviceType, deviceName, "Epstein Island", int(time.time()),) (stored_token.get("phone"), hashed_login, deviceType, deviceName, "Epstein Island",
int(time.time()),)
) )
# Аватарка с биографией # Аватарка с биографией
photoId = None if not account.get("avatar_id") else int(account.get("avatar_id")) photo_id = None if not account.get("avatar_id") else int(account.get("avatar_id"))
avatar_url = None if not photoId else self.config.avatar_base_url + photoId avatar_url = None if not photo_id else self.config.avatar_base_url + photo_id
description = None if not account.get("description") else account.get("description") description = None if not account.get("description") else account.get("description")
# Собираем данные пакета # Собираем данные пакета
@ -271,7 +284,7 @@ class Processors:
id=account.get("id"), id=account.get("id"),
phone=int(account.get("phone")), phone=int(account.get("phone")),
avatarUrl=avatar_url, avatarUrl=avatar_url,
photoId=photoId, photoId=photo_id,
updateTime=int(account.get("updatetime")), updateTime=int(account.get("updatetime")),
firstName=account.get("firstname"), firstName=account.get("firstname"),
lastName=account.get("lastname"), lastName=account.get("lastname"),