Skip to content

Commit

Permalink
fixed google search issue on firefox.
Browse files Browse the repository at this point in the history
  • Loading branch information
netptop committed Mar 7, 2020
1 parent 9aad2b7 commit 6efe746
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
19 changes: 13 additions & 6 deletions Proxy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var express = require('express');
var proxy = require('http-proxy-middleware');
const zlib = require("zlib")
const parse = require('url-parse')
const cookiejar = require('cookiejar')
const iconv = require('iconv-lite')
const {logSave, logGet, logClear} = require('./logger')
Expand Down Expand Up @@ -72,15 +73,21 @@ let getHostFromReq = (req) => { //return target
httpType = 'http'
logSave(`============= host:${host}, req referer:${req.headers['referer']}`)
} else if (req.headers['referer']) { // 'zh.wikipedia.org'
host = req.headers['referer']
httpType = 'https'
var parsed = parse(req.headers['referer'])
if(parsed.hostname) {
host = parsed.hostname
httpType = parsed.protocol.replace(':', '')
} else {
host = req.headers['referer']
httpType = 'https'
}
}
logSave(`getHostFromReq, req.url:${req.url}, referer:${req.headers['referer']}, host:${host}, httpType:${httpType}`)
return {host, httpType}
}


let Proxy = ({httpprefix, serverName, port, cookieDomainRewrite, locationReplaceMap302, regReplaceMap, siteSpecificReplace, pathReplace}) => {
let Proxy = ({urlModify, httpprefix, serverName, port, cookieDomainRewrite, locationReplaceMap302, regReplaceMap, siteSpecificReplace, pathReplace}) => {
let handleRespond = ({req, res, body, gbFlag}) => {
// logSave("res from proxied server:", body);
let myRe
Expand Down Expand Up @@ -151,7 +158,7 @@ let Proxy = ({httpprefix, serverName, port, cookieDomainRewrite, locationReplace
onError: (err, req, res) => {
logSave(`onerror: ${JSON.stringify(err)}`)
try {
if (err.reason.indexOf('Expected') === -1) {
if (err.reason && err.reason.indexOf('Expected') === -1) {
res.status(404).send(`{"error": "${err}"}`)
}
} catch(e) {
Expand Down Expand Up @@ -275,15 +282,15 @@ let Proxy = ({httpprefix, serverName, port, cookieDomainRewrite, locationReplace
if ('origin' in req.headers) {
req.headers['origin'] = host
}
proxyReq.path = proxyReq.url = urlModify({httpType, host, url:req.url})
let newpath = req.url.replace(`/${httpType}/${host}`, '') || '/'
logSave(`httpType:${httpType}, host:${host}, req.url:${req.url}, req.headers:${JSON.stringify(req.headers)}`)
Object.keys(req.headers).forEach(function (key) {
proxyReq.setHeader(key, req.headers[key]);
});
proxyReq.setHeader('Accept-Encoding', 'gzip')
proxyReq.setHeader('referer', host)
proxyReq.path = newpath
logSave(`req host:${host}, req.url:${req.url}, proxyReq.path:${proxyReq.path}, proxyReq.url:${proxyReq.url} proxyReq headers:${JSON.stringify(proxyReq.getHeaders())}`)
logSave(`req host:${host}, req.url:${req.url}, proxyReq.query:${proxyReq.query} proxyReq.path:${proxyReq.path}, proxyReq.url:${proxyReq.url} proxyReq headers:${JSON.stringify(proxyReq.getHeaders())}`)
if(host === '' || !host) {
logSave(`------------------ sending status 404`)
res.status(404).send("{}")
Expand Down
20 changes: 19 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
var express = require('express')
const zlib = require("zlib")
const fs = require("fs")
const queryString = require('query-string')
const parse = require('url-parse')

const cookiejar = require('cookiejar')
const {CookieAccessInfo, CookieJar, Cookie} = cookiejar
const path = require('path')
Expand All @@ -23,6 +26,19 @@ if (process.env.localFlag === 'true') {

let {httpprefix, serverName, port, accessCode} = config

const urlModify = ({httpType, host, url}) => {
// this url is actually a partial url, without https://${host}:${port}
let newpath = url.replace(`/${httpType}/${host}`, '') || '/'
var parsed = parse(newpath)
const parsedQuery = queryString.parse(parsed.query)
if (host.indexOf('www.google.com') !== -1) {
// delete parsedQuery['ved']
}
parsed.set('query', queryString.stringify(parsedQuery))
console.log(`after change: ${parsed.href}`)
return parsed.href
}

const locationReplaceMap302 = ({location, serverName, httpprefix, host, httpType}) => {
let myRe
if (!location) {
Expand Down Expand Up @@ -110,6 +126,8 @@ const siteSpecificReplace = {
'continue=.+?"': 'continue="', // fix the gmail login issue.
's_mda=/.https:(././).+?/http/': `s_mda=/^http:$1`, // recover Ybs regular expression
'href="/https/www.google.com/g(.;)': 'href="/g$1',
'[\(]"/url': `\("/https/www.google.com/url`, //s_Gj("/url?sa=t&source=web&rct=j");s_Nj
'"/url"': `"/https/www.google.com/url"`,
},
'www.gstatic.com': {
'href="/https/www.gstatic.com/g(.;)': 'href="/g$1',
Expand Down Expand Up @@ -180,7 +198,7 @@ const siteSpecificReplace = {
let app = express()
let cookieDomainRewrite = serverName

let proxy = Proxy({httpprefix, serverName, port, cookieDomainRewrite, locationReplaceMap302, regReplaceMap, siteSpecificReplace, pathReplace})
let proxy = Proxy({urlModify, httpprefix, serverName, port, cookieDomainRewrite, locationReplaceMap302, regReplaceMap, siteSpecificReplace, pathReplace})

app.use((req, res, next) => {
console.log(`req.url:${req.url}`)
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
"cookiejar": "^2.1.2",
"express": "^4.17.1",
"http-proxy-middleware": "^0.20.0",
"iconv-lite": "^0.5.1"
"iconv-lite": "^0.5.1",
"query-string": "^6.11.1",
"url-parse": "^1.4.7"
},
"devDependencies": {
"axios": "^0.19.2",
Expand Down

0 comments on commit 6efe746

Please sign in to comment.