thảo luận Hướng dẫn dùng Cloudflare Zero Trust

  • Người tạo chủ đề Người tạo chủ đề Fioren
  • Ngày bắt đầu Ngày bắt đầu
11h đêm mà vẫn bị lag thì mạng kém quá rồi ko còn cách nào khác đâu, xài dns trong nước thôi :shame:

chạy workflow lại thêm một lần nữa xem, nghi do bên cf chập chờn ...
chạy lại vẫn bị, thôi để làm lại từ đầu xem sao
 
sếp ơi cách 1 sao cf không cập nhật DNS nhỉ vào quick edit deloy xong thấy báo hiện ip nhưng k update vào zero
1700814367073.png
 
Cập nhật code update IP từ DDNS lên Cloudflare để set DNS cho tất cả các thiết bị trong nhà (lên router, thiết bị) không cần DoH, DoT:

JavaScript:
addEventListener('scheduled', (event) => {
  event.waitUntil(handleRequest());
});

addEventListener('fetch', (event) => {
  return event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) {
  const accountEmail = '';
  const accountId= '';
  const apiToken = '';
  const domain = '';
  const ip = await resolveDomain(domain);

  if (ip) {
    console.log(`The IP address of ${domain} is: ${ip}`);
    const locationId = await getLocationId(accountId, apiToken, accountEmail);
    if (locationId) {
      console.log(`The location ID is: ${locationId}`);
      const updateResult = await updateLocation(accountId, apiToken, locationId, ip, accountEmail);
      if (updateResult) {
        console.log("Update location successful:");
        console.log(`Location ID: ${updateResult.id}`);
        console.log(`Name: ${updateResult.name}`);
        console.log(`IP: ${updateResult.networks[0].network}`);
        console.log(`Subnet: ${updateResult.networks[0].network.split('/')[1]}`);
        console.log(`Created At: ${updateResult.created_at}`);
        console.log(`Updated At: ${updateResult.updated_at}`);
      } else {
        console.log("No location data found.");
      }
    } else {
      console.log("No locations found.");
    }
  } else {
    console.log(`Failed to resolve the IP address of ${domain}`);
  }

  return new Response('Worker execution completed', { status: 200 });
}

async function resolveDomain(domain) {
  const apiURL = 'https://dns.google.com/resolve';
  const queryURL = new URL(apiURL);
  queryURL.searchParams.append('name', domain);
  queryURL.searchParams.append('type', 'A');

  const response = await fetch(queryURL);
  const data = await response.json();

  if (data.Answer instanceof Array && data.Answer.length > 0) {
    const ipAddresses = data.Answer
      .filter(answer => answer.type === 1)
      .map(answer => answer.data);
    return ipAddresses[0] || null;
  } else {
    return null;
  }
}

async function getLocationId(accountId, apiToken, accountEmail) {

  const url = `https://api.cloudflare.com/client/v4/accounts/${accountId}/gateway/locations`;

  const response = await fetch(url, {
    headers: {
      'Authorization': `Bearer ${apiToken}`,
      'Content-Type': 'application/json',
      'X-Auth-Email': accountEmail,
      'X-Auth-Key': apiToken
    }
  });

  if (response.ok) {
    const data = await response.json();
    const result = data.result;
    return result.length > 0 ? result[0].id : null;
  } else {
    return null;
  }
}

async function updateLocation(accountId, apiToken, locationId, ip, accountEmail) {
  const url = `https://api.cloudflare.com/client/v4/accounts/${accountId}/gateway/locations/${locationId}`;

  const response = await fetch(url, {
    method: 'PUT',
    headers: {
      'Authorization': `Bearer ${apiToken}`,
      'Content-Type': 'application/json',
      'X-Auth-Email': accountEmail,
      'X-Auth-Key': apiToken
    },
    body: JSON.stringify({
      client_default: true,
      ecs_support: true,
      name: 'Home',
      networks: [
        { network: `${ip}/32` }
      ]
    })
  });

  if (response.ok) {
    const data = await response.json();
    return data.result || null;
  } else {
    return null;
  }
}
thím coi thử sao áp code vào có nhận ip mà không update vào zero nhỉ

1700819875075.png
 
Làm thế nào để nó tự động update hàng ngày vậy mọi người. Nó chỉ auto update được một ngày là dừng rồi.
1700822073272.png
 
Cách cài đặt DDNS
Lưu ý: Cách này có hơi vi phạm luật của github... Cho nên tài khoản github có thể bị khóa, nên tạo account phụ để làm.
1/ Tạo DDNS và gắn DDNS vào trong router:
Xem thêm ở thread NextDNS mục IV
thảo luận - Tất tần tật về dịch vụ nextDNS (https://voz.vn/t/tat-tan-tat-ve-dich-vu-nextdns.522718/)
2/ Fork repo này:
GitHub - FiorenMas/Cloudflare-Gateway-Update-DDNS-For-DNS-Locations (https://github.com/FiorenMas/Cloudflare-Gateway-Update-DDNS-For-DNS-Locations)
2/ Vào trang này:
Cloudflare One (https://one.dash.cloudflare.com/)
Rồi vào đây:
GKTSUVy.png

D8I1myD.png

Nhìn lên thanh địa chỉ có url như sau:
https://one.dash.cloudflare.com/de2b42770b857cb4829d123bf2c595c4/gateway/locations/6b7c800392788df2be76d3224a0c6766/edit
Đằng trước /gateway/locations/de2b42770b857cb4829d123bf2c595c4 gọi là CLOUDFLARE_ACCOUNT_ID
Đằng sau /gateway/locations/6b7c800392788df2be76d3224a0c6766 gọi là CLOUDFLARE_LOCATION_ID
Thêm các Secret Action theo các bước như #1 đã hướng dẫn:
Name​
Secret​
CLOUDFLARE_API_KEYNhư #1
CLOUDFLARE_ACCOUNT_IDNhư #1
CLOUDFLARE_ACCOUNT_EMAILNhư #1
CLOUDFLARE_LOCATION_IDNhư trên đã giải thích
CLOUDFLARE_LOCATION_NAMENhư trên đã giải thích
DDNS_HOSTNAMETên DDNS tạo ở trang dynu

Sau khi thêm xong vào tab Action nhấn vào đây:
bAwJ9OM.png

2Au4zce.png

bCURfNA.png


Mặc định là cứ 1 giờ sẽ cập nhật DDNS 1 lần, có thể rút ngắn thời gian lại bằng cách sửa file main.yml chỗ này thành ý thích
Bc37wcN.png

Một vài giá trị tham khảo:
30 phút: */30 * * * *
10 phút: */10 * * * *
5 phút: */5 * * * *
sếp ơi làm thử cách 2 kiểm tra tất cả Secret Action fork lại 2 lần rồi mà vẫn bị dính lỗi {"success":false,"errors":[{"code":10000,"message":"Authentication error"}]}

DDNS_HOSTNAME bắt buộc phải tạo ở Tên DDNS tạo ở trang dynu hay dùng duckdns cũng được hả thím

1701053976391.png
 
sếp ơi làm thử cách 2 kiểm tra tất cả Secret Action fork lại 2 lần rồi mà vẫn bị dính lỗi {"success":false,"errors":[{"code":10000,"message":"Authentication error"}]}

DDNS_HOSTNAME bắt buộc phải tạo ở Tên DDNS tạo ở trang dynu hay dùng duckdns cũng được hả thím

Xem tệp đính kèm 2204399
cái này t dùng api key, fen dùng api token hay api key
 

Thống kê chủ đề

Ngày tạo
Fioren,
Người trả lời cuối
tuananhhd88,
Trả lời
4.335
Lượt xem
418.040
Quay lại
Lên đầu trang