LogPushCloudflareServer.go 9.4 KB

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