forked from typetools/sparta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter-flows.pl
executable file
·72 lines (59 loc) · 1.61 KB
/
filter-flows.pl
1
2
3
4
5
6
7
8
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
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env perl
use warnings;
use strict;
use Getopt::Long;
my $filter = '';
my $flow_file = 'forbiddenFlowLocations.txt';
GetOptions('filter:s' => \$filter, 'flow-file:s' => \$flow_file);
sub parse_flow {
if (length($filter) == 0) {
return ([], [], "");
}
$filter = shift;
$filter =~ s/\s//g;
$filter =~ /(.*)(->|~>)(.*)/ or die "Could not parse match string: $filter";
my $source = $1;
my $type = $2;
my $sink = $3;
my @sources = ();
my @sinks = ();
if (length($source) > 0) {
@sources = split(',', $source);
}
if (length($sink) > 0) {
@sinks = split(',', $sink);
}
return (\@sources, \@sinks, $type);
}
my ($source_ref, $sink_ref, $type) = parse_flow($filter);
my @sources = @$source_ref;
my @sinks = @$sink_ref;
print "Sources: @sources sinks: @sinks\n";
open(my $fh, '<', $flow_file) or die "Cannot open $flow_file: $!";
# Code snippets can span multiple lines, use this var to indicate
# that we should keep printing.
my $print = 0;
lines: while (<$fh>) {
my @parts = split("##", $_);
if ($#parts < 1) {
if ($print) { print $_ };
next;
}
my ($flow_source_ref, $flow_sink_ref, $type) = parse_flow($parts[0]);
my @flow_sources = @$flow_source_ref;
my @flow_sinks = @$flow_sink_ref;
for my $sink (@sinks) {
if (not grep (/$sink/, @flow_sinks)) {
$print = 0;
next lines;
}
}
for my $source (@sources) {
if (not grep (/$source/, @flow_sources)) {
$print = 0;
next lines;
}
}
$print = 1;
print $_;
}