Skip to content

Commit

Permalink
feat: TTL (jeessy2#93)
Browse files Browse the repository at this point in the history
* feat: ttl web

* feat: dnspod ttl

* feat: ali ttl

* feat: cloudflare ttl

* feat: huawei ttl

* fix: 说明

* fix: 显示以前的ttl

* docs: ttl
  • Loading branch information
jeessy2 authored Aug 15, 2021
1 parent b46ce68 commit 9e15189
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
- 网页中配置,简单又方便,可设置 `登录用户名和密码` / `禁止从公网访问`
- 网页中方便快速查看最近50条日志,不需要跑docker中查看
- 支持webhook
- 支持TTL

## 系统中使用

Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Config struct {
Webhook
// 禁止公网访问
NotAllowWanAccess bool
TTL string
}

// DNSConfig DNS配置
Expand Down
14 changes: 12 additions & 2 deletions dns/alidns.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ const (
alidnsEndpoint string = "https://alidns.aliyuncs.com/"
)

// https://help.aliyun.com/document_detail/29776.html?spm=a2c4g.11186623.6.672.715a45caji9dMA
// Alidns Alidns
type Alidns struct {
DNSConfig config.DNSConfig
Domains config.Domains
TTL string
}

// AlidnsSubDomainRecords 记录
Expand All @@ -42,6 +44,12 @@ type AlidnsResp struct {
func (ali *Alidns) Init(conf *config.Config) {
ali.DNSConfig = conf.DNS
ali.Domains.ParseDomain(conf)
if conf.TTL == "" {
// 默认600s
ali.TTL = "600"
} else {
ali.TTL = conf.TTL
}
}

// AddUpdateDomainRecords 添加或更新IPv4/IPv6记录
Expand Down Expand Up @@ -90,11 +98,12 @@ func (ali *Alidns) create(domain *config.Domain, recordType string, ipAddr strin
params.Set("RR", domain.GetSubDomain())
params.Set("Type", recordType)
params.Set("Value", ipAddr)
params.Set("TTL", ali.TTL)

var result AlidnsResp
err := ali.request(params, &result)

if err == nil && "" != result.RecordID {
if err == nil && result.RecordID != "" {
log.Printf("新增域名解析 %s 成功!IP: %s", domain, ipAddr)
domain.UpdateStatus = config.UpdatedSuccess
} else {
Expand All @@ -118,11 +127,12 @@ func (ali *Alidns) modify(record AlidnsSubDomainRecords, domain *config.Domain,
params.Set("RecordId", record.DomainRecords.Record[0].RecordID)
params.Set("Type", recordType)
params.Set("Value", ipAddr)
params.Set("TTL", ali.TTL)

var result AlidnsResp
err := ali.request(params, &result)

if err == nil && "" != result.RecordID {
if err == nil && result.RecordID != "" {
log.Printf("更新域名解析 %s 成功!IP: %s", domain, ipAddr)
domain.UpdateStatus = config.UpdatedSuccess
} else {
Expand Down
17 changes: 15 additions & 2 deletions dns/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"log"
"net/http"
"strconv"
"time"
)

Expand All @@ -19,6 +20,7 @@ const (
type Cloudflare struct {
DNSConfig config.DNSConfig
Domains config.Domains
TTL int
}

// CloudflareZonesResp cloudflare zones返回结果
Expand Down Expand Up @@ -58,6 +60,17 @@ type CloudflareStatus struct {
func (cf *Cloudflare) Init(conf *config.Config) {
cf.DNSConfig = conf.DNS
cf.Domains.ParseDomain(conf)
if conf.TTL == "" {
// 默认1 auto ttl
cf.TTL = 1
} else {
ttl, err := strconv.Atoi(conf.TTL)
if err != nil {
cf.TTL = 1
} else {
cf.TTL = ttl
}
}
}

// AddUpdateDomainRecords 添加或更新IPv4/IPv6记录
Expand Down Expand Up @@ -112,8 +125,7 @@ func (cf *Cloudflare) create(zoneID string, domain *config.Domain, recordType st
Name: domain.String(),
Content: ipAddr,
Proxied: false,
// auto ttl
TTL: 1,
TTL: cf.TTL,
}
var status CloudflareStatus
err := cf.request(
Expand Down Expand Up @@ -142,6 +154,7 @@ func (cf *Cloudflare) modify(result CloudflareRecordsResp, zoneID string, domain
}
var status CloudflareStatus
record.Content = ipAddr
record.TTL = cf.TTL

err := cf.request(
"PUT",
Expand Down
10 changes: 10 additions & 0 deletions dns/dnspod.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ const (
recordCreateAPI string = "https://dnsapi.cn/Record.Create"
)

// https://cloud.tencent.com/document/api/302/8516
// Dnspod 腾讯云dns实现
type Dnspod struct {
DNSConfig config.DNSConfig
Domains config.Domains
TTL string
}

// DnspodRecordListResp recordListAPI结果
Expand All @@ -45,6 +47,12 @@ type DnspodStatus struct {
func (dnspod *Dnspod) Init(conf *config.Config) {
dnspod.DNSConfig = conf.DNS
dnspod.Domains.ParseDomain(conf)
if conf.TTL == "" {
// 默认600s
dnspod.TTL = "600"
} else {
dnspod.TTL = conf.TTL
}
}

// AddUpdateDomainRecords 添加或更新IPv4/IPv6记录
Expand Down Expand Up @@ -88,6 +96,7 @@ func (dnspod *Dnspod) create(result DnspodRecordListResp, domain *config.Domain,
"record_type": {recordType},
"record_line": {"默认"},
"value": {ipAddr},
"ttl": {dnspod.TTL},
"format": {"json"},
},
domain,
Expand Down Expand Up @@ -119,6 +128,7 @@ func (dnspod *Dnspod) modify(result DnspodRecordListResp, domain *config.Domain,
"record_line": {"默认"},
"record_id": {record.ID},
"value": {ipAddr},
"ttl": {dnspod.TTL},
"format": {"json"},
},
domain,
Expand Down
17 changes: 17 additions & 0 deletions dns/huawei.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ import (
"fmt"
"log"
"net/http"
"strconv"
"time"
)

const (
huaweicloudEndpoint string = "https://dns.myhuaweicloud.com"
)

// https://support.huaweicloud.com/api-dns/dns_api_64001.html
// Huaweicloud Huaweicloud
type Huaweicloud struct {
DNSConfig config.DNSConfig
Domains config.Domains
TTL int
}

// HuaweicloudZonesResp zones response
Expand All @@ -42,13 +45,25 @@ type HuaweicloudRecordsets struct {
ZoneID string `json:"zone_id"`
Status string
Type string `json:"type"`
TTL int `json:"ttl"`
Records []string `json:"records"`
}

// Init 初始化
func (hw *Huaweicloud) Init(conf *config.Config) {
hw.DNSConfig = conf.DNS
hw.Domains.ParseDomain(conf)
if conf.TTL == "" {
// 默认300s
hw.TTL = 300
} else {
ttl, err := strconv.Atoi(conf.TTL)
if err != nil {
hw.TTL = 300
} else {
hw.TTL = ttl
}
}
}

// AddUpdateDomainRecords 添加或更新IPv4/IPv6记录
Expand Down Expand Up @@ -122,6 +137,7 @@ func (hw *Huaweicloud) create(domain *config.Domain, recordType string, ipAddr s
Type: recordType,
Name: domain.String() + ".",
Records: []string{ipAddr},
TTL: hw.TTL,
}
var result HuaweicloudRecordsets
err = hw.request(
Expand Down Expand Up @@ -150,6 +166,7 @@ func (hw *Huaweicloud) modify(record HuaweicloudRecordsets, domain *config.Domai

var request map[string]interface{} = make(map[string]interface{})
request["records"] = []string{ipAddr}
request["ttl"] = hw.TTL

var result HuaweicloudRecordsets

Expand Down
1 change: 1 addition & 0 deletions web/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func Save(writer http.ResponseWriter, request *http.Request) {
conf.WebhookRequestBody = strings.TrimSpace(request.FormValue("WebhookRequestBody"))

conf.NotAllowWanAccess = request.FormValue("NotAllowWanAccess") == "on"
conf.TTL = request.FormValue("TTL")

// 保存到用户目录
err := conf.SaveConfig()
Expand Down
18 changes: 18 additions & 0 deletions web/writing.html
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,24 @@ <h5 class="portlet__head">其它配置</h5>
</div>
</div>

<div class="form-group row">
<label class="col-sm-2 col-form-label">TTL</label>
<div class="col-sm-10">
<select class="form-control" name="TTL" value="{{.TTL}}">
<option value="" {{if eq .TTL ""}}selected{{end}}>自动</option>
<option value="1" {{if eq .TTL "1"}}selected{{end}}>1秒</option>
<option value="5" {{if eq .TTL "5"}}selected{{end}}>5秒</option>
<option value="10" {{if eq .TTL "10"}}selected{{end}}>10秒</option>
<option value="60" {{if eq .TTL "60"}}selected{{end}}>1分钟</option>
<option value="120" {{if eq .TTL "120"}}selected{{end}}>2分钟</option>
<option value="600" {{if eq .TTL "600"}}selected{{end}}>10分钟</option>
<option value="1800" {{if eq .TTL "1800"}}selected{{end}}>30分钟</option>
<option value="3600" {{if eq .TTL "3600"}}selected{{end}}>1小时</option>
</select>
<small id="ttl_help" class="form-text text-muted">如账号支持更小的TTL, 可修改. IP有变化时才会更新TTL</small>
</div>
</div>

</div>
</div>

Expand Down

0 comments on commit 9e15189

Please sign in to comment.