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

191 lines (187 loc) 6.91 kB
#!/usr/bin/env node "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.runDynDns = void 0; const DynDnsApp_1 = require("./app/DynDnsApp"); const package_json_1 = require("../package.json"); const fs = __importStar(require("fs")); const path = __importStar(require("path")); /** * Checks if running in development mode by looking for .dev-mode file */ const isDevelopmentMode = fs.existsSync(path.join(process.cwd(), '.dev-mode')); /** * Processes command line arguments into a structured object */ const args = process.argv.slice(2); const cliArgs = { help: args.includes('--help') || args.includes('-h'), version: args.includes('--version') || args.includes('-v'), continuous: args.includes('--continuous') || args.includes('-c'), setup: args.includes('--setup'), debug: args.includes('--debug') }; // Configure development mode environment if (isDevelopmentMode && cliArgs.debug) { console.log('🔧 Running in DEVELOPMENT MODE'); process.env.DEVELOPMENT_MODE = 'true'; } /** * Extracts a value for a command line argument key * @param key Argument key to find * @returns Value following the key or undefined if not found */ function getArgValue(key) { const index = args.indexOf(key); return (index !== -1 && index < args.length - 1) ? args[index + 1] : undefined; } /** * Parse direct configuration parameters from command line arguments */ const directConfig = { API_TOKEN: getArgValue('--api-token'), ZONE_ID: getArgValue('--zone-id'), RECORD_ID: getArgValue('--record-id'), DOMAIN: getArgValue('--domain'), SUBDOMAIN: getArgValue('--subdomain') }; // Parse optional parameters if (getArgValue('--ttl')) { directConfig.TTL = parseInt(getArgValue('--ttl') || '120', 10); } if (args.includes('--proxied')) { directConfig.PROXIED = true; } // Show help text if (cliArgs.help) { console.log(` Cloudflare DynDNS - Update Cloudflare DNS with your dynamic IP address Usage: cloudflare-dyndns [options] Options: -c, --continuous Run in continuous monitoring mode (recommended) -h, --help Show this help message -v, --version Show version information --setup Run the setup wizard --debug Enable debug logging Direct Configuration: --api-token <token> Cloudflare API token --zone-id <id> Cloudflare Zone ID --record-id <id> DNS Record ID --domain <domain> Domain name --subdomain <subdomain> Subdomain --ttl <seconds> TTL in seconds (minimum 60) --proxied Enable Cloudflare proxy (default: false) Examples: cloudflare-dyndns Run once and exit cloudflare-dyndns --continuous Run continuously with adaptive intervals cloudflare-dyndns --api-token xxx --zone-id yyy --record-id zzz --domain example.com --subdomain vpn `); process.exit(0); } // Show version if (cliArgs.version) { console.log(`Cloudflare DynDNS v${package_json_1.version}`); process.exit(0); } /** * Launches the setup wizard if requested */ if (cliArgs.setup) { const setupScriptPath = path.join(__dirname, 'scripts', 'setup.js'); if (fs.existsSync(setupScriptPath)) { console.log('Launching setup wizard...'); try { require('./scripts/setup'); // The setup script will call process.exit() when done } catch (error) { console.error(`Error running setup script: ${error.message}`); console.log(`\nPlease run 'cloudflare-dyndns-setup' instead.`); process.exit(1); } } else { console.error(`Setup script not found at ${setupScriptPath}`); console.log(`\nPlease run 'cloudflare-dyndns-setup' instead.`); process.exit(1); } } /** * Main application function that runs the DynDNS update process * @param options Configuration options * @param options.continuous Whether to run in continuous monitoring mode * @param options.config Direct configuration overrides * @param options.debug Whether to enable debug mode * @returns Promise resolving to true if successful, false otherwise */ const runDynDns = async (options = {}) => { const app = new DynDnsApp_1.DynDnsApp(options.config || {}, options.debug || false); try { if (app.needsSetup()) { console.error('Configuration is missing or incomplete. Please run cloudflare-dyndns-setup first or provide configuration.'); return false; } if (options.continuous) { console.log('Starting Cloudflare DynDNS in continuous monitoring mode...'); await app.startMonitoring(); return true; // This will never actually return if continuous mode is successful } else { console.log('Running Cloudflare DynDNS once...'); return await app.runOnce(); } } catch (error) { console.error(`Fatal error: ${error.message}`); return false; } }; exports.runDynDns = runDynDns; // Only execute this if running as a script, not when imported as a module if (require.main === module) { (async () => { const success = await (0, exports.runDynDns)({ continuous: cliArgs.continuous, config: Object.keys(directConfig).length > 0 ? directConfig : undefined, debug: cliArgs.debug }); if (!cliArgs.continuous) { process.exit(success ? 0 : 1); } })(); } //# sourceMappingURL=index.js.map