CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/ui/text/resource.rb
Views: 11655
1
# -*- coding: binary -*-
2
require 'erb'
3
4
module Rex
5
module Ui
6
module Text
7
8
module Resource
9
10
# Processes a resource script file for the console.
11
#
12
# @param path [String] Path to a resource file to run
13
# @return [void]
14
def load_resource(path)
15
if path == '-'
16
resource_file = $stdin.read
17
path = 'stdin'
18
elsif ::File.exist?(path)
19
resource_file = ::File.read(path)
20
else
21
print_error("Cannot find resource script: #{path}")
22
return
23
end
24
25
# Process ERB directives first
26
print_status "Processing #{path} for ERB directives."
27
erb = ERB.new(resource_file)
28
processed_resource = erb.result(binding)
29
30
lines = processed_resource.each_line.to_a
31
bindings = {}
32
while lines.length > 0
33
34
line = lines.shift
35
break if not line
36
line.strip!
37
next if line.length == 0
38
next if line =~ /^#/
39
40
# Pretty soon, this is going to need an XML parser :)
41
# TODO: case matters for the tag and for binding names
42
if line =~ /<ruby/
43
if line =~ /\s+binding=(?:'(\w+)'|"(\w+)")(>|\s+)/
44
bin = ($~[1] || $~[2])
45
bindings[bin] = binding unless bindings.has_key? bin
46
bin = bindings[bin]
47
else
48
bin = binding
49
end
50
buff = ''
51
while lines.length > 0
52
line = lines.shift
53
break if not line
54
break if line =~ /<\/ruby>/
55
buff << line
56
end
57
if ! buff.empty?
58
print_status("resource (#{path})> Ruby Code (#{buff.length} bytes)")
59
begin
60
eval(buff, bin)
61
rescue ::Interrupt
62
raise $!
63
rescue ::Exception => e
64
print_error("resource (#{path})> Ruby Error: #{e.class} #{e} #{e.backtrace}")
65
end
66
end
67
else
68
print_line("resource (#{path})> #{line}")
69
run_single(line)
70
end
71
end
72
end
73
74
end
75
76
end
77
end
78
end
79
80