-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLKR.pl
259 lines (241 loc) · 5.36 KB
/
BLKR.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/local/bin/perl
use strict; use warnings;
use Archive::Tar;
use File::Path; use File::Copy;
use Digest::SHA qw(sha256_hex); use File::Find::Rule;
use File::stat; use List::Util qw(any);
######################################################
# DEMON - daemon summoning scroll
# INIT ###############################################
my ($que, $path) = @ARGV;
if (not defined $que) { die ('NO ARGV1 que'); }
if (not defined $path) { die ('NO ARGV2 dir'); }
if (substr($path, -1) ne "/")
{ $path .= '/'; }
# DIRS ###############################################
# sea/ : blkr()
# key/ : key()
# graveyard/ : tombstone()
# g/ : XS()
# pool/ : XS()
print "$$\n";
# PREP ###############################################
my $name = name();
chdir('/tmp/');
my $RATE = '100';
my $size = 9968;
my $count = 0;
my $dump = "$name"."_dump/";
my $log = "$name"."_log";
my $SLEEP = "$name"."_SLEEP";
my $SUICIDE = "$name"."_SUICIDE";
mkdir $dump or die "dump FAIL\n";
open(my $Lfh, '>>', $log);
my $born = gmtime();
my $btime = TIME();
print $Lfh "HELLOWORLD $btime\n";
# WORK ################################################
open(my $qfh, '<', $que) or die "cant open que\n";
my @QUE = readline $qfh; chomp @QUE;
my $ttl = @QUE;
print $Lfh "ttl $ttl\n";
foreach my $i (@QUE)
{
if (-e $SUICIDE)
{ SUICIDE(); }
if (-e $SLEEP)
{ SLEEP(); }
print $Lfh "started $i\n";
blkr($i);
$count++;
if ($count % 100 == 0)
{ print $Lfh "$count : $ttl\n"; }
}
my $dtime = TIME(); print $Lfh "FKTHEWRLD $dtime\n";
#tombstone();
# dumpr();
# SUB ###########################################################
sub dumpr
{
my ($i) = @_;
XS($i, "$path".'pool/');
remove_tree($dump);
}
sub rep
{
my $rep = "$name"."_rep";
my @files = File::Find::Rule->file->in($dump);
open(my $rfp, '>', $rep);
print $rfp @files;
return $rep;
}
sub tombstone
{
my $xxtime = TIME(); print $Lfh "farewell $xxtime\n";
my $tombstone = 'graveyard/'."$name".'.tar';
my $tar = Archive::Tar->new;
$tar->write($tombstone);
my $rep = rep();
$tar->add_files($log, $rep);
}
sub SUICIDE
{
unlink $SUICIDE;
my $xtime = TIME(); print $Lfh "FKTHEWORLD $xtime\n";
exit;
}
sub SLEEP
{
open(my $Sfh, '<', $SLEEP);
my $timeout = readline $Sfh; chomp $timeout;
my $ztime = TIME(); print $Lfh "sleep $ztime $timeout\n";
close $Sfh; unlink $SLEEP;
sleep $timeout;
}
sub TIME
{
my $t = localtime;
my $mon = (split(/\s+/, $t))[1];
my $day = (split(/\s+/, $t))[2];
my $hour = (split(/\s+/, $t))[3];
my $time = $mon.'_'.$day.'_'.$hour;
return $time;
}
sub name
{
my $id = int(rand(999));
my $name = $$.'_'.$id;
return $name;
}
sub XS
{
my ($i) = @_;
my $rule = File::Find::Rule->file()->start($i);
my $magic = File::LibMagic->new();
while (defined( my $file = $rule->match))
{
my ($sha) = file_digest($file) or die "couldn't sha $file";
File::Copy::copy($file, "$dump/pool/$sha");
my $cur = "$path/g/g$sha";
open(my $fh, '>>', $cur) or die "Meta File Creation FAIL $file";
printf $fh "%s\n%s\n%s\n%s\n",
xsname($file),
xspath($file),
xssize($file),
file_mime_encoding($file);
}
}
sub file_digest {
my ($filename) = @_;
my $digester = Digest::SHA->new('sha256');
$digester->addfile( $filename, 'b' );
return $digester->hexdigest;
}
sub xsname {
my ($filename) = @_;
$filename =~ s#^.*/##;
return $filename;
}
sub xspath {
my ($filename) = @_;
$filename =~ s#/#_#g;
return $filename;
}
sub file_mime_encoding {
my ($filename) = @_;
my $magic = File::Libmagic->new();
my $info = $magic->info_from_filename($filename);
my $des = $info->{description};
$des =~ s#[/ ]#.#g;
$des =~ s/,/_/g;
my $md = $info->{mime_type};
$md =~ s#[/ ]#.#g;
my $enc = sprintf("%s %s %s", $des, $md, $info->{encoding});
return $enc;
}
sub xssize {
my $size = [ stat $_[0] ]->[7];
return $size;
}
sub uagent
{
my $s_ua = LWP::UserAgent->new(
agent => "Mozilla/50.0.2",
from => 'punxnotdead',
timeout => 45,
);
return $s_ua;
}
# API ###########################################################
sub blkr
{
my ($i) = @_;
my $block = 0;
open(my $ifh, '<', "/otto/pool/$i") || die "Cant open $i: $!\n";
binmode($ifh);
while (read($ifh, $block, $size))
{
my $bsha = sha256_hex($block);
my $bname = $path.'sea/'.$bsha;
open(my $fh, '>', "$bname");
binmode($fh);
print $fh $block;
key($i, $bsha);
}
print $Lfh "YAY $i\n";
}
sub key
{
my ($i, $bsha) = @_;
my $kpath = $path.'key/'.$i;
open(my $kfh, '>>', "$kpath");
print $kfh "$bsha\n";
}
sub build
{
my ($i) = @_;
my $kpath = $path.'key/'.$i;
open(my $kfh, '<', $kpath);
my @set = readline $kfh; chomp @set;
foreach my $part (@set)
{
my $tpath = $dump.$i;
print "$path\n";
my $ipath = $path.'sea/'.$part;
open(my $tfh, '>>', "$tpath") or die"bad $tpath";
open(my $ifh, '<', "$ipath");
my $block;
read($ifh, $block, $size);
print $tfh $block;
# DEBUG
# my $bsha = sha256_hex($block);
# if ($i ne $bsha)
# { print $Lfh "SHAERR $i ne $bsha"; }
}
}
sub vsha
{
my ($i) = @_;
my ($sha) = file_digest($i);
if ($sha ne $i)
{ print $Lfh "ERK! $i ne $sha\n"; }
print $Lfh "YAY $i\n";
}
sub xtrac
{
my ($i) = @_;
my $archive = Archive::Any->new($i);
if ($archive->is_naughty)
{ print $Lfh "ALERT xtrac naughty $i"; next; }
my @files = $archive->files; print $Lfh @files;
$archive->extract($dump);
XS($dump, $path);
print $Lfh "YAY $i\n";
}
sub get
{
my ($i) = @_;
my $ua = uagent();
my $response = $ua->get($i, ':content_file'=>"$dump/$i");
print $Lfh "YAY $i\n";
}