-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
256 lines (241 loc) · 10.6 KB
/
index.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
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
const config = require('./config.json');
const crypto = require('crypto');
const unirest = require('unirest');
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const ac = require("@antiadmin/anticaptchaofficial");
const moment = require('moment');
const packageJSON = require('./package.json');
let PupPage;
(() => {
unirest('GET', 'https://raw.githubusercontent.com/Akshit-WTF/cowin/main/package.json')
.end(function (res) {
res.body = JSON.parse(res.body);
if (res.body.version !== packageJSON.version) {
unirest('GET', 'https://raw.githubusercontent.com/Akshit-WTF/cowin/main/toUpdate.json')
.end(function (res) {
let files = (JSON.parse(res.body)).files;
for (const i of files) {
unirest('GET', `https://raw.githubusercontent.com/Akshit-WTF/cowin/main/${i}`)
.end(function (res) {
fs.writeFileSync(path.join(__dirname, `./${i}`), res.raw_body);
if (files.indexOf(i) === files.length - 1) {
console.log(chalk.redBright(`A NEW VERSION OF THE SCRIPT IS NOW AVAILABLE. It has been updated. Run it again.`));
process.exit(0);
}
});
}
});
}
});
})();
if (config.autotoken === true) {
const io = require("socket.io-client");
const socket = io.connect('https://sms.axit.me/' + config.phone);
socket.on('otp', async function(data) {
let str = data.message;
if (str.toLowerCase().includes('cowin')) {
for (const i of str.split(' ')) {
let maybeOTP = i.slice(0, -1);
if (maybeOTP.length === 6 && !isNaN(parseInt(maybeOTP))) {
token = `Bearer ${await getAuthToken(otpUUID, maybeOTP)}`;
console.log(chalk.greenBright(`${moment().format('LTS')}: Regenerated the authorization token successfully.`));
checkAlive();
}
}
}
});
}
if (config.autotoken === false) {
const puppeteer = require('puppeteer');
(async () => {
let execPath;
if (fs.existsSync('C:/Program Files (x86)/Google/Chrome/Application/chrome.exe')) {
execPath = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe";
}
if (fs.existsSync('C:/Program Files/Google/Chrome/Application/chrome.exe')) {
execPath = "C:/Program Files/Google/Chrome/Application/chrome.exe";
}
if (!execPath) {
console.log(chalk.redBright('To use the manual OTP option, you need Google Chrome installed on your PC.'));
process.exit();
}
const browser = await puppeteer.launch({headless: false, executablePath: execPath});
const page = await browser.newPage();
await page.goto('https://selfregistration.cowin.gov.in');
page.on('response', async (response) => {
const request = response.request();
if (request.url().includes('validateMobileOtp') && request.method() === "POST") {
const body = await response.json();
if (body.token) {
token = `Bearer ${body.token}`;
console.log(chalk.greenBright(`${moment().format('LTS')}: Regenerated the authorization token successfully.`));
checkAlive();
}
}
});
PupPage = page;
})();
}
if (config.beneficiaries.length === 0 || !config.phone || !config["anti-captcha-key"] || (config.dose !== 1 && config.dose !== 2)) {
console.log(chalk.redBright('Variable(s) missing/incorrect in the config file. Please fix to continue.'));
process.exit(0);
}
ac.setAPIKey(config["anti-captcha-key"]);
ac.shutUp();
let otpUUID = '';
let token = '';
let captchaRequested = false;
let isAuthorized = false;
function replace(original, toReplace, replacement) {
return original.replace(new RegExp(toReplace, "gi"), replacement);
}
function requestOTP() {
unirest('POST', 'https://cdn-api.co-vin.in/api/v2/auth/generateMobileOTP')
.headers({
'Content-Type': 'application/json'
})
.send(JSON.stringify({
"mobile": config.phone,
"secret": "U2FsdGVkX1+9pRYVTYpP7nNgkOD7vbxwd74VM9D5OYctbNlcUq9frLeFXrIRFTHlorr+d98h5aaZ33/8O91uxw=="
}))
.end(function (res) {
otpUUID = res.body.txnId;
});
}
(() => {
if (fs.existsSync(path.join(__dirname, './token'))) {
token = fs.readFileSync(path.join(__dirname, './token'), 'utf-8');
}
checkAlive();
})()
function getAuthToken(id, otp) {
return new Promise(function (resolve) {
unirest('POST', 'https://cdn-api.co-vin.in/api/v2/auth/validateMobileOtp')
.headers({
'Content-Type': 'application/json'
})
.send(JSON.stringify({
"otp": crypto.createHash('sha256').update(otp).digest('hex'),
"txnId": id
}))
.end(function (res) {
resolve(res.body.token);
});
});
}
function convertSVGtoPNG(svg) {
return new Promise(function (resolve) {
unirest('POST', 'https://svg2jpg.akshit.me')
.headers({
'Content-Type': 'application/json'
})
.send(JSON.stringify({
"svg": svg
}))
.end(function (res) {
resolve(res.body.jpeg);
});
})
}
function getCaptcha() {
if (captchaRequested === true) return false;
return new Promise(function (resolve, reject) {
unirest('POST', 'https://cdn-api.co-vin.in/api/v2/auth/getRecaptcha')
.headers({
'Authorization': token
})
.end(async function (res) {
captchaRequested = true;
let svg = res.body.captcha;
svg = svg.replace(/\\\//g, "/");
for (let i = 111; i < 1000; i += 111) {
svg = replace(svg, `fill="#${i}"`, 'fill="#fff"')
svg = replace(svg, `stroke="#${i}"`, 'stroke="none"')
}
let base64 = await convertSVGtoPNG(svg);
let captcha = await ac.solveImage(base64);
resolve(captcha);
});
});
}
let sessionCheckInterval = setInterval(function () {
checkAlive();
}, 30000);
function checkAlive() {
unirest('GET', 'https://cdn-api.co-vin.in/api/v2/appointment/beneficiaries')
.headers({
'Authorization': token
})
.end(function (res) {
if (res.status !== 401) {
isAuthorized = true;
console.log(chalk.greenBright(`${moment().format('LTS')}: Session status confirmed to be alive.`));
fs.writeFileSync(path.join(__dirname, './token'), token);
} else {
if (config.autotoken === false) {
if (!PupPage) return;
PupPage.reload();
}
if (config.autotoken === true) {
requestOTP();
}
isAuthorized = false;
console.log(chalk.redBright(`${moment().format('LTS')}: Session has expired.`));
}
});
}
let mainInterval = setInterval(function () {
if (!isAuthorized) return console.log(chalk.redBright(`${moment().format('LTS')}: Session not authorized, not checking for available slots.`));
console.log(chalk.blueBright(`${moment().format('LTS')}: Checking for available slots on the COWIN portal.`));
unirest('GET', 'https://cdn-api.co-vin.in/api/v2/appointment/sessions/calendarByPin?pincode=' + config.pincode + '&date=' + moment().format('DD-MM-YYYY'))
.headers({
'Authorization': token
})
.end(async function (res) {
try {
let foundSlot = false;
for (const centre of res.body.centers) {
for (const session of centre.sessions) {
if ((session.min_age_limit === 18) === config["18"] && session[`available_capacity_dose${config.dose}`] !== 0) {
foundSlot = true;
let captcha = await getCaptcha();
if (captcha === false) return;
unirest('POST', 'https://cdn-api.co-vin.in/api/v2/appointment/schedule')
.headers({
'Authorization': token,
'Content-Type': 'application/json'
})
.send(JSON.stringify({
"center_id": centre.center_id,
"session_id": session.session_id,
"captcha": captcha,
"beneficiaries": config.beneficiaries,
"slot": session.slots[0],
"dose": config.dose
}))
.end(function (res) {
if (res.body.appointment_confirmation_no) {
console.log(chalk.greenBright('Appointment booked successfully! Please login to check.'));
clearInterval(mainInterval);
clearInterval(sessionCheckInterval);
process.exit(0);
} else {
captchaRequested = false;
foundSlot = false;
console.log(chalk.redBright('Tried booking an appointment but failed. Restart this script.'));
console.log(chalk.redBright('Reason: ' + res.body.error));
}
});
}
}
}
if (foundSlot === false) {
console.log(chalk.redBright('No slots are available right now. Will keep checking.'));
}
} catch (e) {
console.log(chalk.redBright('We might be getting rate limited.'));
}
});
}, 1750);