-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·159 lines (135 loc) · 4.49 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
const express = require('express') // expressJS
const app = express() // Initialize express app
const utils = require('./utils-async') // Utilitys/API functions
const config = require('./config.json') // Load configuration data
const logger = require('./logger') // Set up default logger
const winston = require('winston')
const ngrok = require('ngrok');
const open = require('open');
const Sentry = require('@sentry/node');
const Tracing = require("@sentry/tracing");
// Initialize sentry
Sentry.init({
dsn: config.sentryDsn,
integrations: [
// enable HTTP calls tracing
new Sentry.Integrations.Http({ tracing: true }),
// enable Express.js middleware tracing
new Tracing.Integrations.Express({ app }),
],
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for performance monitoring.
// We recommend adjusting this value in production
tracesSampleRate: 1.0,
});
// Add sentry middleware
app.use(Sentry.Handlers.requestHandler());
app.use(Sentry.Handlers.tracingHandler());
// Temporary
app.get('/', (req, res) => {
res.status(410).send({
status: "arbitrary_impositon",
error: `Cleverly done, ${req.ip} but you're not supposed to be here. As a matter of fact, you're not. Get back where you belong, and forget about all this, until we meet again.`
})
})
// API endpoints
app.post('/session/login', (req, res) => {
// Check to make sure a username and password is present. If either one is missing, return with an error.
if (!req.headers.username) {
res.status(400).send({
status: "failed",
error: "Username missing."
});
return;
} else if (!req.headers.password) {
res.status(400).send({
status: "failed",
error: "Password missing."
});
return;
}
utils.loginSSO(req.headers.username, req.headers.password, res);
})
app.get('/user/getDetails', (req, res) => {
if (!req.headers.accesstoken) {
res.status(400).send({
status: "failed",
error: "accessToken missing."
});
return;
} else utils.getStudentData(req.headers.accesstoken, res);
})
app.get('/user/getGrades', (req, res) => {
if (!req.headers.accesstoken) {
res.status(400).send({
status: "failed",
error: "accessToken missing."
});
return;
} else utils.getGrades(req.headers.accesstoken, res);
})
app.get('/user/getSchedule', (req, res) => {
if (!req.headers.accesstoken) {
res.status(400).send({
status: "failed",
error: "accessToken missing."
});
return;
} else utils.getSchedule(req.headers.accesstoken, res);
})
app.post('/session/destroySession', (req, res) => {
// Check for a session ID. If we don't have one, stop.
if (!req.headers.accesstoken) {
res.status(400).send({
status: "failed",
error: "No accessToken cookie given to destroy."
});
return;
} else utils.destroySACSession(req.headers.accesstoken, res);
})
app.get('/server/ping', (req, res) => {
res.send({
status: 'success',
server: {
version: null,
announcement: config.announcement
}
});
})
// Add error handling middleware
app.use(Sentry.Handlers.errorHandler());
// onError middleware
app.use(function onError(err, req, res, next) {
logger.error(err.stack)
res.status(500).send({
status: 'failed',
error: err.message
});
});
// Listen on whatever port is selected
app.listen(config.port, async () => {
winston.info(`Edformer now listening on port ${config.port}.`)
if (config.announcement) {
winston.info(`Announcement found: "${config.announcement}"`)
}
if (config.network.ngrokEnabled === true) {
logger.info('Ngrok is enabled in config, opening...')
let grktunnel = await ngrok.connect(addrconfig.port)
logger.info(`Ngrok tunnel opened at ${grktunnel}`)
if (config.network.openWebUI === true) {
open('http://localhost:4040')
}
}
})
process.on('SIGINT', async function() {
logger.info(`Shutting down Edformer.`)
if (config.network.ngrokEnabled === true) {
logger.info('Closing NGROK tunnel.')
await ngrok.disconnect().then((a) => {
logger.info(`Closed ngrok tunnel: ${a}`)
}).catch((e) => {
logger.error(`Failed to gracefully close NGROK tunnel!`)
})
}
process.exit();
});