DnsCloudflareServer.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package server
  2. import (
  3. "cfTest/cloudflareApi/dns"
  4. "context"
  5. "github.com/cloudflare/cloudflare-go"
  6. "github.com/jinzhu/copier"
  7. "github.com/samber/lo"
  8. )
  9. type DnsServer struct {
  10. dns.UnimplementedDnsCloudflareServer
  11. }
  12. func Go2ProtoDnsRecord(goRecord *cloudflare.DNSRecord) (protoRecord *dns.DnsRecord) {
  13. protoRecord = new(dns.DnsRecord)
  14. err := copier.Copy(protoRecord, goRecord)
  15. if err != nil {
  16. return nil
  17. }
  18. protoRecord.Id = goRecord.ID
  19. protoRecord.ZoneId = goRecord.ZoneID
  20. protoRecord.Ttl = int64(goRecord.TTL)
  21. return
  22. }
  23. func Proto2GoDnsRecord(protoRecord *dns.DnsRecord) (goRecord *cloudflare.DNSRecord) {
  24. goRecord = new(cloudflare.DNSRecord)
  25. err := copier.Copy(goRecord, protoRecord)
  26. if err != nil {
  27. return nil
  28. }
  29. goRecord.TTL = int(protoRecord.Ttl)
  30. goRecord.ID = protoRecord.Id
  31. goRecord.ZoneID = protoRecord.ZoneId
  32. return
  33. }
  34. func (d DnsServer) List(ctx context.Context, request *dns.ListRequest) (*dns.DnsRecordList, error) {
  35. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  36. if err != nil {
  37. return nil, err
  38. }
  39. ctxAPI := context.Background()
  40. records, err := api.DNSRecords(ctxAPI, request.ZoneId, cloudflare.DNSRecord{})
  41. if err != nil {
  42. return nil, err
  43. }
  44. filterMap := lo.FilterMap[cloudflare.DNSRecord, *dns.DnsRecord](records, func(record cloudflare.DNSRecord, _ int) (*dns.DnsRecord, bool) {
  45. protoRecord := Go2ProtoDnsRecord(&record)
  46. if protoRecord == nil {
  47. return nil, false
  48. }
  49. return protoRecord, true
  50. })
  51. return &dns.DnsRecordList{DnsRecord: filterMap}, nil
  52. }
  53. func (d DnsServer) Create(ctx context.Context, request *dns.DnsRecordRequest) (*dns.DnsRecord, error) {
  54. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  55. if err != nil {
  56. return nil, err
  57. }
  58. ctxAPI := context.Background()
  59. inRecord := Proto2GoDnsRecord(request.DnsRecord)
  60. response, err := api.CreateDNSRecord(ctxAPI, request.ZoneId, *inRecord)
  61. if err != nil {
  62. return nil, err
  63. }
  64. outRecord := Go2ProtoDnsRecord(&response.Result)
  65. return outRecord, nil
  66. }
  67. func (d DnsServer) Update(ctx context.Context, request *dns.DnsRecordRequest) (*dns.Empty, error) {
  68. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  69. if err != nil {
  70. return nil, err
  71. }
  72. ctxAPI := context.Background()
  73. inRecord := Proto2GoDnsRecord(request.DnsRecord)
  74. err = api.UpdateDNSRecord(ctxAPI, request.ZoneId, inRecord.ID, *inRecord)
  75. if err != nil {
  76. return nil, err
  77. }
  78. return &dns.Empty{}, nil
  79. }
  80. func (d DnsServer) Delete(ctx context.Context, request *dns.DeleteRequest) (*dns.Empty, error) {
  81. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  82. if err != nil {
  83. return nil, err
  84. }
  85. ctxAPI := context.Background()
  86. err = api.DeleteDNSRecord(ctxAPI, request.ZoneId, request.Id)
  87. if err != nil {
  88. return nil, err
  89. }
  90. return &dns.Empty{}, nil
  91. }