Skip to content

Commit

Permalink
fetch domain stacks from cf rader
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmoudgalalz committed May 20, 2024
1 parent 7536d13 commit 9310844
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"@radix-ui/react-slot": "^1.0.2",
"@radix-ui/react-toast": "^1.1.5",
"@tanstack/react-table": "^8.13.2",
"axios": "^1.7.1",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.0",
"cmdk": "^0.2.1",
Expand Down
92 changes: 92 additions & 0 deletions src/main/api/project.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-disable no-plusplus */
/* eslint-disable import/prefer-default-export */
/* eslint-disable no-console */
import fs from 'fs';
import path from 'path';
import axios from 'axios';
import { defaultToolObj } from '../util';
import { connectJson } from '../db/connect';

Expand Down Expand Up @@ -51,6 +53,96 @@ export function createJsonFile(name: string, domain: string) {
}
}

async function getResults(projectUUID: string) {
try {
const response = await axios.get(
`https://api.cloudflare.com/client/v4/accounts/3cce5a88886b46f56d9ff989b715a588/urlscanner/scan/${projectUUID}`,
{
headers: {
Authorization: 'Bearer -Oiz0_Rh5LTRpiA24XSH_eTyr5CqR0BrnOwRhQLx',
},
},
);
return response;
} catch (error) {
console.error('Error retrieving results:', error);
throw error;
}
}

export async function createRequestToUrlScanner(name: string, domain: string) {
try {
const response = await axios.post(
'https://api.cloudflare.com/client/v4/accounts/3cce5a88886b46f56d9ff989b715a588/urlscanner/scan/',
{
url: `https://${domain}`,
},
{
headers: {
Authorization: 'Bearer -Oiz0_Rh5LTRpiA24XSH_eTyr5CqR0BrnOwRhQLx',
},
},
);
if (response.status === 200) {
const projectUUID = response.data.result.uuid;

let attempts = 0;
const maxAttempts = 5; // Maximum number of retries
const retryDelay = 2000; // Delay between retries in milliseconds

const checkResults = async () => {
try {
const resultResponse = await getResults(projectUUID);
if (resultResponse.status === 202 && attempts < maxAttempts) {
attempts++;
console.log(`Retrying in ${retryDelay / 1000} seconds...`);
setTimeout(checkResults, retryDelay);
} else {
fs.writeFileSync(
path.join(`${PROJECT_DIR}/${name}`, 'cf_scan.json'),
JSON.stringify(resultResponse.data.result),
'utf-8',
);
}
} catch (error) {
console.error('Error retrieving results:', error);
}
};

setTimeout(checkResults, retryDelay);
}
} catch (error: any) {
if (error.response.status === 409) {
const { data } = error.response;
const projectUUID = data.result.tasks[0].uuid;
let attempts = 0;
const maxAttempts = 5; // Maximum number of retries
const retryDelay = 2000; // Delay between retries in milliseconds

const checkResults = async () => {
try {
const resultResponse = await getResults(projectUUID);
if (resultResponse.status === 202 && attempts < maxAttempts) {
attempts++;
console.log(`Retrying in ${retryDelay / 1000} seconds...`);
setTimeout(checkResults, retryDelay);
} else {
fs.writeFileSync(
path.join(`${PROJECT_DIR}/${name}`, 'cf_scan.json'),
JSON.stringify(resultResponse.data.result),
'utf-8',
);
}
} catch (error) {
console.error('Error retrieving results:', error);
}
};

setTimeout(checkResults, retryDelay);
}
}
}

export function appendDateToJson(projectName: string, data: Object) {
try {
const blob = fs.readFileSync(
Expand Down
2 changes: 2 additions & 0 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
PROJECT_DIR,
createJsonFile,
createProjectDir,
createRequestToUrlScanner,
projectDetails,
readDirectoryNames,
} from './api/project';
Expand Down Expand Up @@ -118,6 +119,7 @@ ipcMain.handle('create-project', async (event, args) => {
try {
createProjectDir(projectName);
createJsonFile(projectName, domain);
await createRequestToUrlScanner(projectName, domain);
return { error: false };
} catch (err) {
return { error: true };
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/renderer/components/projectCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function ProjectCard({ name }: { name: string }) {
<CardDescription>{project.domain}</CardDescription>
</CardHeader>
<CardContent>
<p>{formatDistanceToNow(project.updatedAt)}</p>
<p>{formatDistanceToNow(project.updatedAt ?? Date.now())}</p>
</CardContent>
<CardFooter>
<p>Card Footer</p>
Expand Down
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3491,6 +3491,15 @@ axe-core@^4.6.2:
resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.8.4.tgz#90db39a2b316f963f00196434d964e6e23648643"
integrity sha512-CZLSKisu/bhJ2awW4kJndluz2HLZYIHh5Uy1+ZwDRkJi69811xgIXXfdU9HSLX0Th+ILrHj8qfL/5wzamsFtQg==

axios@^1.7.1:
version "1.7.1"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.7.1.tgz#522145622a09dfaf49359837db9649ff245a35b9"
integrity sha512-+LV37nQcd1EpFalkXksWNBiA17NZ5m5/WspmHGmZmdx1qBOg/VNq/c4eRJiA9VQQHBOs+N0ZhhdU10h2TyNK7Q==
dependencies:
follow-redirects "^1.15.6"
form-data "^4.0.0"
proxy-from-env "^1.1.0"

axobject-query@^3.1.1, axobject-query@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-3.2.1.tgz#39c378a6e3b06ca679f29138151e45b2b32da62a"
Expand Down Expand Up @@ -5844,6 +5853,11 @@ follow-redirects@^1.0.0:
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.5.tgz#54d4d6d062c0fa7d9d17feb008461550e3ba8020"
integrity sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw==

follow-redirects@^1.15.6:
version "1.15.6"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.6.tgz#7f815c0cda4249c74ff09e95ef97c23b5fd0399b"
integrity sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==

for-each@^0.3.3:
version "0.3.3"
resolved "https://registry.yarnpkg.com/for-each/-/for-each-0.3.3.tgz#69b447e88a0a5d32c3e7084f3f1710034b21376e"
Expand Down Expand Up @@ -8833,6 +8847,11 @@ proxy-addr@~2.0.7:
forwarded "0.2.0"
ipaddr.js "1.9.1"

proxy-from-env@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==

psl@^1.1.33:
version "1.9.0"
resolved "https://registry.yarnpkg.com/psl/-/psl-1.9.0.tgz#d0df2a137f00794565fcaf3b2c00cd09f8d5a5a7"
Expand Down

0 comments on commit 9310844

Please sign in to comment.