Skip to content
This repository was archived by the owner on Apr 5, 2026. It is now read-only.

Commit f0af62c

Browse files
committed
chore: add session regeneration utility scripts
Helper scripts for managing Telegram sessions: - regenerate_ci_session.py: CI-only StringSession (API_ID 2834) - regenerate_tgp_session.py: tgp bot session (API_ID 4476303) - regenerate_all_sessions.py: both in one go - regenerate_session.py: generic session regeneration
1 parent 7be55da commit f0af62c

4 files changed

Lines changed: 197 additions & 0 deletions

File tree

tests/regenerate_all_sessions.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python3
2+
"""Re-create both tgp and CI sessions in a single login.
3+
4+
One phone code, two auth keys (different API IDs = separate keys).
5+
6+
Usage:
7+
brew services stop tgp
8+
python3 tests/regenerate_all_sessions.py
9+
brew services start tgp
10+
# Then set the CI secret as instructed
11+
"""
12+
13+
import asyncio
14+
import os
15+
import shutil
16+
17+
from telethon import TelegramClient
18+
from telethon.sessions import StringSession
19+
20+
TGP_API_ID = 4476303
21+
TGP_API_HASH = "7d5b2be781f2c7aef991211d8e536e86"
22+
TGP_SESSION_PATH = os.path.expanduser("~/Projects/tgp/sessions/asian")
23+
24+
CI_API_ID = 2834
25+
CI_API_HASH = "68875f756c9b437a8b916ca3de215815"
26+
27+
28+
async def main():
29+
# Step 1: Create tgp session (interactive login)
30+
print("=== Step 1: tgp session (API_ID 4476303) ===\n")
31+
for ext in [".session", ".session-journal"]:
32+
p = TGP_SESSION_PATH + ext
33+
if os.path.exists(p):
34+
os.remove(p)
35+
36+
tgp_client = TelegramClient(TGP_SESSION_PATH, TGP_API_ID, TGP_API_HASH)
37+
await tgp_client.start()
38+
me = await tgp_client.get_me()
39+
print(f"tgp OK: {me.first_name} (id={me.id})")
40+
await tgp_client.disconnect()
41+
42+
# Step 2: Create CI session (separate login — will ask for another code)
43+
print("\n=== Step 2: CI session (API_ID 2834) ===\n")
44+
ci_client = TelegramClient(StringSession(), CI_API_ID, CI_API_HASH)
45+
await ci_client.start()
46+
me = await ci_client.get_me()
47+
ss = ci_client.session.save()
48+
print(f"CI OK: {me.first_name} (id={me.id})")
49+
await ci_client.disconnect()
50+
51+
print(f"\n{'='*60}")
52+
print(f"tgp session saved to: {TGP_SESSION_PATH}.session")
53+
print(f"\nCI StringSession:")
54+
print(ss)
55+
print(f"\nNext steps:")
56+
print(f" 1. echo '{ss}' | gh secret set TG_STRING_SESSION --repo GetPageSpeed/MTProxy")
57+
print(f" 2. brew services start tgp")
58+
print(f" 3. Wait a minute, then trigger CI")
59+
60+
61+
asyncio.run(main())

tests/regenerate_ci_session.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python3
2+
"""Create a CI-only StringSession (API_ID 2834, separate from tgp's 4476303).
3+
4+
Run AFTER tgp is already running with its own session.
5+
6+
Usage:
7+
python3 tests/regenerate_ci_session.py
8+
# Then paste the output into:
9+
# gh secret set TG_STRING_SESSION --repo GetPageSpeed/MTProxy
10+
"""
11+
12+
import asyncio
13+
from telethon import TelegramClient
14+
from telethon.sessions import StringSession
15+
16+
API_ID = 2834
17+
API_HASH = "68875f756c9b437a8b916ca3de215815"
18+
19+
20+
async def main():
21+
client = TelegramClient(StringSession(), API_ID, API_HASH)
22+
await client.start()
23+
me = await client.get_me()
24+
ss = client.session.save()
25+
print(f"Authenticated as: {me.first_name} (id={me.id})")
26+
print(f"\nTG_STRING_SESSION={ss}")
27+
await client.disconnect()
28+
29+
30+
asyncio.run(main())

