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

155 lines · 3068 bytes

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