Passed Conformance Tests

This commit is contained in:
斟酌 鵬兄 2022-03-14 06:39:42 +09:00
parent 5aa072deb2
commit b99ca72213
3 changed files with 52 additions and 10 deletions

View File

@ -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/" // const URI_LOGIN string = "http://127.0.0.1:1234/"
func GetDomainFromZone(Zone string) string {
_segs := strings.Split(strings.TrimSuffix(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) { func _HttpRequest(method string, url string, PostData url.Values, ExCookie *http.Cookie) (*http.Response, string, error) {
client := http.Client{ client := http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error { CheckRedirect: func(req *http.Request, via []*http.Request) error {
@ -157,7 +163,7 @@ loop:
attrKey, attrValue, moreAttr := htmlTokens.TagAttr() attrKey, attrValue, moreAttr := htmlTokens.TagAttr()
_href := string(attrValue) _href := string(attrValue)
if string(attrKey) == "href" && strings.Contains(_href, "/subdomain/?limit=") { if string(attrKey) == "href" && strings.Contains(_href, "/subdomain/?limit=") {
dnsObj.DomainId = strings.TrimLeft(_href, "/subdomain/?limit=") dnsObj.DomainId = strings.TrimPrefix(_href, "/subdomain/?limit=")
fmt.Printf("Domain id for \"%s\" is %s\n", DomainName, dnsObj.DomainId) fmt.Printf("Domain id for \"%s\" is %s\n", DomainName, dnsObj.DomainId)
break loop break loop
} }
@ -293,7 +299,7 @@ loop:
if CurrRecordType == RecordType && CurrRecordAddr == Subdomain { if CurrRecordType == RecordType && CurrRecordAddr == Subdomain {
if _Addr == Address { if _Addr == Address {
return CurrRecordId, nil return CurrRecordId, nil
} else if strings.HasSuffix(_Addr, "...") && strings.HasPrefix(Address, strings.TrimRight(_Addr, "...")) { } else if strings.HasSuffix(_Addr, "...") && strings.HasPrefix(Address, strings.TrimSuffix(_Addr, "...")) {
DeepSearchCandidates = append(DeepSearchCandidates, CurrRecordId) DeepSearchCandidates = append(DeepSearchCandidates, CurrRecordId)
} }
} }
@ -319,7 +325,7 @@ loop:
if string(attrKey) == "href" && strings.Contains(_href, "edit.php?data_id=") { if string(attrKey) == "href" && strings.Contains(_href, "edit.php?data_id=") {
lookForNextTD = 1 lookForNextTD = 1
CurrRecordAddr = "" CurrRecordAddr = ""
CurrRecordId = strings.TrimLeft(_href, "edit.php?data_id=") CurrRecordId = strings.TrimPrefix(_href, "edit.php?data_id=")
break break
} }
if !moreAttr { if !moreAttr {

47
main.go
View File

@ -5,12 +5,14 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
"strings"
extapi "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" extapi "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest" "k8s.io/client-go/rest"
"github.com/cert-manager/webhook-freedns/freedns"
"github.com/jetstack/cert-manager/pkg/acme/webhook/apis/acme/v1alpha1" "github.com/jetstack/cert-manager/pkg/acme/webhook/apis/acme/v1alpha1"
"github.com/jetstack/cert-manager/pkg/acme/webhook/cmd" "github.com/jetstack/cert-manager/pkg/acme/webhook/cmd"
) )
@ -44,6 +46,7 @@ type customDNSProviderSolver struct {
// 4. ensure your webhook's service account has the required RBAC role // 4. ensure your webhook's service account has the required RBAC role
// assigned to it for interacting with the Kubernetes APIs you need. // assigned to it for interacting with the Kubernetes APIs you need.
client *kubernetes.Clientset client *kubernetes.Clientset
freedns *freedns.FreeDNS
} }
// customDNSProviderConfig is a structure that is used to decode into when // customDNSProviderConfig is a structure that is used to decode into when
@ -68,7 +71,6 @@ type customDNSProviderConfig struct {
//Email string `json:"email"` //Email string `json:"email"`
SecretRef string `json:"secretName"` SecretRef string `json:"secretName"`
Domain string `json:"domain"`
//APIKeySecretRef v1.SecretKeySelector `json:"apiKeySecretRef"` //APIKeySecretRef v1.SecretKeySelector `json:"apiKeySecretRef"`
} }
@ -102,9 +104,31 @@ func (c *customDNSProviderSolver) Present(ch *v1alpha1.ChallengeRequest) error {
username := string(secretObj.Data["username"]) username := string(secretObj.Data["username"])
password := string(secretObj.Data["password"]) password := string(secretObj.Data["password"])
fmt.Printf("Domain: %s, Auth: %s %s\n", cfg.Domain, username, password) dnsObj := freedns.FreeDNS{}
err = dnsObj.Login(username, password)
if err != nil {
return err
}
// TODO: add code that sets a record in the DNS provider's console domainName := freedns.GetDomainFromZone(ch.ResolvedZone)
err = dnsObj.SelectDomain(domainName)
if err != nil {
return err
}
_zone := strings.TrimRight(ch.ResolvedFQDN, ".")
_zone = strings.TrimSuffix(_zone, domainName)
_zone = strings.TrimRight(_zone, ".")
_key := "\"" + ch.Key + "\""
fmt.Println("ADD", _zone, _key)
err = dnsObj.AddRecord("TXT", _zone, _key, false, "")
if err != nil {
return err
}
c.freedns = &dnsObj
return nil return nil
} }
@ -115,8 +139,21 @@ func (c *customDNSProviderSolver) Present(ch *v1alpha1.ChallengeRequest) error {
// This is in order to facilitate multiple DNS validations for the same domain // This is in order to facilitate multiple DNS validations for the same domain
// concurrently. // concurrently.
func (c *customDNSProviderSolver) CleanUp(ch *v1alpha1.ChallengeRequest) error { func (c *customDNSProviderSolver) CleanUp(ch *v1alpha1.ChallengeRequest) error {
// TODO: add code that deletes a record from the DNS provider's console
return nil _addr := strings.TrimRight(ch.ResolvedFQDN, ".")
_key := "\"" + ch.Key + "\""
_id, err := c.freedns.FindRecord(_addr, "TXT", _key)
fmt.Println("DEL", _addr)
if _id != "" {
err = c.freedns.DeleteRecord(_id)
if err != nil {
return err
}
}
return c.freedns.Logout()
} }
// Initialize will be called when the webhook first starts. // Initialize will be called when the webhook first starts.

View File

@ -1,4 +1,3 @@
{ {
"secretName": "freedns-auth" "secretName": "freedns-auth"
, "domain": "example.com"
} }