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

Vẫn dùng cho cả nhà dc, chịu khó thêm DoH/DoT từng thiết bị thủ công tí thôi :)
vâng tất nhiên là dùng cho cả nhà đc và dùng ổn định nhưng mà để so về độ tiện ích thì ko bằng next được bác ạ :D
 
vâng tất nhiên là dùng cho cả nhà đc và dùng ổn định nhưng mà để so về độ tiện ích thì ko bằng next được bác ạ :D
Nếu chỉ xét trường hợp gán IP chay vô router (đa phần router ko support DoT, DoH) thì IP của NextDns (45. xxx) sẽ là anycast (HK, Sing, hoặc 1 nước nào đó)
Trong khi đó ABPVN có server VN, trong phần setting của DNS upstream mình sẽ chọn được dns1/2.nextdns.io để sử dụng luôn svr VN
Các chức năng chặn, lọc,... thì cũng ko quá khác nhau. Đồng thời dịch vụ của Abpvn bằng 1/2 giá của NextDNS
Nên nếu chọn mua thì mình chọn mua abpvn thì thấy sẽ lợi hơn 😅
 
Đến thời điểm này các script đã được viết lại hoàn toàn mà theo mình là đã hoàn thiện vì... chưa nghĩ ra chức năng gì thêm. Để dễ dàng cập nhật nhất thì vào Settings và xóa repo đi rồi fork lại, nhớ lưu lại những list các bạn sang sử dụng.

View attachment 2079568

Các điểm khác biệt so với phiên bản ban đầu:
  • Hỗ trợ chạy trên Windows với Node.js do đã loại bỏ gần như toàn bộ Shell Script
  • Block tất cả subdomain thay vì chỉ domain được nhập vào.
  • Các định dạng hỗ trợ và cách nhận biết:
    • Hosts: có 127.0.0.1 hoặc 0.0.0.0 ở đầu
    • Domain + Subdomains: có bao gồm subdomain của domain
    • Adblock: có các ký tự ||, ^, $ hay các URL(URL sẽ được bỏ qua do DNS không chặn được)
    • Wildcard Asterisk: có dấu *. ở đầu
    • Wildcard Domains (hoạt động tốt nhất): chỉ bao gồm subdomain cần thiết và số lượng ít hơn định dạng Domain + Subdomain
  • Hỗ trợ chế độ siêu tốc với biến môi trường FAST_MODE
  • Hỗ trợ blocklist và allowlist từ biến môi trường ALLOWLIST_URLSBLOCKLIST_URLS
  • Hỗ trợ ngừng workflow cũ đang chạy trước khi chạy workflow mới trong GitHub Actions
  • Tốc dộ xử lý list nhanh hơn nhiều lần
  • Log rõ ràng hơn list nào sẽ được sử dụng, bao nhiêu domain đã được xử lý
Ví dụ các định dạng

HaGeZi
View attachment 2079570

OISD
View attachment 2079571

Log
View attachment 2079576

View attachment 2079578

Cách sử dụng script mới nhất:
  • Xóa repo cũ
  • Fork lại
  • Tạo thư mục .github/workflows
  • Copy auto_update_github_action.yml vào .github/workflows
  • Cách thêm biến môi trường tương tự như thêm secret
    • FAST_MODE: không thêm hoặc gán 0 sẽ chạy chế độ cũ, thêm và gán 1 sẽ chạy chế độ mới
    • ALLOWLIST_URLSBLOCKLIST_URLS: không thêm sẽ chạy list có sẵn, thêm thì mỗi list 1 dòng
      View attachment 2079574
  • Chạy workflow
Lưu ý:
  • Cloudflare giới hạn số lượng request là 1200 mỗi 5 phút: https://developers.cloudflare.com/fundamentals/api/reference/limits/
  • Cách tính số lượng request: số lượng list cũ đã xóa + số lượng list mới + 1 request xóa rule cũ + 1 request tạo rule mới
    View attachment 2079579
    View attachment 2079580
    => 166 + 41 + 1 + 1 = 209 requests
  • Để chạy trên máy thì cài Node.js, copy file .env.example ra .env rồi điền giá trị và chạy theo thứ tự:
    Code:
    npm install
    node download_lists.js
    node cf_gateway_rule_delete.js
    node cf_list_delete.js
    node cf_list_create.js
    node cf_gateway_rule_create.js
