Skip to content

Commit

Permalink
feat: support command ip provider
Browse files Browse the repository at this point in the history
  • Loading branch information
gitsang committed Feb 12, 2025
1 parent 5868fcb commit 4eb7114
Show file tree
Hide file tree
Showing 10 changed files with 375 additions and 187 deletions.
78 changes: 48 additions & 30 deletions configs/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,53 @@ log:
maxBackups: 10
compress: true

interval: 30m
ddns:
interval: 30m

aliyun:
endpoint: "dns.aliyuncs.com"
accessKeyId:
accesskeySecret:
provider:
provider: aliyun
aliyun:
endpoint: "dns.aliyuncs.com"
accessKeyId:
accesskeySecret:

ddnss:
- enable: false
type: "A"
rr: "template.home"
domain: "example.com"
interface: "ens18"
prefix: "192.168"
- enable: false
type: "A"
rr: "*.template.home"
domain: "example.com"
interface: "ens18"
prefix: "192.168"
- enable: false
type: "AAAA"
rr: "template.home"
domain: "example.com"
interface: "ens18"
prefix: "240e"
- enable: false
type: "AAAA"
rr: "*.template.home"
domain: "example.com"
interface: "ens18"
prefix: "240e"
configs:
- enable: false
record:
type: "A"
rr: "template"
domain: "example.com"
provider:
type: "interface"
interface:
interface: "ens18"
prefix: "192.168"

- enable: false
record:
type: "A"
rr: "*.template"
domain: "example.com"
provider:
type: "interface"
interface:
interface: "ens18"
prefix: "192.168"

- enable: false
record:
type: "AAAA"
rr: "template"
domain: "example.com"
provider:
type: "command"
command: "ifconfig ens18 | grep inet6 | awk '{print $2}' | grep '^240e'"

- enable: false
record:
type: "AAAA"
rr: "template"
domain: "example.com"
provider:
type: "command"
command: "ifconfig ens18 | grep inet6 | awk '{print $2}' | grep '^240e'"
45 changes: 35 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package main

