forked from netptop/siteproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (31 loc) · 1.31 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
var express = require('express')
const path = require('path')
const fs = require('fs')
let app = express()
var Proxy = require('./Proxy')
let { urlModify, httpprefix, serverName, port, locationReplaceMap302, regReplaceMap, siteSpecificReplace, pathReplace } = require('./config')
let cookieDomainRewrite = serverName
let proxy = Proxy({ urlModify, httpprefix, serverName, port, cookieDomainRewrite, locationReplaceMap302, regReplaceMap, siteSpecificReplace, pathReplace})
const middle1 = (req, res, next) => {
console.log(`req.url:${req.url}`)
const dirPath = path.join(__dirname, req.url)
let fwdStr = req.headers['x-forwarded-for']
if (fwdStr && fwdStr.split(',').length > 3) { // too many forwardings
return res.status(404).send('{"error": "too many redirects"}')
}
if (req.url === '/' || req.url === '/index.html') {
body = fs.readFileSync(path.join(__dirname, './index.html'), encoding = 'utf-8')
res.status(200).send(body)
return
} else
if (fs.existsSync(dirPath) && !fs.lstatSync(dirPath).isDirectory()) {
body = fs.readFileSync(dirPath)
return res.status(200).send(body)
}
next()
}
app.use(middle1)
app.use(proxy)
let reallistenPort = process.env.PORT || 8011
app.listen(reallistenPort)
console.log(`listening on port:${reallistenPort}`)