DnsCloudflareServer.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 goRecord2CreateDnsRecordParams(goRecord *cloudflare.DNSRecord) (createDnsRecordParams *cloudflare.CreateDNSRecordParams) {
  35. createDnsRecordParams = new(cloudflare.CreateDNSRecordParams)
  36. err := copier.Copy(createDnsRecordParams, goRecord)
  37. if err != nil {
  38. return nil
  39. }
  40. return
  41. }
  42. func goRecord2UpdateDnsRecordParams(goRecord *cloudflare.DNSRecord) (updateDnsRecordParams *cloudflare.UpdateDNSRecordParams) {
  43. updateDnsRecordParams = new(cloudflare.UpdateDNSRecordParams)
  44. err := copier.Copy(updateDnsRecordParams, goRecord)
  45. if err != nil {
  46. return nil
  47. }
  48. return
  49. }
  50. func (d DnsServer) List(ctx context.Context, request *dns.ListRequest) (*dns.DnsRecordList, error) {
  51. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  52. if err != nil {
  53. return nil, err
  54. }
  55. ctxAPI := context.Background()
  56. zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
  57. records, _, err := api.ListDNSRecords(ctxAPI, zoneIdentifier, cloudflare.ListDNSRecordsParams{})
  58. //records, err := api.DNSRecords(ctxAPI, zoneIdentifier, cloudflare.DNSRecord{})
  59. if err != nil {
  60. return nil, err
  61. }
  62. filterMap := lo.FilterMap[cloudflare.DNSRecord, *dns.DnsRecord](records, func(record cloudflare.DNSRecord, _ int) (*dns.DnsRecord, bool) {
  63. protoRecord := Go2ProtoDnsRecord(&record)
  64. if protoRecord == nil {
  65. return nil, false
  66. }
  67. return protoRecord, true
  68. })
  69. return &dns.DnsRecordList{DnsRecord: filterMap}, nil
  70. }
  71. func (d DnsServer) Create(ctx context.Context, request *dns.DnsRecordRequest) (*dns.DnsRecord, error) {
  72. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  73. if err != nil {
  74. return nil, err
  75. }
  76. ctxAPI := context.Background()
  77. inRecord := Proto2GoDnsRecord(request.DnsRecord)
  78. zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
  79. createDNSRecordParams := goRecord2CreateDnsRecordParams(inRecord)
  80. response, err := api.CreateDNSRecord(ctxAPI, zoneIdentifier, *createDNSRecordParams)
  81. if err != nil {
  82. return nil, err
  83. }
  84. outRecord := Go2ProtoDnsRecord(&response)
  85. return outRecord, nil
  86. }
  87. func (d DnsServer) Update(ctx context.Context, request *dns.DnsRecordRequest) (*dns.Empty, error) {
  88. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  89. if err != nil {
  90. return nil, err
  91. }
  92. ctxAPI := context.Background()
  93. inRecord := Proto2GoDnsRecord(request.DnsRecord)
  94. zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
  95. updateDNSRecordParams := goRecord2UpdateDnsRecordParams(inRecord)
  96. _, err = api.UpdateDNSRecord(ctxAPI, zoneIdentifier, *updateDNSRecordParams)
  97. if err != nil {
  98. return nil, err
  99. }
  100. return &dns.Empty{}, nil
  101. }
  102. func (d DnsServer) Delete(ctx context.Context, request *dns.DeleteRequest) (*dns.Empty, error) {
  103. api, err := cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  104. if err != nil {
  105. return nil, err
  106. }
  107. ctxAPI := context.Background()
  108. zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
  109. err = api.DeleteDNSRecord(ctxAPI, zoneIdentifier, request.Id)
  110. if err != nil {
  111. return nil, err
  112. }
  113. return &dns.Empty{}, nil
  114. }