NETRAVE/lib/utils/packet_capture.rb

48 lines
1.4 KiB
Ruby
Raw Normal View History

Implement Initial System Setup and Packet Capture 1. Initial System Setup: - Implemented a first run initialization process that guides the user through setting up the necessary environment variables. - Created a method to securely ask for the user's sudo password, test it, and store it in an encrypted form in an environment variable for use during the first run setup process. - Added a method to clear the sudo password from memory and the environment variables at the end of the first run setup process. 2. Packet Capture: - Created a PacketCapture class that uses the PCAPRUB library to capture packets from a specified network interface. - Refactored the packet capture process to add each captured packet to a Redis queue for further processing, instead of processing the packets directly. - Removed the manual packet dissection from the packet capture process, as this will be handled by the workers. 3. Networking Setup: - Created a NetworkingGenie class to handle the setup of the necessary networking components. - Added methods to identify the main network interface, create a dummy network interface, and set up traffic mirroring from the main interface to the dummy interface. 4. Logging: - Implemented logging for all major actions and errors throughout the system. 5. General Refactoring and Code Cleanup: - Refactored and cleaned up various parts of the code to improve readability and maintainability. - Fixed various minor bugs and issues. This commit lays the groundwork for the packet processing workers and the orchestrator that will manage them. The next steps will be to implement these components and integrate them with the existing system.
2023-06-29 22:36:18 -06:00
# frozen_string_literal: true
require 'pcaprub'
require 'socket'
require_relative 'databasemanager'
require_relative 'logg_man'
require_relative 'redis_queue'
# Class used to capture packets and not much else
class PacketCapture
INTERFACE_NAME = 'netrave0'
def initialize(queue, logger)
@loggman = logger
@loggman.log_info("Initializing packet capture for #{INTERFACE_NAME}...")
@capture = Pcap.open_live(INTERFACE_NAME, 65_535, true, 1)
@capture.setfilter('')
@loggman.log_info('Packet capture initialized successfully!')
@queue = queue
end
def start_capture_loop # rubocop:disable Metrics/MethodLength
@loggman.log_info("Starting packet capture loop for #{@interface}...")
packet_count = 0
begin
@loggman.log_info("Packet capture loop started for #{@interface}...")
@capture.each_packet do |packet|
# Add packet to queue
@queue.push(packet)
@loggman.log_info("Packet #{packet_count += 1} added to queue.")
end
rescue StopIteration
@loggman.log_warn("Packet capture loop stopped for #{@interface}.")
rescue StandardError => e
@loggman.log_fatal("Packet capture loop stopped for #{@interface}: #{e.message}\n#{e.backtrace}", false)
sleep 1
retry
ensure
@capture.close
end
end
def stop_capture
@loggman.log_warn("Stopping packet capture loop for #{@interface}...")
@stop_flag = true
end
end