13fa1e53e6
This commit includes several significant changes aimed at improving the functionality, user experience, and code quality of our Ruby project. 1. Decryption Fix: We identified and resolved an issue where the database password was being decrypted multiple times, leading to connection problems. The code was refactored to ensure that decryption only occurs once, thereby enhancing the efficiency and reliability of our database connections. 2. Infinite Loop Resolution: We addressed a critical issue where the program would enter an infinite loop if the .env file was missing or contained incorrect information. The code was updated to handle these situations appropriately, providing meaningful feedback to the user and preventing unnecessary resource consumption. 3. .env File Handling Improvement: We improved the handling of the .env file, ensuring that the program can function correctly even in the absence of this file or if it contains incorrect data. This change enhances the robustness of our application. 4. Curses Alerts Integration: We integrated a new feature to display alerts in the terminal using the Curses library. These alerts can have different severity levels (info, warning, error), which are displayed in different colors (blue, yellow, red). This feature improves the user experience by providing clear and immediate feedback on the program's status. 5. Automatic Alert Dismissal: We implemented a feature where alerts automatically disappear after 5 seconds. This was achieved using Ruby threads, ensuring that the rest of the program is not blocked while the alert is displayed. This change enhances the user experience by preventing the screen from being cluttered with old alerts. 6. Debugging Libraries Exploration: We explored the possibility of using the Tracer and Debug libraries to trace the execution of the program and assist with debugging. While these libraries were not integrated in this commit, they remain a potential resource for future debugging efforts. This commit represents a significant step forward in the development of our Ruby project, improving both the user experience and the quality of our codebase.
100 lines
2.8 KiB
Ruby
100 lines
2.8 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'securerandom'
|
|
require 'digest'
|
|
require 'base64'
|
|
require 'openssl'
|
|
require_relative 'logg_man'
|
|
|
|
# Utiltiies Module
|
|
module Utilities
|
|
# Converts speed from Gbps to Mbps if necessary
|
|
def convert_speed_to_mbps(speed)
|
|
return nil unless speed.is_a?(String) && speed.match?(/\A\d+(gbps|mbps)\z/i)
|
|
|
|
speed.end_with?('gbps') ? speed.to_i * 1000 : speed.to_i
|
|
end
|
|
|
|
# Converts an array of services into a hash
|
|
def services_to_hash(services)
|
|
services_hash = {}
|
|
services.each { |service| services_hash[service] = true }
|
|
services_hash
|
|
end
|
|
|
|
# Calculates total bandwidth from uplink and downlink speeds
|
|
def calculate_total_bandwidth(uplink_speed, downlink_speed)
|
|
uplink_speed + downlink_speed
|
|
end
|
|
|
|
def generate_key
|
|
Base64.encode64(SecureRandom.bytes(32)).chomp
|
|
end
|
|
|
|
def encrypt_string_chacha20(data, key)
|
|
return nil if data.nil? || key.nil?
|
|
|
|
cipher = OpenSSL::Cipher.new('chacha20')
|
|
cipher.encrypt
|
|
cipher.key = Base64.decode64(key) # Decode the key from Base64
|
|
encrypted_data = cipher.update(data) + cipher.final
|
|
|
|
Base64.encode64(encrypted_data).chomp
|
|
rescue OpenSSL::Cipher::CipherError => e
|
|
loggman = LoggMan.new
|
|
loggman.log_error("Failed to encrypt data: #{e.message}")
|
|
nil
|
|
end
|
|
|
|
def decrypt_string_chacha20(encrypted_data, key) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
|
return nil if encrypted_data.nil? || key.nil?
|
|
|
|
cipher = OpenSSL::Cipher.new('chacha20')
|
|
cipher.decrypt
|
|
cipher.key = Base64.decode64(key) # Decode the key from Base64
|
|
decrypted_data = cipher.update(Base64.decode64(encrypted_data)) + cipher.final
|
|
|
|
# Check if the decrypted data is valid ASCII
|
|
decrypted_data.force_encoding('UTF-8')
|
|
if decrypted_data.valid_encoding?
|
|
decrypted_data
|
|
else
|
|
loggman = LoggMan.new
|
|
loggman.log_error("Decrypted data is not valid ASCII: #{decrypted_data.inspect}")
|
|
nil
|
|
end
|
|
rescue OpenSSL::Cipher::CipherError => e
|
|
loggman = LoggMan.new
|
|
loggman.log_error("Failed to decrypt data: #{e.message}")
|
|
nil
|
|
end
|
|
|
|
def display_alert(message, severity) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
|
case severity
|
|
when :info
|
|
Curses.attron(Curses.color_pair(1)) # Blue color
|
|
when :warning
|
|
Curses.attron(Curses.color_pair(3)) # Yellow color
|
|
when :error
|
|
Curses.attron(Curses.color_pair(2)) # Red color
|
|
end
|
|
|
|
Curses.setpos(Curses.lines - 1, 0)
|
|
Curses.addstr(message)
|
|
Curses.refresh
|
|
|
|
Thread.new do
|
|
sleep(5) # Pause for 5 seconds
|
|
|
|
# Clear the alert
|
|
Curses.setpos(Curses.lines - 1, 0)
|
|
Curses.clrtoeol
|
|
Curses.refresh
|
|
end
|
|
|
|
Curses.attroff(Curses.color_pair(1)) if severity == :info
|
|
Curses.attroff(Curses.color_pair(3)) if severity == :warning
|
|
Curses.attroff(Curses.color_pair(2)) if severity == :error
|
|
end
|
|
end
|