Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
feat: cloudflare cron
Browse files Browse the repository at this point in the history
  • Loading branch information
sysatom committed Mar 23, 2021
1 parent 8e64f26 commit 381229c
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
51 changes: 51 additions & 0 deletions internal/app/cron/agent/cloudflare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package agent

import (
"context"
"encoding/json"
"github.com/tsundata/assistant/api/pb"
"github.com/tsundata/assistant/internal/app/cron/pipeline/result"
"github.com/tsundata/assistant/internal/pkg/rulebot"
"github.com/tsundata/assistant/internal/pkg/utils"
"github.com/tsundata/assistant/internal/pkg/vendors/cloudflare"
"time"
)

func DomainAnalyticsReport(b *rulebot.RuleBot) []result.Result {
// get key
ctx := context.Background()
reply, err := b.MidClient.GetCredential(ctx, &pb.CredentialRequest{Name: cloudflare.ID})
if err != nil {
return []result.Result{result.ErrorResult(err)}
}
token := ""
zone := ""
for _, item := range reply.GetContent() {
if item.Key == cloudflare.Token {
token = item.Value
}
if item.Key == cloudflare.ZoneID {
zone = item.Value
}
}
if token == "" || zone == "" {
return []result.Result{result.EmptyResult()}
}

// get dashboard
end := time.Now().Format("2006-01-02T15:04:05Z")
start := time.Now().Add(time.Duration(-7*24) * time.Hour).Format("2006-01-02T15:04:05Z")

cf := cloudflare.NewCloudflare(token, zone)
analytics, err := cf.GetAnalytics(start, end)
if err != nil {
return []result.Result{result.ErrorResult(err)}
}

j, err := json.Marshal(analytics)
if err != nil {
return []result.Result{result.ErrorResult(err)}
}

return []result.Result{result.MessageResult(utils.ByteToString(j))}
}
7 changes: 7 additions & 0 deletions internal/app/cron/rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ var rules = []Rule{
return agent.WorkflowCron(b)
},
},
{
Name: "cloudflare_report",
When: "0 0 * * 0",
Action: func(b *rulebot.RuleBot) []result.Result {
return agent.DomainAnalyticsReport(b)
},
},
}

var Options = []rulebot.Option{
Expand Down
84 changes: 84 additions & 0 deletions internal/pkg/vendors/cloudflare/cloudflare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package cloudflare

import (
"fmt"
"github.com/go-resty/resty/v2"
"net/http"
"time"
)

const (
ID = "cloudflare"
Token = "token"
ZoneID = "zone_id"
AccountID = "account_id"
)

type AnalyticResponse struct {
Data struct {
Viewer struct {
Zones []struct {
FirewallEventsAdaptive []struct {
Action string `json:"action"`
ClientRequestHTTPHost string `json:"clientRequestHTTPHost"`
Datetime *time.Time `json:"datetime"`
RayName string `json:"rayName"`
UserAgent string `json:"userAgent"`
} `json:"firewallEventsAdaptive"`
} `json:"zones"`
} `json:"viewer"`
} `json:"data"`
Errors interface{} `json:"errors"`
}

type Cloudflare struct {
token string
zoneID string
}

func NewCloudflare(token string, zoneID string) *Cloudflare {
return &Cloudflare{token: token, zoneID: zoneID}
}

func (v *Cloudflare) GetAnalytics(start, end string) (*AnalyticResponse, error) {
c := resty.New()
resp, err := c.R().
SetAuthToken(v.token).
SetResult(&AnalyticResponse{}).
SetBody(fmt.Sprintf(`
query
{
viewer
{
zones(filter: { zoneTag: "%s"})
{
firewallEventsAdaptive(
filter: {
datetime_gt: "%s",
datetime_lt: "%s"
},
limit: 2,
orderBy: [datetime_DESC, rayName_DESC])
{
action
datetime
rayName
clientRequestHTTPHost
userAgent
}
}
}
}
`, v.zoneID, start, end)).
Post("https://api.cloudflare.com/client/v4/graphql")
if err != nil {
return nil, err
}

if resp.StatusCode() == http.StatusOK {
result := resp.Result().(*AnalyticResponse)
return result, nil
} else {
return nil, fmt.Errorf("cloudflare api error %d", resp.StatusCode())
}
}
6 changes: 6 additions & 0 deletions internal/pkg/vendors/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/go-redis/redis/v8"
"github.com/gofiber/fiber/v2"
"github.com/tsundata/assistant/api/pb"
"github.com/tsundata/assistant/internal/pkg/vendors/cloudflare"
"github.com/tsundata/assistant/internal/pkg/vendors/dropbox"
"github.com/tsundata/assistant/internal/pkg/vendors/email"
"github.com/tsundata/assistant/internal/pkg/vendors/github"
Expand Down Expand Up @@ -40,6 +41,11 @@ var ProviderCredentialOptions = map[string]interface{}{
email.Username: "Username Mail",
email.Password: "Password",
},
cloudflare.ID: map[string]string{
cloudflare.Token: "Api Token",
cloudflare.ZoneID: "Zone ID",
cloudflare.AccountID: "Account ID",
},
}

type OAuthProvider interface {
Expand Down

0 comments on commit 381229c

Please sign in to comment.