import (
"context"
"fmt"
"log/slog"
"os"

"github.com/gitsang/ddns/pkg/configer"
"github.com/gitsang/ddns/pkg/ddns"
"github.com/gitsang/ddns/pkg/ddns/aliddns"
"github.com/gitsang/ddns/pkg/ddns/alidns"
"github.com/gitsang/ddns/pkg/logi"
"github.com/gitsang/ddns/pkg/util/runtime"

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

type DnsProvider struct {
Provider string `json:"provider" yaml:"provider" default:"aliyun"`
Aliyun AliyunConfig `json:"aliyun" yaml:"aliyun"`
}

type Ddns struct {
Interval string `json:"interval" yaml:"interval" default:"1h" usage:"the interval to check and update dns record in duration format"`
Provider DnsProvider `json:"provider" yaml:"provider"`
Configs []ddns.DdnsConfig `json:"configs" yaml:"configs"`
}

type Config struct {
Log struct {
Default LogConfig `json:"default" yaml:"default"`
Fanouts []LogConfig `json:"fanouts" yaml:"fanouts"`
} `json:"log" yaml:"log"`
Interval string `json:"interval" yaml:"interval" default:"1h" usage:"the interval to check and update dns record in duration format"`
Aliyun AliyunConfig `json:"aliyun" yaml:"aliyun"`
Ddnss []ddns.DdnsConfig `json:"ddnss" yaml:"ddnss"`
Ddns Ddns `json:"ddns" yaml:"ddns"`
}

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -106,13 +116,28 @@ func run() {
slog.Any("config", c),
)

// ddns
svc, err := aliddns.NewService(
aliddns.WithLogHandler(logh),
aliddns.WithAliClient(c.Aliyun.Endpoint, c.Aliyun.AccessKeyId, c.Aliyun.AccessKeySecret),
aliddns.WithInterval(c.Interval),
aliddns.WithDdnsConfigs(c.Ddnss...),
// dnsProvider
var dnsProvider ddns.DnsProvider
switch c.Ddns.Provider.Provider {
case "aliyun":
dnsProvider, err = alidns.NewDnsProvider(
alidns.WithLogHandler(logh),
alidns.WithAliClient(c.Ddns.Provider.Aliyun.Endpoint, c.Ddns.Provider.Aliyun.AccessKeyId, c.Ddns.Provider.Aliyun.AccessKeySecret),
)
default:
panic(fmt.Errorf("unknown provider: %s", c.Ddns.Provider.Provider))
}

// service
svc, err := ddns.NewService(
ddns.WithLogHandler(logh),
ddns.WithInterval(c.Ddns.Interval),
ddns.WithDnsProvider(dnsProvider),
ddns.WithDdnsConfigs(c.Ddns.Configs...),
)
if err != nil {
panic(err)
}

// graceful shutdown
runtime.SetupGracefulShutdown(ctx, func(sig os.Signal) {
Expand Down
125 changes: 0 additions & 125 deletions pkg/ddns/aliddns/service.go

This file was deleted.

32 changes: 15 additions & 17 deletions pkg/ddns/aliddns/api.go → pkg/ddns/alidns/api.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package aliddns
package alidns

import (
"log/slog"
"time"

alidns20150109 "github.com/alibabacloud-go/alidns-20150109/v2/client"
"github.com/alibabacloud-go/tea/tea"
"github.com/gitsang/ddns/pkg/ddns"
)

func (s *Service) FindRecord(domain, rr, typ string) (*alidns20150109.DescribeDomainRecordsResponseBodyDomainRecordsRecord, error) {
func (s *DnsProvider) FindRecord(domain, rr, typ string) (*alidns20150109.DescribeDomainRecordsResponseBodyDomainRecordsRecord, error) {
var (
entryTime = time.Now()
logger = slog.New(s.logh).With(
Expand Down Expand Up @@ -47,7 +48,7 @@ func (s *Service) FindRecord(domain, rr, typ string) (*alidns20150109.DescribeDo
return nil, nil
}

func (s *Service) UpdateRecord(id, rr, typ, value string) error {
func (s *DnsProvider) UpdateRecord(id, rr, typ, value string) error {
var (
entryTime = time.Now()
logger = slog.New(s.logh).With(
Expand Down Expand Up @@ -78,7 +79,7 @@ func (s *Service) UpdateRecord(id, rr, typ, value string) error {
return nil
}

func (s *Service) CreateRecord(domain, rr, typ, value string) error {
func (s *DnsProvider) CreateRecord(domain, rr, typ, value string) error {
var (
entryTime = time.Now()
logger = slog.New(s.logh).With(
Expand Down Expand Up @@ -109,14 +110,12 @@ func (s *Service) CreateRecord(domain, rr, typ, value string) error {
return nil
}

func (s *Service) UpdateOrCreateRecord(domain, rr, typ, rec string) error {
func (s *DnsProvider) UpdateOrCreateRecord(record ddns.Record, value string) error {
var (
entryTime = time.Now()
logger = slog.New(s.logh).With(
slog.String("domain", domain),
slog.String("rr", rr),
slog.String("typ", typ),
slog.String("rec", rec),
slog.Any("record", record),
slog.String("value", value),
)
)

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

// find record
record, err := s.FindRecord(domain, rr, typ)
aliRecord, err := s.FindRecord(record.Domain, record.RR, record.Type)
if err != nil {
return err
}

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

// update if exist
recordId := *record.RecordId
recordValue := *record.Value
recordId := *aliRecord.RecordId
recordValue := *aliRecord.Value
logger = logger.With(slog.String("recordId", recordId), slog.String("recordValue", recordValue))

if recordValue == rec {
if recordValue == value {
logger.Info("record not change, skip")
return nil
}

err = s.UpdateRecord(recordId, rr, typ, rec)
err = s.UpdateRecord(recordId, record.RR, record.Type, value)
if err != nil {
logger = logger.With(slog.Any("err", err))
return err
Expand Down
Loading

0 comments on commit 4eb7114

Please sign in to comment.