Class: Lanet::CLI
- Inherits:
-
Thor
- Object
- Thor
- Lanet::CLI
- Defined in:
- lib/lanet/cli.rb
Class Method Summary collapse
-
.exit_on_failure? ⇒ Boolean
Add this method to silence Thor errors and disable exit on failure.
Instance Method Summary collapse
- #broadcast ⇒ Object
- #keygen ⇒ Object
- #listen ⇒ Object
- #mesh_info ⇒ Object
- #mesh_send ⇒ Object
- #mesh_start ⇒ Object
- #ping(single_host = nil) ⇒ Object
- #receive_file ⇒ Object
- #scan ⇒ Object
- #send ⇒ Object
- #send_file ⇒ Object
- #traceroute(single_host = nil) ⇒ Object
- #version ⇒ Object
Class Method Details
.exit_on_failure? ⇒ Boolean
Add this method to silence Thor errors and disable exit on failure
14 15 16 |
# File 'lib/lanet/cli.rb', line 14 def self.exit_on_failure? false end |
Instance Method Details
#broadcast ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/lanet/cli.rb', line 49 def broadcast sender = Lanet::Sender.new([:port]) private_key = nil if [:private_key_file] begin private_key = File.read([:private_key_file]) puts "Message will be digitally signed" rescue StandardError => e puts "Error reading private key file: #{e.}" return end end = Lanet::Encryptor.([:message], [:key], private_key) sender.broadcast() puts "Message broadcasted to the network" end |
#keygen ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/lanet/cli.rb', line 162 def keygen key_pair = Lanet::Signer.generate_key_pair([:bits]) private_key_file = File.join([:output], "lanet_private.key") public_key_file = File.join([:output], "lanet_public.key") File.write(private_key_file, key_pair[:private_key]) File.write(public_key_file, key_pair[:public_key]) puts "Key pair generated!" puts "Private key saved to: #{private_key_file}" puts "Public key saved to: #{public_key_file}" puts "\nIMPORTANT: Keep your private key secure and never share it." puts "Share your public key with others who need to verify your messages." end |
#listen ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/lanet/cli.rb', line 113 def listen receiver = Lanet::Receiver.new([:port]) public_key = nil if [:public_key_file] begin public_key = File.read([:public_key_file]) puts "Digital signature verification enabled" rescue StandardError => e puts "Error reading public key file: #{e.}" return end end puts "Listening for messages on port #{[:port]}..." puts "Press Ctrl+C to stop" receiver.listen do |data, sender_ip| result = Lanet::Encryptor.(data, [:encryption_key], public_key) puts "\nMessage from #{sender_ip}:" puts "Content: #{result[:content]}" if result.key?(:verified) verification_status = result[:verified] ? "VERIFIED" : "NOT VERIFIED: #{result[:verification_status]}" puts "Signature: #{verification_status}" end puts "-" * 40 end end |
#mesh_info ⇒ Object
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/lanet/cli.rb', line 343 def mesh_info mesh = Lanet::Mesh.new([:port]) # Load state if available mesh.start puts "Mesh Node ID: #{mesh.node_id}" puts "\nConnected nodes:" if mesh.connections.empty? puts " No direct connections" else mesh.connections.each do |node_id, info| last_seen = Time.now.to_i - info[:last_seen] puts " #{node_id} (#{info[:ip]}, last seen #{last_seen}s ago)" end end puts "\nMessage cache: #{mesh..size} messages" mesh.stop end |
#mesh_send ⇒ Object
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/lanet/cli.rb', line 307 def mesh_send mesh = Lanet::Mesh.new([:port]) private_key = nil if [:private_key_file] begin private_key = File.read([:private_key_file]) puts "Message will be digitally signed" rescue StandardError => e puts "Error reading private key file: #{e.}" return end end # Initialize the mesh network mesh.start begin = mesh.( [:target], [:message], [:key], private_key ) puts "Message sent through mesh network" puts "Message ID: #{}" rescue Lanet::Mesh::Error => e puts "Error sending mesh message: #{e.}" ensure mesh.stop end end |
#mesh_start ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/lanet/cli.rb', line 281 def mesh_start mesh = Lanet::Mesh.new([:port], [:max_hops]) puts "Starting mesh network node with ID: #{mesh.node_id}" puts "Listening on port: #{[:port]}" puts "Press Ctrl+C to stop" mesh.start # Keep the process running begin loop do sleep 1 end rescue Interrupt puts "\nStopping mesh network node..." mesh.stop end end |
#ping(single_host = nil) ⇒ Object
152 153 154 155 156 157 |
# File 'lib/lanet/cli.rb', line 152 def ping(single_host = nil) # This is a placeholder for the ping implementation target_host = single_host || [:host] puts "Pinging #{target_host || [:hosts]}..." puts "Ping functionality not implemented yet" end |
#receive_file ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/lanet/cli.rb', line 228 def receive_file public_key = nil if [:public_key_file] begin public_key = File.read([:public_key_file]) puts "Digital signature verification enabled" rescue StandardError => e puts "Error reading public key file: #{e.}" return end end output_dir = File.([:output]) FileUtils.mkdir_p(output_dir) unless Dir.exist?(output_dir) puts "Listening for incoming files on port #{[:port]}..." puts "Files will be saved to #{output_dir}" puts "Press Ctrl+C to stop" file_transfer = Lanet::FileTransfer.new([:port]) begin file_transfer.receive_file( output_dir, [:encryption_key], public_key ) do |event, data| case event when :start puts "\nReceiving file: #{data[:file_name]} from #{data[:sender_ip]}" puts "Size: #{data[:file_size]} bytes" puts "Transfer ID: #{data[:transfer_id]}" when :progress print "\rProgress: #{data[:progress]}% (#{data[:bytes_received]}/#{data[:total_bytes]} bytes)" when :complete puts "\nFile received and saved to: #{data[:file_path]}" when :error puts "\nError during file transfer: #{data[:error]}" end end rescue Interrupt puts "\nFile receiver stopped." end end |
#scan ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/lanet/cli.rb', line 75 def scan scanner = Scanner.new results = scanner.scan( [:range], [:timeout], [:threads], [:verbose] ) puts "\nActive devices:" if [:verbose] results.each do |host| puts "─" * 50 puts "IP: #{host[:ip]}" puts "Hostname: #{host[:hostname]}" if host[:hostname] puts "MAC: #{host[:mac]}" if host[:mac] # Add this line to display MAC addresses puts "Response time: #{host[:response_time]}ms" if host[:response_time] puts "Detection method: #{host[:detection_method]}" if host[:detection_method] next unless host[:ports] && !host[:ports].empty? puts "Open ports:" host[:ports].each do |port, service| puts " - #{port}: #{service}" end end puts "─" * 50 puts "Found #{results.size} active hosts." else results.each { |ip| puts ip } end end |
#send ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/lanet/cli.rb', line 24 def send sender = Lanet::Sender.new([:port]) private_key = nil if [:private_key_file] begin private_key = File.read([:private_key_file]) puts "Message will be digitally signed" rescue StandardError => e puts "Error reading private key file: #{e.}" return end end = Lanet::Encryptor.([:message], [:key], private_key) sender.send_to([:target], ) puts "Message sent to #{[:target]}" end |
#send_file ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/lanet/cli.rb', line 184 def send_file unless File.exist?([:file]) && File.file?([:file]) puts "Error: File not found or is not a regular file: #{[:file]}" return end private_key = nil if [:private_key_file] begin private_key = File.read([:private_key_file]) puts "File will be digitally signed" rescue StandardError => e puts "Error reading private key file: #{e.}" return end end file_transfer = Lanet::FileTransfer.new([:port]) puts "Sending file #{File.basename([:file])} to #{[:target]}..." puts "File size: #{File.size([:file])} bytes" begin file_transfer.send_file( [:target], [:file], [:key], private_key ) do |progress, bytes, total| # Update progress bar print "\rProgress: #{progress}% (#{bytes}/#{total} bytes)" end puts "\nFile sent successfully!" rescue Lanet::FileTransfer::Error => e puts "\nError: #{e.}" end end |
#traceroute(single_host = nil) ⇒ Object
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/lanet/cli.rb', line 372 def traceroute(single_host = nil) # Use the positional parameter if provided, otherwise use the --host option target_host = single_host || [:host] # Ensure we have a host to trace unless target_host puts "Error: No host specified. Please provide a host as an argument or use --host option." return end tracer = Lanet::Traceroute.new( protocol: [:protocol].to_sym, max_hops: [:max_hops], timeout: [:timeout], queries: [:queries] ) puts "Tracing route to #{target_host} using #{[:protocol].upcase} protocol" puts "Maximum hops: #{[:max_hops]}, Timeout: #{[:timeout]}s, Queries: #{[:queries]}" puts "=" * 70 puts format("%3s %-15s %-30s %-10s", "TTL", "IP Address", "Hostname", "Response Time") puts "-" * 70 tracer.trace(target_host).each do |hop| if hop[:ip].nil? puts format("%3d %-15s %-30s %-10s", hop[:ttl], "*", "*", "Request timed out") else hostname = hop[:hostname] || "" time_str = hop[:avg_time] ? "#{hop[:avg_time]}ms" : "*" puts format("%3d %-15s %-30s %-10s", hop[:ttl], hop[:ip], hostname, time_str) # Show all IPs if there are multiple (for load balancing detection) if hop[:all_ips] && hop[:all_ips].size > 1 puts " Multiple IPs detected (possible load balancing):" hop[:all_ips].each do |ip| puts " - #{ip}" end end # Show unreachable marker puts " Destination unreachable" if hop[:unreachable] end end puts "=" * 70 puts "Trace complete." rescue StandardError => e puts "Error performing traceroute: #{e.}" puts e.backtrace if [:verbose] end |