netgrep 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/exe/netgrep +3 -2
- data/lib/netgrep/ip.rb +11 -1
- data/lib/netgrep/netblocks.rb +9 -1
- data/lib/netgrep/options.rb +5 -0
- data/lib/netgrep/runner.rb +12 -5
- data/lib/netgrep/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7598247777cb8eb177816550f6c0f45a5e91b1b3
|
4
|
+
data.tar.gz: d765b270e0544047276980b4eba7c8e4adaad331
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a5aae3ccd8ccefa5039714cf19875202c21e29973442d6f19ebbd5fba3cb7bd77bc19f322a86d88c6174eea141f25feb1adbfd84eb5367f8930cb4bc24e0436
|
7
|
+
data.tar.gz: 9da055d3c4866676e1fb6436e7f26703fb7788a977e0bea3d327789b2e6313136cc51cd8cc947276d1178bddbc9f311d39771d519c8102e476556f35c2422682
|
data/exe/netgrep
CHANGED
@@ -11,11 +11,12 @@ begin
|
|
11
11
|
runner = Netgrep::Runner.new(
|
12
12
|
log_file: opts.log_file,
|
13
13
|
netblocks_file: opts.netblocks_file,
|
14
|
-
num_workers: opts.num_workers
|
14
|
+
num_workers: opts.num_workers,
|
15
|
+
ignore_ipv6: opts.ignore_ipv6
|
15
16
|
)
|
16
17
|
runner.run
|
17
18
|
rescue Errno::ENOENT => err
|
18
19
|
abort "netgrep: #{err.message}"
|
19
20
|
rescue OptionParser::InvalidOption, OptionParser::InvalidArgument
|
20
|
-
abort "usage: netgrep [-w NUM_WORKERS] -f [NETBLOCKS_FILE] [LOG_FILE]"
|
21
|
+
abort "usage: netgrep [-w NUM_WORKERS] -f [NETBLOCKS_FILE] [--ignore-ipv6] [LOG_FILE]"
|
21
22
|
end
|
data/lib/netgrep/ip.rb
CHANGED
@@ -46,6 +46,16 @@ module Netgrep
|
|
46
46
|
end
|
47
47
|
|
48
48
|
module Ip
|
49
|
-
|
49
|
+
IPV4_REGEX = /\b(#{Netgrep::Ipv4::REGEX})\b/xi
|
50
|
+
IPV6_REGEX = /\b(#{Netgrep::Ipv6::REGEX})\b/xi
|
51
|
+
REGEX = /\b(#{Netgrep::Ipv4::REGEX}|#{Netgrep::Ipv6::REGEX})\b/xi
|
52
|
+
|
53
|
+
def self.regex(ignore_ipv6)
|
54
|
+
if ignore_ipv6
|
55
|
+
IPV4_REGEX
|
56
|
+
else
|
57
|
+
REGEX
|
58
|
+
end
|
59
|
+
end
|
50
60
|
end
|
51
61
|
end
|
data/lib/netgrep/netblocks.rb
CHANGED
@@ -6,6 +6,7 @@ module Netgrep
|
|
6
6
|
|
7
7
|
def initialize(file: nil, netblocks: [])
|
8
8
|
@netblocks = file ? File.readlines(file) : netblocks
|
9
|
+
@has_ipv6 = false
|
9
10
|
end
|
10
11
|
|
11
12
|
def include?(ip)
|
@@ -13,6 +14,10 @@ module Netgrep
|
|
13
14
|
ipv6_addresses.any? { |ipv6| ipv6.include?(ip) }
|
14
15
|
end
|
15
16
|
|
17
|
+
def has_ipv6?
|
18
|
+
ipv6_addresses && @has_ipv6
|
19
|
+
end
|
20
|
+
|
16
21
|
private
|
17
22
|
|
18
23
|
def ipv4_addresses
|
@@ -32,7 +37,10 @@ module Netgrep
|
|
32
37
|
@ipv6_addresses ||= begin
|
33
38
|
@netblocks.each_with_object([]) do |netblock, ips|
|
34
39
|
ip_addr = init_ip(netblock)
|
35
|
-
|
40
|
+
if ip_addr.ipv6?
|
41
|
+
ips.push(ip_addr)
|
42
|
+
@has_ipv6 = true
|
43
|
+
end
|
36
44
|
end
|
37
45
|
end
|
38
46
|
end
|
data/lib/netgrep/options.rb
CHANGED
@@ -6,12 +6,17 @@ module Netgrep
|
|
6
6
|
options.netblocks_file = nil
|
7
7
|
options.log_file = nil
|
8
8
|
options.num_workers = 1
|
9
|
+
options.ignore_ipv6 = false
|
9
10
|
|
10
11
|
parser = OptionParser.new do |opts|
|
11
12
|
opts.on("-f NETBLOCKS_FILE", String, "File containing one IP or CIDR per line") do |file_name|
|
12
13
|
options.netblocks_file = File.absolute_path(file_name)
|
13
14
|
end
|
14
15
|
|
16
|
+
opts.on("--ignore-ipv6", "Don't try to match IPV6 Addresses") do
|
17
|
+
options.ignore_ipv6 = true
|
18
|
+
end
|
19
|
+
|
15
20
|
opts.on("-w NUM_WORKERS", Integer, "Number of worker processes to use") do |n|
|
16
21
|
options.num_workers = Integer(n)
|
17
22
|
raise(OptionParser::InvalidArgument) if options.num_workers < 1
|
data/lib/netgrep/runner.rb
CHANGED
@@ -5,10 +5,11 @@ module Netgrep
|
|
5
5
|
class Runner
|
6
6
|
attr_accessor :matches
|
7
7
|
|
8
|
-
def initialize(log_file:, netblocks_file:, num_workers: 1)
|
8
|
+
def initialize(log_file:, netblocks_file:, num_workers: 1, ignore_ipv6: false)
|
9
9
|
@log_file = log_file
|
10
10
|
@netblocks_file = netblocks_file
|
11
11
|
@num_workers = num_workers
|
12
|
+
@ignore_ipv6 = ignore_ipv6?(ignore_ipv6)
|
12
13
|
@matches = []
|
13
14
|
@worker_pids = []
|
14
15
|
@pipe_reader, @pipe_writer = IO.pipe
|
@@ -41,6 +42,11 @@ module Netgrep
|
|
41
42
|
@pipe_writer.close
|
42
43
|
end
|
43
44
|
|
45
|
+
def ignore_ipv6?(ignore_ipv6)
|
46
|
+
return true if ignore_ipv6
|
47
|
+
netblocks.has_ipv6? ? false : true
|
48
|
+
end
|
49
|
+
|
44
50
|
def calc_line_ranges(num_lines, num_ranges)
|
45
51
|
ranges = []
|
46
52
|
range_length = num_lines / num_ranges
|
@@ -54,11 +60,10 @@ module Netgrep
|
|
54
60
|
end
|
55
61
|
|
56
62
|
def select_data_in_range(fd, first_lineno, last_lineno)
|
57
|
-
lineno = first_lineno
|
58
63
|
data = []
|
59
|
-
while
|
64
|
+
while first_lineno <= last_lineno
|
60
65
|
data.push(fd.gets)
|
61
|
-
|
66
|
+
first_lineno += 1
|
62
67
|
end
|
63
68
|
data
|
64
69
|
end
|
@@ -85,8 +90,10 @@ module Netgrep
|
|
85
90
|
end
|
86
91
|
|
87
92
|
def start_worker(writer, netblocks, data)
|
93
|
+
regex = Netgrep::Ip.regex(@ignore_ipv6)
|
94
|
+
|
88
95
|
data.each do |line|
|
89
|
-
line.scan(
|
96
|
+
line.scan(regex).each do |md|
|
90
97
|
ip = md[0]
|
91
98
|
if netblocks.include?(ip)
|
92
99
|
writer.write(line)
|
data/lib/netgrep/version.rb
CHANGED