Skip to content

Commit b4dc301

Browse files
committed
Add the OptIntRange class
1 parent 30de643 commit b4dc301

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

lib/msf/core/opt_int_range.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# -*- coding: binary -*-
2+
3+
module Msf
4+
###
5+
#
6+
# Integer range option. A maximum value can be specified. Negative numbers are
7+
# not supported due to - being used for ranges. Numbers can be excluded by
8+
# using the ! prefix.
9+
#
10+
###
11+
class OptIntRange < OptBase
12+
attr_reader :maximum
13+
14+
def initialize(in_name, attrs = [],
15+
required: true, **kwargs)
16+
super
17+
@maximum = kwargs.fetch(:maximum, nil)
18+
end
19+
20+
def type
21+
'integer range'
22+
end
23+
24+
def normalize(value)
25+
value.to_s.gsub(/\s/, '')
26+
end
27+
28+
def valid?(value, check_empty: true)
29+
return false if check_empty && empty_required_value?(value)
30+
31+
value = value.to_s.gsub(/\s/, '')
32+
return false unless value =~ /\A(!?\d+|!?\d+-\d+)(,(!?\d+|!?\d+-\d+))*\Z/
33+
34+
super
35+
end
36+
37+
def self.parse(value)
38+
include = []
39+
exclude = []
40+
41+
value.split(',').each do |range_str|
42+
destination = range_str.start_with?('!') ? exclude : include
43+
44+
range_str.delete_prefix!('!')
45+
if range_str.include?('-')
46+
start_range, end_range = range_str.split('-').map(&:to_i)
47+
range = (start_range..end_range)
48+
else
49+
single_value = range_str.to_i
50+
range = (single_value..single_value)
51+
end
52+
53+
destination << range
54+
end
55+
56+
Enumerator.new do |yielder|
57+
include.each do |include_range|
58+
include_range.each do |num|
59+
break if @maximum && num > @maximum
60+
next if exclude.any? { |exclude_range| exclude_range.cover?(num) }
61+
62+
yielder << num
63+
end
64+
end
65+
end
66+
end
67+
end
68+
end

lib/msf/core/option_container.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ module Msf
99
autoload :OptAddress, 'msf/core/opt_address'
1010
autoload :OptAddressLocal, 'msf/core/opt_address_local'
1111
autoload :OptAddressRange, 'msf/core/opt_address_range'
12+
autoload :OptAddressRoot, 'msf/core/opt_address_routable'
1213
autoload :OptBool, 'msf/core/opt_bool'
1314
autoload :OptEnum, 'msf/core/opt_enum'
1415
autoload :OptInt, 'msf/core/opt_int'
16+
autoload :OptIntRange, 'msf/core/opt_int_range'
1517
autoload :OptFloat, 'msf/core/opt_float'
1618
autoload :OptPath, 'msf/core/opt_path'
1719
autoload :OptPort, 'msf/core/opt_port'

0 commit comments

Comments
 (0)