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
begin
encrypted_file_path = data["file_path"]
if encrypted_file_path.present?
file_path = Onlylogs::SecureFilePath.decrypt(encrypted_file_path)
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
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
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"
read_entire_file_with_filter(file_path, filter, regexp_mode, start_position, end_position)
else
start_log_watcher(file_path, cursor_position, filter, regexp_mode)
end
end
|