#!/bin/bash
# BDUSP DDoS Guard CLI v5

RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'
CYAN='\033[0;36m'; NC='\033[0m'; BOLD='\033[1m'

BASE="/var/lib/ddos-guard"
BLOCKLIST="$BASE/blocklist.json"
WHITELIST="$BASE/whitelist.json"
QUARANTINE="$BASE/quarantine.json"
CONFIG="$BASE/config.json"
LOGFILE="/var/log/ddos-guard.log"

header() {
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${CYAN}  🛡️  BDUSP DDoS Guard v5${NC}"
    echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
}

py() { python3 -c "$1"; }

valid_ip() {
    python3 -c "
import re,sys
ip='$1'.strip()
m=re.match(r'^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$',ip)
sys.exit(0 if (m and all(0<=int(g)<=255 for g in m.groups())) else 1)
" 2>/dev/null
}

cfg_get() {
    py "
import json
try: d=json.load(open('$CONFIG'))
except: d={}
defs={'window_seconds':30,'block_threshold':300,'perm_threshold':1000,
      'check_interval':5,'q_single_ip':2000,'q_all_ip':6000}
defs.update(d); print(defs.get('$1',''))
"
}

# ── status ────────────────────────────────────────────────────────────────────
cmd_status() {
    header; echo ""
    if systemctl is-active --quiet ddos-guard; then
        echo -e "  Service   : ${GREEN}● Running${NC}"
    else
        echo -e "  Service   : ${RED}● Stopped${NC}"
    fi
    total=$(py "import json
try: d=json.load(open('$BLOCKLIST')); print(len(d))
except: print(0)")
    temp=$(py "import json
try: d=json.load(open('$BLOCKLIST')); print(sum(1 for v in d.values() if not v.get('permanent')))
except: print(0)")
    perm=$(py "import json
try: d=json.load(open('$BLOCKLIST')); print(sum(1 for v in d.values() if v.get('permanent')))
except: print(0)")
    wl=$(py "import json
try: d=json.load(open('$WHITELIST')); print(len(d))
except: print(0)")
    qc=$(py "import json
try: d=json.load(open('$QUARANTINE')); print(len(d))
except: print(0)")
    echo -e "  Blocked   : ${RED}${total} IP(s)${NC} — ${temp} temp, ${perm} permanent"
    echo -e "  Whitelist : ${GREEN}${wl} IP(s)${NC}"
    echo -e "  Quarantine: ${YELLOW}${qc} domain(s)${NC}"
    echo ""
    echo -e "  ${BOLD}IP Block Config (per $(cfg_get window_seconds)s):${NC}"
    echo -e "  7-day block     : $(cfg_get block_threshold) req from same IP"
    echo -e "  Permanent block : $(cfg_get perm_threshold) req from same IP"
    echo ""
    q_single=$(cfg_get q_single_ip)
    q_all=$(cfg_get q_all_ip)
    q_off_single=$((q_single / 2))
    q_off_all=$((q_all / 2))
    echo -e "  ${BOLD}Quarantine Config (per $(cfg_get window_seconds)s):${NC}"
    echo -e "  Single IP → ON  : $q_single req  |  OFF: < $q_off_single req"
    echo -e "  All IP    → ON  : $q_all req  |  OFF: < $q_off_all req"
    echo -e "  Check interval  : $(cfg_get check_interval)s"
    echo ""
}

# ── config ────────────────────────────────────────────────────────────────────
cmd_config() {
    if [ -z "$1" ]; then
        header; echo ""
        echo -e "  ${BOLD}Current Config:${NC}"; echo ""
        echo -e "  ${GREEN}window_seconds${NC}     $(cfg_get window_seconds)    Monitoring window (seconds)"
        echo -e "  ${GREEN}block_threshold${NC}    $(cfg_get block_threshold)   Req/window → 7-day block"
        echo -e "  ${GREEN}perm_threshold${NC}     $(cfg_get perm_threshold)   Req/window → permanent block"
        echo -e "  ${GREEN}q_single_ip${NC}        $(cfg_get q_single_ip)   Same IP req/window → quarantine"
        echo -e "  ${GREEN}q_all_ip${NC}           $(cfg_get q_all_ip)   All IP req/window → quarantine"
        echo -e "  ${GREEN}check_interval${NC}     $(cfg_get check_interval)     Log check every N seconds"
        echo ""
        echo -e "  ${YELLOW}Usage: vg config <key> <value>${NC}"
        echo ""
        return
    fi
    [ -z "$2" ] && echo -e "${RED}Usage: vg config <key> <value>${NC}" && exit 1
    py "
import json,os
os.makedirs('$BASE',exist_ok=True)
try: d=json.load(open('$CONFIG'))
except: d={}
v='$2'
try: v=int(v)
except: pass
d['$1']=v
json.dump(d,open('$CONFIG','w'),indent=2)
print('  ✅ $1 =',v)
"
    systemctl restart ddos-guard 2>/dev/null
    echo -e "${GREEN}✅ Saved. Service restarted.${NC}"
}

# ── quarantine ────────────────────────────────────────────────────────────────
cmd_quarantine() {
    header; echo -e "\n  ${BOLD}Quarantined Domains:${NC}\n"
    py "
import json,time
try: data=json.load(open('$QUARANTINE'))
except: data={}
if not data: print('  No quarantined domains.'); exit()
print(f\"  {'Domain':<35} {'Since'}\")
print(f\"  {'─'*35} {'─'*20}\")
for d,i in data.items():
    since=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(i.get('since',0)))
    print(f'  {d:<35} {since}')
print()
"
}

cmd_quarantine_release() {
    [ -z "$1" ] && echo -e "${RED}Usage: vg quarantine-release <domain>${NC}" && exit 1
    python3 -c "
import json,os,shutil
try: q=json.load(open('$QUARANTINE'))
except: print('  No quarantine data.'); exit()
if '$1' not in q: print('  Not in quarantine.'); exit()
dr=q['$1'].get('docroot')
if not dr: print('  Docroot unknown.'); exit()
ht=os.path.join(dr,'.htaccess')
bk=os.path.join(dr,'.htaccess.bdusp_bak')
php=os.path.join(dr,'.bdusp_ddos_challenge.php')
if os.path.exists(bk): shutil.move(bk,ht)
elif os.path.exists(ht):
    try:
        if 'BDUSP-GUARD' in open(ht).read(): os.remove(ht)
    except: pass
if os.path.exists(php): os.remove(php)
del q['$1']
json.dump(q,open('$QUARANTINE','w'),indent=2)
print('  Released.')
"
    echo -e "${GREEN}✅ $1 released from quarantine.${NC}"
}

# ── blocklist ─────────────────────────────────────────────────────────────────
_show_bl() {
    local f="$1"
    py "
import json,time
try: data=json.load(open('$BLOCKLIST'))
except: data={}
filt='$f'
rows=[]
for ip,i in data.items():
    perm=i.get('permanent',False)
    if filt=='temp' and perm: continue
    if filt=='perm' and not perm: continue
    bt=time.strftime('%Y-%m-%d %H:%M',time.localtime(i.get('time',0)))
    ex='Never' if perm else time.strftime('%Y-%m-%d %H:%M',time.localtime(i.get('time',0)+(i.get('duration') or 0)))
    src='auto' if i.get('auto') else 'manual'
    rows.append((ip,'PERMANENT' if perm else '7 Days',bt,ex,src))
if not rows: print('  No entries.'); exit()
print(f\"  {'IP':<20} {'Type':<12} {'Blocked At':<18} {'Expires':<18} {'Source'}\")
print(f\"  {'─'*20} {'─'*12} {'─'*18} {'─'*18} {'─'*8}\")
for r in rows:
    print(f'  {r[0]:<20} {r[1]:<12} {r[2]:<18} {r[3]:<18} {r[4]}')
print()
"
}

cmd_blocklist()      { header; echo -e "\n  ${BOLD}All Blocked IPs:${NC}\n";        _show_bl all; }
cmd_blocklist_temp() { header; echo -e "\n  ${BOLD}Temp Blocked (7-day):${NC}\n";   _show_bl temp; }
cmd_blocklist_perm() { header; echo -e "\n  ${BOLD}Permanent Blocked:${NC}\n";      _show_bl perm; }

_add_bl() {
    local perm="$1"; shift
    local raw="$*"
    IFS=', ' read -ra ips <<< "$raw"
    local invalid=()
    for ip in "${ips[@]}"; do
        ip=$(echo "$ip" | tr -d ',' | xargs)
        [ -z "$ip" ] && continue
        if ! valid_ip "$ip"; then
            invalid+=("$ip")
            continue
        fi
        iptables -I INPUT -s "$ip" -j DROP 2>/dev/null
        iptables -I OUTPUT -d "$ip" -j DROP 2>/dev/null
        python3 -c "
import json,time,os
os.makedirs('$BASE',exist_ok=True)
try: d=json.load(open('$BLOCKLIST'))
except: d={}
perm=$perm
d['$ip']={'time':time.time(),'duration':None if perm else 604800,'permanent':perm,'label':'PERMANENT' if perm else '7d','auto':False}
json.dump(d,open('$BLOCKLIST','w'),indent=2)
"
        echo -e "${GREEN}  ✅ Blocked: $ip${NC}"
    done
    [ ${#invalid[@]} -gt 0 ] && echo -e "${RED}  Invalid IP(s): ${invalid[*]}${NC}"
}

_rm_bl() {
    local raw="$*"
    IFS=', ' read -ra ips <<< "$raw"
    local invalid=()
    for ip in "${ips[@]}"; do
        ip=$(echo "$ip" | tr -d ',' | xargs)
        [ -z "$ip" ] && continue
        if ! valid_ip "$ip"; then invalid+=("$ip"); continue; fi
        iptables -D INPUT -s "$ip" -j DROP 2>/dev/null
        iptables -D OUTPUT -d "$ip" -j DROP 2>/dev/null
        python3 -c "
import json
try: d=json.load(open('$BLOCKLIST')); d.pop('$ip',None); json.dump(d,open('$BLOCKLIST','w'),indent=2)
except: pass
"
        echo -e "${GREEN}  ✅ Removed: $ip${NC}"
    done
    [ ${#invalid[@]} -gt 0 ] && echo -e "${RED}  Invalid IP(s): ${invalid[*]}${NC}"
}

cmd_blocklist_add()      { [ -z "$1" ] && echo -e "${RED}Usage: vg blocklist-add <ip> [ip2]${NC}" && exit 1; _add_bl True "$@"; }
cmd_blocklist_remove()   { [ -z "$1" ] && echo -e "${RED}Usage: vg blocklist-remove <ip> [ip2]${NC}" && exit 1; _rm_bl "$@"; }

cmd_blocklist_remove_all() {
    echo -e "${YELLOW}Removing ALL blocked IPs...${NC}"
    py "
import json,subprocess
try: data=json.load(open('$BLOCKLIST'))
except: data={}
for ip in data:
    subprocess.run('iptables -D INPUT -s {} -j DROP 2>/dev/null'.format(ip),shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    subprocess.run('iptables -D OUTPUT -d {} -j DROP 2>/dev/null'.format(ip),shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
json.dump({},open('$BLOCKLIST','w'),indent=2)
print('  Removed {} IP(s).'.format(len(data)))
"
    echo -e "${GREEN}✅ Done.${NC}"
}

cmd_blocklist_remove_all_temp() {
    echo -e "${YELLOW}Removing all 7-day blocks...${NC}"
    py "
import json,subprocess
try: data=json.load(open('$BLOCKLIST'))
except: data={}
to_del=[ip for ip,i in data.items() if not i.get('permanent')]
for ip in to_del:
    subprocess.run('iptables -D INPUT -s {} -j DROP 2>/dev/null'.format(ip),shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    subprocess.run('iptables -D OUTPUT -d {} -j DROP 2>/dev/null'.format(ip),shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    del data[ip]
json.dump(data,open('$BLOCKLIST','w'),indent=2)
print('  Removed {} IP(s).'.format(len(to_del)))
"
    echo -e "${GREEN}✅ Done.${NC}"
}

cmd_blocklist_remove_all_perm() {
    echo -e "${YELLOW}Removing all permanent blocks...${NC}"
    py "
import json,subprocess
try: data=json.load(open('$BLOCKLIST'))
except: data={}
to_del=[ip for ip,i in data.items() if i.get('permanent')]
for ip in to_del:
    subprocess.run('iptables -D INPUT -s {} -j DROP 2>/dev/null'.format(ip),shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    subprocess.run('iptables -D OUTPUT -d {} -j DROP 2>/dev/null'.format(ip),shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    del data[ip]
json.dump(data,open('$BLOCKLIST','w'),indent=2)
print('  Removed {} IP(s).'.format(len(to_del)))
"
    echo -e "${GREEN}✅ Done.${NC}"
}

# ── whitelist ─────────────────────────────────────────────────────────────────
cmd_whitelist() {
    header; echo -e "\n  ${BOLD}Whitelisted IPs:${NC}\n"
    py "
import json,time
sys_ips={'127.0.0.1':{'added':0,'note':'system'},'::1':{'added':0,'note':'system'}}
try: u=json.load(open('$WHITELIST')); sys_ips.update(u)
except: pass
print(f\"  {'IP':<22} {'Added':<20} {'Note'}\")
print(f\"  {'─'*22} {'─'*20} {'─'*20}\")
for ip,i in sys_ips.items():
    added=time.strftime('%Y-%m-%d %H:%M',time.localtime(i.get('added',0))) if i.get('added') else 'system'
    print(f\"  {ip:<22} {added:<20} {i.get('note','')}\")
print()
"
}

_add_wl() {
    local raw="$*"
    IFS=', ' read -ra ips <<< "$raw"
    local invalid=()
    for ip in "${ips[@]}"; do
        ip=$(echo "$ip" | tr -d ',' | xargs)
        [ -z "$ip" ] && continue
        if ! valid_ip "$ip"; then invalid+=("$ip"); continue; fi
        python3 -c "
import json,time,os
os.makedirs('$BASE',exist_ok=True)
try: d=json.load(open('$WHITELIST'))
except: d={}
d['$ip']={'added':time.time(),'note':'manual'}
json.dump(d,open('$WHITELIST','w'),indent=2)
"
        echo -e "${GREEN}  ✅ Whitelisted: $ip${NC}"
    done
    [ ${#invalid[@]} -gt 0 ] && echo -e "${RED}  Invalid IP(s): ${invalid[*]}${NC}"
}

_rm_wl() {
    local raw="$*"
    IFS=', ' read -ra ips <<< "$raw"
    local invalid=()
    for ip in "${ips[@]}"; do
        ip=$(echo "$ip" | tr -d ',' | xargs)
        [ -z "$ip" ] && continue
        if ! valid_ip "$ip"; then invalid+=("$ip"); continue; fi
        python3 -c "
import json
try: d=json.load(open('$WHITELIST')); d.pop('$ip',None); json.dump(d,open('$WHITELIST','w'),indent=2)
except: pass
"
        echo -e "${GREEN}  ✅ Removed: $ip${NC}"
    done
    [ ${#invalid[@]} -gt 0 ] && echo -e "${RED}  Invalid IP(s): ${invalid[*]}${NC}"
}

cmd_whitelist_add()        { [ -z "$1" ] && echo -e "${RED}Usage: vg whitelist-add <ip> [ip2]${NC}" && exit 1; _add_wl "$@"; }
cmd_whitelist_remove()     { [ -z "$1" ] && echo -e "${RED}Usage: vg whitelist-remove <ip> [ip2]${NC}" && exit 1; _rm_wl "$@"; }
cmd_whitelist_remove_all() {
    py "import json; json.dump({},open('$WHITELIST','w'),indent=2); print('  Cleared.')"
    echo -e "${GREEN}✅ Done.${NC}"
}

# ── log / service ─────────────────────────────────────────────────────────────
cmd_log()       { echo -e "${CYAN}━━━━ Live Log (Ctrl+C) ━━━━${NC}"; tail -f "$LOGFILE"; }
cmd_log_clear() { > "$LOGFILE" && echo -e "${GREEN}✅ Log cleared.${NC}"; }
cmd_start()     { systemctl start   ddos-guard && echo -e "${GREEN}▶ Started.${NC}"; }
cmd_stop()      { systemctl stop    ddos-guard && echo -e "${YELLOW}⏹ Stopped.${NC}"; }
cmd_restart()   { systemctl restart ddos-guard && echo -e "${GREEN}↺ Restarted.${NC}"; }

# ── help ──────────────────────────────────────────────────────────────────────
cmd_help() {
    header; echo ""
    echo -e "  ${BOLD}INFO${NC}"
    echo -e "  vg status                        Status + config overview"
    echo -e "  vg log                           Live log"
    echo -e "  vg log-clear                     Clear log file"
    echo ""
    echo -e "  ${BOLD}CONFIG${NC}"
    echo -e "  vg config                        Show config"
    echo -e "  vg config <key> <value>          Edit value"
    echo -e "    ${CYAN}window_seconds${NC}     Monitoring window"
    echo -e "    ${CYAN}block_threshold${NC}    Req → 7-day block"
    echo -e "    ${CYAN}perm_threshold${NC}     Req → permanent block"
    echo -e "    ${CYAN}q_single_ip${NC}        Same IP req → quarantine"
    echo -e "    ${CYAN}q_all_ip${NC}           All IP req → quarantine"
    echo ""
    echo -e "  ${BOLD}QUARANTINE${NC}"
    echo -e "  vg quarantine                    List quarantined domains"
    echo -e "  vg quarantine-release <domain>   Release a domain"
    echo ""
    echo -e "  ${BOLD}BLOCKLIST${NC}"
    echo -e "  vg blocklist                     All blocked IPs"
    echo -e "  vg blocklist-temp                7-day blocked only"
    echo -e "  vg blocklist-perm                Permanent blocked only"
    echo -e "  vg blocklist-add <ip> [ip2]      Add (permanent)"
    echo -e "  vg blocklist-remove <ip> [ip2]   Remove"
    echo -e "  vg blocklist-remove-all          Remove all"
    echo -e "  vg blocklist-remove-all-temp     Remove all 7-day"
    echo -e "  vg blocklist-remove-all-perm     Remove all permanent"
    echo ""
    echo -e "  ${BOLD}WHITELIST${NC}"
    echo -e "  vg whitelist                     Show whitelist"
    echo -e "  vg whitelist-add <ip> [ip2]      Add"
    echo -e "  vg whitelist-remove <ip> [ip2]   Remove"
    echo -e "  vg whitelist-remove-all          Remove all"
    echo ""
    echo -e "  ${BOLD}SERVICE${NC}"
    echo -e "  vg start / vg stop / vg restart"
    echo ""
}

# ── router ────────────────────────────────────────────────────────────────────
case "$1" in
    status)                    cmd_status ;;
    config)                    cmd_config "$2" "$3" ;;
    quarantine)                cmd_quarantine ;;
    quarantine-release)        cmd_quarantine_release "$2" ;;
    log)                       cmd_log ;;
    log-clear)                 cmd_log_clear ;;
    blocklist)                 cmd_blocklist ;;
    blocklist-temp)            cmd_blocklist_temp ;;
    blocklist-perm)            cmd_blocklist_perm ;;
    blocklist-add)             shift; cmd_blocklist_add "$@" ;;
    blocklist-remove)          shift; cmd_blocklist_remove "$@" ;;
    blocklist-remove-all)      cmd_blocklist_remove_all ;;
    blocklist-remove-all-temp) cmd_blocklist_remove_all_temp ;;
    blocklist-remove-all-perm) cmd_blocklist_remove_all_perm ;;
    whitelist)                 cmd_whitelist ;;
    whitelist-add)             shift; cmd_whitelist_add "$@" ;;
    whitelist-remove)          shift; cmd_whitelist_remove "$@" ;;
    whitelist-remove-all)      cmd_whitelist_remove_all ;;
    start)                     cmd_start ;;
    stop)                      cmd_stop ;;
    restart)                   cmd_restart ;;
    help|--help|-h)            cmd_help ;;
    *)                         cmd_help ;;
esac
