mirror of
https://github.com/freedbygrace/DynamoDNS.git
synced 2026-07-26 11:38:13 +00:00
9c25b503d4
Replit-Commit-Author: Agent Replit-Commit-Session-Id: 9111ef36-26c8-4085-84ca-a35dc1fec1b5 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/7083d608-d6d3-4a6a-9a27-6286c5109627/eac6d55d-5ae1-43e4-8a86-77381999f6ba.jpg
69 lines
2.0 KiB
TypeScript
69 lines
2.0 KiB
TypeScript
import fetch from 'node-fetch';
|
|
|
|
/**
|
|
* Gets the current public IP address of the server
|
|
* Uses multiple services for redundancy
|
|
*/
|
|
export async function getCurrentIpAddress(): Promise<string | null> {
|
|
const ipServices = [
|
|
'https://api.ipify.org',
|
|
'https://ifconfig.me/ip',
|
|
'https://icanhazip.com',
|
|
'https://ipecho.net/plain'
|
|
];
|
|
|
|
// Try each service in sequence until we get a valid response
|
|
for (const service of ipServices) {
|
|
try {
|
|
console.log(`Attempting to get IP address from ${service}`);
|
|
const controller = new AbortController();
|
|
const timeoutId = setTimeout(() => controller.abort(), 5000);
|
|
|
|
const response = await fetch(service, { signal: controller.signal });
|
|
clearTimeout(timeoutId);
|
|
|
|
if (response.ok) {
|
|
const ip = (await response.text()).trim();
|
|
console.log(`Got IP address: ${ip} from ${service}`);
|
|
|
|
// Basic validation - IP should be a string with numbers and dots
|
|
if (ip && /^[0-9.]+$/.test(ip)) {
|
|
return ip;
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error(`Error getting IP from ${service}:`, error);
|
|
// Continue to the next service
|
|
}
|
|
}
|
|
|
|
console.error('Failed to get IP address from any service');
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Gets the current IPv6 address (if available)
|
|
*/
|
|
export async function getCurrentIpv6Address(): Promise<string | null> {
|
|
try {
|
|
const controller = new AbortController();
|
|
const timeoutId = setTimeout(() => controller.abort(), 5000);
|
|
|
|
const response = await fetch('https://ipv6.icanhazip.com', { signal: controller.signal });
|
|
clearTimeout(timeoutId);
|
|
|
|
if (response.ok) {
|
|
const ip = (await response.text()).trim();
|
|
console.log(`Got IPv6 address: ${ip}`);
|
|
|
|
// Basic validation for IPv6 format
|
|
if (ip && ip.includes(':')) {
|
|
return ip;
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Error getting IPv6 address:', error);
|
|
}
|
|
|
|
return null;
|
|
} |