Skip to content

Commit 4eb7114

Browse files
committed
feat: support command ip provider
1 parent 5868fcb commit 4eb7114

File tree

10 files changed

+375
-187
lines changed

10 files changed

+375
-187
lines changed

configs/example.yaml

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,53 @@ log:
2525
maxBackups: 10
2626
compress: true
2727

28-
interval: 30m
28+
ddns:
29+
interval: 30m
2930

30-
aliyun:
31-
endpoint: "dns.aliyuncs.com"
32-
accessKeyId:
33-
accesskeySecret:
31+
provider:
32+
provider: aliyun
33+
aliyun:
34+
endpoint: "dns.aliyuncs.com"
35+
accessKeyId:
36+
accesskeySecret:
3437

35-
ddnss:
36-
- enable: false
37-
type: "A"
38-
rr: "template.home"
39-
domain: "example.com"
40-
interface: "ens18"
41-
prefix: "192.168"
42-
- enable: false
43-
type: "A"
44-
rr: "*.template.home"
45-
domain: "example.com"
46-
interface: "ens18"
47-
prefix: "192.168"
48-
- enable: false
49-
type: "AAAA"
50-
rr: "template.home"
51-
domain: "example.com"
52-
interface: "ens18"
53-
prefix: "240e"
54-
- enable: false
55-
type: "AAAA"
56-
rr: "*.template.home"
57-
domain: "example.com"
58-
interface: "ens18"
59-
prefix: "240e"
38+
configs:
39+
- enable: false
40+
record:
41+
type: "A"
42+
rr: "template"
43+
domain: "example.com"
44+
provider:
45+
type: "interface"
46+
interface:
47+
interface: "ens18"
48+
prefix: "192.168"
49+
50+
- enable: false
51+
record:
52+
type: "A"
53+
rr: "*.template"
54+
domain: "example.com"
55+
provider:
56+
type: "interface"
57+
interface:
58+
interface: "ens18"
59+
prefix: "192.168"
60+
61+
- enable: false
62+
record:
63+
type: "AAAA"
64+
rr: "template"
65+
domain: "example.com"
66+
provider:
67+
type: "command"
68+
command: "ifconfig ens18 | grep inet6 | awk '{print $2}' | grep '^240e'"
69+
70+
- enable: false
71+
record:
72+
type: "AAAA"
73+
rr: "template"
74+
domain: "example.com"
75+
provider:
76+
type: "command"
77+
command: "ifconfig ens18 | grep inet6 | awk '{print $2}' | grep '^240e'"

main.go

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package main
22

