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