challenge.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package ssl
  2. import (
  3. "github.com/go-acme/lego/v4/challenge/dns01"
  4. log "github.com/sirupsen/logrus"
  5. "pmail/utils/context"
  6. "time"
  7. )
  8. type authInfo struct {
  9. Domain string
  10. Token string
  11. KeyAuth string
  12. }
  13. type HttpChallenge struct {
  14. AuthInfo map[string]*authInfo
  15. }
  16. var instance *HttpChallenge
  17. func (h *HttpChallenge) Present(domain, token, keyAuth string) error {
  18. h.AuthInfo[token] = &authInfo{
  19. Domain: domain,
  20. Token: token,
  21. KeyAuth: keyAuth,
  22. }
  23. return nil
  24. }
  25. func (h *HttpChallenge) CleanUp(domain, token, keyAuth string) error {
  26. delete(h.AuthInfo, token)
  27. return nil
  28. }
  29. func GetHttpChallengeInstance() *HttpChallenge {
  30. if instance == nil {
  31. instance = &HttpChallenge{
  32. AuthInfo: map[string]*authInfo{},
  33. }
  34. }
  35. return instance
  36. }
  37. type DNSChallenge struct {
  38. AuthInfo map[string]*authInfo
  39. }
  40. var dnsInstance *DNSChallenge
  41. func GetDnsChallengeInstance() *DNSChallenge {
  42. if dnsInstance == nil {
  43. dnsInstance = &DNSChallenge{
  44. AuthInfo: map[string]*authInfo{},
  45. }
  46. }
  47. return dnsInstance
  48. }
  49. func (h *DNSChallenge) Present(domain, token, keyAuth string) error {
  50. info := dns01.GetChallengeInfo(domain, keyAuth)
  51. log.Infof("Presenting challenge Info : %+v", info)
  52. h.AuthInfo[token] = &authInfo{
  53. Domain: info.FQDN,
  54. Token: token,
  55. KeyAuth: info.Value,
  56. }
  57. log.Infof("SSL Log:%s %s %s", domain, token, keyAuth)
  58. return nil
  59. }
  60. func (h *DNSChallenge) CleanUp(domain, token, keyAuth string) error {
  61. delete(h.AuthInfo, token)
  62. return nil
  63. }
  64. func (h *DNSChallenge) Timeout() (timeout, interval time.Duration) {
  65. return 60 * time.Minute, 5 * time.Second
  66. }
  67. type DNSItem struct {
  68. Type string `json:"type"`
  69. Host string `json:"host"`
  70. Value string `json:"value"`
  71. TTL int `json:"ttl"`
  72. Tips string `json:"tips"`
  73. }
  74. func (h *DNSChallenge) GetDNSSettings(ctx *context.Context) []*DNSItem {
  75. ret := []*DNSItem{}
  76. for _, info := range h.AuthInfo {
  77. ret = append(ret, &DNSItem{
  78. Type: "TXT",
  79. Host: info.Domain,
  80. Value: info.KeyAuth,
  81. TTL: 3600,
  82. })
  83. }
  84. return ret
  85. }