Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor with wire #12

Merged
merged 5 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.21
go-version: 1.23

- name: Build
run: make build
Expand Down
131 changes: 5 additions & 126 deletions cmd/ddns/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,142 +2,21 @@ package main

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

"github.com/libdns/libdns"
"github.com/orvice/ddns/dns"
"github.com/orvice/ddns/internal/config"
"github.com/orvice/ddns/internal/ip"
"github.com/orvice/ddns/notify"
)

var (
dnsProvider dns.LibDNS
ipGetter ip.Getter
DNSMode string
"github.com/orvice/ddns/internal/wire"
)

var (
IPNotifyFormat = "[%s] ip changed, old IP: %s new IP: %s"
)

func Init() error {
logger := slog.Default()
var err error
err = config.Init()
if err != nil {
return err
}
ipGetter = ip.NewIfconfigCo()

notify.Init()

notifier, err := notify.NewTelegramNotifier(config.GetConfig().TelegramToken, config.GetConfig().TelegramChatID)
if err != nil {
logger.Error("notify init error", "error", err)
} else {
notify.AddNotifier(notifier)
}

dnsProvider = dns.New(config.GetConfig())

return nil
}

func main() {
logger := slog.Default()
err := Init()
app, err := wire.NewApp()
if err != nil {
logger.Error("init error", "error", err)
os.Exit(0)
}

ctx := context.Background()

for {
select {
case <-ctx.Done():
return
default:
err := updateIP(ctx)
if err != nil {
logger.Error("update ip error", "error", err.Error())
os.Exit(1)
}
time.Sleep(time.Minute * 3)
}
}
}

func updateIP(ctx context.Context) error {
logger := slog.Default()
ip, err := ipGetter.GetIP()
if err != nil {
logger.Error("Get ip error", "error", err)
return err
}

name, zone := zoneFromDomain(config.GetConfig().Domain)
logger.Info("zone from domain",
"name", name,
"zone", zone)

currentIP, err := dnsProvider.GetRecords(ctx, zone)
if err != nil {
logger.Error("Get records error", "error", err)
return err
}

var found bool
var record *libdns.Record
for _, r := range currentIP {
if r.Name == name {
found = true
record = &r
break
}
}
if found {
if record.Value == ip {
logger.Info("ip is same, skip update", "ip", ip)
return nil
}
oldIP := record.Value
record.Value = ip
_, err = dnsProvider.SetRecords(ctx, zone, []libdns.Record{
*record,
})
if err != nil {
logger.Error("Set records error", "error", err)
return err
}
notify.Notify(ctx, fmt.Sprintf(IPNotifyFormat, config.GetConfig().Domain, oldIP, ip))
} else {
_, err = dnsProvider.AppendRecords(ctx, zone, []libdns.Record{
{
Name: name,
Value: ip,
Type: "A",
},
})
if err != nil {
logger.Error("Append records error", "error", err)
return err
}
}

return nil
}