33
import (
44
"context"
5+
"fmt"
56
"log/slog"
67
"os"
78

89
"github.com/gitsang/ddns/pkg/configer"
910
"github.com/gitsang/ddns/pkg/ddns"
10-
"github.com/gitsang/ddns/pkg/ddns/aliddns"
11+
"github.com/gitsang/ddns/pkg/ddns/alidns"
1112
"github.com/gitsang/ddns/pkg/logi"
1213
"github.com/gitsang/ddns/pkg/util/runtime"
1314

@@ -42,14 +43,23 @@ type AliyunConfig struct {
4243
AccessKeySecret string `json:"accessKeySecret" yaml:"accessKeySecret" default:"changeit" usage:"aliyun access key secret"`
4344
}
4445

46+
type DnsProvider struct {
47+
Provider string `json:"provider" yaml:"provider" default:"aliyun"`
48+
Aliyun AliyunConfig `json:"aliyun" yaml:"aliyun"`
49+
}
50+
51+
type Ddns struct {
52+
Interval string `json:"interval" yaml:"interval" default:"1h" usage:"the interval to check and update dns record in duration format"`
53+
Provider DnsProvider `json:"provider" yaml:"provider"`
54+
Configs []ddns.DdnsConfig `json:"configs" yaml:"configs"`
55+
}
56+
4557
type Config struct {
4658
Log struct {
4759
Default LogConfig `json:"default" yaml:"default"`
4860
Fanouts []LogConfig `json:"fanouts" yaml:"fanouts"`
4961
} `json:"log" yaml:"log"`
50-
Interval string `json:"interval" yaml:"interval" default:"1h" usage:"the interval to check and update dns record in duration format"`
51-
Aliyun AliyunConfig `json:"aliyun" yaml:"aliyun"`
52-
Ddnss []ddns.DdnsConfig `json:"ddnss" yaml:"ddnss"`
62+
Ddns Ddns `json:"ddns" yaml:"ddns"`
5363
}
5464

5565
var rootCmd = &cobra.Command{
@@ -106,13 +116,28 @@ func run() {
106116
slog.Any("config", c),
107117
)
108118

109-
// ddns
110-
svc, err := aliddns.NewService(
111-
aliddns.WithLogHandler(logh),
112-
aliddns.WithAliClient(c.Aliyun.Endpoint, c.Aliyun.AccessKeyId, c.Aliyun.AccessKeySecret),
113-
aliddns.WithInterval(c.Interval),
114-
aliddns.WithDdnsConfigs(c.Ddnss...),
119+
// dnsProvider
120+
var dnsProvider ddns.DnsProvider
121+
switch c.Ddns.Provider.Provider {
122+
case "aliyun":
123+
dnsProvider, err = alidns.NewDnsProvider(
124+
alidns.WithLogHandler(logh),
125+
alidns.WithAliClient(c.Ddns.Provider.Aliyun.Endpoint, c.Ddns.Provider.Aliyun.AccessKeyId, c.Ddns.Provider.Aliyun.AccessKeySecret),
126+
)
127+
default:
128+
panic(fmt.Errorf("unknown provider: %s", c.Ddns.Provider.Provider))
129+
}
130+
131+
// service
132+
svc, err := ddns.NewService(
133+
ddns.WithLogHandler(logh),
134+
ddns.WithInterval(c.Ddns.Interval),
135+
ddns.WithDnsProvider(dnsProvider),
136+
ddns.WithDdnsConfigs(c.Ddns.Configs...),
115137
)
138+
if err != nil {
139+
panic(err)
140+
}
116141

117142
// graceful shutdown
118143
runtime.SetupGracefulShutdown(ctx, func(sig os.Signal) {

pkg/ddns/aliddns/service.go

Lines changed: 0 additions & 125 deletions
This file was deleted.

pkg/ddns/aliddns/api.go renamed to pkg/ddns/alidns/api.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
package aliddns
1+
package alidns
22

33
import (
44
"log/slog"
55
"time"
66

77
alidns20150109 "github.com/alibabacloud-go/alidns-20150109/v2/client"
88
"github.com/alibabacloud-go/tea/tea"
9+
"github.com/gitsang/ddns/pkg/ddns"
910
)
1011

11-
func (s *Service) FindRecord(domain, rr, typ string) (*alidns20150109.DescribeDomainRecordsResponseBodyDomainRecordsRecord, error) {
12+
func (s *DnsProvider) FindRecord(domain, rr, typ string) (*alidns20150109.DescribeDomainRecordsResponseBodyDomainRecordsRecord, error) {
1213
var (
1314
entryTime = time.Now()
1415
logger = slog.New(s.logh).With(
@@ -47,7 +48,7 @@ func (s *Service) FindRecord(domain, rr, typ string) (*alidns20150109.DescribeDo
4748
return nil, nil
4849
}
4950

50-
func (s *Service) UpdateRecord(id, rr, typ, value string) error {
51+
func (s *DnsProvider) UpdateRecord(id, rr, typ, value string) error {
5152
var (
5253
entryTime = time.Now()
5354
logger = slog.New(s.logh).With(
@@ -78,7 +79,7 @@ func (s *Service) UpdateRecord(id, rr, typ, value string) error {
7879
return nil
7980
}
8081

81-
func (s *Service) CreateRecord(domain, rr, typ, value string) error {
82+
func (s *DnsProvider) CreateRecord(domain, rr, typ, value string) error {
8283
var (
8384
entryTime = time.Now()
8485
logger = slog.New(s.logh).With(
@@ -109,14 +110,12 @@ func (s *Service) CreateRecord(domain, rr, typ, value string) error {
109110
return nil
110111
}
111112

112-
func (s *Service) UpdateOrCreateRecord(domain, rr, typ, rec string) error {
113+
func (s *DnsProvider) UpdateOrCreateRecord(record ddns.Record, value string) error {
113114
var (
114115
entryTime = time.Now()
115116
logger = slog.New(s.logh).With(
116-
slog.String("domain", domain),
117-
slog.String("rr", rr),
118-
slog.String("typ", typ),
119-
slog.String("rec", rec),
117+
slog.Any("record", record),
118+
slog.String("value", value),
120119
)
121120
)
122121

@@ -126,14 +125,14 @@ func (s *Service) UpdateOrCreateRecord(domain, rr, typ, rec string) error {
126125
}()
127126

128127
// find record
129-
record, err := s.FindRecord(domain, rr, typ)
128+
aliRecord, err := s.FindRecord(record.Domain, record.RR, record.Type)
130129
if err != nil {
131130
return err
132131
}
133132

134133
// create if not exist
135-
if record == nil {
136-
err = s.CreateRecord(domain, rr, typ, rec)
134+
if aliRecord == nil {
135+
err = s.CreateRecord(record.Domain, record.RR, record.Type, value)
137136
if err != nil {
138137
logger = logger.With(slog.Any("err", err))
139138
return err
@@ -143,16 +142,15 @@ func (s *Service) UpdateOrCreateRecord(domain, rr, typ, rec string) error {
143142
}
144143

145144
// update if exist
146-
recordId := *record.RecordId
147-
recordValue := *record.Value
145+
recordId := *aliRecord.RecordId
146+
recordValue := *aliRecord.Value
148147
logger = logger.With(slog.String("recordId", recordId), slog.String("recordValue", recordValue))
149-
150-
if recordValue == rec {
148+
if recordValue == value {
151149
logger.Info("record not change, skip")
152150
return nil
153151
}
154152

155-
err = s.UpdateRecord(recordId, rr, typ, rec)
153+
err = s.UpdateRecord(recordId, record.RR, record.Type, value)
156154
if err != nil {
157155
logger = logger.With(slog.Any("err", err))
158156
return err

0 commit comments

Comments
 (0)