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)
if @last_initialize_params == data
Rails.logger.info "Onlylogs: Ignoring duplicate initialize_watcher call"
return
end
@last_initialize_params = data.dup
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.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
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
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_static(file_path, filter, regexp_mode, start_position, end_position)
else
start_log_watcher(file_path, filter, regexp_mode)
end
end
|