本帖最后由 chiunownow 于 2020-10-31 10:46 编辑
在HASS中实现GoDaddy IPv6 DDNS(v4需自改)
为什么?
- 最近把自制NAS换成UNRAID系统,家里同时换了宽带,可以获得IPv6地址,而且443可用
- 在UNRAID的Docker下搞不定GoDaddy IPv6 DDNS,Cloudflare API又禁止免费域名使用(例如.tk)
- 省资源
- 补充:正确配置IPv6是件麻烦事,最好装宽带时就让师傅光猫改桥接,确认路由器支持并且正确配置v6。v6可以理解为没有内外网的明显区隔,子网设备同样能获取能被公网访问的ipv6地址。
怎么做
-
拢共要用到以下组件:
1. RESTful Template Sensor:用于获取本地IPv6地址和GoDaddy Record
2. REST Command:用于向GoDaddy API提交新IP
3. Automation:……
- 注册域名,改Nameserver到GoDaddy,不赘述
- 获得 GoDaddy API Key:参考 https://my.oschina.net/u/4308373/blog/3422912
- 不能在Lovelace完成的两个REST组件,手写YAML
sensor:
# 通过API获取本机IPv6地址,v4使用api.ipify.org
- platform: rest
name: Unraid IPv6 Address
resource: https://api6.ipify.org
# 通过API获取GoDaddy AAAA记录(ipv4是A记录,v6是AAAA)
- platform: rest
name: GoDaddy AAAA Record
resource: https://api.godaddy.com/v1/domains/你的顶级域名(例如abc.xyz)/records/AAAA/@ #留意此处的AAAA,如用于ipv4改为A即可,下略
headers:
Authorization: sso-key APIKEY:Secret(例如jkklasdfljkasdf12123uo123o:adfiouasdfoi0,注意中间用冒号隔开)
value_template: '{{ value_json[0].data }}' # 从返回的JSON中解析出当前AAAA记录
rest_command:
godaddy_ddns_at:
url: https://api.godaddy.com/v1/domains/你的顶级域名(例如abc.xyz)/records/AAAA/@
method: put
content_type: "application/json"
headers:
Authorization: "sso-key APIKEY:Secret(例如jkklasdfljkasdf12123uo123o:adfiouasdfoi0,注意中间用冒号隔开)"
payload: '[{"data":"{{unraid_ipv6_address}}"}]' #此处巨坑,似乎不能直接用template获取地址,必须命名变量后调用前给变量赋值,下略
godaddy_ddns_any:
url: https://api.godaddy.com/v1/domains/你的顶级域名(例如abc.xyz)/records/AAAA/*
method: put
content_type: "application/json"
headers:
Authorization: "sso-key APIKEY:Secret(例如jkklasdfljkasdf12123uo123o:adfiouasdfoi0,注意中间用冒号隔开)"
payload: '[{"data":"{{unraid_ipv6_address}}"}]'
- Automation现在可以在Lovelace里更好地完成了,靠这个我才发现以前好多没用上的功能。推荐
alias: Godaddy DDNS
trigger:
- platform: template
#两个sensor不相同时提交新地址
value_template: >-
{{states.sensor.unraid_ipv6_address.state !=
states.sensor.godaddy_aaaa_record.state}}
# condition条件,用于避免因 API GET 错误导致错误 PUT 新 IP
condition:
- condition: and
conditions:
- condition: template
value_template: '{{states.sensor.godaddy_aaaa_record.state != "None"}}'
- condition: template
value_template: '{{states.sensor.unraid_ipv6_address.state != "None"}}'
- condition: template
value_template: '{{states.sensor.unraid_ipv6_address.state != "unavailable"}}'
- condition: template
value_template: '{{states.sensor.godaddy_aaaa_record.state != "unavailable"}}'
action:
- service: notify.something #通知组件,不需要请删除整个notify模块,下略
data:
title: Unraid IPv6 地址有变动
message: |
本地 IPv6 地址:{{states.sensor.unraid_ipv6_address.state}}
GoDaddy AAAA Record:{{states.sensor.godaddy_aaaa_record.state}}
正在提交 DDNS 请求
- service: rest_command.godaddy_ddns_at
data:
unraid_ipv6_address: '{{states.sensor.unraid_ipv6_address.state}}' #此处对上面命名的变量进行赋值
- service: rest_command.godaddy_ddns_any
data:
unraid_ipv6_address: '{{states.sensor.unraid_ipv6_address.state}}'
- wait_template: >-
{{states.sensor.unraid_ipv6_address.state ==
states.sensor.godaddy_aaaa_record.state}}
- service: notify.something #通知组件,不需要请删除整个notify模块
data:
message: 'Godaddy DNS AAAA Record 已更新为 {{states.sensor.godaddy_aaaa_record.state}}'
mode: single
顺便吐槽
Caddy2真是太难用了 吐了 怀念Caddy1
|