LogPushCloudflareServer.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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=RayID,ZoneName," +
  58. "ClientCountry,ClientIP,ClientRequestBytes,ClientRequestHost,ClientRequestPath," +
  59. "EdgeResponseStatus,EdgeResponseBytes,EdgeStartTimestamp,EdgeEndTimestamp&timestamps=rfc3339",
  60. OwnershipChallenge: requestCloudflare.GetOwnershipChallenge(),
  61. }
  62. result, err := api.CreateZoneLogpushJob(ctxAPI, requestCloudflare.GetZoneId(), job)
  63. if err != nil {
  64. log.Printf("create logpush job error : %v", err)
  65. return nil, err
  66. }
  67. return &logPush.CreateLogPushJobReplay{
  68. Result: Go2ProtoLogPushJob(*result),
  69. }, err
  70. }
  71. func (s *LogPushCloudflareServer) DeleteLogPushJobByZoneId(ctx context.Context,
  72. requestCloudflare *logPush.DeleteLogPushJobRequestByZoneId) (*logPush.DeleteLogPushJobReplay, error) {
  73. log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
  74. log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
  75. log.Printf("zoneId Received: %v", requestCloudflare.GetZoneId())
  76. log.Printf("jobId Received: %v", requestCloudflare.GetJobId())
  77. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  78. if err != nil {
  79. log.Printf("new cloudflare api error : %v", err)
  80. return nil, err
  81. }
  82. ctxAPI := context.Background()
  83. result := api.DeleteZoneLogpushJob(ctxAPI, requestCloudflare.GetZoneId(), int(requestCloudflare.GetJobId()))
  84. if result != nil {
  85. log.Printf("delete zone LogpushJob error: %v", result.Error())
  86. return &logPush.DeleteLogPushJobReplay{
  87. Success: false,
  88. Message: result.Error(),
  89. }, err
  90. }
  91. return &logPush.DeleteLogPushJobReplay{
  92. Success: true,
  93. Message: "success",
  94. }, err
  95. }
  96. func (s *LogPushCloudflareServer) GetOwnershipChallengeByZoneId(ctx context.Context,
  97. requestCloudflare *logPush.GetOwnershipChallengeRequestByZoneId) (*logPush.GetOwnershipChallengeReply, error) {
  98. log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
  99. log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
  100. log.Printf("zoneId Received: %v", requestCloudflare.GetZoneId())
  101. log.Printf("destinationConf Received: %v", requestCloudflare.GetDestinationConf())
  102. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  103. if err != nil {
  104. log.Printf("new cloudflare api error : %v", err)
  105. return nil, err
  106. }
  107. ctxAPI := context.Background()
  108. ownershipChallenge, err := api.GetZoneLogpushOwnershipChallenge(ctxAPI,
  109. requestCloudflare.GetZoneId(), requestCloudflare.GetDestinationConf())
  110. if err != nil {
  111. log.Printf("get logpush job ownershipChallenge error : %v", err)
  112. return nil, err
  113. }
  114. if ownershipChallenge.Valid {
  115. log.Printf("ownershipChallenge success: %v", ownershipChallenge.Filename)
  116. return &logPush.GetOwnershipChallengeReply{
  117. Result: &logPush.OwnershipChallengeEntity{
  118. Filename: ownershipChallenge.Filename,
  119. Valid: ownershipChallenge.Valid,
  120. Message: ownershipChallenge.Message,
  121. },
  122. }, err
  123. }
  124. log.Printf("ownershipChallenge fail: %v", ownershipChallenge.Message)
  125. return nil, errors.New(ownershipChallenge.Message)
  126. }
  127. func (s *LogPushCloudflareServer) CheckDestinationExist(ctx context.Context,
  128. requestCloudflare *logPush.CheckDestinationExistRequest) (*logPush.CheckDestinationExistReply, error) {
  129. log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
  130. log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
  131. log.Printf("zoneId Received: %v", requestCloudflare.GetZoneId())
  132. log.Printf("destinationConf Received: %v", requestCloudflare.GetDestinationConf())
  133. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  134. if err != nil {
  135. log.Printf("new cloudflare api error : %v", err)
  136. return nil, err
  137. }
  138. ctxAPI := context.Background()
  139. exists, err := api.CheckZoneLogpushDestinationExists(ctxAPI, requestCloudflare.GetZoneId(), requestCloudflare.GetDestinationConf())
  140. if err != nil {
  141. log.Printf("check logpush job destination error : %v", err)
  142. return nil, err
  143. }
  144. return &logPush.CheckDestinationExistReply{
  145. Exists: exists,
  146. }, err
  147. }
  148. func (s *LogPushCloudflareServer) ValidateOwnershipChallenge(ctx context.Context,
  149. requestCloudflare *logPush.ValidateOwnershipChallengeRequest) (*logPush.ValidateOwnershipChallengeReply, error) {
  150. log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
  151. log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
  152. log.Printf("zoneId Received: %v", requestCloudflare.GetZoneId())
  153. log.Printf("destinationConf Received: %v", requestCloudflare.GetDestinationConf())
  154. log.Printf("ownershipChallenge Received: %v", requestCloudflare.GetOwnershipChallenge())
  155. api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
  156. if err != nil {
  157. log.Printf("new cloudflare api error : %v", err)
  158. return nil, err
  159. }
  160. ctxAPI := context.Background()
  161. valid, err := api.ValidateZoneLogpushOwnershipChallenge(ctxAPI, requestCloudflare.GetZoneId(),
  162. requestCloudflare.GetDestinationConf(), requestCloudflare.GetOwnershipChallenge())
  163. if err != nil {
  164. log.Printf("validate logpush job ownership challenge error : %v", err)
  165. return nil, err
  166. }
  167. return &logPush.ValidateOwnershipChallengeReply{
  168. Valid: valid,
  169. }, err
  170. }