aixlabDocs
GitHub Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Cloudflare Worker - DDNS

export default {
  
export default {
  async fetch(request, env, ctx) {
    if (request.method === "HEAD") {
      return new Response('nochg');
    };
    const urlParams = new URL(request.url).searchParams;
    const apiToken = urlParams.get('apitoken');
    if (apiToken == null) {
      return new Response('nochg');
    };

    const ipv4 = urlParams.get('ipv4');
    const ipv6 = urlParams.get('ipv6');
  
    if (ipv4 == null || ipv6 == null  ) {
      return new Response('nochg');
    };  




    const zoneID ='41000767c6427f0fab86c75e2611f97a';
    
    const ipv4RecordId = '386ac33e62cb6e33c73812d7accc7002';
    const ipv4Hostame = 'glk15v4.aixlab.de'
    
    const ipv6RecordId = '9467edb09b39b1c4279a4a13dd62adfe';
    const ipv6Hostame = 'glk15v6.aixlab.de';

    const hostame = 'glk15.aixlab.de';
    const aId = 'c9337ad854a43bb7d4f824d31e38dce6';
    const aaaaID = '78b0fb86cddcb682bfef9a91cbfbcc61';


    
    const optionsA = {
      method: 'PUT',
      headers: {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + apiToken},
      body: '{"content": "' +  ipv4 + '" ,"name": "' +  hostame + '","proxied":false,"type":"A","comment":"DynDNS Domain","ttl":60}'
    };
    const optionsAAAA = {
      method: 'PUT',
      headers: {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + apiToken},
      body: '{"content": "' +  ipv6 + '" ,"name": "' +  hostame + '","proxied":false,"type":"AAAA","comment":"DynDNS Domain","ttl":60}'
    };

    const optionsV4 = {
      method: 'PUT',
      headers: {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + apiToken},
      body: '{"content": "' +  ipv4 + '" ,"name": "' +  ipv4Hostame + '","proxied":false,"type":"A","comment":"DynDNS Domain for check with ipv4","ttl":60}'
    };

    const optionsV6 = {
      method: 'PUT',
      headers: {'Content-Type': 'application/json', 'Authorization': 'Bearer ' + apiToken},
      body: '{"content": "' +  ipv6 + '" ,"name": "' +  ipv6Hostame + '","proxied":false,"type":"AAAA","comment":"DynDNS Domain for check with ipv6","ttl":60}'
    };


    const responseV4 = await fetch('https://api.cloudflare.com/client/v4/zones/' + zoneID +  '/dns_records/' + ipv4RecordId, optionsV4);
    const responseV6 = await fetch('https://api.cloudflare.com/client/v4/zones/' + zoneID +  '/dns_records/' + ipv6RecordId, optionsV6);
    const responseA = await fetch('https://api.cloudflare.com/client/v4/zones/' + zoneID +  '/dns_records/' + aId, optionsA);
    const responseAAAA = await fetch('https://api.cloudflare.com/client/v4/zones/' + zoneID +  '/dns_records/' + aaaaID, optionsAAAA);

    //.then(response => response.json())
    //.then(response => console.log(response))
    //.catch(err => console.error(err));

    return responseA;
    return new Response('ok');
  },
};

Generate API Token with appropriate rights

ToDo…