完成了新的 Cloudflare DDNS 镜像包

in cn-dev •  4 years ago  (edited)

前要

之前由于 Cloudflare 的 API 在国内访问不稳定,我使用了 C/S 架构来更新家里的 Public IP 到我的域名。

但是由于用 PHP 写的 C 端的程序稳定性很差,经常挂掉,加上 Cloudflare API 的国内访问很稳定了,所以想要重新构建一下 DDNS 方式。

实施

本来打算自己用 golang 写一个,不过搜索了下 Github,发现有现成的可以使用,那么就直接拿来主义好了。

使用 Docker 封装下,就可以开心的使用了。

Dockerfile 如下:

FROM alpine:latest as builder
WORKDIR /tmp
RUN apk --no-cache add go git
RUN git clone https://github.com/nickw444/cf-ddns.git && \
    cd cf-ddns && \
    go build -o cf-ddns

FROM alpine:latest
RUN apk --no-cache add curl
COPY --from=builder /tmp/cf-ddns/cf-ddns /usr/bin/
ADD run.sh /usr/bin/run.sh
CMD ["/usr/bin/run.sh"]

其中的 run.sh 文件如下:

#!/bin/ash
FILE=/tmp/ip.txt
while :
do
    echo "start"
    lastip=""
    if [ -f $FILE ]; then
        lastip=`cat $FILE`
    fi
    if [ -z $CHECK_IP_URL ];then
        CHECK_IP_URL=ifconfig.co
    fi
    nowip=`curl -s $CHECK_IP_URL`
    if [ "$lastip" = "$nowip" ]; then
        echo "not need update"
    else
        cf-ddns \
            --cf-email=$CF_EMAIL \
            --cf-api-key=$CF_KEY \
            --cf-zone-id=$CF_ZONEID \
            --ip-address=$nowip \
            $DOMAIN
        echo "$nowip" > /tmp/ip.txt
        echo "ip updated"
    fi
    sleep $SLEEP_TIME
done

使用方法:

docker run -itd \
    --restart always \
    --name cf-ddns \
    -e CF_EMAIL=YOUR_CF_EMAIL \
    -e CF_KEY=YOUR_CF_API_KEY \
    -e CF_ZONEID=YOUR_CF_DOMAIN_ZONE_ID \
    -e DOMAIN=YOUR_DOMAIN \
    -e SLEEP_TIME=60 \
    ety001/cf-ddns

效果

目前几天下来,很稳定。并且 Docker 镜像的体积也很小了,17M左右。

如果后面 Cloudflare API 再次不稳定的话,打算直接在容器里设置代理了。

完美收工!


ET碎碎念,每周更新,欢迎订阅,点赞,转发!


好用不贵的VPS推荐

https://1hour.win


感谢你的阅读,我是中文区见证人之一,欢迎通过 AuthSteem 来给我投票,或者打开 https://steemitwallet.com/~witnesses 页面,输入 ety001 进行投票。

2.gif

中文区的见证人目前有:
支持一下他们(按字母顺序),一人可以有30票


Thank you for reading. I'm a witness. I would really appreciate your witness vote! You can vote by SteemLogin. Or open https://steemitwallet.com/~witnesses page, input ety001 to vote.

2.gif

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!