參考: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