-
Notifications
You must be signed in to change notification settings - Fork 0
/
forbid.pl
executable file
·114 lines (96 loc) · 2.77 KB
/
forbid.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
# cpan JSON
use JSON;
my %forbid_record;
my $file = $ARGV[0];
sub say {print @_, "\n"}
sub to_str {
my $ans = "";
my $split = "";
foreach (@_) {
$ans .= $split .= $_;
$split = ":";
}
return $ans;
}
sub filter_forbid {
my ($key, $endtime) = @_;
if ( exists($forbid_record{$key}) ) {
if ($endtime == 0) {
delete $forbid_record{$key};
} elsif ($forbid_record{$key} < $endtime) {
$forbid_record{$key} = $endtime;
}
} else {
my $cur_time = time();
if ($endtime != 0 and $endtime > $cur_time ) {
$forbid_record{$key} = $endtime;
}
}
}
sub dump_json_file {
my ($file) = @_;
open my $fh, ">", $file or die "Counld open $file: $!";
print $fh encode_json \%forbid_record;
close $fh;
}
open my $info, $file or die "Could not open $file: $!";
while( my $line = <$info> ) {
my $domain = "";
my $app = "all";
my $stream = "all";
my $end_time = "";
if ($line =~ "/stream_forbid/domain") {
# filter domain
if ($line =~ "domain=(?<domain>[^&]*)") {
$domain = $+{domain};
# filter end_time
if ($line =~ "end_time=(?<end_time>\\d+)") {
$end_time = $+{end_time};
# handle
filter_forbid(to_str($domain, $app, $stream), $end_time);
}
}
} elsif($line =~ "/stream_forbid/app") {
# filter domain
if ($line =~ "domain=(?<domain>[^&]*)") {
$domain = $+{domain};
# filter app
if ($line =~ "app=(?<app>[^&]*)") {
$app = $+{app};
# filter end_time
if ($line =~ "end_time=(?<end_time>\\d+)") {
$end_time = $+{end_time};
# handle
filter_forbid(to_str($domain, $app, $stream), $end_time);
}
}
}
} elsif($line =~ "/stream_forbid/stream") {
# filter domain
if ($line =~ "domain=(?<domain>[^&]*)") {
$domain = $+{domain};
# filter app
if ($line =~ "app=(?<app>[^&]*)") {
$app = $+{app};
# filter stream
if ($line =~ "stream=(?<stream>[^&]*)") {
$stream = $+{stream};
# filter end_time
if ($line =~ "end_time=(?<end_time>\\d+)") {
$end_time = $+{end_time};
# handle
filter_forbid(to_str($domain, $app, $stream), $end_time);
}
}
}
}
} else {
say $line
}
}
dump_json_file("/tmp/forbid.json");
close $info;