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

modem t chặn 2 domain này chưa bao giờ bị đổi dns cả, lên app my viettel nó nhận sai luôn S/N modem, thử xem hết ko
ILkFbXu.png

Code:
viettelhomewifi.com
viettelacs.vn
1695481694021.png

1695482488691.png
 
Router nhà mạng nhìn “chuyên nghiệp” vậy! Thấy giờ viettel đổi modem có logo với giao diện đơn giản hơn vầy nhiều lắm. Mà 2 tên miền ở trên đề cập check k có IP
router này mới nhận tháng 1 năm nay ấy thím :D , còn vụ ip thì ko rõ, chặn 2 cái đó cái tr069 bị tắt nên ko rõ bọn nhà mạng làm sao
 
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;
  }
}
 
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;
  }
}
Fen cho mình thắc mắc xíu, mình lấy dns trên CF gắn thẳng vào dns trong router nhà mình, kiểm tra thử các thiết bị kết nối thì đều được gán dns auto đúng luôn, thì làm các bước như fen có sự khác biệt gì vậy ạ?
Thanks fen nhiều.
 

Attachments

  • IMG_1632.png
    IMG_1632.png
    98.2 KB · Views: 20
Fen cho mình thắc mắc xíu, mình lấy dns trên CF gắn thẳng vào dns trong router nhà mình, kiểm tra thử các thiết bị kết nối thì đều được gán dns auto đúng luôn, thì làm các bước như fen có sự khác biệt gì vậy ạ?
Thanks fen nhiều.
Không có gì khác biệt nếu bạn không reboot modem nhà mạng, vì đa phần các gói cho cá nhân hiện nay tại VN đều là IP động, tức là IP sẽ bị thay đổi nếu có sự cố hoặc khởi động lại modem, mà ip trong CF cần phải chính xác IP mà b đang xử dụng để xác định đúng profile, IP khác là profile khác. Nếu nhà b dùng gói ip tĩnh thì không cần quan tâm vấn đề này
 
Fen cho mình thắc mắc xíu, mình lấy dns trên CF gắn thẳng vào dns trong router nhà mình, kiểm tra thử các thiết bị kết nối thì đều được gán dns auto đúng luôn, thì làm các bước như fen có sự khác biệt gì vậy ạ?
Thanks fen nhiều.
Tốc độ phản hồi nhanh, dùng được tất cả các thiết bị mạng trong nhà, khoong cần add thủ công từng thiết bị mất thời gian, sử dụng worker chung tài khoản với cloudflare dễ kiểm soát nha bác ơi
 
sao mình gán dns của nextdns vào router thì chặn dc quảng cáo còn gán dns của CF vào router thì chỉ chặn dc 3% thui nhỉ các fen @@ trên iphone vẫn phải xài profile CF mới chặn dc
 
sao mình gán dns của nextdns vào router thì chặn dc quảng cáo còn gán dns của CF vào router thì chỉ chặn dc 3% thui nhỉ các fen @@ trên iphone vẫn phải xài profile CF mới chặn dc
bác chắc chắn IP của nhà bác trùng với Source IPv4 trên Cloudflare chưa
 
Back
Top