Skip to content

Instantly share code, notes, and snippets.

@vulieumang
Created October 3, 2024 08:34
Show Gist options
  • Save vulieumang/edecd9c679e059558f35dd5e2c4858bf to your computer and use it in GitHub Desktop.
Save vulieumang/edecd9c679e059558f35dd5e2c4858bf to your computer and use it in GitHub Desktop.
replace ip cloudflare
<?php
// update_cloudflare_ip.php
// Thiết lập các biến cấu hình
$apiToken = "";
// Old IP address to find
$oldIP = "";
// New IP address to replace the old one with
$newIP = "";
// Cloudflare API Endpoint
$baseUrl = 'https://api.cloudflare.com/client/v4/';
// HTTP Headers với API Token
$headers = [
'Authorization: Bearer ' . $apiToken,
'Content-Type: application/json'
];
// Hàm gửi yêu cầu HTTP đến Cloudflare API
function sendRequest($url, $method = 'GET', $data = [], $headers = []) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
}
if (!empty($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$response = curl_exec($ch);
if (curl_errno($ch)) {
return ['success' => false, 'errors' => [curl_error($ch)]];
}
curl_close($ch);
return json_decode($response, true);
}
// Hàm lấy tất cả các zone (domain) trong tài khoản Cloudflare
function getZones($baseUrl, $headers) {
$zones = [];
$page = 1;
do {
$zonesResponse = sendRequest($baseUrl . 'zones?page=' . $page . '&per_page=50', 'GET', [], $headers);
if (!$zonesResponse['success']) {
return ['success' => false, 'errors' => $zonesResponse['errors']];
}
$zones = array_merge($zones, $zonesResponse['result']);
$page++;
} while ($page <= $zonesResponse['result_info']['total_pages']);
return ['success' => true, 'result' => $zones];
}
// Hàm lấy tất cả các DNS records cho một zone
function getDNSRecords($baseUrl, $zoneId, $headers) {
$dnsRecords = [];
$page = 1;
do {
$dnsResponse = sendRequest($baseUrl . "zones/$zoneId/dns_records?page=$page&per_page=100", 'GET', [], $headers);
if (!$dnsResponse['success']) {
return ['success' => false, 'errors' => $dnsResponse['errors']];
}
$dnsRecords = array_merge($dnsRecords, $dnsResponse['result']);
$page++;
} while ($page <= $dnsResponse['result_info']['total_pages']);
return ['success' => true, 'result' => $dnsRecords];
}
// Hàm cập nhật DNS record
function updateDNSRecord($baseUrl, $zoneId, $dnsRecordId, $dnsRecord, $newIP, $headers) {
$updateData = [
'type' => $dnsRecord['type'],
'name' => $dnsRecord['name'],
'content' => $newIP, // Sửa ở đây để cập nhật IP mới
'ttl' => $dnsRecord['ttl'],
'proxied' => $dnsRecord['proxied']
];
return sendRequest($baseUrl . "zones/$zoneId/dns_records/$dnsRecordId", 'PUT', $updateData, $headers);
}
// Khởi tạo danh sách các bản ghi DNS cần cập nhật
$recordsToUpdate = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['view_records'])) {
// Khi nhấn nút "Xem Các Domain với IP Cũ"
$zonesData = getZones($baseUrl, $headers);
if (!$zonesData['success']) {
$error = 'Không thể lấy danh sách các zone: ' . print_r($zonesData['errors'], true);
} else {
foreach ($zonesData['result'] as $zone) {
$zoneId = $zone['id'];
$dnsData = getDNSRecords($baseUrl, $zoneId, $headers);
if (!$dnsData['success']) {
$error .= 'Không thể lấy DNS records cho zone ' . $zone['name'] . ': ' . print_r($dnsData['errors'], true) . '<br>';
continue;
}
foreach ($dnsData['result'] as $dnsRecord) {
if (($dnsRecord['type'] === 'A' || $dnsRecord['type'] === 'AAAA') && $dnsRecord['content'] === $oldIP) {
$recordsToUpdate[] = [
'zone' => $zone['name'],
'zone_id' => $zoneId,
'record_id' => $dnsRecord['id'],
'name' => $dnsRecord['name'],
'type' => $dnsRecord['type'],
'current_ip' => $dnsRecord['content']
];
}
}
}
}
}
if (isset($_POST['update_ip'])) {
// Khi nhấn nút "Xác Nhận Đổi IP"
if (isset($_POST['records']) && is_array($_POST['records'])) {
foreach ($_POST['records'] as $recordJson) {
// Giải mã chuỗi JSON thành mảng PHP
$record = json_decode($recordJson, true);
if ($record === null) {
$errorMessages[] = "Dữ liệu bản ghi không hợp lệ.";
continue;
}
$zoneId = $record['zone_id'];
$recordId = $record['record_id'];
$dnsRecord = [
'type' => $record['type'],
'name' => $record['name'],
'content' => $record['current_ip'], // Sẽ được sửa thành $newIP trong hàm updateDNSRecord
'ttl' => 1, // TTL = Auto
'proxied' => false // Bạn có thể điều chỉnh nếu cần
];
$updateResponse = updateDNSRecord($baseUrl, $zoneId, $recordId, $dnsRecord, $newIP, $headers);
if ($updateResponse['success']) {
$successMessages[] = "Đã cập nhật {$record['name']} từ IP {$oldIP} sang {$newIP}.";
} else {
$errorMessages[] = "Không thể cập nhật {$record['name']}: " . print_r($updateResponse['errors'], true);
}
}
} else {
$errorMessages[] = "Không có bản ghi nào để cập nhật.";
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Cập Nhật IP Cloudflare</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.container { max-width: 800px; margin: auto; }
.button { padding: 10px 20px; margin: 5px; }
table { width: 100%; border-collapse: collapse; margin-top: 20px;}
table, th, td { border: 1px solid #ddd; }
th, td { padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
.success { color: green; }
.error { color: red; }
</style>
</head>
<body>
<div class="container">
<h1>Cập Nhật IP trên Cloudflare</h1>
<?php if (isset($error)): ?>
<p class="error"><?php echo nl2br(htmlspecialchars($error)); ?></p>
<?php endif; ?>
<?php if (isset($successMessages) && !empty($successMessages)): ?>
<?php foreach ($successMessages as $msg): ?>
<p class="success"><?php echo htmlspecialchars($msg); ?></p>
<?php endforeach; ?>
<?php endif; ?>
<?php if (isset($errorMessages) && !empty($errorMessages)): ?>
<?php foreach ($errorMessages as $msg): ?>
<p class="error"><?php echo htmlspecialchars($msg); ?></p>
<?php endforeach; ?>
<?php endif; ?>
<form method="post">
<button type="submit" name="view_records" class="button">Xem Các Domain với IP Cũ</button>
<?php if (!empty($recordsToUpdate)): ?>
<button type="submit" name="update_ip" class="button" onclick="return confirm('Bạn có chắc chắn muốn cập nhật tất cả các IP này?');">Xác Nhận Đổi IP</button>
<?php endif; ?>
</form>
<?php if (!empty($recordsToUpdate)): ?>
<h2>Danh Sách Các Bản Ghi DNS cần cập nhật:</h2>
<form method="post">
<input type="hidden" name="update_ip" value="1">
<table>
<tr>
<th>Domain</th>
<th>Tên Bản Ghi</th>
<th>Loại</th>
<th>IP Hiện Tại</th>
</tr>
<?php foreach ($recordsToUpdate as $record): ?>
<tr>
<td><?php echo htmlspecialchars($record['zone']); ?></td>
<td><?php echo htmlspecialchars($record['name']); ?></td>
<td><?php echo htmlspecialchars($record['type']); ?></td>
<td><?php echo htmlspecialchars($record['current_ip']); ?></td>
<input type="hidden" name="records[]" value='<?php echo json_encode($record); ?>'>
</tr>
<?php endforeach; ?>
</table>
<button type="submit" name="update_ip" class="button" onclick="return confirm('Bạn có chắc chắn muốn cập nhật tất cả các IP này?');">Xác Nhận Đổi IP</button>
</form>
<?php elseif (isset($_POST['view_records'])): ?>
<p>Không tìm thấy bản ghi DNS nào với IP cũ: <?php echo htmlspecialchars($oldIP); ?></p>
<?php endif; ?>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment