-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
252 lines (207 loc) · 7.81 KB
/
utils.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
import random as rd
import socket, os, time
from subprocess import Popen,CREATE_NEW_CONSOLE,PIPE
import requests.cookies
import psutil, requests, json, urllib3, math, re
from pathlib import Path
CONTROL_PORT = 9051
PROXY_PORT = 9050
GET_IP = "https://api.ipify.org"
MAX_LEN = 14
# return inclusive min to max
def randomUserName(len:int=10)->str:
res = ""
for i in range(len):
r = rd.random()
if (r >= 0.6):
res += str(rd.randint(0,9))
elif (r > 0.3):
res += chr(rd.randint(65,90))
else:
res += chr(rd.randint(97,122))
return res
def command(cmd:str, value:str=None)->str|None:
s = socket.socket()
resp = None
try:
s.connect(("localhost", CONTROL_PORT))
#AUTHENTICATE
s.send("{command}\n".format(command="AUTHENTICATE").encode("utf-8"))
s.recv(1024)
if (value):
s.send("{command} {value}\n".format(command=cmd, value=value).encode("utf-8"))
else:
s.send("{command}\n".format(command=cmd).encode("utf-8"))
resp = s.recv(1024).decode("utf-8")
except ConnectionError:
print("Can't connect to ControlPort! Trying restart...")
handleConnectError()
return command(cmd=cmd, value=value)
finally:
s.close()
return resp
def changeCircuit()->int:
return int(command("SIGNAL", "NEWNYM").split(" ")[0])
def handleConnectError():
runTor()
def generateWish(dataTable={}, wish="No thing", maxLen=MAX_LEN):
return f"{("="*math.floor((dataTable.items().__len__()*maxLen+3*dataTable.items().__len__()*2- wish.__len__())/2))+wish+("="*math.floor((dataTable.items().__len__()*maxLen+3*dataTable.items().__len__()*2- wish.__len__())/2)):^{dataTable.items().__len__()*maxLen+3*dataTable.items().__len__()*2+5}}"
def runTor():
overwriteTorrc()
killTor()
print("Try running Tor...")
os.system(f'start /b {os.getcwd()+"\\Browser\\TorBrowser\\Tor\\tor.exe"}')
attemp = 0
while(not tryConnectToTor()):
attemp+=1
if (attemp >= 15):
runTor()
break
time.sleep(1)
writeLog("app.log", f"Wait for Tor...{attemp} //{time.ctime()}")
# kill tor if its exist
def killTor():
time.sleep(0.1)
pid = get_pid("tor.exe")
print("Stop...")
if (pid):
try:
if (os.system("taskkill -f -pid {}".format(pid)) == 0):
writeLog("app.log","Kill Tor success!")
else:
writeLog("app.log","Kill Tor failed! Trying again...")
killTor()
except Exception as e:
writeLog(error=e)
killTor()
else:
print("Not found Tor process!")
def get_pid(name:int=None)-> int:
if (not name):
return -1
pid = None
for proc in psutil.process_iter():
if proc.name().startswith(name):
pid = proc.pid
break
return pid
def tryConnectToTor():
s = socket.socket()
isSuccess = True
resp = -1
try:
s.connect(("localhost", CONTROL_PORT))
s.send("AUTHENTICATE\n".encode("utf-8"))
resp = int(s.recv(1024).decode("utf-8").split(" ")[0])
except ConnectionError:
isSuccess = False
finally:
s.close()
return isSuccess and resp == 250
def getCurrentIP(checkConnect=False):
proxies = {
'http': "socks5://127.0.0.1:{}".format(PROXY_PORT),
'https': "socks5://127.0.0.1:{}".format(PROXY_PORT)
}
try:
if (checkConnect):
resp = requests.get(GET_IP)
else:
resp = requests.get(GET_IP, proxies=proxies)
except:
return None
return resp.content.decode("utf-8")
def overwriteTorrc():
torFolder = Path(rf"{os.environ.get("APPDATA")+"\\tor"}")
torFolder.mkdir(exist_ok=True, parents=True)
with open(os.environ.get("APPDATA")+"\\tor\\torrc", encoding="utf-8", mode="w+") as file:
file.write("SOCKSPort 9050\nLog notice stdout\nControlPort 9051\nCookieAuthentication 0")
def getTimeHHmmss(sec):
return "{h}:{m}:{s}".format(h=time.gmtime(sec).tm_hour, s=time.gmtime(sec).tm_sec, m=time.gmtime(sec).tm_min)
def clear():
os.system("cls")
def postRequest(url, payload={}, headers={}, cookies={}, proxies={}, allowRedirect=True) -> requests.Response:
res = requests.post(
url= url,
headers=headers,
data=payload,
cookies=cookies,
proxies=proxies,
allow_redirects=allowRedirect
)
return res
def printTable(table:dict={}, maxLen:int = MAX_LEN,padding =3, replace:str = "."):
keys = ""
values = ""
for key in table.keys():
keys += f"{key:^{maxLen+padding*2}}{"|":>}"
print(keys)
for index, val in enumerate(table.values()):
val = str(val)
if (val.__len__()>maxLen):
val = val[:maxLen-1:]+replace*3
values += f"{val:^{maxLen+padding*2}}{"|":>}"
print(values)
def getInviteCount(username = None, password = None):
inviteCount = -1
if (not (username and password)):
return inviteCount
resp1:requests.Response = requests.get("https://ngocrongking.com/dang-nhap", headers={
'Content-Type':'application/x-www-form-urlencoded',
'Referer': 'https://ngocrongking.com/',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
})
inputTag = re.search('<input name="_token" type="hidden" value=".+">', resp1.content.decode("utf-8"))
if (inputTag):
valueAttr = re.search('value=".+"', inputTag.group()).group().split("value=")[1]
else:
valueAttr = ""
_token = valueAttr.removeprefix('"').removesuffix('"')
resp2 = postRequest(
url="https://ngocrongking.com/dang-nhap",
headers={
'Content-Type':'application/x-www-form-urlencoded',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
'Referer': 'https://ngocrongking.com/dang-nhap',
},
cookies={
"laravel_session": resp1.cookies.get("laravel_session"),
"XSRF-TOKEN": resp1.cookies.get('XSRF-TOKEN')
},
payload={
"_token": _token,
"username": username,
"password": password
},
allowRedirect=False
)
resp3 = requests.get("https://ngocrongking.com",headers={
'Content-Type':'application/x-www-form-urlencoded',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
'Referer': 'https://ngocrongking.com/dang-nhap'
}, cookies={
"laravel_session": resp2.cookies.get("laravel_session"),
"XSRF-TOKEN": resp2.cookies.get('XSRF-TOKEN')
})
resp4 = requests.get("https://ngocrongking.com/link-gioi-thieu", cookies={
"laravel_session": resp3.cookies.get("laravel_session"),
"XSRF-TOKEN": resp3.cookies.get('XSRF-TOKEN')
},headers={
'Content-Type':'application/x-www-form-urlencoded',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
'Referer': 'https://ngocrongking.com/'
})
inviteTag = re.search(r"Số lần sử dụng: <strong>\d+</strong>", resp4.content.decode("utf-8"))
if (inviteTag):
inviteCount = int(re.search(r"\d+", inviteTag.group()).group())
resp1.close()
resp2.close()
resp3.close()
resp4.close()
return inviteCount
def writeLog(url="error.log", error:Exception="Unknown error", appendDate=True):
with open(url, "a+") as log:
if (appendDate):
log.write(f"{str(error)} //{time.ctime()}"+"\n")
if (appendDate):
log.write(f"{str(error)}"+"\n")