menu LittleJake's Blog
color_lens
avatar
Jake Liu
Never Settle
creative commons by-nc-sa
hit
Category
keyboard_arrow_down

© 2024 LittleJake's Blog.

萌ICP备20223020号

通过cloudflare API实现个人域名免费ddns效果(手把手教程,续)


cloudflare

According to the TOS of CloudFlare that CDN only support port 80 (native HTTP) and port 443(native HTTPS).
You must change your port 80 to others if you want to use in China. Besides, there is a block on China Telecom that IPV6 port 80 (or like port 8080) can only be accessed in their network.

Preparation

  • A domain hosted in the CloudFlare.
  • A terminal (Mobile, Router, PC, etc.)

Create Sub Domain and Extract API Info

1.First of all, log into CloudFlare and select the domain you want to relay. Then, change the tab to 'DNS'.
20200123192309.png
2.Press F12 to open Chrome DevTool, turn to tab 'Network' and create a subdomain you want to relay.
20200123192515.png
3.Left click a packet named dns_records and turn to the tab 'Preview' you can see 'name', 'id','zone_id'. Mark it down, we'll use it soon.
20200123193010.png
4.Switch to 'My Profile' page. Change to the 'API Tokens' tab and create a Token on the right top of the page.
20200123193255.png
Then, create an API Token with the least privilege principle.
20200123193539.png
Markdown the Token.
5.CloudFlare now uses the way that the OAuth does. So, the authentication header now is like: 'Authorization: Bearer TOKEN'

Script

Replace the name, id, zone, and token that just marked down.

PHP Version (IPV6 version by default)

<?php
!defined('CLOUDFLARE_NAME')&&define('CLOUDFLARE_NAME','replace with name');
!defined('CLOUDFLARE_RECORD_ID')&&define('CLOUDFLARE_RECORD_ID','replace with id');
!defined('CLOUDFLARE_TOKEN')&&define('CLOUDFLARE_TOKEN','replace with token');
!defined('CLOUDFLARE_ZONE_ID')&&define('CLOUDFLARE_ZONE_ID','replace with zone_id');


function puturl($url,$data){
    $data = json_encode($data);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

    curl_setopt ($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Authorization: Bearer '.CLOUDFLARE_TOKEN,
        'Content-Length: ' . strlen($data) 
    ]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"PUT"); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $output = curl_exec($ch);
    curl_close($ch);
    return json_decode($output,true);
}

function geturl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

$ipv4_address = geturl('http://v4.ip.zxinc.org/getip');
$ipv6_address = geturl('http://v6.ip.zxinc.org/getip');
$data_v4 = [
    'type'=>'A',
    'name'=>CLOUDFLARE_NAME,
    'content'=>$ipv4_address,
    'ttl'=> 300,
    'proxied' =>false
];
$data = [
    'type'=>'AAAA',
    'name'=>CLOUDFLARE_NAME,
    'content'=>$ipv6_address,
    'ttl'=> 300,
    'proxied' =>false
];

puturl('https://api.cloudflare.com/client/v4/zones/'.CLOUDFLARE_ZONE_ID.'/dns_records/'.CLOUDFLARE_RECORD_ID, $data);

Shell Version (IPV6 version by default)

#!/bin/sh
CLOUDFLARE_ZONE_ID="replace with zone_id"
CLOUDFLARE_TOKEN="replace with token"
CLOUDFLARE_RECORD_ID="replace with id"
CLOUDFLARE_NAME="replace with name"
IPV6_ADDRESS=`curl http://v6.ip.zxinc.org/getip`
IPV4_ADDRESS=`curl http://v4.ip.zxinc.org/getip`

curl -X PUT "https://api.cloudflare.com/client/v4/zones/NULL/dns_records/$CLOUDFLARE_RECORD_ID" -H "Authorization: Bearer $CLOUDFLARE_TOKEN" -H "Content-Type: application/json" --data "{\"type\":\"AAAA\",\"name\":\"$CLOUDFLARE_NAME\",\"content\":\"$IPV6_ADDRESS\",\"ttl\":300,\"proxied\":false}"

#curl -X PUT "https://api.cloudflare.com/client/v4/zones/NULL/dns_records/$CLOUDFLARE_RECORD_ID" -H "Authorization: Bearer $CLOUDFLARE_TOKEN" -H "Content-Type: application/json" --data "{\"type\":\"A\",\"name\":\"$CLOUDFLARE_NAME\",\"content\":\"$IPV4_ADDRESS\",\"ttl\":300,\"proxied\":false}"

CloudFlare API Wiki

CloudFlareAPI

Important!

1.You can place it into Crond.
2.Please make full use of a firewall to prevent the cyber attack due to IPV6 exposed.

Free Domain

1.Github Education Package
2.Freenom Domain

Buy me a beer

buymeacoffee
Jake Liu
Never Settle

Title: 通过cloudflare API实现个人域名免费ddns效果(手把手教程,续)

Author: Jake Liu

Origin:

Creative Commons License

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) For any re-post you must give appropriate credit.

文章遵循CC许可 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 转载请注明出处

Tag:cloudflare, cdn, ddns, ddnsv6, ipv6, ipv4

评论区

Add a new comment.

Theme