利用CloudFlare API搭建DDNS


参考:GitHub

首先你要有Cloudflare账号,并且有可用域名托管在CF,添加一个A记录(你想用来做DDNS的域名即可)

搭建前需要有 Python 环境,安装依赖

pip install pyyaml
pip install requests

接下来开始获取源码

git clone https://github.com/ethaligan/cloudflare-ddns.git

开始配置

cd cloudflare-ddns/zones
cp example.com.yml xxx.xxx.yml

注意将上面的xxx.xxx替换为你的根域名,然后编辑它,参考如下

%YAML 1.1
# Cloudflare DDNS example configuration
---

# Your Cloudflare email address
cf_email: '你的CF注册账号'

# Your Cloudflare API key
# https://support.cloudflare.com/hc/en-us/articles/200167836-Where-do-I-find-my-Cloudflare-API-key
cf_api_key: 你的Global API Key

# Cloudflare zone name
# If you're updating 'ddns.example.com' set this to 'example.com'
cf_zone: 你的根域名

# List of records
# If you're updating 'example.com' record, set its name to '@'.
# Only write the subdomain ('ddns' for 'ddns.example.com')
cf_records:
    - '@':
        type: A
        proxied: true
        log: ERROR
    - '你的DDNS域名':
        type: A
    - '你的DDNS域名':
        type: AAAA
        ttl: 300
        proxied: false
        log: INFO

# This is the method used to discover the server's IP address
# The faster one is 'dig' but it may not be available on your system
# Available methods: 'http' or 'dig'
cf_resolving_method: 'http'

测试一下

cd ../
python cloudflare-ddns.py -z xxx.xxx

一般是没问题的,用在没有V6的机器上会提示找不到AAAA记录(这不是废话吗。。。)直接注释掉就好了

加个定时任务crontab -e

添加如下两行,即每隔半个小时自动重新获取当前IP并更新一次

# Every 30 minutes, update my Cloudflare records.
*/30 * * * * python /root/cloudflare-ddns/cloudflare-ddns.py -z xxx.xxx


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM