Made a small Node.js tool to auto-enable Cloudflare Under Attack mode when server CPU is high

Automating Cloudflareโ€™s Under Attack Mode Based on Server CPU Usage with a Custom Node.js Solution

In the realm of server security and uptime management, proactive measures are essential to mitigate malicious attacks and maintain optimal performance. Recently, I developed a lightweight Node.js utility designed to enhance your websiteโ€™s resilience by automatically enabling Cloudflareโ€™s Under Attack mode during high server load episodes.

The Concept

This tool continuously monitors your serverโ€™s CPU utilization, and if the load exceeds a defined threshold for a specified duration, it triggers Cloudflareโ€™s Under Attack mode. Once server activity stabilizes, the script reverts to normal operation. Additionally, it offers optional Telegram notifications to keep you informed about these status changes in real time.

Implementation Highlights

  • CPU Monitoring: Utilizes system metrics to assess server load dynamically.
  • Cloudflare API Interaction: Programmatically toggles Under Attack mode via Cloudflareโ€™s API endpoints.
  • Threshold-Based Logic: Activation occurs only when sustained high load conditions are detected, preventing false positives.
  • Notification Support: Integrates with Telegram to send alerts whenever the mode is toggled, providing immediate awareness.

Technical Details

The solution is implemented in Node.js, capitalizing on its efficiency and ease of scripting server-side automation tasks. It requires minimal configurationโ€”primarily setting your Cloudflare API credentials, CPU threshold, and duration.

Download and Usage

Iโ€™ve made the complete codebase publicly available on GitHub, allowing anyone interested to customize and deploy it within their own infrastructure:

Visit the GitHub Repository

Personal Motivation & Potential Applications

This project originated from my own experience managing a Virtual Private Server (VPS) that was under constant threat from malicious traffic. Automating the under attack mode helps safeguard server resources during attack periods without manual intervention, which can be critical for maintaining service availability.

While this tool was built for my specific use case, it can be adapted for various scenarios where automated responses to server load or security threats are beneficial.

Call for Feedback

Iโ€™m keen to improve this utility further and welcome ideas or contributions from the community. Whether you have suggestions for additional features, better integration options, or general improvements, feel free to share!


Interested in enhancing your website security and automation? Check out the repository and tailor this solution to fit your needs.


Leave a Reply

Your email address will not be published. Required fields are marked *