dns.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package setup
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "pmail/dto"
  8. "pmail/i18n"
  9. "pmail/services/auth"
  10. "pmail/utils/errors"
  11. )
  12. type DNSItem struct {
  13. Type string `json:"type"`
  14. Host string `json:"host"`
  15. Value string `json:"value"`
  16. TTL int `json:"ttl"`
  17. Tips string `json:"tips"`
  18. }
  19. func GetDNSSettings(ctx *dto.Context) ([]*DNSItem, error) {
  20. configData, err := ReadConfig()
  21. if err != nil {
  22. return nil, errors.Wrap(err)
  23. }
  24. ret := []*DNSItem{
  25. {Type: "A", Host: "smtp", Value: getIp(), TTL: 3600, Tips: i18n.GetText(ctx.Lang, "ip_taps")},
  26. {Type: "MX", Host: "-", Value: fmt.Sprintf("smtp.%s", configData.Domain), TTL: 3600},
  27. {Type: "TXT", Host: "-", Value: "v=spf1 a mx ~all", TTL: 3600},
  28. {Type: "TXT", Host: "default._domainkey", Value: auth.DkimGen(), TTL: 3600},
  29. }
  30. return ret, nil
  31. }
  32. func getIp() string {
  33. resp, err := http.Get("http://ip-api.com/json/?lang=zh-CN ")
  34. if err != nil {
  35. return "Your Server IP"
  36. }
  37. defer resp.Body.Close()
  38. if resp.StatusCode == 200 {
  39. body, err := io.ReadAll(resp.Body)
  40. if err == nil {
  41. var queryRes map[string]string
  42. _ = json.Unmarshal(body, &queryRes)
  43. return queryRes["query"]
  44. }
  45. }
  46. return "Your Server IP"
  47. }