From 963daacd92414dc84a0ec6930dd252381cb1247a Mon Sep 17 00:00:00 2001 From: Kadi <22912194+lif0@users.noreply.github.com> Date: Thu, 23 Nov 2023 00:56:47 +0300 Subject: [PATCH] Added parse FinalUrl on the JS side & Updated README.md (#283) Co-authored-by: Kadi Gasanguseynov --- README.md | 16 ++++++++----- src/index.ts | 6 ++++- tests/disableRedirect.test.ts | 45 +++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 4ad875bd..2958bf4e 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,8 @@ const initCycleTLS = require('cycletls'); body: '', ja3: '771,4865-4867-4866-49195-49199-52393-52392-49196-49200-49162-49161-49171-49172-51-57-47-53-10,0-23-65281-10-11-35-16-5-51-43-13-45-28-21,29-23-24-25-256-257,0', userAgent: 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:87.0) Gecko/20100101 Firefox/87.0', - proxy: 'http://username:password@hostname.com:443' + proxy: 'http://username:password@hostname.com:443', + insecureSkipVerify: true }, 'get'); console.log(response); @@ -122,6 +123,7 @@ func main() { Body : "", Ja3: "771,4865-4867-4866-49195-49199-52393-52392-49196-49200-49162-49161-49171-49172-51-57-47-53-10,0-23-65281-10-11-35-16-5-51-43-13-45-28-21,29-23-24-25-256-257,0", UserAgent: "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:87.0) Gecko/20100101 Firefox/87.0", + InsecureSkipVerify: true, }, "GET"); if err != nil { log.Print("Request Failed: " + err.Error()) @@ -215,11 +217,12 @@ Url is not optional, config is optional // Amount of seconds before request timeout (default: 7) timeout: 2, // Toggle if CycleTLS should follow redirects - disableRedirect: true + disableRedirect: true, // Custom header order to send with request (This value will overwrite default header order) - headerOrder: ["cache-control", "connection", "host"] + headerOrder: ["cache-control", "connection", "host"], + // Toggle if CycleTLS should skip verify certificate (If InsecureSkipVerify is true, TLS accepts any certificate presented by the server and any host name in that certificate.) + insecureSkipVerify: true } -); ``` @@ -235,9 +238,10 @@ Url is not optional, config is optional headers: { "some": "header", ... - } + }, + // FinalUrl returned from the server (String). This field is useful when redirection is active. + finalUrl: "https://final.url/" } -); ``` diff --git a/src/index.ts b/src/index.ts index b60d9ccf..0d0ecc8d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -43,6 +43,7 @@ export interface CycleTLSRequestOptions { timeout?: number; disableRedirect?: boolean; headerOrder?: string[]; + insecureSkipVerify?: boolean; } export interface CycleTLSResponse { @@ -53,6 +54,7 @@ export interface CycleTLSResponse { headers: { [key: string]: any; }; + finalUrl: string; } let child: ChildProcessWithoutNullStreams; @@ -362,6 +364,7 @@ const initCycleTLS = async ( options.userAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36"; if (!options?.body) options.body = ""; if (!options?.proxy) options.proxy = ""; + if (!options?.insecureSkipVerify) options.insecureSkipVerify = true; //convert simple cookies const cookies = options?.cookies; @@ -393,7 +396,7 @@ const initCycleTLS = async ( response.Body[util.inspect.custom] = function(){ return JSON.stringify( this, undefined, 2); } } catch (e) {} - const { Status: status, Body: body, Headers: headers } = response; + const { Status: status, Body: body, Headers: headers, FinalUrl: finalUrl } = response; if (headers["Set-Cookie"]) headers["Set-Cookie"] = headers["Set-Cookie"].split("/,/"); @@ -401,6 +404,7 @@ const initCycleTLS = async ( status, body, headers, + finalUrl, }); }); }); diff --git a/tests/disableRedirect.test.ts b/tests/disableRedirect.test.ts index bc0703fd..eec01070 100644 --- a/tests/disableRedirect.test.ts +++ b/tests/disableRedirect.test.ts @@ -19,6 +19,11 @@ test("Should return a 301 redirect", async () => { "get" ); expect(redirectResponse.status).toBe(301); + await cycleTLS.exit(); +}); + +test("Should return a 200 redirect", async () => { + const cycleTLS = await initCycleTLS({ port: 9121 }); const normalResponse = await cycleTLS( "https://google.com", @@ -35,3 +40,43 @@ test("Should return a 301 redirect", async () => { cycleTLS.exit(); }); + +test("Should return final url a 301 redirect", async () => { + const cycleTLS = await initCycleTLS({ port: 9122 }); + const url = "https://rb.gy/3hwz5h"; + const redirectResponse = await cycleTLS( + url, + { + body: "", + ja3: ja3, + userAgent: userAgent, + disableRedirect: true, + }, + "get" + ); + expect(redirectResponse.status).toBe(301); + expect(redirectResponse.finalUrl).toBe(url) + + cycleTLS.exit(); +}); + +test("Should return final url a 200 redirect", async () => { + const cycleTLS = await initCycleTLS({ port: 9124 }); + const url = "https://rb.gy/3hwz5h"; + + const normalResponse = await cycleTLS( + url, + { + body: "", + ja3: ja3, + userAgent: userAgent, + disableRedirect: false, + }, + "get" + ); + + expect(normalResponse.status).toBe(200); + expect(normalResponse.finalUrl).toBe("https://www.google.com/"); + + cycleTLS.exit(); +}); \ No newline at end of file