acme_dns.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package acme
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "github.com/go-acme/lego/v4/challenge"
  6. "imuslab.com/zoraxy/mod/acme/acmedns"
  7. )
  8. // Preprocessor function to get DNS challenge provider by name
  9. func GetDnsChallengeProviderByName(dnsProvider string, dnsCredentials string, ppgTimeout int) (challenge.Provider, error) {
  10. //Unpack the dnsCredentials (json string) to map
  11. var dnsCredentialsMap map[string]interface{}
  12. err := json.Unmarshal([]byte(dnsCredentials), &dnsCredentialsMap)
  13. if err != nil {
  14. return nil, err
  15. }
  16. //Clear the PollingInterval and PropagationTimeout field and conert to int
  17. userDefinedPollingInterval := 2
  18. if dnsCredentialsMap["PollingInterval"] != nil {
  19. userDefinedPollingIntervalRaw := dnsCredentialsMap["PollingInterval"].(string)
  20. delete(dnsCredentialsMap, "PollingInterval")
  21. convertedPollingInterval, err := strconv.Atoi(userDefinedPollingIntervalRaw)
  22. if err == nil {
  23. userDefinedPollingInterval = convertedPollingInterval
  24. }
  25. }
  26. userDefinedPropagationTimeout := ppgTimeout
  27. if dnsCredentialsMap["PropagationTimeout"] != nil {
  28. userDefinedPropagationTimeoutRaw := dnsCredentialsMap["PropagationTimeout"].(string)
  29. delete(dnsCredentialsMap, "PropagationTimeout")
  30. convertedPropagationTimeout, err := strconv.Atoi(userDefinedPropagationTimeoutRaw)
  31. if err == nil {
  32. //Overwrite the default propagation timeout if it is requeted from UI
  33. userDefinedPropagationTimeout = convertedPropagationTimeout
  34. }
  35. }
  36. //Restructure dnsCredentials string from map
  37. dnsCredentialsBytes, err := json.Marshal(dnsCredentialsMap)
  38. if err != nil {
  39. return nil, err
  40. }
  41. dnsCredentials = string(dnsCredentialsBytes)
  42. //Using acmedns CICD pipeline generated datatype to optain the DNS provider
  43. return acmedns.GetDNSProviderByJsonConfig(
  44. dnsProvider,
  45. dnsCredentials,
  46. int64(userDefinedPropagationTimeout),
  47. int64(userDefinedPollingInterval),
  48. )
  49. }