Skip to content

Commit

Permalink
Refactor CensysResponse interface to match v2 API; add deduping of do…
Browse files Browse the repository at this point in the history
…main names which reduces dns lookups by 95% in testing.
  • Loading branch information
Matthew-Grayson committed Dec 18, 2023
1 parent 493d1c3 commit 5d71e1c
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions backend/src/tasks/censys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ import saveDomainsToDb from './helpers/saveDomainsToDb';
import { CommandOptions } from './ecs-client';
import getRootDomains from './helpers/getRootDomains';

// TODO: code only returns first 100 results; add cursor to API call to return all results
interface CensysAPIResponse {
status: string;
results: {
names?: string[];
}[];
metadata: {
count: number;
page: number;
pages: number;
result: {
total: number;
hits: [
{
names?: string[];
}
];
};
// links: {
// next: string;
// };
}

const sleep = (milliseconds: number) => {
Expand Down Expand Up @@ -51,6 +55,7 @@ export const handler = async (commandOptions: CommandOptions) => {
console.log('Running censys on organization', organizationName);

const rootDomains = await getRootDomains(organizationId!);
const uniqueNames = new Set<string>(); //used to dedupe domain names
const foundDomains = new Set<{
name: string;
organization: { id: string };
Expand All @@ -60,11 +65,12 @@ export const handler = async (commandOptions: CommandOptions) => {

for (const rootDomain of rootDomains) {
const data = await fetchCensysData(rootDomain);
for (const result of data.results) {
const names = result['names'];
for (const hit of data.result.hits) {
const names = hit['names'];
if (!names) continue;
for (const name of names) {
if (name.endsWith(rootDomain)) {
if (!uniqueNames.has(name) && name.endsWith(rootDomain)) {
uniqueNames.add(name);
foundDomains.add({
name: name.replace('*.', ''),
organization: { id: organizationId! },
Expand Down

0 comments on commit 5d71e1c

Please sign in to comment.