Files
homaroma/cf.js
T

58 lines
1.9 KiB
JavaScript

const CONFIG = {
email: 'ti.ddc.2026@gmail.com', // อีเมลที่ใช้สมัคร Cloudflare
apiKey: 'cfut_fZ4ct5szqJbJxpdEfPT0Q5790SqjKZuU4DlbTukn769a34d8',
zoneId: '82a3c279a9f53ab8a5531848031c7629',
recordName: 'homaroma.shop',
proxied: false
};
async function updateDDNS() {
const headers = {
'Authorization': `Bearer ${CONFIG.apiKey}`,
'Content-Type': 'application/json'
};
try {
// 1. Get current Public IP
const ipRes = await fetch('https://api.ipify.org');
const currentIp = await ipRes.text();
console.log(`[${new Date().toISOString()}] Current IP: ${currentIp}`);
// 2. Get Record ID
const dnsRes = await fetch(`https://api.cloudflare.com/client/v4/zones/${CONFIG.zoneId}/dns_records?name=${CONFIG.recordName}`, { headers });
const dnsData = await dnsRes.json();
if (!dnsData.success) throw new Error(JSON.stringify(dnsData.errors));
const record = dnsData.result[0];
if (record.content === currentIp) {
console.log('✅ IP is already up to date.');
return;
}
// 3. Update IP
const updateRes = await fetch(`https://api.cloudflare.com/client/v4/zones/${CONFIG.zoneId}/dns_records/${record.id}`, {
method: 'PATCH',
headers,
body: JSON.stringify({
type: 'A',
name: CONFIG.recordName,
content: currentIp,
ttl: 1,
proxied: CONFIG.proxied
})
});
const updateData = await updateRes.json();
if (updateData.success) {
console.log(`🚀 Updated: ${record.content} -> ${currentIp}`);
} else {
console.error('❌ Update failed', updateData.errors);
}
} catch (err) {
console.error('❌ Error:', err.message);
}
}
updateDDNS();