File: //data/silvera-firewall-app/bitninja_bridge/host_script.sh
#!/bin/bash
# BitNinja Bridge Script - Executes on host system
# This script processes commands from the Docker container
BRIDGE_DIR="/data/silvera-firewall-app/bitninja_bridge"
COMMAND_FILE="$BRIDGE_DIR/command.txt"
RESULT_FILE="$BRIDGE_DIR/result.txt"
LOCK_FILE="$BRIDGE_DIR/lock"
# Function to execute BitNinja CLI commands
execute_bitninja() {
local action="$1"
local ip="$2"
local comment="$3"
case "$action" in
"add")
/usr/sbin/bitninjacli --whitelist --add="$ip" --comment="$comment" 2>&1
;;
"remove")
/usr/sbin/bitninjacli --whitelist --del="$ip" 2>&1
;;
"list")
# Get IPs directly from ipset file (file-based approach to avoid CLI restart)
if [[ -f "/var/lib/bitninja/ipsets/heimdall-user-whitelist.txt.gz" ]]; then
gunzip -c /var/lib/bitninja/ipsets/heimdall-user-whitelist.txt.gz | grep "^add heimdall-user-whitelist" | awk '{print $3}' | sort
else
echo "Error: BitNinja whitelist file not found"
fi
;;
"force_sync")
# FORCE SYNC: Use same method as regular list but with fresh data
echo "Starting force sync with live data..." >&2
# Use the same method as regular "list" command for consistency
if [[ -f "/var/lib/bitninja/ipsets/heimdall-user-whitelist.txt.gz" ]]; then
potential_ips=$(gunzip -c /var/lib/bitninja/ipsets/heimdall-user-whitelist.txt.gz | grep "^add heimdall-user-whitelist" | awk '{print $3}')
echo "$potential_ips" | sort
else
echo "Error: BitNinja whitelist file not found"
fi
;;
"check")
/usr/sbin/bitninjacli --whitelist --check="$ip" 2>&1
;;
*)
echo "Error: Unknown action '$action'"
exit 1
;;
esac
}
# Main execution loop
while true; do
if [[ -f "$COMMAND_FILE" && ! -f "$LOCK_FILE" ]]; then
# Create lock file
touch "$LOCK_FILE"
# Read command
if [[ -s "$COMMAND_FILE" ]]; then
read -r action ip comment < "$COMMAND_FILE"
# Log the command for debugging
echo "$(date): Executing $action $ip $comment" >> "$BRIDGE_DIR/debug.log"
# Execute command and save result
result=$(execute_bitninja "$action" "$ip" "$comment")
echo "$result" > "$RESULT_FILE"
# Log the result
echo "$(date): Result: $result" >> "$BRIDGE_DIR/debug.log"
# Clean up
rm -f "$COMMAND_FILE"
fi
# Remove lock
rm -f "$LOCK_FILE"
fi
sleep 1
done