-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgenerate_session.py
182 lines (164 loc) · 6.21 KB
/
generate_session.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Copyright 2020-2023 nunopenim @github
# Copyright 2020-2023 prototype74 @github
#
# Licensed under the PEL (Penim Enterprises License), v1.0
#
# You may not use this file or any of the content within it, unless in
# compliance with the PE License
from sys import version_info
if (version_info.major, version_info.minor) < (3, 8):
print("Python v3.8+ is required to start String-Session-Generator! "
"Please update Python to v3.8 or newer "
"(current version: {}.{}.{}).".format(
version_info.major, version_info.minor, version_info.micro))
quit(1)
from os import environ, execle, name # noqa: E402
from platform import system # noqa: E402
from sys import executable, platform # noqa: E402
from telethon import version # noqa: E402
from telethon.sync import TelegramClient # noqa: E402
from telethon.sessions import StringSession # noqa: E402
IS_WINDOWS = (True if system().lower() == "windows" or
name == "nt" or platform.startswith("win") else False)
PY_EXEC = executable if " " not in executable else '"' + executable + '"'
if IS_WINDOWS:
try:
import colorama
colorama.init()
WIN_COLOR_ENABLED = True
except (ImportError, ModuleNotFoundError):
WIN_COLOR_ENABLED = False
except Exception as e:
WIN_COLOR_ENABLED = False
print(f"Exception: {e}")
class Colors:
GREEN = "\033[92m"
RED = "\033[91m"
YELLOW = "\033[93m"
CYAN = "\033[96m"
END = "\033[0m"
def setColorText(text: str, color: Colors) -> str:
if IS_WINDOWS and not WIN_COLOR_ENABLED:
return text # don't use ANSI codes
return f"{color}{text}{Colors.END}"
telethon_version = tuple(map(int, version.__version__.split(".")))
if telethon_version < (1, 32, 1):
print(setColorText(
"Telethon version 1.32.1+ is required! Please update "
"Telethon to v1.32.1 or newer "
f"(current version: {version.__version__}).", Colors.RED))
quit(1)
print(setColorText("Welcome to HyperUBot's String-Session-Generator!",
Colors.CYAN))
print("The String-Session-Generator generates a new String Session "
"in combination with your App api_id (API Key) and App api_hash "
"(API Hash) to allow HyperUBot to login into your account to "
"interact as 'user'bot. Please follow the steps below to obtain "
"your API Key and API Hash to finally generate a String Session:")
print()
print("1. Login to My Telegram: https://my.telegram.org")
print("2. Go to 'API development tools' and fill out the form")
print("3. Get your App api_id and App api_hash. You will need them for the "
"next step")
print()
print(setColorText("Note: Always remember not to share your App api_id and "
"App api_hash!", Colors.YELLOW))
print()
try:
while True:
con = input("Continue? (y/n): ")
if con.lower() in ("y", "yes"):
break
elif con.lower() in ("n", "no"):
raise KeyboardInterrupt
else:
print(setColorText("Invalid input. Try again...", Colors.YELLOW))
except KeyboardInterrupt:
print()
print("Exiting...")
quit()
print()
print("As we want to interact as user, the Telegram client will ask for "
"your phone number, don't worry it's only required for "
"user authorization and will not be send to anyone else. If "
"Two-factor authentication (2FA) is enabled it will also ask for the "
"password!")
print()
try:
while True:
con = input("Ready? (y/n): ")
if con.lower() in ("y", "yes"):
print()
break
elif con.lower() in ("n", "no"):
raise KeyboardInterrupt
else:
print(setColorText("Invalid input. Try again...", Colors.YELLOW))
print(setColorText("==== TELEGRAM CLIENT ====", Colors.CYAN))
while True:
API_KEY = input("Please enter your App api_id: ")
try:
API_KEY = int(API_KEY)
break
except ValueError:
print(setColorText("Invalid input. Try again...", Colors.YELLOW))
while True:
API_HASH = input("Please enter your App api_hash: ")
if len(API_HASH) == 32:
break
elif len(API_HASH) > 0:
print(setColorText("Invalid input. API Hash should have a "
"length of 32 characters!", Colors.YELLOW))
else:
print(setColorText("Invalid input. Try again...", Colors.YELLOW))
except KeyboardInterrupt:
print()
print("Exiting...")
quit()
try:
client = TelegramClient(StringSession(), API_KEY, API_HASH)
with client:
print("Alright there we go! This long string below is your new "
"String Session:\n\n")
print(setColorText(client.session.save(), Colors.GREEN))
print("\n\nPlease keep this string to a safe place and " +
setColorText("DON'T SHARE IT WITH ANYONE!!", Colors.RED))
print(setColorText("=========================", Colors.CYAN))
except KeyboardInterrupt:
print()
print("Exiting...")
quit()
except Exception as e:
print(setColorText(f"Unable to obtain a new string session: {e}",
Colors.RED))
quit(1)
start_scfg_updater = False
print("Next step is to start Secure-Config-Updater to store your new "
"String Session safe in a secured config file\n")
if IS_WINDOWS:
print("Run the following command to start the "
"Secure-Config-Updater: " +
setColorText("python update_secure_cfg.py", Colors.CYAN))
else:
try:
while True:
inp = input("Start Secure-Config-Updater? (y/n): ")
if inp.lower() in ("y", "yes"):
start_scfg_updater = True
break
elif inp.lower() in ("n", "no"):
break
else:
print(setColorText("Invalid input. Try again...",
Colors.YELLOW))
except KeyboardInterrupt:
print()
print("Exiting...")
if start_scfg_updater:
try:
tcmd = [PY_EXEC, "update_secure_cfg.py"]
execle(PY_EXEC, *tcmd, environ)
except Exception as e:
print(setColorText(f"Failed to start Secure-Config-Updater: {e}",
Colors.RED))
quit()