This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
124 lines (100 loc) Β· 3.74 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
const { default: fetchSession } = require("fetch-session");
const session = fetchSession();
// Default UNAB URL
const defaultUrl = "https://unab-form.dentidesk.cl";
function censorRut(rut) {
return rut.toString().replace(/.(?=.{3})/g, 'x')
}
async function loadConfig() {
let { RUT } = process.env;
try {
RUT = parseInt(RUT);
if (!RUT || !Number.isInteger(RUT)) {
throw new Error("Invalid RUT");
}
} catch (e) {
throw new Error('RUT is required or must be an integer!');
}
console.log(`[ π€ ] RUT ${censorRut(RUT)} loaded!`);
return RUT;
}
async function getAndSaveHeaders() {
await session.fetch(defaultUrl);
let xsrf = session.cookieJar.getCookieValue('XSRF-TOKEN');
let headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36',
'x-xsrf-token': xsrf,
'x-csrf-token': xsrf,
'x-requested-with': 'XMLHttpRequest'
}
return headers;
}
async function getData(rut) {
const headers = await getAndSaveHeaders();
headers['Content-Type'] = 'application/json';
const response = await session.fetch(`${defaultUrl}/api/getPaciente`, {
method: 'POST',
headers: headers,
body: JSON.stringify({
'rut': {
'rut': rut,
},
'tipoUsuario': 'ACCESO_INTERNO'
})
});
return response.body;
}
async function checkIfAuthorized(rut) {
const json = await getData(rut);
if (json.status !== 200) throw new Error(`[ π€ ] RUT ${censorRut(rut)} not found!`);
const { autorizado_hasta } = json[0];
const date = new Date(autorizado_hasta);
const now = new Date(new Date().toLocaleString("en-US", {timeZone: "America/Santiago"}));
if (date < now) {
console.log(`[ π« ] Rut ${censorRut(rut)} is not authorized!`);
return false;
} else {
console.log(`[ β
] Rut ${censorRut(rut)} is authorized!`);
return true;
}
}
async function processRut(rut) {
console.log(`[ π ] Starting process for ${censorRut(rut)}...`);
const RUTData = await getData(rut);
postBody = {
"data": ["20"],
"datosUsuario": {
"_token": session.cookieJar.getCookieValue('XSRF-TOKEN'),
"tipoAcceso": "ACCESO_INTERNO",
"datosUsuario": RUTData[0]
}
}
const headers = await getAndSaveHeaders();
headers['Content-Type'] = 'application/json';
console.log(`[ π© ] Sending request`)
const response = await session.fetch(`${defaultUrl}/sintomas`, {
method: 'POST',
headers: headers,
body: JSON.stringify(postBody)
});
if (response.body.icono == 'success') {
console.log('[ β
] Success, user should be authorized!');
return true;
}else{
console.log('[ π« ] Error! Please check the user manually.');
return false;
}
}
(async () => {
console.log(`Bienvenido! este script fue desarrollado para utilizacion personal y unica de la persona quien lo ejecute, el creador del mismo no se hace responsable en el caso de que la persona edite/no informe/detenga el script en el caso de presentar un sitoma de COVID-19.\nStay safe, stay home!\n`);
console.log('[ π ] Starting...');
try {
const rut = await loadConfig();
if(await checkIfAuthorized(rut)) return
await processRut(rut);
console.log(`[ π ] Last check for ${censorRut(rut)}`);
await checkIfAuthorized(rut);
} catch (error) {
console.error(`ERROR: ${error.message}`);
}
})();