LogPushCloudflareServer.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package server
  2. import (
  3. "cfTest/cloudflareApi/logPush"
  4. "context"
  5. "errors"
  6. "github.com/cloudflare/cloudflare-go"
  7. "log"
  8. )
  9. type LogPushCloudflareServer struct {
  10. logPush.UnimplementedLogPushCloudflareServer
  11. }
  12. func (s *LogPushCloudflareServer) ListLogPushJobsByZoneId(ctx context.Context,
  13. requestCloudflare *logPush.ListLogPushJobRequestByZoneId) (*logPush.ListLogPushJobReplay, error) {
  14. log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
  15. log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
  16. log.Printf("zoneId Received: %v", requestCloudflare.GetZoneId())
  17. log.Printf("dataSet Received: %v", requestCloudflare.GetDataSet())
  18. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  19. if err != nil {
  20. log.Printf("new cloudflare api error : %v", err)
  21. return nil, err
  22. }
  23. ctxAPI := context.Background()
  24. LogPushJobs, err := api.ListZoneLogpushJobsForDataset(ctxAPI,
  25. requestCloudflare.GetZoneId(), requestCloudflare.GetDataSet())
  26. if err != nil {
  27. log.Printf("list logpush job error : %v", err)
  28. return nil, err
  29. }
  30. list := make([]*logPush.LogPushJobEntity, len(LogPushJobs))
  31. for i, job := range LogPushJobs {
  32. list[i] = Go2ProtoLogPushJob(job)
  33. }
  34. return &logPush.ListLogPushJobReplay{
  35. Result: list,
  36. }, err
  37. }
  38. func (s *LogPushCloudflareServer) CreateLogPushJobByZoneId(ctx context.Context,
  39. requestCloudflare *logPush.CreateLogPushJobRequestByZoneId) (*logPush.CreateLogPushJobReplay, error) {
  40. log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
  41. log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
  42. log.Printf("zoneId Received: %v", requestCloudflare.GetZoneId())
  43. log.Printf("dataSet Received: %v", requestCloudflare.GetDataSet())
  44. log.Printf("destinationConf Received: %v", requestCloudflare.GetDestinationConf())
  45. log.Printf("ownershipChallenge Received: %v", requestCloudflare.GetOwnershipChallenge())
  46. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  47. if err != nil {
  48. log.Printf("new cloudflare api error : %v", err)
  49. return nil, err
  50. }
  51. ctxAPI := context.Background()
  52. job := cloudflare.LogpushJob{
  53. Dataset: requestCloudflare.GetDataSet(),
  54. DestinationConf: requestCloudflare.GetDestinationConf(),
  55. Enabled: true,
  56. Frequency: "low",
  57. LogpullOptions: "fields=ClientIP,ClientCountry,ClientRequestMethod,ClientRequestProtocol," +
  58. "ClientRequestHost,ClientRequestPath,ClientRequestURI,ClientRequestBytes," +
  59. "EdgeResponseStatus,EdgeResponseBytes,ClientRequestUserAgent,CacheCacheStatus,CacheResponseBytes," +
  60. "EdgeStartTimestamp,EdgeEndTimestamp&timestamps=rfc3339",
  61. OwnershipChallenge: requestCloudflare.GetOwnershipChallenge(),
  62. }
  63. result, err := api.CreateZoneLogpushJob(ctxAPI, requestCloudflare.GetZoneId(), job)
  64. if err != nil {
  65. log.Printf("create logpush job error : %v", err)
  66. return nil, err
  67. }
  68. return &logPush.CreateLogPushJobReplay{
  69. Result: Go2ProtoLogPushJob(*result),
  70. }, err
  71. }
  72. func (s *LogPushCloudflareServer) DeleteLogPushJobByZoneId(ctx context.Context,
  73. requestCloudflare *logPush.DeleteLogPushJobRequestByZoneId) (*logPush.DeleteLogPushJobReplay, error) {
  74. log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
  75. log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
  76. log.Printf("zoneId Received: %v", requestCloudflare.GetZoneId())
  77. log.Printf("jobId Received: %v", requestCloudflare.GetJobId())
  78. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  79. if err != nil {
  80. log.Printf("new cloudflare api error : %v", err)
  81. return nil, err
  82. }
  83. ctxAPI := context.Background()
  84. result := api.DeleteZoneLogpushJob(ctxAPI, requestCloudflare.GetZoneId(), int(requestCloudflare.GetJobId()))
  85. if result != nil {
  86. log.Printf("delete zone LogpushJob error: %v", result.Error())
  87. return &logPush.DeleteLogPushJobReplay{
  88. Success: false,
  89. Message: result.Error(),
  90. }, err
  91. }
  92. return &logPush.DeleteLogPushJobReplay{
  93. Success: true,
  94. Message: "success",
  95. }, err
  96. }
  97. func (s *LogPushCloudflareServer) GetOwnershipChallengeByZoneId(ctx context.Context,
  98. requestCloudflare *logPush.GetOwnershipChallengeRequestByZoneId) (*logPush.GetOwnershipChallengeReply, error) {
  99. log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
  100. log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
  101. log.Printf("zoneId Received: %v", requestCloudflare.GetZoneId())
  102. log.Printf("destinationConf Received: %v", requestCloudflare.GetDestinationConf())
  103. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  104. if err != nil {
  105. log.Printf("new cloudflare api error : %v", err)
  106. return nil, err
  107. }
  108. ctxAPI := context.Background()
  109. ownershipChallenge, err := api.GetZoneLogpushOwnershipChallenge(ctxAPI,
  110. requestCloudflare.GetZoneId(), requestCloudflare.GetDestinationConf())
  111. if err != nil {
  112. log.Printf("get logpush job ownershipChallenge error : %v", err)
  113. return nil, err
  114. }
  115. if ownershipChallenge.Valid {
  116. log.Printf("ownershipChallenge success: %v", ownershipChallenge.Filename)
  117. return &logPush.GetOwnershipChallengeReply{
  118. Result: &logPush.OwnershipChallengeEntity{
  119. Filename: ownershipChallenge.Filename,
  120. Valid: ownershipChallenge.Valid,
  121. Message: ownershipChallenge.Message,
  122. },
  123. }, err
  124. }
  125. log.Printf("ownershipChallenge fail: %v", ownershipChallenge.Message)
  126. return nil, errors.New(ownershipChallenge.Message)
  127. }
  128. func (s *LogPushCloudflareServer) CheckDestinationExist(ctx context.Context,
  129. requestCloudflare *logPush.CheckDestinationExistRequest) (*logPush.CheckDestinationExistReply, error) {
  130. log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
  131. log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
  132. log.Printf("zoneId Received: %v", requestCloudflare.GetZoneId())
  133. log.Printf("destinationConf Received: %v", requestCloudflare.GetDestinationConf())
  134. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  135. if err != nil {
  136. log.Printf("new cloudflare api error : %v", err)
  137. return nil, err
  138. }
  139. ctxAPI := context.Background()
  140. exists, err := api.CheckZoneLogpushDestinationExists(ctxAPI, requestCloudflare.GetZoneId(), requestCloudflare.GetDestinationConf())
  141. if err != nil {
  142. log.Printf("check logpush job destination error : %v", err)
  143. return nil, err
  144. }
  145. return &logPush.CheckDestinationExistReply{
  146. Exists: exists,
  147. }, err
  148. }
  149. func (s *LogPushCloudflareServer) ValidateOwnershipChallenge(ctx context.Context,
  150. requestCloudflare *logPush.ValidateOwnershipChallengeRequest) (*logPush.ValidateOwnershipChallengeReply, error) {
  151. log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
  152. log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
  153. log.Printf("zoneId Received: %v", requestCloudflare.GetZoneId())
  154. log.Printf("destinationConf Received: %v", requestCloudflare.GetDestinationConf())
  155. log.Printf("ownershipChallenge Received: %v", requestCloudflare.GetOwnershipChallenge())
  156. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  157. if err != nil {
  158. log.Printf("new cloudflare api error : %v", err)
  159. return nil, err
  160. }
  161. ctxAPI := context.Background()
  162. valid, err := api.ValidateZoneLogpushOwnershipChallenge(ctxAPI, requestCloudflare.GetZoneId(),
  163. requestCloudflare.GetDestinationConf(), requestCloudflare.GetOwnershipChallenge())
  164. if err != nil {
  165. log.Printf("validate logpush job ownership challenge error : %v", err)
  166. return nil, err
  167. }
  168. return &logPush.ValidateOwnershipChallengeReply{
  169. Valid: valid,
  170. }, err
  171. }