package com.paymentlink.service; import jakarta.annotation.PostConstruct; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class DnsService { private static final Logger logger = LoggerFactory.getLogger(DnsService.class); @Value("${cloudns.auth.id:}") private String authId; @Value("${cloudns.auth.password:}") private String authPassword; @Value("${app.tld:ironpath.ai}") private String tld; private final NodeIdService nodeIdService; private final RestTemplate restTemplate = new RestTemplate(); public DnsService(NodeIdService nodeIdService) { this.nodeIdService = nodeIdService; } /** * Update or create DNS record on startup */ @PostConstruct public void updateOrCreateDNSRecord() { if (authId == null || authId.isEmpty() || authPassword == null || authPassword.isEmpty()) { logger.warn("CloudNS credentials not configured, skipping DNS update"); return; } try { String serverIP = getServerIP(); String shortId = nodeIdService.getShortId(); logger.info("Updating DNS record for {} with IP {}", shortId, serverIP); // Note: Actual CloudNS API implementation would go here // This is a simplified version - the full implementation would: // 1. List records to find existing // 2. Update if exists, create if not // 3. Handle CloudNS API specifics logger.info("DNS record update completed (simulated)"); } catch (Exception e) { logger.error("Failed to update DNS record", e); } } /** * Get server's public IP address */ private String getServerIP() { try { // Try ipify first String url = "https://api.ipify.org?format=text"; ResponseEntity response = restTemplate.getForEntity(url, String.class); if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) { return response.getBody().trim(); } } catch (Exception e) { logger.warn("Failed to get IP from ipify, trying fallback", e); } try { // Fallback to ipapi.co String url = "https://ipapi.co/ip/"; ResponseEntity response = restTemplate.getForEntity(url, String.class); if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) { return response.getBody().trim(); } } catch (Exception e) { logger.error("Failed to get IP from fallback", e); } throw new RuntimeException("Could not determine server IP"); } }