Skip to content

Commit

Permalink
Bypass rate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
martinhaus committed Oct 7, 2020
1 parent 2de2d9a commit ebf0ba5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 38 deletions.
45 changes: 8 additions & 37 deletions cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type zoneResp struct {
Groups []struct {
Dimensions struct {
Datetime string `json:"datetime"`
}
} `json:"dimensions"`
Sum struct {
Bytes uint64 `json:"bytes"`
CachedBytes uint64 `json:"cachedBytes"`
Expand Down Expand Up @@ -49,11 +49,11 @@ type zoneResp struct {
Requests uint64 `json:"requests"`
Threats uint64 `json:"threats"`
} `json:"countryMap"`
EncryptedBytes uint64 `json:encryptedBytes`
EncryptedRequests uint64 `json:encryptedRequests`
EncryptedBytes uint64 `json:"encryptedBytes"`
EncryptedRequests uint64 `json:"encryptedRequests"`
IPClass []struct {
Type string `json:"ipType"`
Requests uint64 `json:requests`
Requests uint64 `json:"requests"`
} `json:"ipClassMap"`
PageViews uint64 `json:"pageViews"`
ResponseStatus []struct {
Expand All @@ -65,7 +65,7 @@ type zoneResp struct {
Requests uint64 `json:"requests "`
} `json:"threatPathingMap"`
Threats uint64 `json:"threats"`
}
} `json:"sum"`
} `json:"httpRequests1mGroups"`

ColoGroups []struct {
Expand Down Expand Up @@ -113,37 +113,8 @@ func fetchZones() []cloudflare.Zone {

}

// APIv4 will be deprecated Nov 2020
func fetchZoneTotals(zoneID string) *cloudflare.ZoneAnalytics {

api, err := cloudflare.New(os.Getenv("CF_API_KEY"), os.Getenv("CF_API_EMAIL"))
if err != nil {
log.Fatal(err)
}

now := time.Now().Add(time.Duration(-60) * time.Second)
then := now.Add(time.Duration(-60) * time.Second)
continuous := false

options := &cloudflare.ZoneAnalyticsOptions{
Since: &then,
Until: &now,
Continuous: &continuous,
}

zone, err := api.ZoneAnalyticsDashboard(zoneID, *options)
if err != nil {
log.Fatal(err)
return nil
}

zoneTotals := zone.Totals

return &zoneTotals
}

func fetchZoneTotalsV2(zoneID string) (*cloudflareResponse, error) {
now := time.Now().Add(time.Duration(-60) * time.Second).UTC()
func fetchZoneTotals(zoneID string) (*cloudflareResponse, error) {
now := time.Now().Add(time.Duration(-180) * time.Second).UTC()
s := 60 * time.Second
now = now.Truncate(s)

Expand Down Expand Up @@ -231,7 +202,7 @@ query ($zoneID: String!, $time: Time!, $limit: Int!) {

func fetchColoTotals(zoneID string) (*cloudflareResponse, error) {

now := time.Now().Add(time.Duration(-60) * time.Second).UTC()
now := time.Now().Add(time.Duration(-180) * time.Second).UTC()
s := 60 * time.Second
now = now.Truncate(s)

Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func fetchMetrics() {
zones := fetchZones()
for _, z := range zones {
go fetchZoneAnalytics(z.ID, z.Name, &wg)
// Sleep due to rate limiting
time.Sleep(500 * time.Nanosecond)
go fetchZoneColocationAnalytics(z.ID, z.Name, &wg)
}
wg.Wait()
Expand Down
2 changes: 1 addition & 1 deletion prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func fetchZoneColocationAnalytics(ID string, name string, wg *sync.WaitGroup) {

func fetchZoneAnalytics(ID string, name string, wg *sync.WaitGroup) {
wg.Add(1)
r, err := fetchZoneTotalsV2(ID)
r, err := fetchZoneTotals(ID)

if err != nil || len(r.Viewer.Zones[0].Groups) == 0 {
return
Expand Down

0 comments on commit ebf0ba5

Please sign in to comment.