Skip to content

Commit

Permalink
fixed 302 redirect issue and http post issue for cloudflare
Browse files Browse the repository at this point in the history
  • Loading branch information
netptop committed Sep 19, 2020
1 parent 3416324 commit b9ef54a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,8 @@ let Proxy = ({ProxyMiddleware, blockedSites, urlModify, httpprefix, serverName,
}
});
res.statusCode = proxyRes.statusCode

locationMod302({res, serverName, httpprefix, host, httpType})
logSave(`res.status:${res.statusCode} res.url:${res.url}, res.headers:${JSON.stringify(res.getHeaders())}`)
if (res.statusCode === 404) {
try {
Expand Down
2 changes: 1 addition & 1 deletion build/worker.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cf_convert2middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var req2middleware = (cf_request) => { // modify CF request so that it can be us
let middle_req = {
url: cf_request.url || '',
headers,
body: cf_request.body,
body: cf_request.body, // Uint8Array
bodyUsed: cf_request.bodyUsed,
cf: cf_request.cf,
method: cf_request.method,
Expand Down
8 changes: 6 additions & 2 deletions cf_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var handleRequest = (request) => { // resolve a cf_res eventually.
console.log(`sending response ...`)
middle_res.replied = true
let cf_res = middle2res({middle_res, readable})
console.log(`cf_res:${JSON.stringify(cf_res)}`)
console.log(`sent cf_res:${JSON.stringify(cf_res)}`)
resolve(cf_res)
}
if (data) {
Expand Down Expand Up @@ -74,7 +74,11 @@ var handleRequest = (request) => { // resolve a cf_res eventually.
console.log(`proxy called`)
const target = global_router(middle_req) // get target: `${httpType}://${host}`
console.log(`target:${target}`)
let middle_proxyReq = req2middleware(new Request(target, {redirect: middle2req.redirect, method: middle_req.method}))
let middle_proxyReq = req2middleware(new Request(target, {
redirect: middle_req.redirect,
method: middle_req.method,
body: middle_req.body,
}))
console.log(`calling onProxyReq...`)
global_onProxyReq(middle_proxyReq, middle_req, middle_res)
console.log(`====> after onProxyReq, middle_proxyReq:${JSON.stringify(middle_proxyReq)}`)
Expand Down
33 changes: 31 additions & 2 deletions test/cloudflare.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,36 @@ test('return a string instead of Uint8Array', async () => {
method: 'get',
url,
})
console.log(`${JSON.stringify(response.data)}`)
// console.log(`${JSON.stringify(response.data)}`)
// expect(sum(1, 2)).toBe(3);
expect(response.data.indexOf(`http://127.0.0.1:8787/https`)).not.toBe(-1)
}, 30000);
}, 30000);

test('post operation', async () => {
const url = `${httpprefix}://${serverName}:${port}/https/postman-echo.com/post`
const response = await axios({
method: 'post',
headers: {
},
url,
data: 'test data123',
})
// console.log(`${JSON.stringify(response.headers)}`)
//console.log(`${JSON.stringify(response.data)}`)
expect(JSON.stringify(response.data).indexOf(`test data123`)).not.toBe(-1)
}, 15000); // should be done within 3 seconds.

test('youtube redirect 302', async () => {
const url = `${httpprefix}://${serverName}:${port}/https/www.youtube.com/channel/UCAq_xQV8pJ2Q_KOszzaYPBg/feed?disable_polymer=1`
const response = await axios({
method: 'get',
maxRedirects: 0,
validateStatus: null, // important for status 302
headers: {
},
url,
})
// console.log(`${JSON.stringify(response.headers)}`)
//console.log(`${JSON.stringify(response.data)}`)
expect(response.headers['location'].indexOf(`https://m.youtube`)).toBe(-1)
}, 15000); // should be done within 3 seconds.

0 comments on commit b9ef54a

Please sign in to comment.