web/src/utils/fetch-website-info.ts

154 lines · 3022 bytes

  1import { error } from './logger';
  2
  3export const fetchWebsiteInfo = async (
  4  url: string,
  5): Promise<WebsiteData | null> => {
  6  const endpoint = `https://site-info-fetch.as93.workers.dev/?url=${url}`;
  7  try {
  8    const res = await fetch(endpoint);
  9    if (!res.ok) {
 10      error('Website', `HTTP ${res.status} for ${url} (${endpoint})`);
 11      return null;
 12    }
 13    return await res.json();
 14  } catch (err) {
 15    error('Website', `Network error for ${url}: ${err}`);
 16    return null;
 17  }
 18};
 19
 20interface DNSRecord {
 21  target: string;
 22  ip: string;
 23  country_code: string;
 24  country_name: string;
 25  isp: string;
 26}
 27
 28interface DNSRecords {
 29  ns: {
 30    records: DNSRecord[];
 31  };
 32  mx: {
 33    records: DNSRecord[];
 34  };
 35}
 36
 37interface Engine {
 38  name: string;
 39  reference: string;
 40  detected: boolean;
 41}
 42
 43interface DomainBlacklist {
 44  engines: Engine[];
 45  detections: number;
 46}
 47
 48interface FileType {
 49  signature: string;
 50  extension: string;
 51  headers: string;
 52}
 53
 54interface GeoLocation {
 55  countries: string[];
 56}
 57
 58interface HtmlForms {
 59  number_of_total_forms: number;
 60  number_of_total_input_fields: number;
 61  two_text_inputs_in_a_form: boolean;
 62  credit_card_field_present: boolean;
 63  password_field_present: boolean;
 64  email_field_present: boolean;
 65}
 66
 67interface Redirection {
 68  found: boolean;
 69  external: boolean;
 70  url: string;
 71  redirects: string[];
 72}
 73
 74interface ResponseHeaders {
 75  code: number;
 76  status: string;
 77  date: string;
 78  last_modified: string;
 79  etag: string;
 80  accept_ranges: string;
 81  vary: string;
 82  content_encoding: string;
 83  cache_control: string;
 84  content_length: string;
 85  content_type: string;
 86  age: string;
 87  content_security_policy_report_only: string;
 88  strict_transport_security: string;
 89  public_key_pins_report_only: string;
 90  x_frame_options: string;
 91  x_content_type_options: string;
 92  x_xss_protection: string;
 93  referrer_policy: string;
 94  x_permitted_cross_domain_policies: string;
 95  onion_location: string;
 96}
 97
 98interface RiskScore {
 99  result: number;
100}
101
102interface SecurityChecks {
103  [key: string]: boolean | string;
104}
105
106interface ServerDetails {
107  ip: string;
108  hostname: string;
109  continent_code: string;
110  continent_name: string;
111  country_code: string;
112  country_name: string;
113  region_name: string;
114  city_name: string;
115  latitude: number;
116  longitude: number;
117  isp: string;
118  asn: string;
119}
120
121interface SiteCategory {
122  [key: string]: boolean;
123}
124
125interface UrlParts {
126  scheme: string;
127  host: string;
128  host_nowww: string;
129  port: null | number;
130  path: null | string;
131  query: null | string;
132}
133
134interface WebPage {
135  title: string;
136  description: string;
137  keywords: string;
138}
139
140export interface WebsiteData {
141  dns_records: DNSRecords;
142  domain_blacklist: DomainBlacklist;
143  file_type: FileType;
144  geo_location: GeoLocation;
145  html_forms: HtmlForms;
146  redirection: Redirection;
147  response_headers: ResponseHeaders;
148  risk_score: RiskScore;
149  security_checks: SecurityChecks;
150  server_details: ServerDetails;
151  site_category: SiteCategory;
152  url_parts: UrlParts;
153  web_page: WebPage;
154}