From fb99ace0264410899bac26404554ccf42cf4d22d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=9F=E9=85=8C=20=E9=B5=AC=E5=85=84?= Date: Mon, 14 Mar 2022 07:05:38 +0900 Subject: [PATCH] Do not use ch.DNSName --- freedns/freedns.go | 6 ++++++ main.go | 24 +++++++++++------------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/freedns/freedns.go b/freedns/freedns.go index 59248f2..329d0b7 100755 --- a/freedns/freedns.go +++ b/freedns/freedns.go @@ -34,6 +34,12 @@ const URI_DELETE_RECORD = "https://freedns.afraid.org/subdomain/delete2.php?data // const URI_LOGIN string = "http://127.0.0.1:1234/" +func GetDomainFromZone(Zone string) string { + _segs := strings.Split(Zone, ".") + _segs = _segs[len(_segs)-2:] + return strings.Join(_segs, ".") +} + func _HttpRequest(method string, url string, PostData url.Values, ExCookie *http.Cookie) (*http.Response, string, error) { client := http.Client{ CheckRedirect: func(req *http.Request, via []*http.Request) error { diff --git a/main.go b/main.go index dd383ba..45a4003 100644 --- a/main.go +++ b/main.go @@ -104,22 +104,23 @@ func (c *customDNSProviderSolver) Present(ch *v1alpha1.ChallengeRequest) error { username := string(secretObj.Data["username"]) password := string(secretObj.Data["password"]) - freedns := freedns.FreeDNS{} - err = freedns.Login(username, password) + dnsObj := freedns.FreeDNS{} + err = dnsObj.Login(username, password) if err != nil { return err } - err = freedns.SelectDomain(ch.DNSName) + domainName := freedns.GetDomainFromZone(ch.ResolvedZone) + err = dnsObj.SelectDomain(domainName) if err != nil { return err } - _zone := strings.TrimRight(ch.ResolvedZone, ch.DNSName) + _zone := strings.TrimRight(ch.ResolvedZone, domainName) _key := "\"" + ch.Key + "\"" - freedns.AddRecord("TXT", _zone, _key, true, "") + dnsObj.AddRecord("TXT", _zone, _key, true, "") - c.freedns = &freedns + c.freedns = &dnsObj return nil } @@ -130,13 +131,9 @@ func (c *customDNSProviderSolver) Present(ch *v1alpha1.ChallengeRequest) error { // This is in order to facilitate multiple DNS validations for the same domain // concurrently. func (c *customDNSProviderSolver) CleanUp(ch *v1alpha1.ChallengeRequest) error { - _zone := strings.TrimRight(ch.ResolvedZone, ch.DNSName) - _key := "\"" + ch.Key + "\"" - _id, err := c.freedns.FindRecord(_zone, "TXT", _key) - if err != nil { - return err - } + _key := "\"" + ch.Key + "\"" + _id, err := c.freedns.FindRecord(ch.ResolvedZone, "TXT", _key) if _id != "" { err = c.freedns.DeleteRecord(_id) @@ -144,7 +141,8 @@ func (c *customDNSProviderSolver) CleanUp(ch *v1alpha1.ChallengeRequest) error { return err } } - return nil + + return c.freedns.Logout() } // Initialize will be called when the webhook first starts.