Webqq/Plugin/Perlcode.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 180 CC 26 SCAR 61.7
CC
26
Cov
0%
call
650.0
BEGIN
6.0
count_lines
2.0
CRAP 478.7
1 package Mojo::Webqq::Plugin::Perlcode;
2 our $PRIORITY = 97;
3 use File::Temp qw/tempfile/;
4 use File::Path qw/mkpath rmtree/;
5 use POSIX qw(strftime);
6 use Term::ANSIColor;
7 use Storable;
8 BEGIN{
9 Storable::nfreeze({}); #这一句是为了 提前加载 auto/Storable/nfreeze.al 防止chroot后再加载导致报错
10 eval{require BSD::Resource};
11 our $is_hold_bsd_resource = 1 unless $@;
12 }
13 sub call{
14 my $client = shift;
15 $client->die(__PACKAGE__ . "只能运行在linux系统上") if $^O !~ /linux/;
16 $client->die(__PACKAGE__ . "依赖BSD::Resource模块,请先安装该模块") if !$is_hold_bsd_resource;
17 my $callback = sub{
18 my($client,$msg) = @_;
19 return if not $msg->allow_plugin;
20 my $content = $msg->content; $content=~s/>/>/g;
21 if($content=~/^(?:>>>)(.*?)(?:__END__|$)/s or $content =~/perl\s+-e\s+'([^']+)'/s){
22 $msg->allow_plugin(0);
23 return if $msg->class eq "send" and $msg->from ne "api" and $msg->from ne "irc";
24 my $doc = '';
25 my $code = $1;
26 $code=~s/^\s+|\s+$//g;
27 $code=~s/CORE:://g;
28 $code=~s/CORE::GLOBAL:://g;
29 return unless $code;
30 $code=~s/(\n^__DATA__\s*?\n(.*?))(?:^__[A-Z]+__|\z)//gms;
31 my $__data__ = $2;
32 if(defined $__data__){
33 unless(open(Mojo::Webqq::Plugin::Perlcode::Sandbox::DATA ,"<",\$__data__)){
34 $client->warn("处理__DATA__出现异常: $!");
35 return
36 }
37 }
38 $code = q#package Mojo::Webqq::Plugin::Perlcode::Sandbox;use feature qw(say);local $|=1;BEGIN{$^W=0;use File::Path;use BSD::Resource;setrlimit(RLIMIT_NOFILE,100,100);setrlimit(RLIMIT_CPU,8,8);setrlimit(RLIMIT_FSIZE,1024,1024);setrlimit(RLIMIT_NPROC,5,5);setrlimit(RLIMIT_STACK,1024*1024*10,1024*1024*10);setrlimit(RLIMIT_DATA,1024*1024*10,1024*1024*10);*CORE::GLOBAL::fork=sub{};}$|=1;{my($u,$g)=((getpwnam("nobody"))[2],(getgrnam("nobody"))[2]);mkpath('/tmp/webqq/bin/',{owner=>$u,group=>$g,mode=>0555}) unless -e '/tmp/webqq/bin';chdir '/tmp/webqq/bin' or die $!;chroot '/tmp/webqq/bin' or die "chroot fail: $!";chdir "/";($(, $))=($g,"$g $g");($<,$>)=($u,$u);}local %ENV=();# . $code;
39 my ($stdout_buf,$stderr_buf,$is_stdout_cut,$is_stderr_cut);
40 my $run;$run = $client->spawn(
41 cmd =>sub{eval $code;print STDERR $@ if $@;},
42 exec_timeout => 3,
43 stdout_cb => sub {
44 my ($pid, $chunk) = @_;
45 $stdout_buf.=$chunk if defined $chunk;
46 if(count_lines($stdout_buf) > 8){
47 $run->kill($pid);
48 $stdout_buf = join "\n",(split /\r?\n/,$stdout_buf,11)[0..9];
49 $stdout_buf .= "(已截断)";
50 }
51 elsif(length($stdout_buf) > 200){
52 $run->kill($pid);
53 $stdout_buf = substr($stdout_buf,0,200);
54 $stdout_buf .= "(已截断)";
55 }
56 },
57 stderr_cb => sub {
58 my ($pid, $chunk) = @_;
59 $stderr_buf.=$chunk if defined $chunk;
60 if(count_lines($stderr_buf) > 8){
61 $run->kill($pid);
62 $stderr_buf = join "\n",(split /\r?\n/,$stderr_buf,11)[0..9];
63 $stderr_buf .= "(已截断)";
64 }
65 elsif(length($stderr_buf) > 350){
66 $run->kill($pid);
67 $stderr_buf = substr($stderr_buf,0,500);
68 $stderr_buf .= "(已截断)";
69 }
70 },
71 exit_cb => sub {
72 my($pid,$res)=@_;
73 my $content;
74 if(defined $stderr_buf){
75 $stderr_buf =~s/(?<=at )\(eval .+?\)(?= line)/CODE/g;
76 $stderr_buf =~s/Mojo::Webqq::Plugin::Perlcode::Sandbox:://g;
77 }
78 $stderr_buf.= "(执行超时)" if $res->{is_timeout};
79 eval{
80 $stderr_buf = Term::ANSIColor::colorstrip($stderr_buf) if defined $stderr_buf;
81 $stdout_buf = Term::ANSIColor::colorstrip($stdout_buf) if defined $stdout_buf;
82 };
83 if(defined $stdout_buf and $stderr_buf){
84 if($stdout_buf=~/\n$/){$content = $stdout_buf.$stderr_buf}
85 else{$content = $stdout_buf."\n".$stderr_buf}
86 }
87 elsif(defined $stdout_buf){$content=$stdout_buf}
88 elsif(defined $stderr_buf){$content=$stderr_buf}
89 $content = "代码打印内容为空" if ( !defined $content or $content eq "");
90 $client->reply_message($msg,$content);
91 },
92 );
93 }
94 };
95 $client->on(receive_message=>$callback);
96 $client->on(send_message=>$callback);
97 }
98  
99 sub count_lines{
100 my $data = shift;
101 my $count =()=$data=~/\r?\n/g;
102 return $count++;
103 }
104  
105 1;