Class: Onlylogs::LogsChannel

Inherits:
ActionCable::Channel::Base
  • Object
show all
Defined in:
app/channels/onlylogs/logs_channel.rb

Instance Method Summary collapse

Instance Method Details

#initialize_watcher(data) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/channels/onlylogs/logs_channel.rb', line 11

def initialize_watcher(data)
  cleanup_existing_operations

  # Decrypt and verify the file path
  begin
    encrypted_file_path = data["file_path"]
    if encrypted_file_path.present?
      file_path = Onlylogs::SecureFilePath.decrypt(encrypted_file_path)

      # Verify the decrypted path is still allowed
      unless Onlylogs.allowed_file_path?(file_path)
        Rails.logger.error "Onlylogs: Attempted to access non-allowed file: #{file_path}"
        transmit({ action: "error", content: "Access denied" })
        return
      end
    else
      # Fallback to default if no encrypted path provided
      file_path = Onlylogs.default_log_file_path
    end
  rescue Onlylogs::SecureFilePath::SecurityError => e
    Rails.logger.error "Onlylogs: Security violation - #{e.message}"
    transmit({ action: "error", content: "Access denied" })
    return
  end

  # Check if the file is a text file
  unless Onlylogs::File.text_file?(file_path)
    transmit({ action: "error", content: "Cannot read file: File is not a text file" })
    return
  end

  cursor_position = data["cursor_position"] || 0
  filter = data["filter"].presence
  mode = data["mode"] || "live"
  regexp_mode = data["regexp_mode"] == true || data["regexp_mode"] == "true"
  start_position = data["start_position"]&.to_i || 0
  end_position = data["end_position"]&.to_i

  if mode == "search"
    # For search mode, read the entire file with filter and send all matching lines
    read_entire_file_with_filter(file_path, filter, regexp_mode, start_position, end_position)
  else
    # For live mode, start the watcher
    start_log_watcher(file_path, cursor_position, filter, regexp_mode)
  end
end

#stop_watcherObject



58
59
60
61
# File 'app/channels/onlylogs/logs_channel.rb', line 58

def stop_watcher
  cleanup_existing_operations
  transmit({ action: "finish", content: "Search stopped." })
end

#subscribedObject



5
6
7
8
9
# File 'app/channels/onlylogs/logs_channel.rb', line 5

def subscribed
  # Rails.logger.info "Client subscribed to Onlylogs::LogsChannel"
  # Wait for the client to send the cursor position
  # start_log_watcher will be called from the initialize_watcher method
end

#unsubscribedObject



63
64
65
# File 'app/channels/onlylogs/logs_channel.rb', line 63

def unsubscribed
  cleanup_existing_operations
end