2021-02-27 17:28:08 -05:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2021-02-27 17:12:11 -05:00
|
|
|
const express = require('express')
|
|
|
|
const netApi = require('net-browserify')
|
|
|
|
const request = require('request')
|
|
|
|
const compression = require('compression')
|
2021-02-27 17:35:59 -05:00
|
|
|
const path = require('path')
|
2021-02-27 17:12:11 -05:00
|
|
|
|
|
|
|
// Create our app
|
|
|
|
const app = express()
|
|
|
|
|
|
|
|
app.use(function (req, res, next) {
|
|
|
|
res.header('Access-Control-Allow-Origin', req.get('Origin') || '*')
|
|
|
|
res.header('Access-Control-Allow-Credentials', 'true')
|
|
|
|
res.header('Access-Control-Allow-Methods', 'GET,HEAD,PUT,PATCH,POST,DELETE')
|
|
|
|
res.header('Access-Control-Expose-Headers', 'Content-Length')
|
|
|
|
res.header(
|
|
|
|
'Access-Control-Allow-Headers',
|
|
|
|
'Accept, Authorization, Content-Type, X-Requested-With, Range'
|
|
|
|
)
|
|
|
|
if (req.method === 'OPTIONS') {
|
|
|
|
return res.send(200)
|
|
|
|
} else {
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.use(compression())
|
|
|
|
app.use(netApi())
|
2021-02-27 17:35:59 -05:00
|
|
|
app.use(express.static(path.join(__dirname, './public')))
|
2021-02-27 17:12:11 -05:00
|
|
|
|
2021-03-01 03:41:12 -05:00
|
|
|
app.use(express.json({ limit: '100kb' }))
|
2021-02-27 17:12:11 -05:00
|
|
|
|
|
|
|
app.all('*', function (req, res, next) {
|
|
|
|
// Set CORS headers: allow all origins, methods, and headers: you may want to lock this down in a production environment
|
|
|
|
res.header('Access-Control-Allow-Origin', '*')
|
|
|
|
res.header('Access-Control-Allow-Methods', 'GET, PUT, PATCH, POST, DELETE')
|
|
|
|
res.header(
|
|
|
|
'Access-Control-Allow-Headers',
|
|
|
|
req.header('access-control-request-headers')
|
|
|
|
)
|
|
|
|
|
|
|
|
if (req.method === 'OPTIONS') {
|
|
|
|
// CORS Preflight
|
|
|
|
res.send()
|
|
|
|
} else {
|
|
|
|
const targetURL = req.header('Target-URL')
|
|
|
|
if (!targetURL) {
|
|
|
|
res.status(404).send({ error: '404 Not Found' })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const newHeaders = req.headers
|
|
|
|
newHeaders.host = targetURL
|
|
|
|
.replace('https://', '')
|
|
|
|
.replace('http://', '')
|
|
|
|
.split('/')[0]
|
|
|
|
request(
|
|
|
|
{
|
|
|
|
url: targetURL + req.url,
|
|
|
|
method: req.method,
|
|
|
|
json: req.body,
|
|
|
|
headers: req.headers
|
|
|
|
},
|
|
|
|
function (error, response, body) {
|
|
|
|
if (error) {
|
|
|
|
console.error(error)
|
|
|
|
console.error('error: ' + response.statusCode)
|
|
|
|
}
|
|
|
|
// console.log(body);
|
|
|
|
}
|
|
|
|
).pipe(res)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Start the server
|
2021-03-03 03:59:36 -05:00
|
|
|
const server = app.listen(process.argv[2] === undefined ? 8080 : process.argv[2], function () {
|
2021-02-27 17:12:11 -05:00
|
|
|
console.log('Server listening on port ' + server.address().port)
|
|
|
|
})
|