tests/regenerate_session.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python3
2+
"""Regenerate Telegram session after auth key revocation.
3+
4+
Interactive — you'll receive a login code in Telegram.
5+
Saves to both tgp session file and prints StringSession for GitHub.
6+
7+
Usage:
8+
python3 tests/regenerate_session.py
9+
"""
10+
11+
import asyncio
12+
import os
13+
import shutil
14+
15+
from telethon import TelegramClient
16+
from telethon.sessions import StringSession
17+
18+
API_ID = 2834
19+
API_HASH = "68875f756c9b437a8b916ca3de215815"
20+
21+
TGP_SESSION = os.path.expanduser("~/Projects/tgp/sessions/asian.session")
22+
23+
24+
async def main():
25+
# 1. Create fresh session via interactive login
26+
print("Creating new session (interactive login)...\n")
27+
client = TelegramClient(StringSession(), API_ID, API_HASH)
28+
await client.start()
29+
me = await client.get_me()
30+
print(f"\nAuthenticated as: {me.first_name} (id={me.id})")
31+
32+
# 2. Print StringSession for GitHub secret
33+
session_str = client.session.save()
34+
print(f"\n=== StringSession (for GitHub) ===")
35+
print(session_str)
36+
37+
# 3. Save as SQLite session for tgp
38+
print(f"\n=== Saving tgp session ===")
39+
if os.path.exists(TGP_SESSION):
40+
backup = TGP_SESSION + ".revoked"
41+
shutil.move(TGP_SESSION, backup)
42+
print(f"Backed up revoked session to {backup}")
43+
44+
file_client = TelegramClient(TGP_SESSION.replace(".session", ""), API_ID, API_HASH)
45+
file_client.session.set_dc(
46+
client.session.dc_id,
47+
client.session.server_address,
48+
client.session.port,
49+
)
50+
file_client.session.auth_key = client.session.auth_key
51+
file_client.session.save()
52+
print(f"Saved to {TGP_SESSION}")
53+
54+
await client.disconnect()
55+
56+
# 4. Instructions
57+
print(f"\nNext steps:")
58+
print(f" 1. gh secret set TG_STRING_SESSION --repo GetPageSpeed/MTProxy")
59+
print(f" (paste the string above)")
60+
print(f" 2. brew services restart tgp")
61+
print(f" 3. Re-run CI: gh run rerun 23658850397 --repo GetPageSpeed/MTProxy --failed")
62+
63+
64+
asyncio.run(main())

tests/regenerate_tgp_session.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python3
2+
"""Create a SEPARATE auth key for tgp (using tgp's own API ID).
3+
4+
This prevents auth key conflicts between tgp (local) and CI (GitHub).
5+
The CI StringSession uses API_ID 2834, tgp uses 4476303 — different
6+
API IDs produce different auth keys for the same phone number.
7+
8+
Usage:
9+
brew services stop tgp
10+
python3 tests/regenerate_tgp_session.py
11+
brew services start tgp
12+
"""
13+
14+
import asyncio
15+
import os
16+
17+
from telethon import TelegramClient
18+
19+
# tgp's own API credentials (from tgp main.py)
20+
API_ID = 4476303
21+
API_HASH = "7d5b2be781f2c7aef991211d8e536e86"
22+
SESSION_PATH = os.path.expanduser("~/Projects/tgp/sessions/asian")
23+
24+
25+
async def main():
26+
# Remove broken session
27+
for ext in [".session", ".session-journal"]:
28+
p = SESSION_PATH + ext
29+
if os.path.exists(p):
30+
os.remove(p)
31+
print(f"Removed {p}")
32+
33+
client = TelegramClient(SESSION_PATH, API_ID, API_HASH)
34+
await client.start()
35+
me = await client.get_me()
36+
print(f"\ntgp session created: {me.first_name} (id={me.id})")
37+
print(f"Saved to {SESSION_PATH}.session")
38+
print(f"\nNow run: brew services start tgp")
39+
await client.disconnect()
40+
41+
42+
asyncio.run(main())

0 commit comments

Comments
 (0)