#!/bin/bash

# Copyright 2014,2015,2016,2017,2018,2019,2020,2021 Security Onion Solutions, LLC
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Usage: so-tcpreplay "/opt/samples/*"

. /usr/sbin/so-common
. /usr/sbin/so-image-common

REPLAYIFACE=${REPLAYIFACE:-$(lookup_pillar interface sensor)}
REPLAYSPEED=${REPLAYSPEED:-10}

mkdir -p /opt/so/samples

if [[ $# -lt 1 ]]; then
	echo "Replays one or more PCAP sample files to the Security Onion monitoring interface."
	echo
	echo "Usage: $0 <pcap-sample(s)>"
	echo 
	echo "All PCAPs must be placed in the /opt/so/samples directory unless replaying"
	echo "a sample pcap that is included in the so-tcpreplay image. Those PCAP sampes"
	echo "are located in the /opt/samples directory inside of the image."
	echo 
	echo "Customer provided PCAP example:"
	echo "  $0 /opt/so/samples/some_event.pcap"
	echo
	echo "Security Onion-provided PCAP example:"
	echo "  $0 /opt/samples/4in6.pcap"

	exit 1
fi

if ! docker ps | grep -q so-tcpreplay; then
  echo "Replay functionality not enabled; attempting to enable now (may require Internet access)..."
  echo

  if is_manager_node; then
  	set_version
  	if ! docker images | grep so-tcpreplay | grep ":5000" | grep -q $VERSION ; then
  		echo "Pulling so-tcpreplay image"
		  TRUSTED_CONTAINERS=("so-tcpreplay")
		  mkdir -p /opt/so/log/tcpreplay
		  update_docker_containers "tcpreplay" "" "" "/opt/so/log/tcpreplay/init.log"
		else
			echo "so-tcpreplay image exists."
		fi
	fi
	if is_sensor_node; then
		if ! is_manager_node; then
			echo "Attempting to start replay container. If this fails then you may need to run this command on the manager first."
		fi
  	so-tcpreplay-start || fail "Unable to initialize tcpreplay"
  fi
fi

if is_sensor_node; then
	echo "Replaying PCAP(s) at ${REPLAYSPEED} Mbps on interface ${REPLAYIFACE}..."
	docker exec so-tcpreplay /usr/bin/bash -c "/usr/local/bin/tcpreplay -i ${REPLAYIFACE} -M${REPLAYSPEED} $@"

	echo "Replay completed. Warnings shown above are typically expected."
elif is_manager_node; then
	echo "The sensor nodes in this grid can now replay traffic."
else
	echo "Unable to replay traffic since this node is not a sensor node."
fi
