Skip to content

Commit

Permalink
fixed youtube issue, now we can play video on firefox fluently.
Browse files Browse the repository at this point in the history
  • Loading branch information
netptop committed Mar 9, 2020
1 parent 3139ae1 commit dfd73cd
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 32 deletions.
38 changes: 32 additions & 6 deletions Proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,22 @@ let Proxy = ({urlModify, httpprefix, serverName, port, cookieDomainRewrite, loca
if (gbFlag) {
body = iconv.encode(body, 'gbk')
}
// googlevideo.com manual redirection
if (typeof(body) === 'string' && body.startsWith(`${httpprefix}://${serverName}`) && body.indexOf('googlevideo.com') !== -1) {
// need to manually redirect it for youtube workaround.
console.log(`============== redirect googlevideo.com`)
res.setHeader('location', body)
res.statusCode = '302'
}
body = zlib.gzipSync(body)
try {
res.setHeader('content-encoding', 'gzip');
logSave(`handleRespond: res.headers:${JSON.stringify(res.getHeaders())}`)
// console.log(`1=================${logGet()}`)
logSave(`handleRespond: res.statusCode:${res.statusCode}, res.headers:${JSON.stringify(res.getHeaders())}`)
if (req.headers['debugflag']==='true') {
res.removeHeader('content-encoding')
res.setHeader('content-type','text/plain')
body=logGet()
}
res.end(body);
} catch(e) {
logSave(`error: ${e}`)
Expand Down Expand Up @@ -172,7 +183,7 @@ let Proxy = ({urlModify, httpprefix, serverName, port, cookieDomainRewrite, loca
onProxyRes: (proxyRes, req, res) => {
let {host, httpType} = getHostFromReq(req)
logSave(`proxyRes.status:${proxyRes.statusCode} proxyRes.headers:${JSON.stringify(proxyRes.headers)}`)
var body = Buffer.from('');
let body = Buffer.from('');
proxyRes.on('data', function(data) {
body = Buffer.concat([body, data]);
})
Expand Down Expand Up @@ -219,6 +230,11 @@ let Proxy = ({urlModify, httpprefix, serverName, port, cookieDomainRewrite, loca
res.setHeader(key, proxyRes.headers[key]);
}
logSave(`2: res.headers:${JSON.stringify(res.getHeaders())}`)
if (req.headers['debugflag']==='true') {
res.removeHeader('content-encoding')
res.setHeader('content-type','text/plain')
body=logGet()
}
res.end(body)
}

Expand All @@ -239,7 +255,12 @@ let Proxy = ({urlModify, httpprefix, serverName, port, cookieDomainRewrite, loca
}
handleRespond({req, res, body, gbFlag})
} else {
// console.log(`3========>${logGet()}`)
logSave(`3========>${logGet()}`)
if (req.headers['debugflag']==='true') {
res.removeHeader('content-encoding')
res.setHeader('content-type','text/plain')
body=logGet()
}
res.end(body)
}
})
Expand Down Expand Up @@ -292,8 +313,13 @@ let Proxy = ({urlModify, httpprefix, serverName, port, cookieDomainRewrite, loca
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]);
});
if (key.indexOf('x-') === 0) {
logSave(`remove key=${key},`)
proxyReq.removeHeader(key)
}
logSave(`set key=${key},`)
proxyReq.setHeader(key, req.headers[key])
})
proxyReq.setHeader('Accept-Encoding', 'gzip')
proxyReq.setHeader('referer', host)
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())}`)
Expand Down
21 changes: 8 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var Proxy = require('./Proxy')

let config = {
httpprefix: 'https', port: 443,
serverName: 'siteproxy.now.sh',
serverName: 'siteproxylocal.now.sh',
}
if (process.env.herokuAddr) {
config.serverName = process.env.herokuAddr
Expand All @@ -38,7 +38,7 @@ const urlModify = ({httpType, host, url}) => {
}
}
parsed.set('query', queryString.stringify(parsedQuery))
console.log(`after change: ${parsed.href}`)
// console.log(`after change: ${parsed.href}`)
return parsed.href
}

Expand Down Expand Up @@ -158,6 +158,7 @@ const siteSpecificReplace = {
// '(this\..\.logo\.hidden.*?[,;])': ``,
// '(&&this\..\.content\.insertBefore.*?;)': `;`, // && this.$.content.insertBefore(this.$.guide, this.$["page-manager"]);
'[&]{2}this\.connectedCallback[(][)][)]:': `):`, // &&this.connectedCallback()):
'="/sw.js"': `="/https/www.youtube.com/sw.js"`,
},
'search.yahoo.com': {
'"./ra./click"': `"\\/https\\/search.yahoo.com\\/ra\\/click"`,
Expand Down Expand Up @@ -205,21 +206,15 @@ let proxy = Proxy({urlModify, httpprefix, serverName, port, cookieDomainRewrite,

app.use((req, res, next) => {
console.log(`req.url:${req.url}`)

if (req.url === `/bg-gr-v.png`) {
body = fs.readFileSync(path.join(__dirname, './bg-gr-v.png'))
res.status(200).send(body)
return
} else
if (req.url === `/style.css`) {
body = fs.readFileSync(path.join(__dirname, './style.css'), encoding='utf-8')
res.status(200).send(body)
return
} else
const dirPath = path.join(__dirname, req.url)
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()
})
Expand Down
3 changes: 1 addition & 2 deletions logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ let logStr = ''
let logSave = (addStr) => {
if (process.env.localFlag === 'true') {
console.log(`${addStr}`)
} else {
logStr += `${addStr}\n`
}
logStr += `${addStr}\n`
}
let logGet = () => {
return logStr
Expand Down
30 changes: 19 additions & 11 deletions test/siteproxylocal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,6 @@ test('mitbbs home page img src issue', async () => {
expect(response.data.indexOf(`img src="../img`)).toBe(-1)
}, 30000);

