Fix request.post method and clean error traces

This commit is contained in:
ngosang 2021-10-20 01:00:35 +02:00
parent d16b982bb9
commit c4f890f9a1
2 changed files with 18 additions and 9 deletions

View File

@ -124,9 +124,14 @@ async function resolveChallenge(params: V1Request, session: SessionsCacheItem):
} }
async function gotoPage(params: V1Request, page: Page): Promise<Response> { async function gotoPage(params: V1Request, page: Page): Promise<Response> {
const response = await page.goto(params.url, { waitUntil: 'domcontentloaded' }); let response: Response;
if (params.method == 'POST') { if (params.method != 'POST') {
response = await page.goto(params.url, {waitUntil: 'domcontentloaded'});
} else {
// post hack // post hack
// first request a page without cloudflare
response = await page.goto("https://www.google.com", {waitUntil: 'domcontentloaded'});
await page.setContent( await page.setContent(
` `
<!DOCTYPE html> <!DOCTYPE html>
@ -167,7 +172,11 @@ async function gotoPage(params: V1Request, page: Page): Promise<Response> {
</html> </html>
` `
); );
await page.waitFor(2000); await page.waitFor(2000)
try {
await page.waitForNavigation({waitUntil: 'domcontentloaded', timeout: 2000})
} catch (e) {}
} }
return response return response
} }
@ -193,7 +202,7 @@ export async function browserRequest(params: V1Request): Promise<ChallengeResolu
try { try {
return await resolveChallengeWithTimeout(params, session) return await resolveChallengeWithTimeout(params, session)
} catch (error) { } catch (error) {
throw Error("Unable to process browser request. Error: " + error) throw Error("Unable to process browser request. " + error)
} finally { } finally {
if (oneTimeSession) { if (oneTimeSession) {
await sessions.destroy(session.sessionId) await sessions.destroy(session.sessionId)

View File

@ -209,7 +209,7 @@ describe("Test '/v1' path", () => {
* edit => Syslog Off * edit => Syslog Off
* sudo tinyproxy -d * sudo tinyproxy -d
* sudo tail -f /tmp/tinyproxy.log * sudo tail -f /tmp/tinyproxy.log
*/ */
const payload = { const payload = {
"cmd": "request.get", "cmd": "request.get",
"url": googleUrl, "url": googleUrl,
@ -238,7 +238,7 @@ describe("Test '/v1' path", () => {
* add => BasicAuth testuser testpass * add => BasicAuth testuser testpass
* sudo tinyproxy -d * sudo tinyproxy -d
* sudo tail -f /tmp/tinyproxy.log * sudo tail -f /tmp/tinyproxy.log
*/ */
const payload = { const payload = {
"cmd": "request.get", "cmd": "request.get",
"url": googleUrl, "url": googleUrl,
@ -272,7 +272,7 @@ describe("Test '/v1' path", () => {
const apiResponse: V1ResponseSolution = response.body; const apiResponse: V1ResponseSolution = response.body;
expect(apiResponse.status).toBe("error"); expect(apiResponse.status).toBe("error");
expect(apiResponse.message).toBe("Error: Unable to process browser request. Error: Error: NS_ERROR_PROXY_CONNECTION_REFUSED at https://www.google.com"); expect(apiResponse.message).toBe("Error: Unable to process browser request. Error: NS_ERROR_PROXY_CONNECTION_REFUSED at https://www.google.com");
}); });
test("Cmd 'request.get' should return fail with timeout", async () => { test("Cmd 'request.get' should return fail with timeout", async () => {
@ -286,7 +286,7 @@ describe("Test '/v1' path", () => {
const apiResponse: V1ResponseBase = response.body; const apiResponse: V1ResponseBase = response.body;
expect(apiResponse.status).toBe("error"); expect(apiResponse.status).toBe("error");
expect(apiResponse.message).toBe("Error: Unable to process browser request. Error: Error: Maximum timeout reached. maxTimeout=10 (ms)"); expect(apiResponse.message).toBe("Error: Unable to process browser request. Error: Maximum timeout reached. maxTimeout=10 (ms)");
expect(apiResponse.startTimestamp).toBeGreaterThan(1000); expect(apiResponse.startTimestamp).toBeGreaterThan(1000);
expect(apiResponse.endTimestamp).toBeGreaterThan(apiResponse.startTimestamp); expect(apiResponse.endTimestamp).toBeGreaterThan(apiResponse.startTimestamp);
expect(apiResponse.version).toBe(version); expect(apiResponse.version).toBe(version);
@ -302,7 +302,7 @@ describe("Test '/v1' path", () => {
const apiResponse: V1ResponseBase = response.body; const apiResponse: V1ResponseBase = response.body;
expect(apiResponse.status).toBe("error"); expect(apiResponse.status).toBe("error");
expect(apiResponse.message).toBe("Error: Unable to process browser request. Error: Error: NS_ERROR_UNKNOWN_HOST at https://www.google.combad"); expect(apiResponse.message).toBe("Error: Unable to process browser request. Error: NS_ERROR_UNKNOWN_HOST at https://www.google.combad");
}); });
test("Cmd 'request.get' should accept deprecated params", async () => { test("Cmd 'request.get' should accept deprecated params", async () => {