GoAdmio

waf

Block an IP address in Cloudflare for one domain or all hosted domains, or reset the Cloudflare firewall rules and filters on every domain

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


Overview

/go/waf uses the Cloudflare API to block IP addresses before they reach your server. It has two jobs:

It uses the Cloudflare account ID and API token saved in GoAdmio's configuration. If you run it as a normal user, it re-runs itself with sudo.

[!WARNING]

  • One argument means "block this IP on all domains". /go/waf example.com does not block anything on example.com. It tries to block the text example.com as an IP everywhere.
  • There is no way to unblock a single IP with this command. reset does not remove IP Access Rules. See Unblocking an IP.
  • reset can delete your own Cloudflare rules. It removes the first rules and filters it finds, whoever created them.
  • The IP address is not checked, so you can block your own IP by mistake.

Quick start

# Block an IP on one domain
/go/waf example.com 203.0.113.5

# Block an IP on all domains
/go/waf 203.0.113.5

Syntax

# Block an IP on all domains (one argument)
/go/waf <ip>
/go/waf all <ip>

# Block an IP on one domain
/go/waf <domain> <ip>

# Delete firewall rules and filters on all domains
/go/waf reset

Arguments

Form Domain IP Notes
/go/waf <ip> All The argument One argument is always read as an IP.
/go/waf all <ip> All The second argument Same result, written out.
/go/waf <domain> <ip> The first argument The second argument The domain needs a /home/<domain>/www folder.
/go/waf reset All None Deletes rules and filters. See Reset.

Arguments after the second are ignored.

IP rules

Rule Detail
Format A single IPv4 or IPv6 address.
Ranges Not supported. A range such as 203.0.113.0/24 is rejected by Cloudflare.
Validation None in the command. A wrong value is sent to Cloudflare, which answers with an error.

Output

Blocking

[1/3] Blocking IP 203.0.113.5 [example.com]...
{"result":{ ... },"success":true,"errors":[],"messages":[]}

The sample above is illustrative and shortened.

Part Meaning
[<n>/<total>] Blocking IP <ip> [<domain>]... Progress when blocking on all domains. With one domain the line is Blocking IP <ip> [<domain>]....
"success":true Cloudflare accepted the rule.
"success":false and an errors list The rule was refused. The message says why.

Reset

[1/3] Bulk Unbocking [example.com]...

Only the progress line is shown (the word is spelled Unbocking in the command's output). Cloudflare's answers are hidden, and the exit code is always 1.

Nothing printed

If you name a domain that has no /home/<domain>/www folder, the command prints nothing and does nothing.


Examples

# Block one IP on one domain
/go/waf example.com 203.0.113.5
# Block one IP on every hosted domain
/go/waf 203.0.113.5
# The same, spelled out
/go/waf all 203.0.113.5
# Remove firewall rules and filters from every domain
/go/waf reset
# Check the recent runs
tail /var/log/go/waf.log

Troubleshooting

Symptom Likely cause Fix
/go/waf example.com ran against every domain and printed errors One argument is read as an IP. Pass both: /go/waf example.com 203.0.113.5.
Nothing happens for a domain There is no /home/<domain>/www folder (typo, or an alias). Check the spelling and use the primary domain.
"success":false with an authentication error The API token is missing, expired, or lacks permission to edit firewall rules. Check the Cloudflare settings with config and create a token with the right permission.
"success":false and an empty zone in the error The domain has no saved Cloudflare zone ID. Add the domain to Cloudflare with dns.
An "invalid IP" error The value is not a single valid IP address. Use one address. Ranges are not supported.
A "duplicate" error The IP is already blocked on that domain. Nothing to do.
I need to unblock an IP The command only adds blocks. Remove the rule in Cloudflare. See Unblocking an IP.
reset did not unblock my IP reset removes firewall rules and filters, not IP Access Rules. Remove the access rule in Cloudflare.
reset removed my own rules It deletes the first rules and filters it finds. Restore them in Cloudflare, and avoid reset if you use custom rules.
reset left some rules behind It removes at most 5 rules and 5 filters per domain per run. Run it again.
reset seems to do nothing Its answers are hidden, so failures are not shown. Check in the Cloudflare dashboard. Cloudflare has been replacing its older firewall rules and filters endpoints with WAF custom rules, so check Cloudflare's current documentation.
Errors about too many requests during reset Each domain needs 11 or more API calls, and Cloudflare limits how many it accepts (documented as 1,200 per 5 minutes). Run it again later, or in smaller batches.

Details

What it does

flowchart TD
    A["/go/waf args"] --> B{"Root?"}
    B -- "No" --> C["Re-run with sudo"]
    C --> D
    B -- "Yes" --> D["Append the command to /var/log/go/waf.log"]
    D --> E["Read Cloudflare account ID and API token"]
    E --> F{"First argument is reset?"}
    F -- "Yes" --> G["For each domain with a www folder: find its zone, delete up to 5 rules and 5 filters"]
    F -- "No" --> H{"One argument?"}
    H -- "Yes" --> I["Domain = all, IP = the argument"]
    H -- "No" --> J["Domain = first, IP = second"]
    I --> K["For each target domain: read its zone ID, create the block rule"]
    J --> K

Steps in order

  1. Re-runs itself with sudo if not root.
  2. Appends a timestamped line with the arguments to /var/log/go/waf.log.
  3. Reads the Cloudflare account ID and API token from GoAdmio's configuration.
  4. For reset: handles every folder in /home that has a www folder (see Reset) and stops.
  5. Works out the domain and IP from the arguments.
  6. For each target domain with a www folder, reads its zone ID from the Site table and sends the block rule to Cloudflare.

Block rule

Each block creates one Cloudflare IP Access Rule:

Field Value
Action block
Target The single IP address
Note Blocking IP due to malicious activity

Cloudflare then stops requests from that address at its edge, before they reach your server. The note appears next to the rule in the Cloudflare dashboard.

The zone ID comes from the ZoneID saved for the domain in GoAdmio, so only domains added to Cloudflare through GoAdmio can be blocked this way.

Reset

For each folder in /home that has a www folder:

  1. Looks up the zone ID through the Cloudflare API, by domain name.
  2. Five times in a row: lists the firewall rules, and deletes the first one found.
  3. Five times in a row: lists the filters, and deletes the first one found.

Unblocking an IP

This command has no unblock option. To remove an IP that was blocked with it, delete the rule in the Cloudflare dashboard, under the IP Access Rules of each domain (or account), where you can search by IP. For a block on all domains, repeat it for each domain. The server-level commands block and unblock are separate and act on the server itself.

Cloudflare API calls

Mode Call Purpose
Block POST /zones/<zone>/firewall/access_rules/rules Create the block rule
Reset GET /zones?name=<domain> Find the zone ID
Reset GET and DELETE /zones/<zone>/firewall/rules Delete the first rule, 5 times
Reset GET and DELETE /zones/<zone>/filters Delete the first filter, 5 times

Notes


See also

Command Description
cloudflare Manage DNS records using Cloudflare's API
dns Add your domain to Cloudflare and edit the DNS zone
block Block an IP address on a GoAdmio server
unblock Unblock an IP address on a GoAdmio server
underattack Enable Cloudflare Under Attack Mode for a domain
securitylevel Set the Cloudflare security level for a domain or all hosted domains
fightmode Enable or disable Bot Fight Mode on Cloudflare
ddos Track and block DDoS attack IPs in GoAdmio