UNPKG

@monkeyscanjump/cloudflare-dyndns

Version:

A robust TypeScript application that automatically updates Cloudflare DNS records when your public IP address changes. Perfect for maintaining consistent domain names for home servers, WireGuard VPN, self-hosted services, or any system with a dynamic IP a

113 lines (112 loc) 3.61 kB
import { IConfig } from '../types'; import { Logger } from '../utils/Logger'; /** * Service for interacting with the Cloudflare API * Handles zone and DNS record management with automatic discovery */ export declare class CloudflareService { private config; private logger; private axiosConfig; private apiVersion; private apiBaseUrl; /** * Creates a new Cloudflare service instance * @param config Application configuration * @param logger Logger instance */ constructor(config: IConfig, logger: Logger); /** * Gets authorization headers for API requests * @returns Headers object with authorization */ private getHeaders; /** * Builds full API URL with version and endpoint * @param endpoint API endpoint path * @returns Complete API URL */ private getApiUrl; /** * Makes an API request with fallback for endpoint changes * @param endpoint API endpoint to call * @param method HTTP method * @param data Optional request body * @returns API response data */ private makeApiRequest; /** * Safely gets result array from API response * @param response API response that might have undefined result * @returns Guaranteed array (empty if result was undefined) */ private safeGetResultArray; /** * Safely gets single result from API response * @param response API response that might have undefined result * @returns Result or null if undefined */ private safeGetResult; /** * Handles API changes by trying alternative endpoints or versions * @param endpoint Original endpoint that failed * @param method HTTP method * @param data Optional request body * @param originalError Original error * @returns API response from alternative endpoint */ private handleApiChange; /** * Initializes the service by discovering missing configuration * @returns True if initialization succeeded */ initialize(): Promise<boolean>; /** * Detects which Cloudflare API version is available * @returns Detected API version string */ private detectApiVersion; /** * Looks up the Zone ID for the user's domain * @returns Zone ID or null if not found */ private lookupZoneId; /** * Looks up the DNS record ID based on FQDN * @returns Record ID or null if not found */ lookupRecordId(): Promise<string | null>; /** * Finds a suitable A record to update if specific details aren't provided * Only called when no DOMAIN/SUBDOMAIN is configured * @returns Record information or null if no suitable record found */ private findSuitableRecord; /** * Looks up the FQDN from a record ID * @returns FQDN or null if not found */ private lookupFqdnFromRecordId; /** * Creates a new DNS record with the current IP address * @returns New record ID or null if creation failed */ private createDnsRecord; /** * Verifies that the API credentials are valid * @returns True if credentials are valid */ verifyCredentials(): Promise<boolean>; /** * Updates the DNS record with a new IP address * @param newIp New IP address to set * @returns True if update was successful */ updateDnsRecord(newIp: string): Promise<boolean>; /** * Retries an operation with exponential backoff * @param operation Function to retry * @returns Result of the operation */ private retryOperation; }