-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
229 lines (187 loc) · 8.18 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
const http = require("http")
const https = require("https")
const fs = require("fs")
const path = require("path")
const { WebSocket, WebSocketServer } = require("ws")
const ejs = require("ejs")
const express = require("express")
const panel = express()
var plugin_pages = []
var plugin_scripts = []
var config = require("./data/config.json") //vars because they can be updated at runtime
var httpDomains = require("./data/httpDomains.json")
var wsDomains = require("./data/wsDomains.json")
var current_panel_domain = fs.readFileSync(__dirname + "/data/currentpaneldomain.txt", "utf-8")
const startTime = new Date()
function calculateUptime(){
var now = new Date()
var difference = now - startTime //in ms
return new Date(difference)
}
const files = fs.readdirSync(config.plugin_folder) // reading files from folders
files.map(file => {
if(fs.lstatSync(config.plugin_folder + "/" + file).isFile()){
var plugin = require(config.plugin_folder + "/" + file)
panel.use("/plugins/" + plugin.name, plugin.web)
plugin_pages.push({ name: file.split('.').shift(), pages: plugin.pages })
}
})
if(config.useSSL){
var server = https.createServer({
cert: fs.readFileSync(config.certificate),
key: fs.readFileSync(config.privateKey)
})
} else var server = http.createServer()
server.on('request', (originalReq, originalRes) => {
if(originalReq.headers["cf-connecting-ip"]) var ip = originalReq.headers["cf-connecting-ip"]
else if(originalReq.headers["x-forwarded-for"]) var ip = originalReq.headers["x-forwarded-for"]
else var ip = originalReq.connection.remoteAddress
var domain = httpDomains.find(a => a.domain === originalReq.headers.host)
if(!domain){
originalRes.writeHead(404, { 'Content-Type': "text/html" })
return originalRes.end(
ejs.render(
fs.readFileSync(path.join(__dirname, "./views/404.ejs"), "utf-8"),
{ host: originalReq.headers.host + originalReq.url, ip, uptime: calculateUptime().getTime() }
)
)
}
if(domain.domain === current_panel_domain) return panel(originalReq, originalRes)
//insert log function here laters
var options = {
hostname: domain.ip,
port: domain.port,
path: originalReq.url,
method: originalReq.method,
headers: originalReq.headers
}
var proxy = http.request(options, res => {
originalRes.writeHead(res.statusCode, res.headers)
res.pipe(originalRes, { end: true })
})
proxy.on('error', err => { //log error here too maybe
// console.error(`Error encountered making the secondary ${originalReq.method} request for requested host ${originalReq.host}${originalReq.url} to ${domain.ip}:${domain.path}`, err)
originalRes.writeHead(500, { 'Content-Type': "text/html" })
originalRes.end(
ejs.render(
fs.readFileSync(path.join(__dirname, "./views/500.ejs"), "utf-8"),{ host: originalReq.headers.host, ip, uptime: calculateUptime().getTime(), error: err.toString() }
)
)
})
originalReq.pipe(proxy, { end: true })
})
// if((config.http_port === config.ws_port) && config.http_active && config.ws_active){ //ws and http are sharing same server
if(config.ws_active){
// const wss = new ws.WebSocketServer({ server });
let wss
if((config.http_port === config.ws_port) && config.http_active) wss = new WebSocketServer({ server });
else {
if(config.useSSLWS){
let httpServer = https.createServer({
cert: fs.readFileSync(config.sslWS.cert),
key: fs.readFileSync(config.sslWS.private_key)
})
wss = new WebSocketServer({ httpServer });
}
else wss = new WebSocketServer({ port: config.ws_port })
}
wss.on('connection', (ws, req) => {
// Extract the 'Host' header from the request headers
const host = req.headers.host;
// Use the host value to determine the target server to proxy the WebSocket to
let target = '';
let found = wsDomains.find(a => a.domain === host)
if(!found){
ws.send(JSON.stringify({ success: false, error: "Domain not found" }))
return ws.close()
} else target = `ws://${found.ip}:${found.port}`
// Create a new WebSocket connection to the target server
const proxy = new WebSocket(target, {
Headers: {
Host: host
}
});
// Forward messages from the client WebSocket to the target WebSocket
ws.on('message', (message) => {
proxy.send(message);
});
// Forward messages from the target WebSocket to the client WebSocket
proxy.on('message', (message) => {
console.log(message)
ws.send(message.toString());
});
// Handle errors from the target WebSocket
proxy.on('error', (error) => {
ws.send(JSON.stringify({ success: false, error: error.toString() }))
ws.close();
});
});
}
if(config.http_active) server.listen(config.http_port)
const cookieParser = require('cookie-parser')
panel.use(cookieParser())
panel.use(express.json())
panel.use("/panel/public", (req, res, next) => {
if(fs.existsSync(__dirname + "/public" + req.path)){
res.sendFile(__dirname + "/public" + req.path)
} else next()
})
panel.get("/panel/login", (req, res) => {
res.sendFile(__dirname + "/views/panel/login.html")
})
panel.post("/panel/login", (req, res) => {
if(req.body.password === config.panel_password) res.cookie("password", config.panel_password).send("ok")
else res.status(403).send("Wrong password")
})
panel.use((req, res, next) => {
if(!req.cookies.password) return res.redirect("/panel/login")
if(req.cookies.password !== config.panel_password) return res.redirect("/panel/login")
var ip = req.headers["x-forwarded-for"]
if(!ip) ip = req.headers["cf-connecting-ip"]
if(!ip) ip = req.connection.remoteAddress
req.c_ip = ip
next()
})
panel.get("/", (req, res) => {
res.redirect("/panel")
})
panel.get("/panel", (req, res) => {
res.render(__dirname + "/views/panel/index.ejs", { wsDomains, httpDomains, current_panel_domain, plugin_pages })
})
panel.post("/panel/setpaneldomain", (req, res) => {
var domain = req.body.domain
current_panel_domain = req.body.domain
fs.writeFileSync(__dirname + "/data/current_panel_domain.txt", current_panel_domain)
res.json({status: "ok", domain})
})
panel.post("/panel/create_domain/http", (req, res) => {
if(httpDomains.find(a => a.domain === req.body.domain)) return res.send("not ok")
httpDomains.push(req.body)
fs.writeFileSync(__dirname + "/data/httpDomains.json", JSON.stringify(httpDomains), "utf-8")
res.send("ok")
})
panel.post("/panel/delete_domain/http", (req, res) => {
httpDomains = httpDomains.filter(a => a.domain !== req.body.domain)
fs.writeFileSync(__dirname + "/data/httpDomains.json", JSON.stringify(httpDomains), "utf-8")
res.send("ok")
})
panel.post("/panel/create_domain/ws", (req, res) => {
if(wsDomains.find(a => a.domain === req.body.domain)) return res.send("not ok")
wsDomains.push(req.body)
fs.writeFileSync(__dirname + "/data/wsDomains.json", JSON.stringify(wsDomains), "utf-8")
res.send("ok")
})
panel.post("/panel/delete_domain/ws", (req, res) => {
wsDomains = wsDomains.filter(a => a.domain !== req.body.domain)
fs.writeFileSync(__dirname + "/data/wsDomains.json", JSON.stringify(wsDomains), "utf-8")
res.send("ok")
})
panel.use((req, res) => {
res.status(404).render(__dirname + "/views/404.ejs", { host: req.headers.host + req.path, ip: req.c_ip, uptime: calculateUptime().getTime() })
})
process.on('uncaughtException', err => {
console.error("Catastrophic error made not catastrophic:", err)
})
process.on('unhandledRejection', err => {
console.error("Catastrophic error made not catastrophic:", err)
})