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



9
10
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
57
58
59
60
61
62
# File 'app/channels/onlylogs/logs_channel.rb', line 9

def initialize_watcher(data)
  # Prevent duplicate calls with identical parameters
  if @last_initialize_params == data
    Rails.logger.info "Onlylogs: Ignoring duplicate initialize_watcher call"
    return
  end

  @last_initialize_params = data.dup
  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.file_path_permitted?(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

  filter = data["filter"].presence
  mode = data["mode"] || "live"
  regexp_mode = data["regexp_mode"] == true || data["regexp_mode"] == "true"

  file_size = ::File.size(file_path)
  start_position = (data["start_position"]&.to_i || 0).clamp(0, file_size)
  end_position = data["end_position"]&.to_i&.clamp(0, file_size) if data["end_position"]

  if mode == "static"
    # Read the entire file with filter and send all matching lines
    read_static(file_path, filter, regexp_mode, start_position, end_position)
  else
    # Follow the tail of the file indefinitely
    start_log_watcher(file_path, filter, regexp_mode)
  end
end

#stop_watcherObject



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

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

#subscribedObject



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

def subscribed
  @last_initialize_params = nil
end

#unsubscribedObject



69
70
71
# File 'app/channels/onlylogs/logs_channel.rb', line 69

def unsubscribed
  cleanup_existing_operations
end