Skip to content

Commit

Permalink
refactor the ip helper
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyYe committed Feb 3, 2024
1 parent 7767024 commit 3ba96bc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
7 changes: 6 additions & 1 deletion internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,22 @@ var (
)

type Handler struct {
ctx context.Context
Configuration *settings.Settings
dnsProvider provider.IDNSProvider
notificationManager notification.INotificationManager
ipManager *lib.IPHelper
cachedIP string
}

func (handler *Handler) SetContext(ctx context.Context) {
handler.ctx = ctx
}

func (handler *Handler) SetConfiguration(conf *settings.Settings) {
handler.Configuration = conf
handler.notificationManager = notification.GetNotificationManager(handler.Configuration)
handler.ipManager = lib.NewIPHelper(handler.Configuration)
handler.ipManager = lib.GetIPHelperInstance(handler.Configuration)
}

func (handler *Handler) SetProvider(provider provider.IDNSProvider) {
Expand Down
8 changes: 5 additions & 3 deletions internal/manager/dns_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ func (manager *DNSManager) Build() error {
return err
}

ctx, cancel := context.WithCancel(context.Background())
manager.ctx = ctx
manager.cancel = cancel

manager.provider = dnsProvider
manager.handler = &handler.Handler{}
manager.handler.SetContext(manager.ctx)
manager.handler.SetConfiguration(manager.configuration)
manager.handler.SetProvider(manager.provider)

ctx, cancel := context.WithCancel(context.Background())
manager.ctx = ctx
manager.cancel = cancel
return nil
}

Expand Down
39 changes: 25 additions & 14 deletions pkg/lib/ip_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,44 +27,55 @@ type IPHelper struct {
idx int64
}

func NewIPHelper(conf *settings.Settings) *IPHelper {
manager := &IPHelper{
configuration: conf,
idx: -1,
}
var (
helperInstance *IPHelper
helperOnce sync.Once
)

func (helper *IPHelper) loadConfiguration(conf *settings.Settings) {
if conf.IPType == "" || strings.ToUpper(conf.IPType) == utils.IPV4 {
// filter empty urls
for _, url := range conf.IPUrls {
if url != "" {
manager.reqURLs = append(manager.reqURLs, url)
helper.reqURLs = append(helper.reqURLs, url)
}
}

if conf.IPUrl != "" {
manager.reqURLs = append(manager.reqURLs, conf.IPUrl)
helper.reqURLs = append(helper.reqURLs, conf.IPUrl)
}
} else {
// filter empty urls
for _, url := range conf.IPV6Urls {
if url != "" {
manager.reqURLs = append(manager.reqURLs, url)
helper.reqURLs = append(helper.reqURLs, url)
}
}

if conf.IPV6Url != "" {
manager.reqURLs = append(manager.reqURLs, conf.IPV6Url)
helper.reqURLs = append(helper.reqURLs, conf.IPV6Url)
}
}
}

SafeGo(func() {
for {
manager.getCurrentIP()
time.Sleep(time.Second * time.Duration(conf.Interval))
func GetIPHelperInstance(conf *settings.Settings) *IPHelper {
once.Do(func() {
helperInstance = &IPHelper{
configuration: conf,
idx: -1,
}

helperInstance.loadConfiguration(conf)

SafeGo(func() {
for {
helperInstance.getCurrentIP()
time.Sleep(time.Second * time.Duration(conf.Interval))
}
})
})

return manager
return helperInstance
}

func (helper *IPHelper) GetCurrentIP() string {
Expand Down
3 changes: 2 additions & 1 deletion pkg/lib/ip_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"testing"

"github.com/TimothyYe/godns/internal/settings"
"github.com/TimothyYe/godns/pkg/lib"
)

func TestGetCurrentIP(t *testing.T) {
t.Skip()
conf := &settings.Settings{IPUrls: []string{"https://myip.biturl.top"}}
helper := NewIPHelper(conf)
helper := lib.GetIPHelperInstance(conf)
ip := helper.GetCurrentIP()

if ip == "" {
Expand Down

0 comments on commit 3ba96bc

Please sign in to comment.