-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathserver.js
More file actions
78 lines (63 loc) · 2.19 KB
/
server.js
File metadata and controls
78 lines (63 loc) · 2.19 KB
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
const {readFileSync} = require("fs")
const {join} = require("path")
const {createServer} = require("https")
const express = require("express")
const Corrosion = require("corrosion")
module.exports = config => {
const blacklist = ({url, clientResponse: res}) => {
if (config.blacklist.includes(url.hostname)) {
res.redirect(308, "/blacklisted")
}
}
const corrosion = new Corrosion({
codec: "xor",
prefix: "/main/",
title: "Forage",
requestMiddleware: [blacklist],
...config.corrosion
})
const app = express()
const server = createServer({
key: readFileSync(join(__dirname, "dummy.key")),
cert: readFileSync(join(__dirname, "dummy.cert"))
}, app)
app.get("/gateway", (req, res) => {
let givenUrl = req.query.url
// Stolen from Corrosion :P
if (!givenUrl) {
res.statusCode = 400
res.end("Forage gateway requires URL query string parameter.")
return
}
const urlRegex = /https?:\/\/([a-zA-Z0-9\-\_])|([a-zA-Z0-9\-\_])\.([a-zA-Z])/
if (urlRegex.test(givenUrl)) {
if (!/^https?:\/\//.test(givenUrl)) {
givenUrl = "http://" + givenUrl
}
} else {
givenUrl = "https://google.com/search?q=" + givenUrl
}
urlObject = new URL(givenUrl)
if (urlObject.hostname in config.redirects) {
const hostnameRedirects = config.redirects[urlObject.hostname]
if (urlObject.pathname in hostnameRedirects) {
urlObject.pathname = hostnameRedirects[urlObject.pathname]
}
}
let targetUrl
if (config.womginx.includes(urlObject.hostname)) {
targetUrl = "/wgnx/" + urlObject
} else {
targetUrl = corrosion.url.wrap(urlObject.toString())
}
res.writeHead(308, {Location: targetUrl})
res.end("Forage is redirecting you to your desired page...")
})
app.all("/main/*", (req, res) => {
corrosion.request(req, res)
})
server.on("upgrade", (req, conn, head) => {
corrosion.upgrade(req, conn, head)
})
return server
}