GoAdmio

underattack

Set a domain's Cloudflare security level (Under Attack, High, or Medium) and switch Under Attack Mode off again automatically after an hour

On this page: Overview · Quick start · Syntax · Options · Output · Examples · Troubleshooting · Details · See also


Overview

Cloudflare's Under Attack Mode is an emergency setting for a site that is being flooded with traffic. Every visitor first sees a short browser check before they reach the site, which stops most bots.

/go/underattack turns it on for one domain or for all domains. For each domain it:

  1. turns on the Cloudflare proxy for the domain's A record and its www record (Under Attack Mode only works on proxied traffic)
  2. sets the Cloudflare security level of the domain
  3. remembers the time, so the mode can be switched off again later

Run with no arguments, the command does the opposite. It ends Under Attack Mode on every domain where it has been on for at least an hour. This is meant to run from cron (see cron).

The command doesn't need sudo. If you aren't root, it runs itself again with sudo.

[!WARNING] Every time the command changes a domain, it also sets SSLRedirect=0 for all sites in GoAdmio's database. See Side effects.


Quick start

# Turn on Under Attack Mode for one domain
/go/underattack example.com

# Turn it back down to High
/go/underattack example.com off

Syntax

/go/underattack <domain> [level]
/go/underattack all [level]
/go/underattack
Form What it does
<domain> [level] Changes the security level of one domain.
all [level] Changes it for every domain on the server.
No arguments Switches off Under Attack Mode where it has been on for an hour or more.

Options

Argument Required Default Notes
<domain> or all No — A hosting account's domain (/home/<domain>/www must exist), or all.
[level] No Under Attack See the table below.

How the level word is read:

You pass Level set in Cloudflare
Nothing, or any other word (true, on, under_attack, and so on) under_attack
medium medium
high, off, no, none, false, or 0 high

[!NOTE] "Off" doesn't mean the lowest level. It sets the level to High. The command can't set Low or Off.


Output

For each domain, the command prints a line, then the answer Cloudflare returned for the security level change:

example.com (under_attack)...
{"result":{"id":"security_level","value":"under_attack", ... },"success":true,"errors":[],"messages":[]}

With all, each line starts with a counter, such as [3/12] example.com (under_attack)....

Look for "success":true. When it is false, the errors list says why.

When the no-argument run switches a domain off, it prints:

Turn Off Under Attack Mode: example.com
Outcome What you get
Level changed The line and Cloudflare's answer.
Domain folder missing No output. Nothing is changed.
Domain opted out Domain <domain> under attack mode disabled. To enable under attack mode remove file /home/<domain>/www/underattack
No-argument run Exit status 1.

Every run is logged to /var/log/go/underattack.log with a timestamp and the arguments used. Automatic switch-offs are logged there as well.


Examples

# Turn on Under Attack Mode for one domain
/go/underattack example.com
# Turn on Under Attack Mode everywhere
/go/underattack all
# Go back to High (this does not restore the old level)
/go/underattack example.com off
# Use Medium
/go/underattack example.com medium
# Switch off the domains that have been under attack for an hour or more
/go/underattack
# Stop Under Attack Mode being enabled for one domain by this command
touch /home/example.com/www/underattack

Troubleshooting

Symptom Likely cause Fix
No output at all for a domain There is no /home/<domain>/www folder. Check the domain name. The command gives no error for this.
Domain <domain> under attack mode disabled... The opt-out file /home/<domain>/www/underattack exists. Delete the file. It is ignored when you use all.
"success":false in the output The API token is wrong or lacks permissions, or the domain isn't in your Cloudflare account. Check the Cloudflare account ID and token (see cloudflare), and add the domain with dns.
Under Attack Mode never switches off The no-argument run isn't scheduled. Add it to cron, or run /go/underattack <domain> medium yourself.
The domain stayed under attack longer than an hour The scheduled run happens at intervals, so the end can be later than one hour. Run /go/underattack by hand.
The old security level didn't come back The command doesn't remember it. After switching off, the level is medium (automatic) or high (off). Set the level you want with /go/underattack <domain> medium, or in Cloudflare.
The www record isn't proxied Only a CNAME record named www is changed. Turn the proxy on by hand, or use proxy.
HTTP no longer redirects to HTTPS on some sites The command clears SSLRedirect for all sites. See Side effects.
API clients, webhooks, or apps get blocked Under Attack Mode challenges every visitor. Use medium, or add a Cloudflare rule for that traffic.

Details

How it works

flowchart TD
    A["/go/underattack"] --> B{"Arguments?"}
    B -- "None" --> C["Find domains under attack for 1 hour or more"]
    C --> D["Clear the time stamp, set Medium, run proxy"]
    B -- "all" --> E["Each domain in /home with a www folder"]
    B -- "domain" --> F{"www folder exists and not opted out?"}
    E --> G
    F -- "Yes" --> G["Proxy A and www records, set security level"]
    G --> H{"Level is under_attack?"}
    H -- "Yes" --> I["Save the time stamp in site.db"]

For each domain, the command looks up the Cloudflare zone and then:

  1. Finds the domain's A record and the www CNAME record and sets both to proxied. If a record is missing, that step fails quietly.
  2. Sets the zone's security level to under_attack, high, or medium.
  3. For under_attack only, saves the time in the UnderAttackStamp column of the Site table.

Switching off automatically

The no-argument run reads every site whose UnderAttackStamp is older than one hour. For each one it clears the stamp, runs /go/underattack <domain> medium, and then runs proxy for the domain.

Side effects

Files and requirements

Item Used for
/var/lib/sqlite/config.db The Cloudflare account ID and API token.
/var/lib/sqlite/site.db UnderAttackStamp and SSLRedirect.
/home/<domain>/www/underattack The opt-out file for one domain.
/var/log/go/underattack.log The command log.
curl, sqlite3 Calling Cloudflare's API and reading the databases.

The API token must be allowed to read zones and to edit DNS records and zone settings. It is sent on the curl command line, so it can be seen briefly in the process list.


See also

Command Description
securitylevel Set the Cloudflare security level for a domain or all hosted domains
proxy Change Cloudflare's proxy setting
fightmode Enable or disable Bot Fight Mode on Cloudflare
waf Block IPs via Cloudflare WAF
purge Delete the Cloudflare edge cache (purge everything)
ddos Track and block DDoS attack IPs in GoAdmio
cloudflare Manage DNS records using Cloudflare's API
cron Set up main cron jobs in GoAdmio