Như vậy với lần cập nhật này chỉ cần copy 1 file vào .github/workflows, sau đó thêm biến môi trường, có thay đổi gì thì sửa biến rồi chạy lại workflow là xong, không cần sửa file gì nữa :big_smile:
Bị "Error: Process completed with exit code 1.” thì phải làm gì ạ?
https://photos.app.goo.gl/QJgrcTzAccX8GVBt7
 
Last edited:
Hướng dẫn sử dụng Cloudflare Worker để cập nhật IP match với Cloudflare qua DDNS:
Tại sao nên dùng: Bạn có thể set dns dạng 172.64.36.1, 172.64.36.2 thẳng vào router và sử dụng cho tất cả các thiết bị mạng có trong nhà mà không cần phải dùng DoH, DoT. Tốc độ truy vấn rất nhanh
  • Yêu cầu có ddns trỏ về IP nhà.
  • Cloudflare API, Cloudflare Email, API Key, Cloudflare ID (có hướng dẫn lấy tại #1)
(1) Tạo Worker và chỉnh sửa code theo code dưới đây:
OqargG6.png

Tiếp theo
D1c93dC.png

Tiếp theo
MtT1t73.png
Tiếp theo, nhập Name cho Worker
Tx5e8on.png

Kéo xuống cuối trang nhấn Deploy
EowsqQ5.png

Tiếp theo, nhấn Edit code
0aUoPq9.png

Tiếp theo, xoá tất cả các code, sửa code theo dưới đây:
IpczZnM.png

Điền các thông tin sau:
TZVbNKN.png

- Chỉnh sửa các thông tin: accountEmail, accountId (Cloudflare ID), apiKey, domain (DDNS của bạn).
Code:
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: 'RouterZTE',
      networks: [
        { network: `${ip}/32` }
      ]
    })
  });

  if (response.ok) {
    const data = await response.json();
    return data.result || null;
  } else {
    return null;
  }
}
=> Save and deploy.
(2) Tạo Cron trigger để tự động chạy script cập nhật IP qua DDNS mỗi 1 phút (rất nhanh).
uxUmAud.png



(3) Chỉnh sửa giá trị Minutes là 1 (Chạy worker mỗi phút), sau đó nhấn Add Trigger
QAPmDJl.png


(4) Kết quả như thế này là hoàn tất:
oZXiESp.png


4. Cập nhật DNS cho thiết bị của bạn:
IPV4:

172.64.36.1
172.64.36.2
---
Máy tính:
rX0BoIt.png


Các thiết bị khác tương tự
Tốt nhất là đặt lên Router tổng để áp dụng cho tất cả các thiết bị trong nhà.
5. Kết thúc

