From 46c75dce3054ac0dde126b72b61a933dea614e86 Mon Sep 17 00:00:00 2001 From: orvice L Date: Wed, 23 Oct 2024 23:59:58 +0800 Subject: [PATCH] fix lint --- cmd/ddns/main.go | 11 +++----- config/config.go | 4 +-- dns/mu.go | 55 --------------------------------------- go.mod | 5 +--- go.sum | 2 -- internal/config/config.go | 27 +++++-------------- internal/ip/ip.go | 10 +++---- notify/notify.go | 2 +- 8 files changed, 20 insertions(+), 96 deletions(-) delete mode 100644 dns/mu.go diff --git a/cmd/ddns/main.go b/cmd/ddns/main.go index 18b1e74..7ca4543 100644 --- a/cmd/ddns/main.go +++ b/cmd/ddns/main.go @@ -8,7 +8,6 @@ import ( "strings" "time" - "github.com/catpie/musdk-go" "github.com/libdns/libdns" "github.com/weeon/log" "github.com/weeon/utils/task" @@ -21,27 +20,25 @@ import ( var ( dnsProvider dns.LibDNS - ipGetter ip.IPGetter - muCli *musdk.Client + ipGetter ip.Getter + DNSMode string ) func Init() error { - var err error config.GetConfigFromEnv() ipGetter = ip.NewIfconfigCo() - muCli = musdk.ClientFromEnv(slog.Default()) notify.Init() - notifier, err := notify.NewTelegramNotifier(config.TELEGRAM_TOKEN, config.TELEGRAM_CHATID) + notifier, err := notify.NewTelegramNotifier(config.TelegramToken, config.TelegramChatID) if err != nil { log.Errorf("notify init error %v", err) } else { notify.AddNotifier(notifier) } - switch config.DNS_MODE { + switch config.DNSMode { default: dnsProvider = dns.NewCloudFlare() } diff --git a/config/config.go b/config/config.go index f41d7e4..cca219e 100644 --- a/config/config.go +++ b/config/config.go @@ -3,12 +3,12 @@ package config import "github.com/orvice/utils/env" var ( - IpNotifyFormat = "[%s] ip changed, old IP: %s new IP: %s" + IPNotifyFormat = "[%s] ip changed, old IP: %s new IP: %s" ) func Init() { inf := env.Get("IP_CHANGE_FORMAT") if len(inf) != 0 { - IpNotifyFormat = inf + IPNotifyFormat = inf } } diff --git a/dns/mu.go b/dns/mu.go deleted file mode 100644 index 3d5751b..0000000 --- a/dns/mu.go +++ /dev/null @@ -1,55 +0,0 @@ -package dns - -import ( - "bytes" - "context" - "encoding/json" - "fmt" - "io/ioutil" - "net/http" - - "github.com/weeon/log" -) - -type Mu struct { - apiUrl string - nodeID int -} - -func NewMu(apiUrl string, nodeID int) (*Mu, error) { - return &Mu{ - apiUrl: apiUrl, - nodeID: nodeID, - }, nil -} - -func (m *Mu) GetIP(ctx context.Context, domain string) (string, error) { - return "", nil -} -func (m *Mu) UpdateIP(tx context.Context, domain, ip string) error { - uri := fmt.Sprintf("%s/nodes/%d/ip", m.apiUrl, m.nodeID) - ma := map[string]interface{}{ - "ip": ip, - "domain": domain, - } - - body, err := json.Marshal(ma) - if err != nil { - return err - } - input := bytes.NewBuffer(body) - - resp, err := http.DefaultClient.Post(uri, "application/json", input) - if err != nil { - return err - } - - defer resp.Body.Close() - - b, err := ioutil.ReadAll(resp.Body) - log.Debugw("update ip ", - "response", string(b), - ) - - return nil -} diff --git a/go.mod b/go.mod index 6f306be..30b2ad4 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,6 @@ module github.com/orvice/ddns require ( - github.com/catpie/musdk-go v0.0.0-20230815021801-7d27a00f9c60 github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 github.com/libdns/cloudflare v0.1.1 github.com/libdns/libdns v0.2.2 @@ -28,6 +27,4 @@ require ( gopkg.in/yaml.v2 v2.4.0 // indirect ) -go 1.21 - -toolchain go1.21.0 +go 1.23 diff --git a/go.sum b/go.sum index 5405b7a..eb24dce 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,6 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/catpie/musdk-go v0.0.0-20230815021801-7d27a00f9c60 h1:2PpsQ5gc13fFhvEG1qtObPAWFveq8TBcL8qbWBuP7cU= -github.com/catpie/musdk-go v0.0.0-20230815021801-7d27a00f9c60/go.mod h1:4W31RomrUPiZpHE9E9biaDASJImNRJDcMjUBJCQQWaY= github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= diff --git a/internal/config/config.go b/internal/config/config.go index c1c8471..a6367ac 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -3,33 +3,20 @@ package config import "github.com/orvice/utils/env" var ( - DOMAIN string - UPDATE_TIME int -) - -const ( - DNS_MODE_MU = "mu" + DOMAIN string ) var ( - DNS_MODE string - - NODE_ID int - API_URI string + DNSMode string - TELEGRAM_TOKEN string - TELEGRAM_CHATID int64 + TelegramToken string + TelegramChatID int64 ) func GetConfigFromEnv() { - DNS_MODE = env.Get("DNS_MODE", "cf") - + DNSMode = env.Get("DNS_MODE", "cf") DOMAIN = env.Get("DOMAIN") - UPDATE_TIME = env.GetInt("UPDATE_TIME", 300) - - NODE_ID = env.GetInt("MU_NODE_ID") - API_URI = env.Get("API_URI") - TELEGRAM_CHATID = int64(env.GetInt("TELEGRAM_CHATID")) - TELEGRAM_TOKEN = env.Get("TELEGRAM_TOKEN") + TelegramChatID = int64(env.GetInt("TELEGRAM_CHATID")) + TelegramToken = env.Get("TELEGRAM_TOKEN") } diff --git a/internal/ip/ip.go b/internal/ip/ip.go index 68af536..3419ca1 100644 --- a/internal/ip/ip.go +++ b/internal/ip/ip.go @@ -7,14 +7,14 @@ import ( ) const ( - IpConfigCoAddr = "https://ifconfig.co/json" + ipConfigCoAddr = "https://ifconfig.co/json" ) -type IPResponse struct { +type Response struct { IP string `json:"ip"` } -type IPGetter interface { +type Getter interface { GetIP() (string, error) } @@ -28,7 +28,7 @@ func NewIfconfigCo() *IfconfigCo { func (i *IfconfigCo) GetIP() (string, error) { cli := http.Client{} defer cli.CloseIdleConnections() - resp, err := cli.Get(IpConfigCoAddr) + resp, err := cli.Get(ipConfigCoAddr) if err != nil { return "", err } @@ -38,7 +38,7 @@ func (i *IfconfigCo) GetIP() (string, error) { if err != nil { return "", err } - var ret IPResponse + var ret Response err = json.Unmarshal(s, &ret) if err != nil { return "", err diff --git a/notify/notify.go b/notify/notify.go index 5706849..e0481f8 100644 --- a/notify/notify.go +++ b/notify/notify.go @@ -33,7 +33,7 @@ func NewTelegramNotifier(token string, chatID int64) (*TelegramNotifier, error) }, nil } -func (t *TelegramNotifier) Send(ctx context.Context, s string) error { +func (t *TelegramNotifier) Send(_ context.Context, s string) error { msg := tgbotapi.NewMessage(t.chatID, s) resp, err := t.bot.Send(msg) if err != nil {