// zoneFromDomain return zone and domain
func zoneFromDomain(domain string) (string, string) {
arr := strings.SplitN(domain, ".", 2)
if len(arr) == 1 {
return "", ""
slog.Error("init app error", "error", err)
os.Exit(1)
}
return arr[0], arr[1]
app.Run(context.Background())
}
14 changes: 7 additions & 7 deletions dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ type LibDNS interface {
func New(conf *config.Config) LibDNS {
switch conf.DNSProvider {
case "cloudflare":
return NewCloudFlare()
return NewCloudFlare(conf)
case "aliyun":
return NewAliyun()
return NewAliyun(conf)
}
return nil
}

// cloudflare
func NewCloudFlare() LibDNS {
provider := cloudflare.Provider{APIToken: config.GetConfig().CFToken}
func NewCloudFlare(conf *config.Config) LibDNS {
provider := cloudflare.Provider{APIToken: conf.CFToken}
return &provider
}

// aliyun
func NewAliyun() LibDNS {
func NewAliyun(conf *config.Config) LibDNS {
provider := alidns.Provider{
AccKeyID: config.GetConfig().AliyunAccessKeyID,
AccKeySecret: config.GetConfig().AliyunAccessKeySecret,
AccKeyID: conf.AliyunAccessKeyID,
AccKeySecret: conf.AliyunAccessKeySecret,
}
return &provider
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ go 1.23

require (
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/google/wire v0.6.0
github.com/hashicorp/go-retryablehttp v0.7.7
github.com/libdns/alidns v1.0.3
github.com/libdns/cloudflare v0.1.1
github.com/libdns/libdns v0.2.2
github.com/lmittmann/tint v1.0.5
github.com/spf13/viper v1.18.0
github.com/stretchr/testify v1.9.0
github.com/weppos/publicsuffix-go v0.40.2
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
Expand All @@ -25,6 +26,9 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github/v50 v50.2.0/go.mod h1:VBY8FB6yPIjrtKhozXv4FQupxKLS6H4m6xFZlT43q8Q=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/google/wire v0.6.0 h1:HBkoIh4BdSxoyo9PveV8giw7ZsaBOvzWKfcg/6MrVwI=
github.com/google/wire v0.6.0/go.mod h1:F4QhpQ9EDIdJ1Mbop/NZBRB+5yrR6qg3BnctaoUk6NA=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k=
Expand All @@ -44,6 +48,8 @@ github.com/libdns/cloudflare v0.1.1/go.mod h1:9VK91idpOjg6v7/WbjkEW49bSCxj00ALes
github.com/libdns/libdns v0.2.0/go.mod h1:yQCXzk1lEZmmCPa857bnk4TsOiqYasqpyOEeSObbb40=
github.com/libdns/libdns v0.2.2 h1:O6ws7bAfRPaBsgAYt8MDe2HcNBGC29hkZ9MX2eUSX3s=
github.com/libdns/libdns v0.2.2/go.mod h1:4Bj9+5CQiNMVGf87wjX4CY3HQJypUHRuLvlsfsZqLWQ=
github.com/lmittmann/tint v1.0.5 h1:NQclAutOfYsqs2F1Lenue6OoWCajs5wJcP3DfWVpePw=
github.com/lmittmann/tint v1.0.5/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
Expand Down Expand Up @@ -96,6 +102,7 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
Expand All @@ -104,6 +111,7 @@ golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqR
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
Expand All @@ -114,6 +122,7 @@ golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
Expand All @@ -135,6 +144,7 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
Expand All @@ -146,6 +156,7 @@ golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4=
Expand All @@ -166,6 +177,7 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
122 changes: 122 additions & 0 deletions internal/app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package app

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

"github.com/libdns/libdns"
"github.com/orvice/ddns/dns"
"github.com/orvice/ddns/internal/config"
"github.com/orvice/ddns/internal/ip"
"github.com/orvice/ddns/notify"
)

var (
IPNotifyFormat = "[%s] ip changed, old IP: %s new IP: %s"
)

type App struct {
logger *slog.Logger
config *config.Config
dnsProvider dns.LibDNS
ipGetter ip.Getter
notifier notify.Notifier
}

func New(logger *slog.Logger, config *config.Config, dnsProvider dns.LibDNS, ipGetter ip.Getter, notifier notify.Notifier) *App {
return &App{
logger: logger,
config: config,
dnsProvider: dnsProvider,
ipGetter: ipGetter,
notifier: notifier,
}
}

func (a *App) Run(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
default:
err := a.updateIP(ctx)
if err != nil {
a.logger.Error("update ip error", "error", err.Error())
os.Exit(1)
}
time.Sleep(time.Minute * 3)
}
}
}

func (a *App) updateIP(ctx context.Context) error {
ip, err := a.ipGetter.GetIP()
if err != nil {
a.logger.Error("Get ip error", "error", err)
return err
}

name, zone := zoneFromDomain(a.config.Domain)
a.logger.Info("zone from domain",
"name", name,
"zone", zone)

currentIP, err := a.dnsProvider.GetRecords(ctx, zone)
if err != nil {
a.logger.Error("Get records error", "error", err)
return err
}

var found bool
var record *libdns.Record
for _, r := range currentIP {
if r.Name == name {
found = true
record = &r
break
}
}
if found {
if record.Value == ip {
a.logger.Info("ip is same, skip update", "ip", ip)
return nil
}
oldIP := record.Value
record.Value = ip
_, err = a.dnsProvider.SetRecords(ctx, zone, []libdns.Record{
*record,
})
if err != nil {
a.logger.Error("Set records error", "error", err)
return err
}
_ = a.notifier.Send(ctx, fmt.Sprintf(IPNotifyFormat, a.config.Domain, oldIP, ip))
} else {
_, err = a.dnsProvider.AppendRecords(ctx, zone, []libdns.Record{
{
Name: name,
Value: ip,
Type: "A",
},
})
if err != nil {
a.logger.Error("Append records error", "error", err)
return err
}
}

return nil
}

// zoneFromDomain return zone and domain
func zoneFromDomain(domain string) (string, string) {
arr := strings.SplitN(domain, ".", 2)
if len(arr) == 1 {
return "", ""
}
return arr[0], arr[1]
}
Loading
Loading