Skip to content

Commit

Permalink
added ORIGINALHOST cookie to fix host="" issue
Browse files Browse the repository at this point in the history
  • Loading branch information
netptop committed Sep 5, 2020
1 parent fe14d56 commit dcde239
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
41 changes: 34 additions & 7 deletions Proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var redirect2HomePage = function({res, httpprefix, serverName,} ) {
res.status(302).send(``)
}

let getHostFromReq = (req) => { //return target
let getHostFromReq = ({req, serverName}) => { //return target
// url: http://127.0.0.1:8011/https/www.youtube.com/xxx/xxx/...
let https_prefix = '/https/'
let http_prefix = '/http/'
Expand Down Expand Up @@ -106,6 +106,23 @@ let getHostFromReq = (req) => { //return target
httpType = 'https'
}
}
let originalHost = ''
if (req.headers['cookie']) {
let cookiesList = req.headers['cookie'].split(' ')
.map(str => new cookiejar.Cookie(str))
.map(cookie => {
if (cookie.name === 'ORIGINALHOST') {
originalHost = cookie.value
}
})
}
if (host === '') {
if (originalHost !== '') {
httpType = originalHost.split('/')[0]
host = originalHost.split('/')[1]
logSave(`getHostFromReq, use ORIGINALHOST, ${httpType},${host}`)
}
}
logSave(`getHostFromReq, req.url:${req.url}, referer:${req.headers['referer']}, host:${host}, httpType:${httpType}`)
return {host, httpType}
}
Expand All @@ -115,7 +132,7 @@ let Proxy = ({blockedSites, urlModify, httpprefix, serverName, port, cookieDomai
let stream = fs.createWriteStream("web-records.csv", {flags:'a'})
let handleRespond = ({req, res, body, gbFlag}) => { // text file
let myRe
let {host, httpType} = getHostFromReq(req)
let {host, httpType} = getHostFromReq({req, serverName})
let location = res.getHeaders()['location']
if (res.statusCode == '301' || res.statusCode == '302' || res.statusCode == '303' ||res.statusCode == '307' || res.statusCode == '308') {
location = locationReplaceMap302({location, serverName, httpprefix, host, httpType})
Expand Down Expand Up @@ -193,7 +210,7 @@ let Proxy = ({blockedSites, urlModify, httpprefix, serverName, port, cookieDomai
let myRe = new RegExp(`/http[s]?/${serverName}.*?/`, 'g') // match group
req.url = req.url.replace(myRe, '/')

let {host, httpType} = getHostFromReq(req)
let {host, httpType} = getHostFromReq({req, serverName})
let target = `${httpType}://${host}`
logSave(`router, target:${target}, req.url:${req.url}`)
return target
Expand All @@ -204,7 +221,7 @@ let Proxy = ({blockedSites, urlModify, httpprefix, serverName, port, cookieDomai
router,
/*
pathRewrite: (path, req) => {
let {host, httpType} = getHostFromReq(req)
let {host, httpType} = getHostFromReq({req, serverName})
let newpath = path.replace(`/https/${host}`, '') || '/'
logSave(`newpath:${newpath}`)
return newpath
Expand Down Expand Up @@ -233,7 +250,7 @@ let Proxy = ({blockedSites, urlModify, httpprefix, serverName, port, cookieDomai
},
selfHandleResponse: true, // so that the onProxyRes takes care of sending the response
onProxyRes: (proxyRes, req, res) => {
let {host, httpType} = getHostFromReq(req)
let {host, httpType} = getHostFromReq({req, serverName})
logSave(`proxyRes.status:${proxyRes.statusCode} proxyRes.headers:${JSON.stringify(proxyRes.headers)}`)
let bodyList = []
let bodyLength = 0
Expand Down Expand Up @@ -361,6 +378,14 @@ let Proxy = ({blockedSites, urlModify, httpprefix, serverName, port, cookieDomai
return cookie
})
.map(cookie => cookie.toString())
let cookie_originalHost= new cookiejar.Cookie()
cookie_originalHost.name = 'ORIGINALHOST'
cookie_originalHost.value = `${httpType}/${host}`
cookie_originalHost.domain = `${serverName}`
cookie_originalHost.expiration_date = datestr
cookie_originalHost.path = `/`
cookie_originalHost.secure = false
modifiedSetCookieHeaders.push(cookie_originalHost.toString())
proxyRes.headers['set-cookie'] = modifiedSetCookieHeaders
Object.keys(proxyRes.headers).forEach(function (key) {
if (key === 'content-security-policy' ||
Expand All @@ -384,7 +409,9 @@ let Proxy = ({blockedSites, urlModify, httpprefix, serverName, port, cookieDomai
logSave(`res.status:${res.statusCode} res.url:${res.url}, res.headers:${JSON.stringify(res.getHeaders())}`)
if (res.statusCode === 404) {
try {
delete res.headers['content-length'] //remove content-length field
if (res.headers && res.headers['content-length']) {
delete res.headers['content-length'] //remove content-length field
}
redirect2HomePage({res, httpprefix, serverName,})
} catch(e) {
logSave(`error: ${e}`)
Expand All @@ -401,7 +428,7 @@ let Proxy = ({blockedSites, urlModify, httpprefix, serverName, port, cookieDomai

let fwdStr = req.headers['X-Forwarded-For'] || req.headers['x-forwarded-for']

let {host, httpType} = getHostFromReq(req)
let {host, httpType} = getHostFromReq({req, serverName})
for (let i=0; i<blockedSites.length; i++) {
let site = blockedSites[i]
if (site === host) {
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ user browser +-------------->+ siteproxy +-------> wikipedia
1. 注册一个heroku账户: https://www.heroku.com/
2. 没有github账户的话, 注册一个github账户,fork本repo
3. 在heroku的控制台里面创建一个应用, 且绑定到你刚才fork的repo上, 会得到一个域名类似的域名:your-domain-name.herokuapp.com
4. 在github上修改你刚fork的repo, 将procfile里的域名修改为你的新域名:
4. 在heroku的'Deloy'页面, 点击'Enable Automatic Deploys'按钮
5. 在github上修改你刚fork的repo, 将procfile里的域名修改为你的新域名(记得不要加https前缀):
"web: herokuAddr=siteproxy.herokuapp.com npm run start"
====> "web: herokuAddr=your-domain-name.herokuapp.com npm run start"
5. 现在可以在浏览器里面访问你的新域名了: https://your-domain-name.herokuapp.com
6. 现在可以在浏览器里面访问你的新域名了: https://your-domain-name.herokuapp.com
```
## 部署到vps服务器
```
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ <h1 class="logo">
</a>
</div>
<div class="col-md-3 col-sm-4 col-xs-6">
<a href="/http/news.creaders.net/headline/" class="card-link" target="_blank">
<a href="/https/news.creaders.net/breaking/" class="card-link" target="_blank">
<div class="card-tit">万维读者</div>
</a>
</div>
Expand Down

0 comments on commit dcde239

Please sign in to comment.