-
Notifications
You must be signed in to change notification settings - Fork 1
/
keeper.py
346 lines (296 loc) · 12.5 KB
/
keeper.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from telethon import events
from telethon.sync import TelegramClient
from telethon.tl.types import InputPhoneContact
from telethon.tl.functions.account import UpdateStatusRequest
from telethon.network.connection.tcpmtproxy import ConnectionTcpMTProxyRandomizedIntermediate
from telethon.errors import PhoneNumberInvalidError, SessionPasswordNeededError, FloodWaitError, PhoneNumberBannedError, PhoneNumberUnoccupiedError
from requests import ConnectionError
import re
import os
import sys
import json
import time
import random
import string
import logging
logging.basicConfig(level=logging.ERROR)
if not os.path.exists("sessions/"):
os.mkdir("sessions")
if not os.path.exists("anotherSessions"):
os.mkdir("anotherSessions")
if not os.path.exists("accounts.json"):
with open('accounts.json', 'w') as f:
json.dump({}, f, indent=4, sort_keys=True)
if not os.path.exists("proxies.txt"):
with open('proxies.txt', 'w') as f:
f.write("Please fill server ony by one and use this format => ip,port,secret")
if not os.path.exists("config.json"):
print("ERROR: Config file not found , please copy and edit config.example.json.")
config = json.load(open("config.json", "r", encoding="utf-8"))
proxies = list(map(lambda proxy:
(proxy.split(",")[0], int(
proxy.split(",")[1]), proxy.split(",")[2]),
[proxy for proxy in open("proxies.txt", "r").read().split("\n")[1:] if proxy != ""]))
accounts = json.load(open("accounts.json", "r", encoding="utf-8"))
tmpClient = {}
def randomString(stringLength):
letters = string.ascii_lowercase + string.digits
return str(''.join(random.choice(letters) for i in range(stringLength))).upper()
def saveConfig():
json.dump(accounts, open('accounts.json', 'w', encoding="utf-8"),
indent=4, sort_keys=True, ensure_ascii=False)
class Account:
def __init__(self, phone, twoVerify=None, nick=None):
self.phone = phone
self.twoVerify = twoVerify or ""
self.nick = nick or ""
def save(self):
if not hasattr(accounts, self.phone):
accounts[self.phone] = {}
accounts[self.phone]["phone"] = self.phone.replace('\t','').replace(' ','')
accounts[self.phone]["twoVerify"] = self.twoVerify
accounts[self.phone]["nick"] = self.nick
saveConfig()
def remove(self):
if hasattr(accounts, self.phone):
del accounts[self.phone]
saveConfig()
os.remove("sessions/"+self.phone+".session")
def editPhone(self, phone):
self.phone = phone
self.save()
def editTwoVerify(self, twoVerify):
self.twoVerify = twoVerify
self.save()
def editNick(self, nick):
self.nick = nick
self.save()
def generateClient(account, sessionsPath="sessions/"):
sessionName = sessionsPath + account.phone
if len(proxies) > 0:
proxy = random.choice(proxies)
print("INFO: Proxies list available , use proxy server.")
print("INFO: Proxy server %s:%d selected." % (proxy[0], proxy[1]))
client = TelegramClient(session=sessionName,
api_id=config["api"]["id"],
api_hash=config["api"]["hash"],
connection=ConnectionTcpMTProxyRandomizedIntermediate,
proxy=proxy)
else:
client = TelegramClient(session=sessionName,
api_id=config["api"]["id"],
api_hash=config["api"]["hash"])
return(client)
def generateAuthedClient(account, autoExit=True):
try:
client = generateClient(account)
print('INFO: Connecting to Telegram Server...')
client.connect()
client.sign_in(phone=account.phone)
if client.is_user_authorized():
print('INFO: Client login! Account still alive!')
return(client)
else:
print("ERROR: Client not login, Please add it and run this method again..")
if autoExit:
sys.exit()
return(False)
except ConnectionError:
return(generateAuthedClient(account, autoExit))
def getAccount(idOrPhone, add=False):
if len(idOrPhone) < 3:
i = 0
for k in accounts:
i = i + 1
if i == int(idOrPhone):
print("INFO : Phone %s selected." % (accounts[k]["phone"]))
return(
Account(accounts[k]["phone"], accounts[k]
["twoVerify"], accounts[k]["nick"])
)
elif idOrPhone in accounts:
print("INFO: Phone %s selected." % (accounts[idOrPhone]["phone"]))
return(
Account(accounts[idOrPhone]["phone"], accounts[idOrPhone]
["twoVerify"], accounts[idOrPhone]["nick"])
)
if add:
account = Account(idOrPhone)
return(account)
print("ERROR: No phone selected.")
sys.exit()
def main():
print()
print("Here are your accounts.")
print()
print("%-3s %-20s %-32s %-20s" % ("ID", "Phone", "Password", "Nickname"))
print("==============================================================================")
i = 0
for k in accounts:
i = i + 1
print("%-3s %-20s %-32s %-20s" %
(str(i), accounts[k]["phone"], accounts[k]["twoVerify"], accounts[k]["nick"]))
if len(sys.argv) == 2 and sys.argv[1] == "keepall":
method = "keepall"
else:
print("INPUT: Select method :")
print("METHOD: %-30s - %s" % ("Add", "Add account"))
print("METHOD: %-30s - %s" % ("Remove", "Remove account"))
print("METHOD: %-30s - %s" % ("Nick", "Set Nickname"))
print("METHOD: %-30s - %s" %
("2FA", "Change two verification code (Random)"))
print("METHOD: %-30s - %s" % ("Receive", "Receive messages"))
print("METHOD: %-30s - %s" %
("GenerateAnotherSessionFiles", "Copy another session files"))
print("METHOD: %-30s - %s" %
("Keep", "[Default] Send message to keep account alive."))
print("METHOD: %-30s - %s" %
("Exit", "Exit keeper."))
method = input("> ").lower()
if method == "add":
account = getAccount(input("INPUT: Phone Number : "), add=True)
client = generateClient(account)
print('INFO: Connecting to Telegram Server...')
client.connect()
try:
client.sign_in(phone=account.phone)
if not client.is_user_authorized():
sent = client.send_code_request(
phone=account.phone, force_sms=True)
code = input("INPUT: Verification Code : ")
try:
client.sign_in(account.phone, code)
except PhoneNumberUnoccupiedError:
client.sign_up(phone=account.phone,
first_name=".", last_name=".", code=code)
twoVerify = randomString(10)
client.edit_2fa(current_password=account.twoVerify)
account.editTwoVerify(twoVerify)
client.edit_2fa(new_password=twoVerify)
print("INFO: Two verification code set to => %s ." %
account.twoVerify)
except SessionPasswordNeededError:
code = input("INPUT: Two Step Verification Code : ")
account.editTwoVerify(code)
client.sign_in(password=code)
if client.is_user_authorized():
print("INFO: Client login , operation successfully completed!")
client.disconnect()
account.save()
else:
print("ERROR: Something wrong , please try again later.")
client.disconnect()
account.remove()
except:
print("ERROR: Something wrong , please try again later.")
client.disconnect()
account.remove()
import traceback
traceback.print_exc()
elif method == "remove":
account = getAccount(input("INPUT: Phone Number / ID : "))
if input("WARN: Do you really want to delete this account? [y/N]").lower() == "y":
account.remove()
else:
print("INFO: Operation cancle.")
elif method == "receive":
account = getAccount(input("INPUT: Phone Number / ID : "))
client = generateAuthedClient(account)
@client.on(events.NewMessage)
async def handler(event):
print("INFO: New message => " + event.raw_text)
client.start()
client.run_until_disconnected()
elif method == "2fa":
account = getAccount(input("INPUT: Phone Number / ID : "))
client = generateAuthedClient(account)
twoVerify = randomString(10)
client.edit_2fa(current_password=account.twoVerify)
account.editTwoVerify(twoVerify)
client.edit_2fa(new_password=twoVerify)
client.disconnect()
print("INFO: Two verification code set to => %s , operation successfully completed !" %
account.twoVerify)
elif method == "nick":
account = getAccount(input("INPUT: Phone Number / ID : "))
nick = input("INPUT: Nickname : ")
account.editNick(nick)
elif method == "generateanothersessionfiles":
account = input("INPUT: Phone Number / ID / all : ")
def copy(account):
account = getAccount(account)
client = generateAuthedClient(account)
newClient = generateClient(account, "anotherSessions/")
@client.on(events.NewMessage)
async def handler(event):
r = re.match(r"Login code: ([0-9]+).", event.raw_text)
if r:
try:
await newClient.sign_in(account.phone, r.group(1))
except SessionPasswordNeededError:
await newClient.sign_in(password=account.twoVerify)
print(
"INFO: Copy done! You can find it in anotherSessions directory.")
waitSec = random.randint(
config["keeper"]["antiFloodWaitRange"]["min"], config["keeper"]["antiFloodWaitRange"]["max"])
print("INFO: Wait for %d second for anti flood wait" % waitSec)
await time.sleep(waitSec)
await client.disconnect()
await newClient.disconnect()
client.start()
newClient.connect()
newClient.send_code_request(phone=account.phone)
print("INFO: Waiting verification code....")
client.run_until_disconnected()
if account == "all":
for k in accounts:
try:
copy(k)
except PhoneNumberBannedError:
print("WARN: Account has been gone.")
else:
copy(account)
elif method == "exit":
return("Exit")
elif method == "keep" or method == "" or method == "keepall":
account = input("INPUT: Phone Number / ID / all : ")
def keep(account):
print("INFO: This operation may take 1 minute to run.")
account = getAccount(account)
client = generateAuthedClient(account)
time.sleep(random.randint(3, 8))
client(UpdateStatusRequest(offline=False))
print("INFO: Set account status to Online.")
time.sleep(random.randint(5, 20))
client(UpdateStatusRequest(offline=True))
print("INFO: Set account status to Offline.")
time.sleep(random.randint(3, 8))
client.disconnect()
if account == "all":
for k in accounts:
try:
keep(k)
waitSec = random.randint(
config["keeper"]["antiFloodWaitRange"]["min"], config["keeper"]["antiFloodWaitRange"]["max"])
print("INFO: Wait for %d second for anti flood wait" % waitSec)
time.sleep(waitSec)
except PhoneNumberBannedError:
print("WARN: Account has been gone.")
else:
keep(account)
while True:
try:
if main() == "Exit":
break
except FloodWaitError:
print("WARN: FLOOD LIMIT")
pass
except KeyboardInterrupt:
print("WARN: Ctrl+C exiting..")
sys.exit()
except:
import traceback
traceback.print_exc()
pass