54d348c99f
This commit introduces several significant enhancements to the way commands are executed and logged in the application. The changes are primarily focused on improving the robustness, reliability, and transparency of the command execution process, as well as enhancing the quality and usefulness of the log output. 1. Command Execution Enhancements: The use_sudo method has been refactored to handle commands that do not return any output. Previously, the method was designed to capture and return the output of the command being executed. However, some commands (such as modprobe) do not return any output, which caused issues with the previous implementation. The method now checks the exit status of the command to determine whether it was successful or not, and returns a success or failure message accordingly. This change improves the robustness of the command execution process and ensures that it can handle a wider range of commands. 2. Error Handling Improvements: The use_sudo method now includes more comprehensive error handling. If a command fails to execute within a specified timeout period, an error message is logged and the method returns a failure message. Additionally, if a command fails to execute for any other reason, the method logs the error and returns a failure message with the command's exit status. These changes make it easier to identify and troubleshoot issues with command execution. 3. Logging Enhancements: The logging mechanism has been enhanced to provide more detailed and useful information. The use_sudo method now logs the command being executed and its outcome (success or failure). If a command fails, the method logs the command's exit status. These changes improve the transparency of the command execution process and make it easier to identify and troubleshoot issues. 4. Code Refactoring: Several methods have been refactored for improved readability and maintainability. The use_sudo method has been refactored to reduce its complexity and improve its readability. The first_run_setup method has been refactored to ensure that the main interface name and the dummy interface name are properly passed to the setup_traffic_mirroring method. 5. Bug Fixes: A bug in the create_dummy_interface method that caused it to return an array of Alert objects instead of the dummy interface name has been fixed. The method now correctly returns the dummy interface name. These changes represent a significant improvement to the command execution and logging mechanisms in the application, and lay the groundwork for further enhancements in the future.
83 lines
2.8 KiB
Ruby
83 lines
2.8 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'English'
|
|
require 'socket'
|
|
require_relative 'logg_man'
|
|
require_relative 'alert_manager'
|
|
|
|
# The class for setting up all the necessary system networking stuff for NETRAVE to work with without
|
|
# interferring with the rest of the system
|
|
class NetworkingGenie
|
|
attr_accessor :main_interface, :dummy_interface
|
|
|
|
include Utilities
|
|
|
|
def initialize(logger, alert_queue_manager)
|
|
@loggman = logger
|
|
@alert_queue_manager = alert_queue_manager
|
|
end
|
|
|
|
def find_main_interface # rubocop:disable Metrics/MethodLength
|
|
alert = Alert.new('Identifying the main network interface...', :info)
|
|
@alert_queue_manager.enqueue_alert(alert)
|
|
@loggman.log_info('Identifying main network interface...')
|
|
route_info = `routel`.split("\n")
|
|
default_route = route_info.find { |line| line.include?('default') }
|
|
if default_route
|
|
main_interface = default_route.split.last
|
|
@loggman.log_info("Main network interface identified as: #{main_interface}")
|
|
alert = Alert.new("Main network interface identified as: #{main_interface}", :info)
|
|
@alert_queue_manager.enqueue_alert(alert)
|
|
main_interface
|
|
else
|
|
@loggman.log_error('Failed to identify main network interface.')
|
|
nil
|
|
end
|
|
rescue StandardError => e
|
|
@loggman.log_error("Error occurred while identifying main network interface: #{e.message}")
|
|
nil
|
|
end
|
|
|
|
def create_dummy_interface(interface_name = 'netrave0') # rubocop:disable Metrics/MethodLength
|
|
alert = Alert.new('Creating the NETRAVE dummy interface...', :info)
|
|
@alert_queue_manager.enqueue_alert(alert)
|
|
# Check if the dummy module is loaded
|
|
use_sudo('modprobe dummy')
|
|
|
|
# Check if the interface already exists
|
|
if `ip link show #{interface_name}`.empty?
|
|
# Create the dummy interface
|
|
use_sudo("ip link add #{interface_name} type dummy")
|
|
@dummy_interface = interface_name
|
|
# Set the interface up
|
|
use_sudo("ip link set #{interface_name} up")
|
|
else
|
|
@loggman.log_info("Interface #{interface_name} already exists.")
|
|
alert = Alert.new("Interface #{interface_name} already exists.", :info)
|
|
@alert_queue_manager.enqueue_alert(alert)
|
|
end
|
|
|
|
# Return the name of the dummy interface
|
|
interface_name
|
|
end
|
|
|
|
def setup_traffic_mirroring(main_interface, dummy_interface) # rubocop:disable Metrics/MethodLength
|
|
commands = [
|
|
"tc qdisc del dev #{main_interface} ingress",
|
|
"tc qdisc add dev #{main_interface} handle ffff: ingress",
|
|
"tc filter add dev #{main_interface} parent ffff: u32 match " \
|
|
"u32 0 0 action mirred egress mirror dev #{dummy_interface}"
|
|
]
|
|
|
|
begin
|
|
commands.each do |command|
|
|
use_sudo(command)
|
|
end
|
|
rescue StandardError => e
|
|
@loggman.log_error(e.message)
|
|
alert = Alert.new(e.message, :error)
|
|
@alert_queue_manager.enqueue_alert(alert)
|
|
end
|
|
end
|
|
end
|