Upload folder using huggingface_hub
Browse files- .dockerignore +7 -0
- .env.example +21 -0
- Dockerfile +15 -0
- README.md +115 -6
- app.py +44 -0
- bot.py +299 -0
- docker-compose.yml +7 -0
- requirements.txt +4 -0
- telegram-api-proxy-worker.js +87 -0
.dockerignore
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.py[co]
|
| 3 |
+
*.log
|
| 4 |
+
.env
|
| 5 |
+
.venv/
|
| 6 |
+
.git
|
| 7 |
+
.gitignore
|
.env.example
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Telegram bot token obtained from BotFather
|
| 2 |
+
BOT_TOKEN=123456789:ABCDEF-your-token
|
| 3 |
+
|
| 4 |
+
# Custom Telegram API URL (required for Hugging Face deployment with Cloudflare Worker)
|
| 5 |
+
# Example: TELEGRAM_API_URL=https://your-worker.your-subdomain.workers.dev/telegram-api
|
| 6 |
+
TELEGRAM_API_URL=
|
| 7 |
+
|
| 8 |
+
# Backend endpoint that will receive the normalized proxy payload (optional)
|
| 9 |
+
BACKEND_URL=https://your-backend.example.com/api/proxies
|
| 10 |
+
|
| 11 |
+
# Optional bearer token sent as Authorization header to the backend
|
| 12 |
+
BACKEND_TOKEN=change-me
|
| 13 |
+
|
| 14 |
+
# Optional overrides for testing behaviour
|
| 15 |
+
# PROXY_TEST_URL=https://api.ipify.org?format=json
|
| 16 |
+
# PROXY_TEST_TIMEOUT=10
|
| 17 |
+
# MAX_CONCURRENT_TESTS=5
|
| 18 |
+
|
| 19 |
+
# Comma or semicolon separated Telegram user IDs allowed to use the bot
|
| 20 |
+
# Example: ALLOWED_USER_IDS=123456789,987654321
|
| 21 |
+
ALLOWED_USER_IDS=
|
Dockerfile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
ENV PYTHONDONTWRITEBYTECODE=1 \
|
| 4 |
+
PYTHONUNBUFFERED=1
|
| 5 |
+
|
| 6 |
+
WORKDIR /app
|
| 7 |
+
|
| 8 |
+
COPY requirements.txt ./
|
| 9 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 10 |
+
|
| 11 |
+
COPY . ./
|
| 12 |
+
|
| 13 |
+
EXPOSE 7860
|
| 14 |
+
|
| 15 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
|
@@ -1,10 +1,119 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji: 😻
|
| 4 |
-
colorFrom: gray
|
| 5 |
-
colorTo: gray
|
| 6 |
sdk: docker
|
| 7 |
-
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Telegram SOCKS5 Proxy Bot
|
|
|
|
|
|
|
|
|
|
| 3 |
sdk: docker
|
| 4 |
+
app_port: 7860
|
| 5 |
---
|
| 6 |
|
| 7 |
+
# Telegram SOCKS5 Proxy Bot
|
| 8 |
+
|
| 9 |
+
This bot normalizes SOCKS5 proxy strings sent via Telegram, forwards them to an optional backend, and tests whether the proxies are working.
|
| 10 |
+
|
| 11 |
+
## Telegram setup
|
| 12 |
+
|
| 13 |
+
1. 在 Telegram 中寻找 @BotFather,发送 `/newbot` 创建机器人并获取 `BOT_TOKEN`。
|
| 14 |
+
2. 使用 @userinfobot 或内置方法记录你的 Telegram 用户 ID。
|
| 15 |
+
3. 在 `.env` 中填写 `BOT_TOKEN`,并将允许访问的用户 ID 列在 `ALLOWED_USER_IDS`(逗号或分号分隔)。若留空则任何人都可以使用机器人。
|
| 16 |
+
|
| 17 |
+
## Prerequisites
|
| 18 |
+
|
| 19 |
+
- Python 3.10+
|
| 20 |
+
- A Telegram bot token from @BotFather
|
| 21 |
+
|
| 22 |
+
## Installation
|
| 23 |
+
|
| 24 |
+
```bash
|
| 25 |
+
python3 -m venv .venv
|
| 26 |
+
source .venv/bin/activate
|
| 27 |
+
pip install -r requirements.txt
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
Copy the environment template and fill in the values you need:
|
| 31 |
+
|
| 32 |
+
```bash
|
| 33 |
+
cp .env.example .env
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
Edit `.env` and add at least your `BOT_TOKEN`. Optionally set:
|
| 37 |
+
|
| 38 |
+
- `BACKEND_URL` and `BACKEND_TOKEN` if you want to push normalized proxies to an HTTP endpoint
|
| 39 |
+
- `PROXY_TEST_URL`, `PROXY_TEST_TIMEOUT`, and `MAX_CONCURRENT_TESTS` to tune validation behaviour
|
| 40 |
+
- `ALLOWED_USER_IDS` to restrict the bot to specific Telegram accounts
|
| 41 |
+
|
| 42 |
+
## Running the bot
|
| 43 |
+
|
| 44 |
+
```bash
|
| 45 |
+
python bot.py
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
The bot uses long polling by default. You can containerize or run the script under a process supervisor if you need persistent availability.
|
| 49 |
+
|
| 50 |
+
## Docker
|
| 51 |
+
|
| 52 |
+
Build and run directly:
|
| 53 |
+
|
| 54 |
+
```bash
|
| 55 |
+
docker build -t socks5-bot .
|
| 56 |
+
docker run --rm --env-file .env socks5-bot
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
Or keep it running with `docker-compose` (uses `docker-compose.yml` in this repo):
|
| 60 |
+
|
| 61 |
+
```bash
|
| 62 |
+
docker-compose up --build -d
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
Stop the compose stack when needed:
|
| 66 |
+
|
| 67 |
+
```bash
|
| 68 |
+
docker-compose down
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
## Deploying to Hugging Face Spaces
|
| 72 |
+
|
| 73 |
+
Hugging Face 仅开放 7860 端口,因此空间需要以 Docker 方式运行:
|
| 74 |
+
|
| 75 |
+
1. 在 Hugging Face 新建 Space,选择 **Docker** 模式,并将此仓库推送过去。
|
| 76 |
+
2. 在 Space 的 *Settings → Secrets* 中配置 `.env` 中用到的环境变量(例如 `BOT_TOKEN`、`BACKEND_URL` 等)。
|
| 77 |
+
3. 该仓库的 `Dockerfile` 会启动 `uvicorn app:app --port 7860`,并暴露一个 `/` 和 `/healthz` 健康检查端点以满足 Hugging Face 的存活检测,Telegram 机器人在后台轮询。
|
| 78 |
+
4. 如需更新配置,修改 Secrets 后在 *Actions → Restart* 重启 Space 即可。
|
| 79 |
+
|
| 80 |
+
## Using the bot
|
| 81 |
+
|
| 82 |
+
- Send one proxy per line in any of these formats:
|
| 83 |
+
- `user:password@host:port`
|
| 84 |
+
- `host:port:user:password`
|
| 85 |
+
- Lines with spaces, commas, pipes, or tabs between elements are also normalized
|
| 86 |
+
- The bot replies with the proxies it accepted, the test result for each proxy, any rejected lines, and optional backend feedback.
|
| 87 |
+
|
| 88 |
+
## Backend payload
|
| 89 |
+
|
| 90 |
+
When `BACKEND_URL` is set, the bot `POST`s JSON like the following:
|
| 91 |
+
|
| 92 |
+
```json
|
| 93 |
+
{
|
| 94 |
+
"proxies": [
|
| 95 |
+
"user:password@host:1080"
|
| 96 |
+
]
|
| 97 |
+
}
|
| 98 |
+
```
|
| 99 |
+
|
| 100 |
+
If `BACKEND_TOKEN` is set it is sent as a Bearer token in the `Authorization` header.
|
| 101 |
+
|
| 102 |
+
## Notes
|
| 103 |
+
|
| 104 |
+
- The proxy test makes a GET request to the configured test URL using the SOCKS5 proxy. Pick an endpoint that can be reached through the proxies you receive.
|
| 105 |
+
- Be mindful of the rate limits imposed by Telegram and the remote test endpoint.
|
| 106 |
+
|
| 107 |
+
## Solving Network Restrictions (e.g., on Hugging Face)
|
| 108 |
+
|
| 109 |
+
If you're deploying on platforms like Hugging Face Spaces that block access to `api.telegram.org`, you can use the Cloudflare Worker proxy solution:
|
| 110 |
+
|
| 111 |
+
1. Create a Cloudflare account at https://www.cloudflare.com/
|
| 112 |
+
2. Create a new Worker in your Cloudflare dashboard
|
| 113 |
+
3. Copy the content from `telegram-api-proxy-worker.js` and paste it into your Worker
|
| 114 |
+
4. Deploy the Worker and note your Worker URL (e.g., `https://your-worker.your-subdomain.workers.dev`)
|
| 115 |
+
5. Update your environment variables to include `TELEGRAM_API_URL` pointing to your worker (append `/telegram-api` to the URL):
|
| 116 |
+
- `TELEGRAM_API_URL=https://your-worker.your-subdomain.workers.dev/telegram-api`
|
| 117 |
+
6. The Docker setup already uses the patched version that supports custom API URLs
|
| 118 |
+
|
| 119 |
+
This solution allows your bot to work even when direct access to the Telegram API is blocked.
|
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from contextlib import asynccontextmanager, suppress
|
| 2 |
+
|
| 3 |
+
from fastapi import FastAPI, Response
|
| 4 |
+
|
| 5 |
+
from bot import build_application
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
telegram_application = None
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
@asynccontextmanager
|
| 12 |
+
async def lifespan(app: FastAPI):
|
| 13 |
+
# Startup
|
| 14 |
+
global telegram_application
|
| 15 |
+
telegram_application = build_application()
|
| 16 |
+
await telegram_application.initialize()
|
| 17 |
+
await telegram_application.start()
|
| 18 |
+
if telegram_application.updater:
|
| 19 |
+
await telegram_application.updater.start_polling()
|
| 20 |
+
|
| 21 |
+
yield
|
| 22 |
+
|
| 23 |
+
# Shutdown
|
| 24 |
+
if telegram_application:
|
| 25 |
+
if telegram_application.updater:
|
| 26 |
+
with suppress(Exception):
|
| 27 |
+
await telegram_application.updater.stop()
|
| 28 |
+
with suppress(Exception):
|
| 29 |
+
await telegram_application.stop()
|
| 30 |
+
with suppress(Exception):
|
| 31 |
+
await telegram_application.shutdown()
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
app = FastAPI(title="Telegram Credit Card Generator Bot", lifespan=lifespan)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
@app.get("/")
|
| 38 |
+
async def root() -> dict[str, str]:
|
| 39 |
+
return {"status": "ok"}
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
@app.get("/healthz")
|
| 43 |
+
async def health() -> Response:
|
| 44 |
+
return Response(content="ok", media_type="text/plain")
|
bot.py
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import asyncio
|
| 4 |
+
import logging
|
| 5 |
+
import os
|
| 6 |
+
import re
|
| 7 |
+
import random
|
| 8 |
+
from typing import Optional
|
| 9 |
+
|
| 10 |
+
from dotenv import load_dotenv
|
| 11 |
+
from telegram import Update
|
| 12 |
+
from telegram.ext import (
|
| 13 |
+
ApplicationBuilder,
|
| 14 |
+
CommandHandler,
|
| 15 |
+
ContextTypes,
|
| 16 |
+
MessageHandler,
|
| 17 |
+
filters,
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
logging.basicConfig(
|
| 21 |
+
format="%(asctime)s %(levelname)s %(name)s %(message)s",
|
| 22 |
+
level=logging.INFO,
|
| 23 |
+
)
|
| 24 |
+
logger = logging.getLogger(__name__)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# 信用卡生成相关函数
|
| 28 |
+
def luhn_checksum(card_number: str) -> int:
|
| 29 |
+
"""计算Luhn校验和"""
|
| 30 |
+
def digits_of(n):
|
| 31 |
+
return [int(d) for d in str(n)]
|
| 32 |
+
|
| 33 |
+
digits = digits_of(card_number)
|
| 34 |
+
odd_digits = digits[-1::-2] # 从右到左,奇数位
|
| 35 |
+
even_digits = digits[-2::-2] # 从右到左,偶数位
|
| 36 |
+
|
| 37 |
+
checksum = 0
|
| 38 |
+
checksum += sum(odd_digits)
|
| 39 |
+
|
| 40 |
+
for d in even_digits:
|
| 41 |
+
checksum += sum(digits_of(d * 2))
|
| 42 |
+
|
| 43 |
+
return checksum % 10
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def is_luhn_valid(card_number: str) -> bool:
|
| 47 |
+
"""验证卡号是否符合Luhn算法"""
|
| 48 |
+
return luhn_checksum(card_number) == 0
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def generate_luhn_valid_number(prefix: str, length: int = 16) -> str:
|
| 52 |
+
"""生成符合Luhn算法的卡号"""
|
| 53 |
+
# 确保前缀是数字字符串
|
| 54 |
+
prefix = str(prefix)
|
| 55 |
+
|
| 56 |
+
# 如果前缀已经达到指定长度,直接验证并返回
|
| 57 |
+
if len(prefix) >= length:
|
| 58 |
+
if is_luhn_valid(prefix[:length]):
|
| 59 |
+
return prefix[:length]
|
| 60 |
+
else:
|
| 61 |
+
raise ValueError("前缀长度已达到但不符合Luhn算法")
|
| 62 |
+
|
| 63 |
+
# 生成随机数字填充到长度-1(最后一位是校验位)
|
| 64 |
+
remaining_length = length - len(prefix) - 1
|
| 65 |
+
random_digits = ''.join([str(random.randint(0, 9)) for _ in range(remaining_length)])
|
| 66 |
+
|
| 67 |
+
# 组合前缀、随机数字和临时校验位0
|
| 68 |
+
card_number = prefix + random_digits + '0'
|
| 69 |
+
|
| 70 |
+
# 计算正确的校验位
|
| 71 |
+
checksum = luhn_checksum(card_number)
|
| 72 |
+
correct_check_digit = (10 - checksum) % 10
|
| 73 |
+
|
| 74 |
+
# 替换最后一位为正确的校验位
|
| 75 |
+
valid_card_number = card_number[:-1] + str(correct_check_digit)
|
| 76 |
+
|
| 77 |
+
return valid_card_number
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def parse_and_generate_card(input_string: str) -> dict:
|
| 81 |
+
"""解析输入字符串并生成完整的信用卡信息"""
|
| 82 |
+
# 更灵活的正则表达式匹配:前缀|月份|年份|CVV
|
| 83 |
+
# 前缀可以包含x,CVV部分可以包含x或省略
|
| 84 |
+
pattern = r'^(\d+x*)\|(\d{1,2})\|(\d{2,4})(?:\|(x*))?$'
|
| 85 |
+
match = re.match(pattern, input_string)
|
| 86 |
+
|
| 87 |
+
if not match:
|
| 88 |
+
raise ValueError("输入格式不正确,应为:前缀|月份|年份|CVV (CVV可选)")
|
| 89 |
+
|
| 90 |
+
prefix_part = match.group(1)
|
| 91 |
+
month = match.group(2)
|
| 92 |
+
year = match.group(3)
|
| 93 |
+
cvv_part = match.group(4) if match.group(4) else 'xxx' # 默认CVV长度
|
| 94 |
+
|
| 95 |
+
# 分离前缀中的数字和x
|
| 96 |
+
prefix_match = re.match(r'^(\d+)(x*)$', prefix_part)
|
| 97 |
+
if not prefix_match:
|
| 98 |
+
raise ValueError("前缀格式不正确,应为数字后跟x")
|
| 99 |
+
|
| 100 |
+
prefix = prefix_match.group(1)
|
| 101 |
+
|
| 102 |
+
# 确定卡号总长度(通常是16位,但Amex是15位)
|
| 103 |
+
if prefix.startswith('34') or prefix.startswith('37'):
|
| 104 |
+
total_length = 15 # American Express
|
| 105 |
+
else:
|
| 106 |
+
total_length = 16 # Visa, Mastercard, Discover等
|
| 107 |
+
|
| 108 |
+
# 生成完整的卡号
|
| 109 |
+
full_card_number = generate_luhn_valid_number(prefix, total_length)
|
| 110 |
+
|
| 111 |
+
# 格式化月份和年份
|
| 112 |
+
month = month.zfill(2)
|
| 113 |
+
if len(year) == 2:
|
| 114 |
+
# 假设是20xx年
|
| 115 |
+
year = '20' + year
|
| 116 |
+
|
| 117 |
+
# 生成随机CVV(通常是3位,Amex是4位)
|
| 118 |
+
if prefix.startswith('34') or prefix.startswith('37'):
|
| 119 |
+
cvv_length = 4
|
| 120 |
+
else:
|
| 121 |
+
cvv_length = 3
|
| 122 |
+
|
| 123 |
+
cvv = ''.join([str(random.randint(0, 9)) for _ in range(cvv_length)])
|
| 124 |
+
|
| 125 |
+
return {
|
| 126 |
+
'card_number': full_card_number,
|
| 127 |
+
'expiry_month': month,
|
| 128 |
+
'expiry_year': year,
|
| 129 |
+
'cvv': cvv,
|
| 130 |
+
'formatted': f"{full_card_number}|{month}|{year}|{cvv}"
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def is_credit_card_format(text: str) -> bool:
|
| 135 |
+
"""检测文本是否为信用卡格式"""
|
| 136 |
+
# 检测格式:数字+x|数字|数字|可选的x
|
| 137 |
+
pattern = r'^\d+x*\|\d{1,2}\|\d{2,4}(?:\|x*)?$'
|
| 138 |
+
return bool(re.match(pattern, text.strip()))
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def generate_multiple_cards(input_string: str, count: int = 10) -> list:
|
| 142 |
+
"""根据输入格式生成多张信用卡"""
|
| 143 |
+
cards = []
|
| 144 |
+
for _ in range(count):
|
| 145 |
+
try:
|
| 146 |
+
card_info = parse_and_generate_card(input_string)
|
| 147 |
+
cards.append(card_info)
|
| 148 |
+
except ValueError as e:
|
| 149 |
+
logger.warning(f"生成信用卡失败: {e}")
|
| 150 |
+
continue
|
| 151 |
+
return cards
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
def _parse_allowed_users(raw_value: str) -> set[int]:
|
| 155 |
+
"""解析允许的用户ID列表"""
|
| 156 |
+
if not raw_value.strip():
|
| 157 |
+
return set()
|
| 158 |
+
allowed = set()
|
| 159 |
+
for token in raw_value.replace(";", ",").split(","):
|
| 160 |
+
token = token.strip()
|
| 161 |
+
if not token:
|
| 162 |
+
continue
|
| 163 |
+
try:
|
| 164 |
+
allowed.add(int(token))
|
| 165 |
+
except ValueError:
|
| 166 |
+
logger.warning("Ignoring invalid user id token: %s", token)
|
| 167 |
+
return allowed
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
| 171 |
+
"""处理/start命令"""
|
| 172 |
+
if not await _ensure_authorized(update, context):
|
| 173 |
+
return
|
| 174 |
+
|
| 175 |
+
await update.message.reply_text(
|
| 176 |
+
"欢迎使用信用卡生成机器人!\n\n"
|
| 177 |
+
"请发送信用卡格式,我将为您生成10张随机信用卡。\n"
|
| 178 |
+
"格式示例:\n"
|
| 179 |
+
"• 440393xxxxx|3|27|xxx\n"
|
| 180 |
+
"• 424242|11|28\n"
|
| 181 |
+
"• 34xxxxx|6|25|xxxx (American Express)\n\n"
|
| 182 |
+
"格式说明:前缀|月份|年份|CVV(CVV可选)"
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
async def handle_text(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
| 187 |
+
"""处理文本消息"""
|
| 188 |
+
assert update.message is not None
|
| 189 |
+
if not await _ensure_authorized(update, context):
|
| 190 |
+
return
|
| 191 |
+
|
| 192 |
+
text = update.message.text or ""
|
| 193 |
+
|
| 194 |
+
# 检查是否为信用卡格式
|
| 195 |
+
if is_credit_card_format(text):
|
| 196 |
+
await handle_credit_card_request(update, text)
|
| 197 |
+
else:
|
| 198 |
+
await update.message.reply_text(
|
| 199 |
+
"格式不正确!请使用以下格式:\n"
|
| 200 |
+
"• 440393xxxxx|3|27|xxx\n"
|
| 201 |
+
"• 424242|11|28\n\n"
|
| 202 |
+
"格式说明:前缀|月份|年份|CVV(CVV可选)"
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
async def handle_credit_card_request(update: Update, input_format: str) -> None:
|
| 207 |
+
"""处理信用卡生成请求"""
|
| 208 |
+
try:
|
| 209 |
+
# 生成10张信用卡
|
| 210 |
+
cards = generate_multiple_cards(input_format, 10)
|
| 211 |
+
|
| 212 |
+
if not cards:
|
| 213 |
+
await update.message.reply_text("生成信用卡失败,请检查输入格式。")
|
| 214 |
+
return
|
| 215 |
+
|
| 216 |
+
# 格式化输出
|
| 217 |
+
response_lines = [f"🎉 成功生成 {len(cards)} 张信用卡:\n"]
|
| 218 |
+
|
| 219 |
+
for i, card in enumerate(cards, 1):
|
| 220 |
+
response_lines.append(
|
| 221 |
+
f"{i}. {card['formatted']}"
|
| 222 |
+
)
|
| 223 |
+
|
| 224 |
+
# 分批发送消息,避免消息过长
|
| 225 |
+
batch_size = 5
|
| 226 |
+
for i in range(0, len(response_lines), batch_size * 2): # 每批包含标题+5张卡
|
| 227 |
+
batch = response_lines[i:i + batch_size * 2]
|
| 228 |
+
await update.message.reply_text("\n".join(batch))
|
| 229 |
+
|
| 230 |
+
except Exception as e:
|
| 231 |
+
logger.exception("处理信用卡请求时出错")
|
| 232 |
+
await update.message.reply_text(f"生成信用卡时出错:{str(e)}")
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
def build_application():
|
| 236 |
+
"""构建Telegram应用"""
|
| 237 |
+
load_dotenv()
|
| 238 |
+
|
| 239 |
+
bot_token = os.getenv("BOT_TOKEN")
|
| 240 |
+
if not bot_token:
|
| 241 |
+
raise RuntimeError("BOT_TOKEN is not set")
|
| 242 |
+
|
| 243 |
+
# Create the application builder
|
| 244 |
+
application_builder = ApplicationBuilder().token(bot_token)
|
| 245 |
+
|
| 246 |
+
# Check for custom API URL (for proxy)
|
| 247 |
+
custom_api_url = os.getenv("TELEGRAM_API_URL", "").strip()
|
| 248 |
+
if custom_api_url:
|
| 249 |
+
logger.info(f"Using custom Telegram API URL: {custom_api_url}")
|
| 250 |
+
api_base = f"{custom_api_url}/bot"
|
| 251 |
+
file_base = f"{custom_api_url}/file/bot"
|
| 252 |
+
logger.info(f"Setting API base to: {api_base}")
|
| 253 |
+
logger.info(f"Setting file base to: {file_base}")
|
| 254 |
+
application_builder = application_builder.base_url(api_base).base_file_url(file_base)
|
| 255 |
+
else:
|
| 256 |
+
logger.info("Using default Telegram API URL")
|
| 257 |
+
|
| 258 |
+
allowed_ids = _parse_allowed_users(os.getenv("ALLOWED_USER_IDS", ""))
|
| 259 |
+
|
| 260 |
+
application = application_builder.build()
|
| 261 |
+
|
| 262 |
+
# Store the custom API URL in the application's bot_data
|
| 263 |
+
if custom_api_url:
|
| 264 |
+
application.bot_data["custom_api_url"] = custom_api_url
|
| 265 |
+
|
| 266 |
+
application.bot_data["allowed_ids"] = allowed_ids
|
| 267 |
+
|
| 268 |
+
application.add_handler(CommandHandler("start", start))
|
| 269 |
+
application.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), handle_text))
|
| 270 |
+
|
| 271 |
+
return application
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
def main() -> None:
|
| 275 |
+
"""主函数"""
|
| 276 |
+
application = build_application()
|
| 277 |
+
application.run_polling()
|
| 278 |
+
|
| 279 |
+
|
| 280 |
+
if __name__ == "__main__":
|
| 281 |
+
main()
|
| 282 |
+
|
| 283 |
+
|
| 284 |
+
async def _ensure_authorized(update: Update, context: ContextTypes.DEFAULT_TYPE) -> bool:
|
| 285 |
+
"""确保用户有权限使用机器人"""
|
| 286 |
+
allowed_ids: set[int] = context.application.bot_data.get("allowed_ids", set())
|
| 287 |
+
if not allowed_ids:
|
| 288 |
+
return True
|
| 289 |
+
|
| 290 |
+
user = update.effective_user
|
| 291 |
+
if user and user.id in allowed_ids:
|
| 292 |
+
return True
|
| 293 |
+
|
| 294 |
+
message = update.effective_message
|
| 295 |
+
if message:
|
| 296 |
+
await message.reply_text("您没有权限使用此机器人。")
|
| 297 |
+
|
| 298 |
+
logger.info("Unauthorized access attempt by user id %s", user.id if user else "unknown")
|
| 299 |
+
return False
|
docker-compose.yml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version: "3.9"
|
| 2 |
+
services:
|
| 3 |
+
bot:
|
| 4 |
+
build: .
|
| 5 |
+
restart: unless-stopped
|
| 6 |
+
env_file:
|
| 7 |
+
- .env
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
python-telegram-bot>=20.8,<20.9
|
| 2 |
+
python-dotenv>=1.0.0
|
| 3 |
+
fastapi>=0.110
|
| 4 |
+
uvicorn[standard]>=0.27
|
telegram-api-proxy-worker.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Cloudflare Worker script to proxy Telegram API requests
|
| 2 |
+
// This script intercepts requests to the Telegram Bot API and forwards them
|
| 3 |
+
// allowing your bot to work even when direct access to api.telegram.org is blocked
|
| 4 |
+
|
| 5 |
+
export default {
|
| 6 |
+
async fetch(request, env, ctx) {
|
| 7 |
+
// Get the original request URL
|
| 8 |
+
const originalUrl = new URL(request.url);
|
| 9 |
+
|
| 10 |
+
// Handle CORS preflight requests
|
| 11 |
+
if (request.method === 'OPTIONS') {
|
| 12 |
+
return new Response(null, {
|
| 13 |
+
headers: {
|
| 14 |
+
'Access-Control-Allow-Origin': '*',
|
| 15 |
+
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
|
| 16 |
+
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
| 17 |
+
},
|
| 18 |
+
});
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
// Check if this is a request to our worker
|
| 22 |
+
// If the path starts with /telegram-api, forward to Telegram API
|
| 23 |
+
if (originalUrl.pathname.startsWith('/telegram-api')) {
|
| 24 |
+
// Extract the path after /telegram-api
|
| 25 |
+
// The full path should be like /telegram-api/bot{TOKEN}/{METHOD}
|
| 26 |
+
const telegramApiPath = originalUrl.pathname.replace('/telegram-api', '');
|
| 27 |
+
|
| 28 |
+
// Construct the target URL for Telegram API
|
| 29 |
+
const telegramApiUrl = `https://api.telegram.org${telegramApiPath}`;
|
| 30 |
+
|
| 31 |
+
// Clone the original request to modify it
|
| 32 |
+
// Remove the Cloudflare-specific headers that might cause issues
|
| 33 |
+
const newHeaders = new Headers(request.headers);
|
| 34 |
+
newHeaders.delete('cf-ray');
|
| 35 |
+
newHeaders.delete('cf-connecting-ip');
|
| 36 |
+
newHeaders.delete('x-forwarded-for');
|
| 37 |
+
|
| 38 |
+
const modifiedRequest = new Request(telegramApiUrl, {
|
| 39 |
+
method: request.method,
|
| 40 |
+
headers: newHeaders,
|
| 41 |
+
body: request.body,
|
| 42 |
+
redirect: 'follow'
|
| 43 |
+
});
|
| 44 |
+
|
| 45 |
+
try {
|
| 46 |
+
// Send request to Telegram API
|
| 47 |
+
const response = await fetch(modifiedRequest);
|
| 48 |
+
|
| 49 |
+
// Return the response from Telegram API with CORS headers
|
| 50 |
+
const modifiedResponse = new Response(response.body, {
|
| 51 |
+
status: response.status,
|
| 52 |
+
statusText: response.statusText,
|
| 53 |
+
headers: {
|
| 54 |
+
...response.headers,
|
| 55 |
+
'Access-Control-Allow-Origin': '*',
|
| 56 |
+
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
|
| 57 |
+
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
| 58 |
+
}
|
| 59 |
+
});
|
| 60 |
+
|
| 61 |
+
return modifiedResponse;
|
| 62 |
+
} catch (error) {
|
| 63 |
+
console.error('Proxy error:', error);
|
| 64 |
+
return new Response(JSON.stringify({
|
| 65 |
+
error: 'Proxy error',
|
| 66 |
+
details: error.message,
|
| 67 |
+
message: 'Failed to connect to Telegram API'
|
| 68 |
+
}), {
|
| 69 |
+
status: 502, // Bad Gateway
|
| 70 |
+
headers: {
|
| 71 |
+
'Content-Type': 'application/json',
|
| 72 |
+
'Access-Control-Allow-Origin': '*',
|
| 73 |
+
}
|
| 74 |
+
});
|
| 75 |
+
}
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
// If not a Telegram API request, return info about the proxy
|
| 79 |
+
return new Response('Telegram API Proxy Worker - Ready to forward requests to Telegram API', {
|
| 80 |
+
status: 200,
|
| 81 |
+
headers: {
|
| 82 |
+
'Content-Type': 'text/plain',
|
| 83 |
+
'Access-Control-Allow-Origin': '*',
|
| 84 |
+
}
|
| 85 |
+
});
|
| 86 |
+
}
|
| 87 |
+
};
|