Skip to content

Commit

Permalink
feat: ip cache time configable
Browse files Browse the repository at this point in the history
  • Loading branch information
feranwq committed May 24, 2023
1 parent cc9be6e commit 35fb976
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
- [可选] 服务卸载
- Mac/Linux: `sudo ./ddns-go -s uninstall`
- Win(以管理员打开cmd): `.\ddns-go.exe -s uninstall`
- [可选] 支持安装或启动时带参数 `-l`监听地址 `-f`同步间隔时间(秒) `-c`自定义配置文件路径 `-noweb`不启动web服务 `-skipVerify`跳过证书验证。如:`./ddns-go -s install -l :9877 -f 600 -c /Users/name/ddns-go.yaml`
- [可选] 支持安装或启动时带参数 `-l`监听地址 `-f`同步间隔时间(秒) `-cacheTimes`缓存触发更新次数 `-c`自定义配置文件路径 `-noweb`不启动web服务 `-skipVerify`跳过证书验证。如:`./ddns-go -s install -l :9877 -f 600 -t 6 -c /Users/name/ddns-go.yaml`

> **Note** 通过合理的配置 `-f``-cacheTimes` 可以实现 IP 变化即时触发更新且不会被 DDNS 服务商限流, 例如 `-f 10 -cacheTimes 360` 效果为每 10 秒检查一次本地 IP 变化, 每小时去公网对比一下 IP 变化
## Docker中使用

Expand All @@ -59,10 +61,10 @@
docker run -d --name ddns-go --restart=always --net=host -v /opt/ddns-go:/root ghcr.io/jeessy2/ddns-go
```

- [可选] 支持启动带参数 `-l`监听地址 `-f`间隔时间(秒)
- [可选] 支持启动带参数 `-l`监听地址 `-f`间隔时间(秒) `-cacheTimes`缓存触发更新次数

```bash
docker run -d --name ddns-go --restart=always --net=host -v /opt/ddns-go:/root jeessy/ddns-go -l :9877 -f 600
docker run -d --name ddns-go --restart=always --net=host -v /opt/ddns-go:/root jeessy/ddns-go -l :9877 -f 600 -t 6
```

- [可选] 不使用docker host模式
Expand Down Expand Up @@ -179,7 +181,7 @@

- Discord任意客户端 -> 伺服器 -> 频道设置 -> 整合 -> 查看Webhook -> 新Webhook -> 复制Webhook网址
- URL中输入Discord复制的 `Webhook网址`
- RequestBody中输入
- RequestBody中输入
```json
{
"content": "域名 #{ipv4Domains} 动态解析 #{ipv4Result}.",
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ var listen = flag.String("l", ":9876", "监听地址")
// 更新频率(秒)
var every = flag.Int("f", 300, "同步间隔时间(秒)")

// 缓存次数
var ipCacheTimes = flag.Int("cacheTimes", 6, "IP缓存触发更新次数")

// 服务管理
var serviceType = flag.String("s", "", "服务管理, 支持install, uninstall")

Expand Down Expand Up @@ -66,6 +69,7 @@ func main() {
if *customDNSServer != "" {
os.Setenv(util.DNSServerEnv, *customDNSServer+":53")
}
os.Setenv(util.IPCacheTimesENV, strconv.Itoa(*ipCacheTimes))
switch *serviceType {
case "install":
installService()
Expand Down
13 changes: 12 additions & 1 deletion util/ip_cache.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package util

import (
"os"
"strconv"
)

const IPCacheTimesENV = "DDNS_IP_CACHE_TIMES"

// IpCache 上次IP缓存
type IpCache struct {
Addr string // 缓存地址
Expand All @@ -15,8 +22,12 @@ func (d *IpCache) Check(newAddr string) bool {
}
// 地址改变 或 达到剩余次数
if d.Addr != newAddr || d.Times <= 1 {
IPCacheTimes, err := strconv.Atoi(os.Getenv(IPCacheTimesENV))
if err != nil {
IPCacheTimes = 6
}
d.Addr = newAddr
d.Times = 6
d.Times = IPCacheTimes
return true
}
d.Addr = newAddr
Expand Down

0 comments on commit 35fb976

Please sign in to comment.