-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
217 lines (174 loc) · 5.84 KB
/
utils.js
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
const os = require("os");
const util = require("util");
const axios = require("axios");
const exec = util.promisify(require('child_process').exec);
const writeFileAsync = util.promisify(require('fs').writeFile);
const mailProvider = require('@cemalgnlts/mailjs');
const MailJS = new mailProvider();
const getRandomAccount = async ()=>{
let {status, data: account} = await MailJS.createOneAccount();
if (!status){
throw new Error("Account creation failed!");
}
return account;
}
const getLicense = (message_text)=>{
/* Replace linebreaks with "|" and replace multi spaces to one space. */
let message = message_text.replace(/(\r\n|\n|\r)/gm, "|").replace(/\s+/g, " ");
/* Data patterns */
const id_pattern = /License\/Public ID:(\|){2}([A-Z\-\d]+)(\|){2}/;
const product_pattern = /Product:(\|){2}(.+?)(\|){2}/;
const license_pattern = /License key:(\|){2}([A-Z\-\d]+)(\|){2}/;
const username_pattern = /Username:(\|){2}([A-Z]{3}-\d+)(\|){2}/;
const password_pattern = /Password:(\|){2}([a-z\d]+)(\|){2}/;
const quantity_pattern = /Quantity:(\|){2}(\d+)(\|){2}/;
const expiration_pattern = /Expiration:(\|){2}(\d+\/\d+\/\d+)(\|){2}/;
let response;
try{
/* Match patterns with message */
let id_match = message.match(id_pattern)
let product_match = message.match(product_pattern)
let license_match = message.match(license_pattern)
let username_match = message.match(username_pattern)
let password_match = message.match(password_pattern)
let quantity_match = message.match(quantity_pattern)
let expiration_match = message.match(expiration_pattern)
/* Create Object */
let license = {
public_id: id_match[2],
product: product_match[2],
license_key: license_match[2],
username: username_match[2],
password: password_match[2],
quantity: quantity_match[2],
expiration: expiration_match[2],
}
response = {
status: true,
license: license,
};
}catch (err){
response = {
status: false,
license: null,
}
}
return response;
}
const fetchLicenseMessage = async (user)=>{
await MailJS.login(user.username, user.password);
let {status: allStatus, data: messages} = await MailJS.getMessages();
if(!allStatus){
return false;
}
let [license_mail] = messages.filter((message)=>{
return /license information/.test(message.subject) || /LICENSE INFORMATION/.test(message.intro);
});
if (!license_mail){
return false;
}
let {status: singleStatus, data: message} = await MailJS.getMessage(license_mail.id)
if(!singleStatus){
return false;
}
let {status: licenseStatus, license} = getLicense(message.text);
if (!licenseStatus){
return false;
}
return license;
}
const deleteUser = async (user)=>{
await MailJS.login(user.username, user.password);
const { status, data, message } = await MailJS.deleteMe();
return Promise.resolve(status);
}
const connectVPN = async () => {
let provider = {
exe: 'hsscp.exe',
shortcut: '.\\Hotspot.lnk',
interface: 'HotspotShield',
};
/* Find PID Hotspot VPN */
let vpn_pid = await findPidByName(provider.exe);
if (vpn_pid) { // running
process.kill(vpn_pid);
}
/* Get my ip */
let {ip: MyIp} = await checkIP();
/* Start Vpn app */
await exec(`start ${provider.shortcut}`)
vpn_pid = await findPidByName(provider.exe);
/* Wait for app launching */
console.log("Connecting to VPN...")
await Sleep(10000);
await waitConnectionEstablished();
/* Get VPN Ip */
let {status, ip} = await checkIP();
console.log("Checking ips...")
/* Check IP changed */
if (!status || MyIp === ip) {
return {
status: false,
pid: vpn_pid,
};
}
console.log("Connected to VPN...")
console.log(`IP: ${ip}`);
return {
status: true,
pid: vpn_pid,
ip: ip,
local_ip: MyIp,
};
}
const DisconnectVPN = async () => {
let provider = {
exe: 'hsscp.exe',
shortcut: '.\\Hotspot.lnk',
interface: 'HotspotShield',
};
let vpn_pid = await findPidByName(provider.exe);
if (vpn_pid) { // running
process.kill(vpn_pid);
return true;
}
return false;
}
const checkIP = async () => {
let response = await axios.get("http://ip-api.com/json/", {timeout: 5000})
.catch((err)=> {
console.log(err.message);
return {
status: false,
ip: null,
};
});
return {
status: response.status === 200 && response.data && response.data.status === "success" && response.data.countryCode !== "TR",
ip: response.data.query,
};
}
const findPidByName = async (name) => {
let {stdout} = await exec(`tasklist /FI "IMAGENAME eq ${name}"`);
let lines = stdout.toString().replace(/\r/g, "").split('\n').filter((v) => v !== '');
let process_data = lines.pop().split(" ").filter((v) => v !== "");
if (process_data[0] === name) {
return parseInt(process_data[1]);
}
return false;
}
const waitConnectionEstablished = () => {
return new Promise((resolve, reject) => {
let interval = setInterval(() => {
let interfaces = Object.keys(os.networkInterfaces());
if (interfaces.includes('HotspotShield')) {
clearInterval(interval);
resolve(true)
}
}, 500)
})
}
const Sleep = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
}
module.exports = { connectVPN, DisconnectVPN, Sleep, getRandomAccount, fetchLicenseMessage, deleteUser, writeFileAsync }