Chúc các bạn cấu hình thành công. Sử dụng DNS dạng này cảm giác lướt web rất nhanh, không bị lỗi DoH (Mikrotik DoH không ổn định).
Kiểm tra xem đã chặn quảng cáo chưa:
Link 1:
Test Ad Block - Toolz (https://d3ward.github.io/toolz/adblock.html)
Link 2: AdBlock Test (https://iblockads.net/test)
Nhớ flush dns trước khi test nhé.
CMD: ipconfig /flushdns
*đã cập nhật ảnh lỗi
 

Attachments

  • 1695540875340.png
    1695540875340.png
    46 KB · Views: 21
Last edited:
Hướng dẫn sử dụng Cloudflare Worker cập nhật Source IPv4 address trong DNS Location của Cloudflare Zero Trust qua DDNS:
View attachment 2084145
Ảnh mô tả.
1. Yêu cầu:
  • Có DDNS trỏ tới địa chỉ IP của Router cần match với Cloudflare.
  • Có tài khoản Cloudflare (đa số đã có khi sử dụng Cloudflare Zero Trust như #1)
2. Bắt đầu:
- Truy cập Worker: https://dash.cloudflare.com/?to=/:account/workers-and-pages
(1) Create Worker
View attachment 2084160(2) Điền mục "Name" là updatelocation hoặc bất kì
View attachment 2084167
(3) Kéo xuống cuối trang nhấn Deploy
View attachment 2084169
(4) Nhấn Edit code
View attachment 2084176
(5) Xoá tất cả các dòng code có sẵn, copy code dưới đây vào khung

JavaScript:
addEventListener('scheduled', (event) => {
  event.waitUntil(handleRequest());
});
addEventListener('fetch', (event) => {
  return event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
  const accountEmail = '[email protected]';
  const accountId= '09e1ce4beeee721da60efa2c06287551';
  const apiToken = '181af2890ac1bea1646afc0cf12a529fd10d8';
  const domain = 'userxvpn2409.ddns.net';
  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: 'RouterZTE',
      networks: [
        { network: `${ip}/32` }
      ]
    })
  });
  if (response.ok) {
    const data = await response.json();
    return data.result || null;
  } else {
    return null;
  }
}
(6) Chỉnh sửa các thông tin cho các biến sau:

    • accountEmail: email cloudflare của bạn.
    • accountId: id account cloudflare của bạn, hướng dẫn lấy ở #1.
    • apiToken: apiToken cloudflare của bạn, hướng dẫn lấy ở #1.
    • domain: điền url của ddns của bạn vào.
View attachment 2084182
(7) Save and deploy
View attachment 2084184
3. Tạo cron trigger để tự động chạy sau mỗi phút
View attachment 2084188
(1) Chọn vào Triggers
View attachment 2084190

Tiếp tục ở reply sau
(2) Add Cron Trigger
uxUmAud.png


(3) Chỉnh sửa giá trị Minutes là 1 (Chạy worker mỗi phút), sau đó nhấn Add Trigger
QAPmDJl.png

(4) Kết quả như thế này là hoàn tất:
oZXiESp.png

4. Cập nhật DNS cho thiết bị của bạn:
IPV4:

172.64.36.1
172.64.36.2
---
Máy tính:
rX0BoIt.png

Các thiết bị khác tương tự
Tốt nhất là đặt lên Router tổng để áp dụng cho tất cả các thiết bị trong nhà.
5. Kết thúc

Chúc các bạn cấu hình thành công. Sử dụng DNS dạng này cảm giác lướt web rất nhanh, không bị lỗi DoH (Mikrotik DoH không ổn định).
Kiểm tra xem đã chặn quảng cáo chưa:
Link 1:
https://d3ward.github.io/toolz/adblock.html
Link 2: https://iblockads.net/test
Nhớ flush dns trước khi test nhé.
CMD: ipconfig /flushdns
*đã cập nhật ảnh lỗi
 