test('boxun /hero issue', async () => {
const url = `${httpprefix}://${serverName}:${port}/https/blog.boxun.com`
const response = await axios({
method: 'get',
url,
})
// console.log(`${response.data}`)
expect(response.data.indexOf(`href=/https/github.com/hero`)).toBe(-1)
}, 30000);

test('google next click issue', async () => {
const url = `${httpprefix}://${serverName}:${port}/https/www.google.com/search?q=%E6%B5%B7%E5%A4%96%E8%AE%BA%E5%9D%9B&oq=%E6%B5%B7%E5%A4%96%E8%AE%BA%E5%9D%9B`
const response = await axios({
Expand Down Expand Up @@ -177,7 +167,7 @@ test('youtube url check issue', async () => {
url,
})
// console.log(`${JSON.stringify(response.headers)}`)
console.log(`${response.data}`)
// console.log(`${response.data}`)
expect(response.data.indexOf(`"Captions URL"`)).toBe(-1)
expect(response.data.indexOf(`("//${serverName}:${port}/https/"+this.`)).not.toBe(-1)
expect(response.data.indexOf(`://i1.ytimg.com/vi`)).toBe(-1)
Expand All @@ -200,6 +190,7 @@ test('youtube desktop_polymer_v2.js issue', async () => {
expect(response.data.indexOf(`g+("/youtubei/"`)).toBe(-1)
expect(response.data.indexOf(`"/service_ajax"`)).toBe(-1)
expect(response.data.indexOf(`&&this.connectedCallback()):`)).toBe(-1)
expect(response.data.indexOf(`="/sw.js"`)).toBe(-1)
}, 30000);


Expand All @@ -218,3 +209,20 @@ test('youtube url conversion', async () => {
// console.log(`${response.data}`)
expect(response.data.indexOf(`"\\/service_ajax\\",`)).toBe(-1)
}, 30000);

test('googlevideo.com issue', async () => {
const url = `${httpprefix}://${serverName}:${port}/https/r1---sn-n4v7sn7l.googlevideo.com/videoplayback?expire=1583814270&ei=HsJmXur9K4aikgamvbfABQ&ip=18.144.99.15&id=o-AIaKnNqqplhX9b0RsF0Kh1zyrCsOl-JkWPmg5f_y2Qr5&itag=243&aitags=133%2C134%2C135%2C136%2C137%2C160%2C242%2C243%2C244%2C247%2C248%2C278&source=youtube&requiressl=yes&mm=31%2C26&mn=sn-n4v7sn7l%2Csn-a5mekn7k&ms=au%2Conr&mv=u&mvi=0&pl=23&vprv=1&mime=video%2Fwebm&gir=yes&clen=63524647&dur=1299.598&lmt=1582963684900655&mt=1583792083&fvip=1&keepalive=yes&fexp=23842630&c=WEB&txp=5431432&sparams=expire%2Cei%2Cip%2Cid%2Caitags%2Csource%2Crequiressl%2Cvprv%2Cmime%2Cgir%2Cclen%2Cdur%2Clmt&sig=ADKhkGMwRAIgRS5K8gl7Lo2QwK2z00wSbdPS36IrpaTo2OcEMd5h8j4CIDUtLjxXZAClS1mVDGEKH8nwtOz_aQRL72KhkGsEcx1_&lsparams=mm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl&lsig=ABSNjpQwRAIgX9FV_e-Uoi-euNxsIjfX0Azwgv9N4pThqdLAyUtvTMoCIEgSdyw6mI2gY3ryLd9pkGH7pXcaXlVjpvuH6yAlGibq&alr=yes&cpn=hwivuvx84GVZ6T0m&cver=2.20200304.01.00&range=113485-231845&rn=8&rbuf=2176`
const response = await axios({
method: 'get',
headers: {
'Accept': `text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9`,
'debugflag': 'true',
// maxRedirects: 0,
// validateStatus: null, // important for status 302
},
url,
})
// console.log(`${JSON.stringify(response.headers)}`)
console.log(`${response.data}`)
expect(response.data.indexOf(`desktop_polymer_v2`)).toBe(-1)
}, 30000);

0 comments on commit dfd73cd

Please sign in to comment.