-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
128 lines (114 loc) · 4.37 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
const fs = require('fs')
const url = require('url')
const mime = require('mime-types').lookup
const bot = require('./bot.js')
const connectionHandler = require('./connectionHandler.js')
const options = {}
try { // Use HTTPS if keys are available
options.key = fs.readFileSync('secrets/key.pem')
options.cert = fs.readFileSync('secrets/cert.pem')
console.log('Found keys - using HTTPS!')
} catch (err) {
console.log('No keys found - using HTTP!')
}
const usinghttps = !!options.key
const http = usinghttps ? require('https') : require('http')
var auth = require('./authentication.js')
auth.setHTTPS(usinghttps) // Determines whether cookies have the Secure; option
var indexpage = require('./pages/index.js')
var loginpage = require('./pages/login.js')
var guestpage = require('./pages/guest.js')
var registerpage = require('./pages/register.js')
var forgotpage = require('./pages/forgot.js')
var channelpage = require('./pages/channel.js')
var serverpage = require('./pages/server.js')
var sendpage = require('./pages/send.js')
bot.startBot()
function strReplace (string, needle, replacement) {
return string.split(needle).join(replacement || '')
};
// https://stackoverflow.com/questions/1967119/why-does-javascript-replace-only-first-instance-when-using-replace
// create a server object:
const server = http.createServer(options)
connectionHandler.startWsServer(server)
async function servePage (filename, res, type, textToReplace, replacement) { // textToReplace and replacement allow for dynamic pages (not used anymore as far as I know)
if (!type) {
type = mime(filename)
}
if (filename.endsWith('/')) {
filename += 'index.html'
}
fs.readFile(filename, function (err, data) {
if (err) {
if (filename.endsWith('index.html')) {
res.writeHead(404, { 'Content-Type': type })
return res.end('404 Not Found')
} else {
servePage(filename + '/index.html', res)
return
}
}
res.writeHead(200, { 'Content-Type': type })
if (textToReplace && replacement) {
res.write(data.toString().replace(textToReplace, replacement))
} else {
res.write(data)
}
return res.end()
})
}
server.on('request', async (req, res) => {
console.log(req.url)
if (req.method === 'POST') {
let body = '' // https://itnext.io/how-to-handle-the-post-request-body-in-node-js-without-using-a-framework-cd2038b93190
req.on('data', chunk => {
body += chunk.toString() // convert Buffer to string
})
req.on('end', () => {
auth.handleLoginRegister(req, res, body)
})
} else {
const parsedurl = url.parse(req.url, true)
const args = strReplace(parsedurl.pathname, '?', '/').split('/') // Split by / or ?
if (args[1] === 'send') {
const discordID = await auth.checkAuth(req, res)
if (discordID) {
sendpage.sendMessage(bot, req, res, args, discordID)
}
} else if (args[1] === 'logout') {
const discordID = await auth.checkAuth(req, res, true) // True = no redirect to login page
if (discordID) {
auth.logout(discordID)
}
res.writeHead(302, { Location: '/' })
res.end()
} else if (args[1] === 'server') {
const discordID = await auth.checkAuth(req, res)
if (discordID) {
serverpage.processServer(bot, req, res, args, discordID)
}
} else if (args[1] === 'channels') {
const discordID = await auth.checkAuth(req, res)
if (discordID) {
channelpage.processChannel(bot, req, res, args, discordID)
}
} else if (args[1] === 'login.html') {
loginpage.processLogin(bot, req, res, args)
} else if (args[1] === 'guest.html') {
guestpage.processGuestLogin(bot, req, res, args)
} else if (args[1] === 'register.html') {
registerpage.processRegister(bot, req, res, args)
} else if (args[1] === 'forgot.html') {
forgotpage.processForgot(bot, req, res, args)
} else if (args[1] === 'index.html' || parsedurl.pathname === '/') {
indexpage.processIndex(bot, req, res, args)
} else if (args[1] === 'longpoll.js' || args[1] === 'longpoll-xhr' || args[1] === 'api.js') { // Connection
connectionHandler.processRequest(req, res)
} else {
let filename = 'pages/static' + parsedurl.pathname
filename = strReplace(filename, '..', '') // Attempt to stop private files from being served
await servePage(filename, res)
}
}
})
server.listen(4000)