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

104 lines 3.73 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.IpFileManager = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); /** * Manages the storage and retrieval of the last detected IP address * Handles file operations for persisting IP information between runs */ class IpFileManager { /** * Creates a new IP file manager * @param ipFilePath Path to the file where IP address will be stored * @param logger Logger instance for recording file operations */ constructor(ipFilePath, logger) { this.ipFilePath = ipFilePath; this.logger = logger; this.ensureIpFileDirectoryExists(); } /** * Ensures the directory for the IP file exists, creating it if necessary * @throws Error if the directory cannot be created */ ensureIpFileDirectoryExists() { const ipDir = path.dirname(this.ipFilePath); if (!fs.existsSync(ipDir)) { try { fs.mkdirSync(ipDir, { recursive: true }); this.logger.debug(`Created IP storage directory: ${ipDir}`); } catch (err) { this.logger.error(`Could not create IP storage directory ${ipDir}: ${err.message}`); throw err; } } } /** * Retrieves the last saved IP address from storage * @returns The last saved IP address or null if none exists */ getLastIp() { try { if (fs.existsSync(this.ipFilePath)) { return fs.readFileSync(this.ipFilePath, 'utf8').trim(); } } catch (error) { this.logger.warn(`Could not read last IP file: ${error.message}`); } return null; } /** * Saves the current IP address to storage * @param ip Current IP address to save * @returns True if the save operation was successful */ saveIp(ip) { try { fs.writeFileSync(this.ipFilePath, ip); this.logger.debug(`Saved IP ${ip} to ${this.ipFilePath}`); return true; } catch (error) { this.logger.error(`Failed to save IP to file: ${error.message}`); return false; } } } exports.IpFileManager = IpFileManager; //# sourceMappingURL=IpFileManager.js.map