Skip to content

Commit

Permalink
Added parse FinalUrl on the JS side & Updated README.md (#283)
Browse files Browse the repository at this point in the history
Co-authored-by: Kadi Gasanguseynov <[email protected]>
  • Loading branch information
lif0 and Kadi Gasanguseynov authored Nov 22, 2023
1 parent 7c67106 commit 963daac
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]:443'
proxy: 'http://username:[email protected]:443',
insecureSkipVerify: true
}, 'get');

console.log(response);
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
}
);

```

Expand All @@ -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/"
}
);

```

Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface CycleTLSRequestOptions {
timeout?: number;
disableRedirect?: boolean;
headerOrder?: string[];
insecureSkipVerify?: boolean;
}

export interface CycleTLSResponse {
Expand All @@ -53,6 +54,7 @@ export interface CycleTLSResponse {
headers: {
[key: string]: any;
};
finalUrl: string;
}

let child: ChildProcessWithoutNullStreams;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -393,14 +396,15 @@ 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("/,/");
resolveRequest({
status,
body,
headers,
finalUrl,
});
});
});
Expand Down
45 changes: 45 additions & 0 deletions tests/disableRedirect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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();
});

0 comments on commit 963daac

Please sign in to comment.