CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/config/boot.rb
Views: 1904
1
require 'pathname'
2
require 'rubygems'
3
4
GEMFILE_EXTENSIONS = [
5
'.local',
6
''
7
]
8
9
msfenv_real_pathname = Pathname.new(__FILE__).realpath
10
root = msfenv_real_pathname.parent.parent
11
12
unless ENV['BUNDLE_GEMFILE']
13
require 'pathname'
14
15
GEMFILE_EXTENSIONS.each do |extension|
16
extension_pathname = root.join("Gemfile#{extension}")
17
18
if extension_pathname.readable?
19
ENV['BUNDLE_GEMFILE'] = extension_pathname.to_path
20
break
21
end
22
end
23
end
24
25
begin
26
require 'bundler/setup'
27
rescue LoadError => e
28
$stderr.puts "[*] Bundler failed to load and returned this error:"
29
$stderr.puts
30
$stderr.puts " '#{e}'"
31
$stderr.puts
32
$stderr.puts "[*] You may need to uninstall or upgrade bundler"
33
exit(1)
34
end
35
36
lib_path = root.join('lib').to_path
37
38
unless $LOAD_PATH.include? lib_path
39
$LOAD_PATH.unshift lib_path
40
end
41
42
require 'digest'
43
require 'metasploit/framework/version'
44
require 'msf/base/config'
45
46
# Invalidate and delete the bootsnap cache if required. For instance if the metasploit-framework version has changed.
47
#
48
# @param [Hash] bootsnap_config See https://github.com/Shopify/bootsnap/blob/95e8d170aea99a831fd484ce09ad2f195644e740/lib/bootsnap.rb#L38
49
# @return [void]
50
def invalidate_bootsnap_cache!(bootsnap_config)
51
expected_cache_metadata = {
52
'metasploit_framework_version' => Metasploit::Framework::Version::VERSION,
53
'ruby_description' => RUBY_DESCRIPTION,
54
'bundler_lockfile_hash' => Digest::MD5.hexdigest(Bundler.read_file(Bundler.default_lockfile)),
55
'bootsnap_config' => {
56
'load_path_cache' => bootsnap_config[:load_path_cache],
57
'compile_cache_iseq' => bootsnap_config[:compile_cache_iseq],
58
'compile_cache_yaml' => bootsnap_config[:compile_cache_yaml],
59
}
60
}
61
62
cache_metadata_path = File.join(bootsnap_config[:cache_dir], "metadata.yaml")
63
if File.exist?(cache_metadata_path)
64
cache_metadata = YAML.safe_load(File.binread(cache_metadata_path))
65
if cache_metadata != expected_cache_metadata
66
FileUtils.rm_rf(bootsnap_config[:cache_dir], secure: true)
67
end
68
end
69
70
FileUtils.mkdir_p(bootsnap_config[:cache_dir])
71
File.binwrite(cache_metadata_path, expected_cache_metadata.to_yaml)
72
73
nil
74
end
75
76
# Attempt to use bootsnap caching for improved startup time
77
begin
78
require 'bootsnap'
79
env = ENV['RAILS_ENV'] || ENV['RACK_ENV'] || ENV['ENV']
80
development_mode = ['', nil, 'development'].include?(env)
81
82
cache_dir = ::File.join(Msf::Config.config_directory, "bootsnap_cache")
83
bootsnap_config = {
84
cache_dir: cache_dir,
85
ignore_directories: [],
86
development_mode: development_mode,
87
load_path_cache: true, # Optimize the LOAD_PATH with a cache
88
compile_cache_iseq: false, # Don't compile Ruby code into ISeq cache, breaks coverage reporting.
89
compile_cache_yaml: false, # Don't compile YAML into a cache
90
readonly: false, # Update caches - https://github.com/Shopify/bootsnap/commit/b51397f96c33aa421fd5c29484fb9574df9eb451
91
}
92
invalidate_bootsnap_cache!(bootsnap_config)
93
Bootsnap.setup(**bootsnap_config)
94
rescue => e
95
$stderr.puts "Warning: Failed bootsnap cache setup - #{e.class} #{e} #{e.backtrace}"
96
begin
97
FileUtils.rm_rf(cache_dir, secure: true)
98
rescue
99
$stderr.puts 'Warning: Failed deleting bootsnap cache'
100
end
101
end
102
103