This commit is contained in:
xtaodada 2023-01-27 20:02:46 +08:00
parent 7217de89f7
commit 5d1d7179fb
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
5 changed files with 69 additions and 0 deletions

2
.gitignore vendored
View File

@ -127,3 +127,5 @@ dmypy.json
# Pyre type checker
.pyre/
.idea/

32
main.py Normal file
View File

@ -0,0 +1,32 @@
import uuid
from fastapi import FastAPI
from starlette.responses import RedirectResponse
from utils import gen_url, get_auth
app = FastAPI()
@app.get('/gen')
async def gen(
*,
username: str,
host: str,
back_host: str,
):
session = str(uuid.uuid4())
return RedirectResponse(gen_url(username, host, back_host, session))
@app.get("/{username}/{host}")
async def back_to_telegram(
*,
username: str,
host: str,
session: str,
):
data = await get_auth(host, session)
if data.get("ok", False):
return RedirectResponse(f"https://t.me/{username}?start={data['token']}")
else:
return data

4
requirements.txt Normal file
View File

@ -0,0 +1,4 @@
httpx==0.23.3
fastapi==0.89.1
starlette==0.23.1
uvicorn==0.20.0

27
utils.py Normal file
View File

@ -0,0 +1,27 @@
import urllib.parse
from httpx import AsyncClient
client = AsyncClient()
async def get_auth(host: str, session: str):
url = f"https://{host}/api/miauth/{session}/check"
response = await client.post(url)
return response.json()
def callback_url(username: str, host: str, back_host: str):
return f"https://{back_host}/{username}/{host}"
def gen_url(username: str, host: str, back_host: str, session: str):
return f"https://{host}/miauth/{session}?name=Misskey2Telegram&callback=" \
f"{urllib.parse.quote(callback_url(username, host, back_host))}" \
f"&permission=read:account,write:account,read:blocks,write:blocks,read:drive," \
f"write:drive,read:favorites,write:favorites,read:following,write:following," \
f"read:messaging,write:messaging,read:mutes,write:mutes,write:notes,read:notifications," \
f"write:notifications,read:reactions,write:reactions,write:votes,read:pages," \
f"write:pages,write:page-likes,read:page-likes,read:user-groups,write:user-groups," \
f"read:channels,write:channels,read:gallery,write:gallery,read:gallery-likes," \
f"write:gallery-likes"

4
vercel.json Normal file
View File

@ -0,0 +1,4 @@
{
"builds": [{ "src": "main.py", "use": "@vercel/python" }],
"routes": [{ "src": "/(.*)", "dest": "main.py" }]
}