DnsCloudflareServer.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. "log"
  9. "strings"
  10. )
  11. type DnsServer struct {
  12. dns.UnimplementedDnsCloudflareServer
  13. }
  14. func Go2ProtoDnsRecord(goRecord *cloudflare.DNSRecord) (protoRecord *dns.DnsRecord) {
  15. protoRecord = new(dns.DnsRecord)
  16. err := copier.Copy(protoRecord, goRecord)
  17. if err != nil {
  18. return nil
  19. }
  20. protoRecord.Id = goRecord.ID
  21. protoRecord.Ttl = int64(goRecord.TTL)
  22. return
  23. }
  24. func Proto2GoDnsRecord(protoRecord *dns.DnsRecord) (goRecord *cloudflare.DNSRecord) {
  25. goRecord = new(cloudflare.DNSRecord)
  26. err := copier.Copy(goRecord, protoRecord)
  27. if err != nil {
  28. return nil
  29. }
  30. goRecord.TTL = int(protoRecord.Ttl)
  31. goRecord.ID = protoRecord.Id
  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. if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
  52. api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  53. } else {
  54. api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  55. }
  56. if err != nil {
  57. return nil, err
  58. }
  59. ctxAPI := context.Background()
  60. zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
  61. records, _, err := api.ListDNSRecords(ctxAPI, zoneIdentifier, cloudflare.ListDNSRecordsParams{})
  62. log.Printf("Cloudflare DNSList API response: %v err:%s", records, err)
  63. //records, err := api.DNSRecords(ctxAPI, zoneIdentifier, cloudflare.DNSRecord{})
  64. if err != nil {
  65. return nil, err
  66. }
  67. filterMap := lo.FilterMap[cloudflare.DNSRecord, *dns.DnsRecord](records, func(record cloudflare.DNSRecord, _ int) (*dns.DnsRecord, bool) {
  68. protoRecord := Go2ProtoDnsRecord(&record)
  69. if protoRecord == nil {
  70. return nil, false
  71. }
  72. return protoRecord, true
  73. })
  74. return &dns.DnsRecordList{DnsRecord: filterMap}, nil
  75. }
  76. func (d DnsServer) Create(ctx context.Context, request *dns.DnsRecordRequest) (*dns.DnsRecord, error) {
  77. if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
  78. api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  79. } else {
  80. api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  81. }
  82. if err != nil {
  83. return nil, err
  84. }
  85. ctxAPI := context.Background()
  86. inRecord := Proto2GoDnsRecord(request.DnsRecord)
  87. zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
  88. createDNSRecordParams := goRecord2CreateDnsRecordParams(inRecord)
  89. response, err := api.CreateDNSRecord(ctxAPI, zoneIdentifier, *createDNSRecordParams)
  90. log.Printf("Cloudflare DNSCreate API response: %v err:%s", response, err)
  91. if err != nil {
  92. return nil, err
  93. }
  94. outRecord := Go2ProtoDnsRecord(&response)
  95. return outRecord, nil
  96. }
  97. func (d DnsServer) Update(ctx context.Context, request *dns.DnsRecordRequest) (*dns.Empty, error) {
  98. if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
  99. api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  100. } else {
  101. api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  102. }
  103. if err != nil {
  104. return nil, err
  105. }
  106. ctxAPI := context.Background()
  107. inRecord := Proto2GoDnsRecord(request.DnsRecord)
  108. zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
  109. updateDNSRecordParams := goRecord2UpdateDnsRecordParams(inRecord)
  110. _, err = api.UpdateDNSRecord(ctxAPI, zoneIdentifier, *updateDNSRecordParams)
  111. log.Printf("Cloudflare DNSUpdate API err:%s", err)
  112. if err != nil {
  113. return nil, err
  114. }
  115. return &dns.Empty{}, nil
  116. }
  117. func (d DnsServer) Delete(ctx context.Context, request *dns.DeleteRequest) (*dns.Empty, error) {
  118. if strings.HasPrefix(request.GetApiKey(), TOKEN_PREFIX) {
  119. api, err = cloudflare.NewWithAPIToken(request.GetApiKey()[TOKEN_PREFIX_LENGTH:])
  120. } else {
  121. api, err = cloudflare.New(request.GetApiKey(), request.GetApiEmail())
  122. }
  123. if err != nil {
  124. return nil, err
  125. }
  126. ctxAPI := context.Background()
  127. zoneIdentifier := cloudflare.ZoneIdentifier(request.ZoneId)
  128. err = api.DeleteDNSRecord(ctxAPI, zoneIdentifier, request.Id)
  129. log.Printf("Cloudflare DNSDelete API err:%s", err)
  130. if err != nil {
  131. return nil, err
  132. }
  133. return &dns.Empty{}, nil
  134. }