Skip to content

Commit

Permalink
chore(fetch): add retry option
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Dec 1, 2022
1 parent 6daa52f commit 16eb3ec
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
3 changes: 0 additions & 3 deletions .github/FUNDING.yml

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-iframe",
"version": "1.8.5",
"version": "1.8.9",
"description": "create iframes on your server to bypass CORS. Reverse engineer security issues.",
"main": "dist/iframe.js",
"scripts": {
Expand Down
17 changes: 11 additions & 6 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,32 @@ if (!fetcher) {
}

const getHttp = (url: string) =>
url.includes("https://") ? https : http;
url.startsWith("https://") ? https : http;

const get = (
url: string,
options?: {
headers?: Record<string, any>;
agent?: string | (() => string);
}
},
retry?: number
) => {
const { agent, headers } = options ?? {};
let fetchOptions = {};

if (typeof agent !== "undefined") {
if (typeof agent === "function") {
const ua = agent();
if (typeof ua !== "undefined") {
options = { agent: ua };
fetchOptions = { agent: ua };
}
} else {
options = { agent };
fetchOptions = { agent };
}
}

if (typeof headers !== "undefined") {
options = { ...options, headers };
fetchOptions = { ...fetchOptions, headers };
}

const httpMethod = getHttp(url);
Expand All @@ -59,7 +61,10 @@ if (!fetcher) {
let body = "";

httpMethod
.get(url, options, (res) => {
.get(url, fetchOptions, (res) => {
if((res.statusCode === 301 || res.statusCode === 302) && retry) {
return get(res.headers.location, options, --retry)
}
res.setEncoding("utf8");

res.on("data", (d) => {
Expand Down
13 changes: 7 additions & 6 deletions src/iframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ function configureAgent() {
}

// NOTE: control type like wappalyzer for usage only on websites that use specefic frameworks like old versions of react, angular, vue, and etc
const mutateSource = async ({ src = "", key }, url, $html, headers) => {
const mutateSource = async ({ src = "", key }, url, $html, headers, retry) => {
if (src && src[0] === "/") {
try {
const res = await fetcher(`${url}/${src}`, {
headers,
agent,
});

}, retry);
if (res) {
const source = typeof process !== "undefined" ? res : await res.text();
$html(key).html(source);
Expand All @@ -109,7 +109,7 @@ function renderErrorHtml({ url, server, noPage = false }) {
}

async function renderHtml(
{ url, baseHref, config, head = {} },
{ url, baseHref, config, head = {}, retry },
server = false
) {
if (!url) {
Expand All @@ -135,7 +135,8 @@ async function renderHtml(
response = await fetcher(url, {
headers: head,
agent,
});
},
retry);
} catch (e) {
console.error(e);
}
Expand Down Expand Up @@ -178,7 +179,7 @@ async function renderHtml(
for (const com of inlineMutations) {
const { key, attribute, src } = com;
const element = `${key}[${attribute}="${src}"]`;
await mutateSource({ key: element, src }, url, $html, headers);
await mutateSource({ key: element, src }, url, $html, headers, retry);
$html(element).removeAttr(attribute);
}

Expand Down

0 comments on commit 16eb3ec

Please sign in to comment.