dns.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package setup
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "pmail/i18n"
  8. "pmail/services/auth"
  9. "pmail/utils/context"
  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 *context.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: "A", Host: "pop", Value: getIp(), TTL: 3600, Tips: i18n.GetText(ctx.Lang, "ip_taps")},
  27. {Type: "MX", Host: "-", Value: fmt.Sprintf("smtp.%s", configData.Domain), TTL: 3600},
  28. {Type: "TXT", Host: "-", Value: "v=spf1 a mx ~all", TTL: 3600},
  29. {Type: "TXT", Host: "default._domainkey", Value: auth.DkimGen(), TTL: 3600},
  30. }
  31. return ret, nil
  32. }
  33. func getIp() string {
  34. resp, err := http.Get("http://ip-api.com/json/?lang=zh-CN ")
  35. if err != nil {
  36. return "Your Server IP"
  37. }
  38. defer resp.Body.Close()
  39. if resp.StatusCode == 200 {
  40. body, err := io.ReadAll(resp.Body)
  41. if err == nil {
  42. var queryRes map[string]string
  43. _ = json.Unmarshal(body, &queryRes)
  44. return queryRes["query"]
  45. }
  46. }
  47. return "Your Server IP"
  48. }