Last edited:
Hướng dẫn sử dụng Router Mikrotik tự động cập nhật IP Wan (IP Public không NAT) lên Cloudflare DNS Location thông qua Cloudflare API.
Cách này là cách cập nhật IP nhanh hơn DDNS bởi vì vừa có IP là nó cập nhật lên Cloudflare không đợi update record DNS toàn cầu.
1. Yêu cầu:
  • Account id (lấy ở #1).
  • API KEY (lấy ở #1).
  • Account Email: email cloudflare của bạn.
  • Tên interface WAN trong router Mikrotik (của mình là pppoe-out1).
  • Router Mikrotik

2. Phiên bản RouterOS: Hiện tại mình test chạy ổn định 3.000 lượt run script không lỗi lầm trên Router OS 7.12 beta 7.

3. Bắt đầu:

- Lấy tên của Interface WAN của bạn như sau:
Mở Winbox, chọn PPP, tên interface WAN của bạn là ở Name (của mình là pppoe-out1):
totPKiV.png

(1) Tạo Script fetch tới Cloudflare API
bTDmCXz.png

Nhấn dấu (+) để tạo Script mới
7JAK6k5.png

Thiết lập tên script và code cho script này:
Code mẫu:
Code:
:local ip
set $ip [/ip address get [find interface=<EDIT PPPOE INTERFACE>] address]
:local ipWithoutMask [:pick $ip 0 [:find $ip "/"]]
/tool fetch url="https://api.cloudflare.com/client/v4/accounts/<EDIT_ACCOUNT_ID>/gateway/locations/<EDIT_DNS_LOCATION>" \
  mode=https http-method=put \
  http-header-field="Authorization: Bearer <EDIT_API_TOKEN>, Content-Type: application/json, X-Auth-Email: <EDIT_ACCOUNT_EMAIL>, X-Auth-Key: <EDIT_API_TOKEN>" \
  http-data="{ \"client_default\": true, \"ecs_support\": true, \"name\": \"RouterMikrotik\", \"networks\": [{ \"network\": \"$ip\" }] }" \
  output=none
Vui lòng sửa các thông tin dưới đây (không để dấu <>, ví dụ: interface=pppoe-out1):
1. <EDIT PPPOE INTERFACE>
2. <EDIT_ACCOUNT_ID>
3. <EDIT_API_TOKEN>
4. <EDIT_ACCOUNT_EMAIL>
ZBu964N.png

Sau đó nhấn OK để lưu lại.
Như thế này là thành công:
1kNJBkQ.png

(2) Tạo Scheduler để chạy Script mỗi 1 phút (có thể tuỳ chỉnh theo ý bạn)
Chọn System → Scheduler
XEl6L2G.png

Tiếp theo:
ec0ELs4.png


Code:
Code:
/system script run update
=> Lưu lại và xem thành quả thôi, như thế này là OK.
Fz33ceO.png

Nếu các bạn muốn xem kết quả sau khi fetch tới Cloudflare, sửa code tại Script như sau:
pfY4s8t.png

Code:
Code:
dst-path="result.txt"
Chúc các bác cấu hình thành công.
 
Last edited:
Đến thời điểm này các script đã được viết lại hoàn toàn mà theo mình là đã hoàn thiện vì... chưa nghĩ ra chức năng gì thêm. Để dễ dàng cập nhật nhất thì vào Settings và xóa repo đi rồi fork lại, nhớ lưu lại những list các bạn sang sử dụng.

View attachment 2079568

Các điểm khác biệt so với phiên bản ban đầu:
  • Hỗ trợ chạy trên Windows với Node.js do đã loại bỏ gần như toàn bộ Shell Script
  • Block tất cả subdomain thay vì chỉ domain được nhập vào.
  • Các định dạng hỗ trợ và cách nhận biết:
    • Hosts: có 127.0.0.1 hoặc 0.0.0.0 ở đầu
    • Domain + Subdomains: có bao gồm subdomain của domain
    • Adblock: có các ký tự ||, ^, $ hay các URL(URL sẽ được bỏ qua do DNS không chặn được)
    • Wildcard Asterisk: có dấu *. ở đầu
    • Wildcard Domains (hoạt động tốt nhất): chỉ bao gồm subdomain cần thiết và số lượng ít hơn định dạng Domain + Subdomain
  • Hỗ trợ chế độ siêu tốc với biến môi trường FAST_MODE
  • Hỗ trợ blocklist và allowlist từ biến môi trường ALLOWLIST_URLSBLOCKLIST_URLS
  • Hỗ trợ ngừng workflow cũ đang chạy trước khi chạy workflow mới trong GitHub Actions
  • Tốc dộ xử lý list nhanh hơn nhiều lần
  • Log rõ ràng hơn list nào sẽ được sử dụng, bao nhiêu domain đã được xử lý
Ví dụ các định dạng

HaGeZi
View attachment 2079570

OISD
View attachment 2079571

Log
View attachment 2079576

View attachment 2079578

Cách sử dụng script mới nhất:
  • Xóa repo cũ
  • Fork lại
  • Tạo thư mục .github/workflows
  • Copy auto_update_github_action.yml vào .github/workflows
  • Cách thêm biến môi trường tương tự như thêm secret
    • FAST_MODE: không thêm hoặc gán 0 sẽ chạy chế độ cũ, thêm và gán 1 sẽ chạy chế độ mới
    • ALLOWLIST_URLSBLOCKLIST_URLS: không thêm sẽ chạy list có sẵn, thêm thì mỗi list 1 dòng
      View attachment 2079574
  • Chạy workflow
Lưu ý:
  • Cloudflare giới hạn số lượng request là 1200 mỗi 5 phút: https://developers.cloudflare.com/fundamentals/api/reference/limits/
  • Cách tính số lượng request: số lượng list cũ đã xóa + số lượng list mới + 1 request xóa rule cũ + 1 request tạo rule mới
    View attachment 2079579
    View attachment 2079580
    => 166 + 41 + 1 + 1 = 209 requests
  • Để chạy trên máy thì cài Node.js, copy file .env.example ra .env rồi điền giá trị và chạy theo thứ tự:
    Code:
    npm install
    node download_lists.js
    node cf_gateway_rule_delete.js
    node cf_list_delete.js
    node cf_list_create.js
    node cf_gateway_rule_create.js
Như vậy với lần cập nhật này chỉ cần copy 1 file vào .github/workflows, sau đó thêm biến môi trường, có thay đổi gì thì sửa biến rồi chạy lại workflow là xong, không cần sửa file gì nữa :big_smile:
e bị lỗi hình bác ơi
 
Hướng dẫn sử dụng Cloudflare Worker cập nhật Source IPv4 address trong DNS Location của Cloudflare Zero Trust qua DDNS:
Vì sao lại có điều này:
  • Các bạn muốn áp dụng cho tất cả các thiết bị trong nhà dùng DNS Cloudflare, các thiết bị không thể cài DoT, DoH.
  • Tốc độ phản hồi rất nhanh.
  • IP được cập nhật mỗi phút.
CPNnvDs.png

Ảnh mô tả.
1. Yêu cầu:
  • Có DDNS trỏ tới địa chỉ IP của Router cần match với Cloudflare.
  • Có tài khoản Cloudflare (đa số đã có khi sử dụng Cloudflare Zero Trust như #1)
2. Bắt đầu:
- Truy cập Worker: https://dash.cloudflare.com/?to=/:account/workers-and-pages
(1) Create Worker
rMU9SsE.png


(2) Điền mục "Name" là updatelocation hoặc bất kì
hXctXJ2.png

(3) Kéo xuống cuối trang nhấn Deploy
L5fyIRj.png

(4) Nhấn Edit code
7m1SzUe.png

(5) Xoá tất cả các dòng code có sẵn, copy code dưới đây vào khung
w0HCdZk.png

JavaScript:
addEventListener('fetch', event => {
    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: 'RouterZTE',
        networks: [
          { network: `${ip}/32` }
        ]
      })
    });
 
    if (response.ok) {
      const data = await response.json();
      return data.result || null;
    } else {
      return null;
    }
  }
