#!/bin/bash
# Author: Zhys
# Date  : 2018

# 填Cloudflare Email邮箱
CFEMAIL="freehao123@gmail.com"
# 填Cloudflare API key
CFAPIKEY="xxxxxxxxxxxxxxxx"
# 填Cloudflare Zones ID 域名对应的ID
ZONESID="xxxxxxxxxxxxxxxxxxxx"

# /data/wwwlogs/black.txt存放恶意攻击的IP列表
# IP一行一个。
IPADDR=$(</data/wwwlogs/black.txt)

# 循环提交 IPs 到 Cloudflare  防火墙黑名单
# 模式（mode）有 block, challenge, whitelist, js_challenge
for IPADDR in ${IPADDR[@]}; do
echo $IPADDR
curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$ZONESID/firewall/access_rules/rules" \
  -H "X-Auth-Email: $CFEMAIL" \
  -H "X-Auth-Key: $CFAPIKEY" \
  -H "Content-Type: application/json" \
  --data '{"mode":"block","configuration":{"target":"ip","value":"'$IPADDR'"},"notes":"CC Attatch"}'
done

# 删除 IPs 文件收拾干净
rm -rf /data/wwwlogs/black.txt