Some proper logging

This commit is contained in:
2022-03-14 11:14:53 +09:00
parent b99ca72213
commit 4436f3d7c0
4 changed files with 30 additions and 10 deletions

View File

@@ -8,6 +8,7 @@ import (
"net/url"
"strings"
logf "github.com/jetstack/cert-manager/pkg/logs"
"golang.org/x/net/html"
)
@@ -34,6 +35,16 @@ const URI_DELETE_RECORD = "https://freedns.afraid.org/subdomain/delete2.php?data
// const URI_LOGIN string = "http://127.0.0.1:1234/"
func LogInfo(Mesg string) {
fmt.Println(Mesg)
logf.V(logf.InfoLevel).Info(Mesg)
}
func LogDebug(Mesg string) {
fmt.Println(Mesg)
logf.V(logf.DebugLevel).Info(Mesg)
}
func GetDomainFromZone(Zone string) string {
_segs := strings.Split(strings.TrimSuffix(Zone, "."), ".")
_segs = _segs[len(_segs)-2:]
@@ -113,7 +124,7 @@ func (dnsObj *FreeDNS) Logout() error {
return nil
}
_, _, err := _HttpRequest("GET", URI_DOMAIN, nil, dnsObj.AuthCookie)
_, _, err := _HttpRequest("GET", URI_LOGOUT, nil, dnsObj.AuthCookie)
if err != nil {
return err
}
@@ -149,7 +160,7 @@ loop:
break loop
case html.TextToken:
if inBold && strings.TrimSpace(htmlTokens.Token().Data) == DomainName {
fmt.Println("Found " + DomainName + ", looking for domain id")
LogInfo("Found " + DomainName + ", looking for domain id")
lookForA = true
}
// The [Manage] anchor is next to the bold tag
@@ -164,7 +175,7 @@ loop:
_href := string(attrValue)
if string(attrKey) == "href" && strings.Contains(_href, "/subdomain/?limit=") {
dnsObj.DomainId = strings.TrimPrefix(_href, "/subdomain/?limit=")
fmt.Printf("Domain id for \"%s\" is %s\n", DomainName, dnsObj.DomainId)
LogDebug(fmt.Sprintf("Domain id for \"%s\" is %s\n", DomainName, dnsObj.DomainId))
break loop
}
if !moreAttr {
@@ -205,7 +216,7 @@ func (dnsObj *FreeDNS) AddRecord(RecordType string, Subdomain string, Address st
// Record already exists, treat this as success
if strings.Contains(respStr, "already have another already existent") {
fmt.Println("Record already exists")
LogInfo("Record already exists")
return nil
}
@@ -230,7 +241,7 @@ func (dnsObj *FreeDNS) AddRecord(RecordType string, Subdomain string, Address st
}
if strings.Contains(_Location.Path, "/zc.php") {
fmt.Println("Error on AddRecord: Cookie expired")
LogDebug("Error on AddRecord: Cookie expired")
return errors.New("dns_cookie maybe expired")
}
@@ -340,7 +351,7 @@ loop:
// Begin deep search for truncated records
htmlAddr := strings.ReplaceAll(html.EscapeString(Address), """, """)
for _, RecordId := range DeepSearchCandidates {
fmt.Println("Searching in " + RecordId)
LogDebug("Searching in " + RecordId)
_, respStr, err := _HttpRequest("GET", URI_SUBDOMAIN_EDIT+RecordId, nil, dnsObj.AuthCookie)
if err != nil {
continue