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) {
- 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()
- zoneIdentifier := cloudflare.ZoneIdentifier(requestCloudflare.GetZoneId())
- listLogpushJobsForDatasetParams := cloudflare.ListLogpushJobsForDatasetParams{
- Dataset: requestCloudflare.DataSet,
- }
- LogPushJobs, err := api.ListLogpushJobsForDataset(ctxAPI, zoneIdentifier, listLogpushJobsForDatasetParams)
- //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) {
- 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(),
- //}
- zoneIdentifier := cloudflare.ZoneIdentifier(requestCloudflare.GetZoneId())
- createLogpushJobParams := cloudflare.CreateLogpushJobParams{
- 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.CreateLogpushJob(ctxAPI, zoneIdentifier, createLogpushJobParams)
- //result, err := api.CreateZoneLogpushJob(ctxAPI,zoneIdentifier, 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) {
- 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()
- zoneIdentifier := cloudflare.ZoneIdentifier(requestCloudflare.GetZoneId())
- result := api.DeleteLogpushJob(ctxAPI, zoneIdentifier, int(requestCloudflare.GetJobId()))
- //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) {
- 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()
- zoneIdentifier := cloudflare.ZoneIdentifier(requestCloudflare.GetZoneId())
- getLogpushOwnershipChallengeParams := cloudflare.GetLogpushOwnershipChallengeParams{
- DestinationConf: requestCloudflare.DestinationConf,
- }
- ownershipChallenge, err := api.GetLogpushOwnershipChallenge(ctxAPI, zoneIdentifier, getLogpushOwnershipChallengeParams)
- //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) {
- 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()
- zoneIdentifier := cloudflare.ZoneIdentifier(requestCloudflare.GetZoneId())
- exists, err := api.CheckLogpushDestinationExists(ctxAPI, zoneIdentifier, requestCloudflare.GetDestinationConf())
- //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) {
- 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()
- zoneIdentifier := cloudflare.ZoneIdentifier(requestCloudflare.GetZoneId())
- validateLogpushOwnershipChallengeParams := cloudflare.ValidateLogpushOwnershipChallengeParams{
- DestinationConf: requestCloudflare.DestinationConf,
- OwnershipChallenge: requestCloudflare.OwnershipChallenge,
- }
- valid, err := api.ValidateLogpushOwnershipChallenge(ctxAPI, zoneIdentifier, validateLogpushOwnershipChallengeParams)
- //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
- }
|