DnsCloudflareServer.go 4.3 KB

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