This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters