123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- package server
- import (
- "cfTest/cloudflareApi/logPush"
- "context"
- "errors"
- "github.com/cloudflare/cloudflare-go"
- "log"
- )
- type LogPushCloudflareServer struct {
- logPush.UnimplementedLogPushCloudflareServer
- }
- func (s *LogPushCloudflareServer) ListLogPushJobsByZoneId(ctx context.Context,
- requestCloudflare *logPush.ListLogPushJobRequestByZoneId) (*logPush.ListLogPushJobReplay, error) {
- log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
- log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
- log.Printf("zoneId Received: %v", requestCloudflare.GetZoneId())
- log.Printf("dataSet Received: %v", requestCloudflare.GetDataSet())
- api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
- if err != nil {
- log.Printf("new cloudflare api error : %v", err)
- return nil, err
- }
- ctxAPI := context.Background()
- LogPushJobs, err := api.ListZoneLogpushJobsForDataset(ctxAPI,
- requestCloudflare.GetZoneId(), requestCloudflare.GetDataSet())
- if err != nil {
- log.Printf("list logpush job error : %v", err)
- return nil, err
- }
- list := make([]*logPush.LogPushJobEntity, len(LogPushJobs))
- for i, job := range LogPushJobs {
- list[i] = Go2ProtoLogPushJob(job)
- }
- return &logPush.ListLogPushJobReplay{
- Result: list,
- }, err
- }
- func (s *LogPushCloudflareServer) CreateLogPushJobByZoneId(ctx context.Context,
- requestCloudflare *logPush.CreateLogPushJobRequestByZoneId) (*logPush.CreateLogPushJobReplay, error) {
- log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
- log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
- log.Printf("zoneId Received: %v", requestCloudflare.GetZoneId())
- log.Printf("dataSet Received: %v", requestCloudflare.GetDataSet())
- log.Printf("destinationConf Received: %v", requestCloudflare.GetDestinationConf())
- log.Printf("ownershipChallenge Received: %v", requestCloudflare.GetOwnershipChallenge())
- api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
- if err != nil {
- log.Printf("new cloudflare api error : %v", err)
- return nil, err
- }
- ctxAPI := context.Background()
- job := cloudflare.LogpushJob{
- Dataset: requestCloudflare.GetDataSet(),
- DestinationConf: requestCloudflare.GetDestinationConf(),
- Enabled: true,
- Frequency: "low",
- LogpullOptions: "fields=ClientIP,ClientCountry,ClientRequestMethod,ClientRequestProtocol," +
- "ClientRequestHost,ClientRequestPath,ClientRequestURI,ClientRequestBytes," +
- "EdgeResponseStatus,EdgeResponseBytes,ClientRequestUserAgent,CacheCacheStatus,CacheResponseBytes," +
- "EdgeStartTimestamp,EdgeEndTimestamp×tamps=rfc3339",
- OwnershipChallenge: requestCloudflare.GetOwnershipChallenge(),
- }
- result, err := api.CreateZoneLogpushJob(ctxAPI, requestCloudflare.GetZoneId(), job)
- if err != nil {
- log.Printf("create logpush job error : %v", err)
- return nil, err
- }
- return &logPush.CreateLogPushJobReplay{
- Result: Go2ProtoLogPushJob(*result),
- }, err
- }
- func (s *LogPushCloudflareServer) DeleteLogPushJobByZoneId(ctx context.Context,
- requestCloudflare *logPush.DeleteLogPushJobRequestByZoneId) (*logPush.DeleteLogPushJobReplay, error) {
- log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
- log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
- log.Printf("zoneId Received: %v", requestCloudflare.GetZoneId())
- log.Printf("jobId Received: %v", requestCloudflare.GetJobId())
- api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
- if err != nil {
- log.Printf("new cloudflare api error : %v", err)
- return nil, err
- }
- ctxAPI := context.Background()
- result := api.DeleteZoneLogpushJob(ctxAPI, requestCloudflare.GetZoneId(), int(requestCloudflare.GetJobId()))
- if result != nil {
- log.Printf("delete zone LogpushJob error: %v", result.Error())
- return &logPush.DeleteLogPushJobReplay{
- Success: false,
- Message: result.Error(),
- }, err
- }
- return &logPush.DeleteLogPushJobReplay{
- Success: true,
- Message: "success",
- }, err
- }
- func (s *LogPushCloudflareServer) GetOwnershipChallengeByZoneId(ctx context.Context,
- requestCloudflare *logPush.GetOwnershipChallengeRequestByZoneId) (*logPush.GetOwnershipChallengeReply, error) {
- log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
- log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
- log.Printf("zoneId Received: %v", requestCloudflare.GetZoneId())
- log.Printf("destinationConf Received: %v", requestCloudflare.GetDestinationConf())
- api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
- if err != nil {
- log.Printf("new cloudflare api error : %v", err)
- return nil, err
- }
- ctxAPI := context.Background()
- ownershipChallenge, err := api.GetZoneLogpushOwnershipChallenge(ctxAPI,
- requestCloudflare.GetZoneId(), requestCloudflare.GetDestinationConf())
- if err != nil {
- log.Printf("get logpush job ownershipChallenge error : %v", err)
- return nil, err
- }
- if ownershipChallenge.Valid {
- log.Printf("ownershipChallenge success: %v", ownershipChallenge.Filename)
- return &logPush.GetOwnershipChallengeReply{
- Result: &logPush.OwnershipChallengeEntity{
- Filename: ownershipChallenge.Filename,
- Valid: ownershipChallenge.Valid,
- Message: ownershipChallenge.Message,
- },
- }, err
- }
- log.Printf("ownershipChallenge fail: %v", ownershipChallenge.Message)
- return nil, errors.New(ownershipChallenge.Message)
- }
- func (s *LogPushCloudflareServer) CheckDestinationExist(ctx context.Context,
- requestCloudflare *logPush.CheckDestinationExistRequest) (*logPush.CheckDestinationExistReply, error) {
- log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
- log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
- log.Printf("zoneId Received: %v", requestCloudflare.GetZoneId())
- log.Printf("destinationConf Received: %v", requestCloudflare.GetDestinationConf())
- api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
- if err != nil {
- log.Printf("new cloudflare api error : %v", err)
- return nil, err
- }
- ctxAPI := context.Background()
- exists, err := api.CheckZoneLogpushDestinationExists(ctxAPI, requestCloudflare.GetZoneId(), requestCloudflare.GetDestinationConf())
- if err != nil {
- log.Printf("check logpush job destination error : %v", err)
- return nil, err
- }
- return &logPush.CheckDestinationExistReply{
- Exists: exists,
- }, err
- }
- func (s *LogPushCloudflareServer) ValidateOwnershipChallenge(ctx context.Context,
- requestCloudflare *logPush.ValidateOwnershipChallengeRequest) (*logPush.ValidateOwnershipChallengeReply, error) {
- log.Printf("apiKey Received: %v", requestCloudflare.GetApiKey())
- log.Printf("apiEmail Received: %v", requestCloudflare.GetApiEmail())
- log.Printf("zoneId Received: %v", requestCloudflare.GetZoneId())
- log.Printf("destinationConf Received: %v", requestCloudflare.GetDestinationConf())
- log.Printf("ownershipChallenge Received: %v", requestCloudflare.GetOwnershipChallenge())
- api, err := cloudflare.New(requestCloudflare.GetApiKey(), requestCloudflare.GetApiEmail())
- if err != nil {
- log.Printf("new cloudflare api error : %v", err)
- return nil, err
- }
- ctxAPI := context.Background()
- valid, err := api.ValidateZoneLogpushOwnershipChallenge(ctxAPI, requestCloudflare.GetZoneId(),
- requestCloudflare.GetDestinationConf(), requestCloudflare.GetOwnershipChallenge())
- if err != nil {
- log.Printf("validate logpush job ownership challenge error : %v", err)
- return nil, err
- }
- return &logPush.ValidateOwnershipChallengeReply{
- Valid: valid,
- }, err
- }
|