This repository has been archived by the owner on Nov 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
155 lines (130 loc) · 4.21 KB
/
main.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
#System Libs
import json
import time
#Custom Libs
import pyotp
import telepot
from telepot.loop import MessageLoop
#Config
import config
import FileWork as fw
class password():
'''
Your password store
'''
def __init__(self):
self.password = None
def setPass(self, pwd):
self.password = pwd
def getPass(self):
return self.password
def getTime():
time_last = int(time.strftime("%S"))
if time_last>=30:
time_last = time_last - 30
time_last = 30 - time_last
return ("(You have %s seconds left) " % str(time_last))
def NullPass():
pwd.setPass(None)
def botSendAdmin(msg):
'''
Sending message to admin chat id
'''
bot.sendMessage(config.admin_id, msg)
def generateCodes(secure_code):
'''
Generate 2FA Codes
'''
totp = pyotp.TOTP(secure_code)
return totp.now()
def getCodes(checkName= None):
'''
If checkName is None sending list of (Name of Service + 2fa code)
Else sending only 1 item with name 'checkName' + 2fa code for this service
'''
if pwd.getPass() != None:
try:
codes = fw.OpenJson("codes", pwd.getPass() )
for code in codes:
if checkName==code:
return generateCodes( codes[code] )
if checkName == None:
botSendAdmin(getTime() + code + " token is:")
botSendAdmin( generateCodes( codes[code] ) )
time.sleep(1)
except Exception as e:
botSendAdmin("List of 2FA doesn't exist " + str(e))
return None
else:
botSendAdmin("Password Null")
def addCodes(name, secure_code):
'''
Add codes to codes.enc
'''
if pwd.getPass() != None:
try:
codes = fw.OpenJson("codes", pwd.getPass())
except:
codes = dict()
codes[name] = secure_code
fw.SaveJson("codes", codes, pwd.getPass())
else:
botSendAdmin("Password Null")
def checkCorrectly(secure_code):
'''
If length secure_code > 4 and generateCodes return valid value
'''
try:
if len(secure_code) > 4 and (generateCodes(secure_code) != None ) :
return True
else:
return False
except:
return False
def parseMsg(msg):
command = msg.split(" ")
if ( len(command) == 1 ):
if command[0] == '/get':
getCodes()
elif command[0] == '/null':
NullPass()
botSendAdmin("Succeful Null-ed")
else:
botSendAdmin("Incorrect input")
else:
if command[0] == '/add':
if (checkCorrectly( command[2]) and (getCodes(checkName= command[1] )==None ) ):
addCodes(name= command[1], secure_code= command[2])
botSendAdmin("Successful add %s" % command[1])
else:
botSendAdmin("Incorrect 2FA format or/and this name already used")
elif command[0] == "/get":
code = getCodes(checkName= command [1])
if code != None:
botSendAdmin(getTime() + command[1] + " token is" )
botSendAdmin( getCodes(checkName= command [1]) )
else:
botSendAdmin("There is no service with this name.")
elif command[0] == '/del':
botSendAdmin("This function now unviable.")
elif command[0] == '/pass':
pwd.setPass(command[1])
botSendAdmin("Succeful!")
def handle(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
print("Chat id: %s" % str(chat_id) )
if chat_id == config.admin_id:
parseMsg(msg['text'])
pwd = password()
isNotConn = True
while isNotConn:
try:
bot = telepot.Bot(config.token)
MessageLoop(bot, handle).run_as_thread()
isNotConn = False
except Exception as e:
print(str(e))
time.sleep(10)
isNotConn = True
while 1:
time.sleep(10)