(6) Chỉnh sửa các thông tin cho các biến sau:
  • accountEmail: email cloudflare của bạn.
  • accountId: id account cloudflare của bạn, hướng dẫn lấy ở #1.
  • apiToken: apiToken cloudflare của bạn, hướng dẫn lấy ở #1.
  • domain: điền url của ddns của bạn vào.

bdKMLxh.png

(7) Save and deploy
JoVArNE.png

3. Tạo cron trigger để tự động chạy sau mỗi phút
jHp4A26.png

(1) Chọn vào Triggers
d3mQuSg.png

Tiếp tục ở reply sau
*đã cập nhật ảnh lỗi
Thank bác, bác gãi ngay đúng cái chỗ mình ngứa bấy lâu.
 
có fen nào xài còn f670y của viettel cứ reset phát là nó về mặc định ko lưu cấu hình ko 🙄 làm mấy nay cứ tưởng mình cấu hình lỗi chỗ nào vào modem mới thấy hóa ra nó ko lưu
 
có fen nào xài còn f670y của viettel cứ reset phát là nó về mặc định ko lưu cấu hình ko 🙄 làm mấy nay cứ tưởng mình cấu hình lỗi chỗ nào vào modem mới thấy hóa ra nó ko lưu
Theo Miêu biết thì router nào của Vịt Heo cũng bị đẩy cấu hình từ nhà mạng xuống đè lên. Muốn tự set cấu hình phải gọi kĩ thuật can thiệp từ trên tổng đài
 
Back
Top