-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
130 lines (114 loc) · 3.61 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
const axios = require('axios');
var moment = require('moment');
var moment = require("moment-timezone");
const qs = require('querystring')
exports.handler = async (event) => {
await run(username, password)
const response = {
statusCode: 200,
body: JSON.stringify('HELLO NUS!!'),
};
return response;
};
/* Config */
const CLIENT_ID = "97F0D1CACA7D41DE87538F9362924CCB-184318"
const now = moment.tz(Date.now(), "Asia/Singapore").format("DD/MM/YYYY");
const username = process.env.NUSNET_ID;
const password = process.env.NUSNET_PASSWORD;
const isMorning = parseInt(moment.tz(Date.now(), "Asia/Singapore").format("HH")) < 12
/* Init */
axios.defaults.headers.common['User-Agent'] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36";
const instance = axios.create();
instance.defaults.timeout = 20000;
const ALLOWED_TEMPS = [
"35.8",
"35.9",
"36.0",
"36.1",
"36.2",
"36.3",
"36.4",
"36.5",
"36.6",
"36.7",
"36.8",
"36.9",
"36.0"
];
/* Helper Functions */
const get_random_temp = () => ALLOWED_TEMPS[Math.floor(Math.random() * ALLOWED_TEMPS.length)]
async function run(username, password){
if (!username || !password) {
return console.log("[ERROR] Missing username and/or password.")
}
const code_url = await auth(username, password);
if (!code_url) {
return console.log("[ERR] Missing code_url");
}
const session_id = await get_session_id(code_url);
if (!session_id) {
return console.log("[ERR] Missing session_id");
}
await submit_temp(session_id);
console.log("[SUCCESS]");
}
async function auth(username, password){
// Login request woundent work without MSIS auth cookies
console.log("[AUTH]")
const auth_url = 'https://vafs.nus.edu.sg/adfs/oauth2/authorize'
const config = {
maxRedirects: 0,
headers : {
'Content-Type': 'application/x-www-form-urlencoded'
},
params: {
'response_type': 'code',
'client_id': CLIENT_ID,
'resource': 'sg_edu_nus_oauth',
'redirect_uri': 'https://myaces.nus.edu.sg:443/htd/htd'
}
}
const data = qs.stringify({
'UserName': 'nusstu\\' + username,
'Password': password,
'AuthMethod': "FormsAuthentication"
})
const MSISAuthCookie = await axios.post(auth_url, data, config)
.then(response => response)
.catch(err => err.response.headers['set-cookie'][0].split(";")[0]) // shoud reach heres due to 302
config.headers = {
'Cookie': MSISAuthCookie
}
return await axios.post(auth_url, data, config)
.then(response => response.headers.location)
.catch(err => err.response.headers.location)
}
async function get_session_id(code_url) {
console.log(`[GET_SESSION_ID]`)
const jsessionid = await axios.get(code_url, null)
.then(response => response.headers['set-cookie'][0].split(";")[0])
.catch(err => err.response.headers)
return jsessionid
}
async function submit_temp(session_id) {
console.log(`[SUBMIT_TEMP] session_id='${session_id}'`)
const temp = get_random_temp();
console.log(`[DEBUG] Reporting '${temp}' for ${isMorning ? "AM" : "PM"} on (${now})`)
const htd_url = "https://myaces.nus.edu.sg/htd/htd";
const config = {
headers : {
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie': session_id
}
}
const data = qs.stringify({
'actionName': 'dlytemperature',
'tempDeclOn': now,
'declFrequency': isMorning ? "A" : "P",
'temperature': temp,
'symptomsFlag': "N"
})
return await axios.post(htd_url, data, config)
.then(response => response)
.catch(err => err.response)
}