public bool OpenRateLimit(string url, int threshold, int period, out AuditLogEntity errorLog) { var zoneTableId = ZoneBusiness.GetZoneByZoneId(_zoneId).TableID; errorLog = null; var ratelimit = GetRateLimitRule(url, threshold, period); if (null != ratelimit) { ratelimit.Disabled = false; var response = UpdateRateLimit(ratelimit); if (!response.success) { errorLog = new AuditLogEntity(zoneTableId, LogLevel.Error, $"Open rate limiting rule of Cloudflare failure,the reason is:[{(null != response.errors && response.errors.Length > 0 ? response.errors[0].message : "No error message from Cloudflare.")}].<br />"); } return(response.success); } else { var response = CreateRateLimit(new CloudflareRateLimitRule(url, threshold, period, $"[Auto Prevention] {url}")); if (!response.success) { errorLog = new AuditLogEntity(zoneTableId, LogLevel.Error, $"Create rate limiting rule of Cloudflare failure,the reason is:[{(response.errors !=null && response.errors .Length > 0 ? response.errors[0].message : "No error message from Cloudflare.")}].<br />"); } return(response.success); } }
public List <CloudflareLog> GetLogs(DateTime start, DateTime end, double sample, out bool retry) { var zoneTableId = ZoneBusiness.GetZoneByZoneId(_zoneId).TableID; retry = false; var cloudflareLogs = new List <CloudflareLog>(); try { string fields = "RayID,ClientIP,ClientRequestHost,ClientRequestMethod,ClientRequestURI,EdgeEndTimestamp,EdgeResponseBytes,EdgeResponseStatus,EdgeStartTimestamp,CacheResponseStatus,ClientRequestBytes,CacheCacheStatus,OriginResponseStatus,OriginResponseTime"; string startTime = GetUTCTimeString(start); string endTime = GetUTCTimeString(end); string url = "{5}/zones/{0}/logs/received?start={1}&end={2}&fields={3}&sample={4}"; url = string.Format(url, _zoneId, startTime, endTime, fields, sample, _apiUrlPrefix); string content = HttpGet(url, 240); if (content.Contains(@"""success"":false")) { if (content.Contains("429 Too Many Requests")) { retry = true; } else { var errorResponse = JsonConvert.DeserializeObject <CloudflareLogErrorResponse>(content); AuditLogBusiness.Add(new AuditLogEntity(zoneTableId, LogLevel.Error, $"Got logs failure, the reason is:[{ (errorResponse.Errors.Count > 0 ? errorResponse.Errors[0].Message : "No error message from Cloudflare.")}].")); } } else { content = content.Replace("\"}", "\"},"); cloudflareLogs = JsonConvert.DeserializeObject <List <CloudflareLog> >($"[{content}]"); cloudflareLogs.RemoveAll(x => (null != x.CacheCacheStatus && x.CacheCacheStatus.ToLower().Equals("hit")) || 0 == x.OriginResponseStatus); } return(cloudflareLogs); } catch (Exception ex) { retry = true; AuditLogBusiness.Add(new AuditLogEntity(zoneTableId, LogLevel.Error, $"Got logs failure, the reason is:[{ex.Message}]. <br />stack trace:{ex.StackTrace}].")); return(cloudflareLogs); } }