| 1 | package Mojo::Weixin::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 | BEGIN{ | ||
| 8 | eval{require BSD::Resource}; | ||
| 9 | our $is_hold_bsd_resource = 1 unless $@; | ||
| 10 | } | ||
| 11 | sub call{ | ||
| 12 | my $client = shift; | ||
| 13 | $client->die(__PACKAGE__ . "只能运行在linux系统上") if $^O !~ /linux/; | ||
| 14 | $client->die(__PACKAGE__ . "依赖BSD::Resource模块,请先安装该模块") if !$is_hold_bsd_resource; | ||
| 15 | my $callback = sub{ | ||
| 16 | my($client,$msg) = @_; | ||
| 17 | return if not $msg->allow_plugin; | ||
| 18 | if($msg->content=~/^(?:>>>)(.*?)(?:__END__|$)/s or $msg->content =~/perl\s+-e\s+'([^']+)'/s){ | ||
| 19 | $msg->allow_plugin(0); | ||
| 20 | return if $msg->from eq "bot"; | ||
| 21 | my $doc = ''; | ||
| 22 | my $code = $1; | ||
| 23 | $code=~s/^\s+|\s+$//g; | ||
| 24 | $code=~s/CORE:://g; | ||
| 25 | $code=~s/CORE::GLOBAL:://g; | ||
| 26 | return unless $code; | ||
| 27 | $code=~s/(\n^__DATA__\s*?\n(.*?))(?:^__[A-Z]+__|\z)//gms; | ||
| 28 | my $__data__ = $2; | ||
| 29 | if(defined $__data__){ | ||
| 30 | unless(open(Mojo::Weixin::Plugin::Perlcode::Sandbox::DATA ,"<",\$__data__)){ | ||
| 31 | $client->warn("处理__DATA__出现异常: $!"); | ||
| 32 | return | ||
| 33 | } | ||
| 34 | } | ||
| 35 | $code = q#package Mojo::Weixin::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; | ||
| 36 | my ($stdout_buf,$stderr_buf,$is_stdout_cut,$is_stderr_cut); | ||
| 37 | my $run;$run = $client->spawn( | ||
| 38 | cmd =>sub{eval $code;print STDERR $@ if $@;}, | ||
| 39 | exec_timeout => 3, | ||
| 40 | stdout_cb => sub { | ||
| 41 | my ($pid, $chunk) = @_; | ||
| 42 | $stdout_buf.=$chunk if defined $chunk; | ||
| 43 | if(count_lines($stdout_buf) > 10){ | ||
| 44 | $run->kill($pid); | ||
| 45 | $stdout_buf = join "\n",(split /\r?\n/,$stdout_buf,11)[0..9]; | ||
| 46 | $stdout_buf .= "(已截断)"; | ||
| 47 | } | ||
| 48 | elsif(length($stdout_buf) > 200){ | ||
| 49 | $run->kill($pid); | ||
| 50 | $stdout_buf = substr($stdout_buf,0,200); | ||
| 51 | $stdout_buf .= "(已截断)"; | ||
| 52 | } | ||
| 53 | }, | ||
| 54 | stderr_cb => sub { | ||
| 55 | my ($pid, $chunk) = @_; | ||
| 56 | $stderr_buf.=$chunk if defined $chunk; | ||
| 57 | if(count_lines($stderr_buf) > 10){ | ||
| 58 | $run->kill($pid); | ||
| 59 | $stderr_buf = join "\n",(split /\r?\n/,$stderr_buf,11)[0..9]; | ||
| 60 | $stderr_buf .= "(已截断)"; | ||
| 61 | } | ||
| 62 | elsif(length($stderr_buf) > 200){ | ||
| 63 | $run->kill($pid); | ||
| 64 | $stderr_buf = substr($stderr_buf,0,500); | ||
| 65 | $stderr_buf .= "(已截断)"; | ||
| 66 | } | ||
| 67 | }, | ||
| 68 | exit_cb => sub { | ||
| 69 | my($pid,$res)=@_; | ||
| 70 | my $content; | ||
| 71 | if(defined $stderr_buf){ | ||
| 72 | $stderr_buf =~s/(?<=at )\(eval .+?\)(?= line)/CODE/g; | ||
| 73 | $stderr_buf =~s/Mojo::Weixin::Plugin::Perlcode::Sandbox:://g; | ||
| 74 | } | ||
| 75 | $stderr_buf.= "(执行超时)" if $res->{is_timeout}; | ||
| 76 | eval{ | ||
| 77 | $stderr_buf = Term::ANSIColor::colorstrip($stderr_buf) if defined $stderr_buf; | ||
| 78 | $stdout_buf = Term::ANSIColor::colorstrip($stdout_buf) if defined $stdout_buf; | ||
| 79 | }; | ||
| 80 | if(defined $stdout_buf and $stderr_buf){ | ||
| 81 | if($stdout_buf=~/\n$/){$content = $stdout_buf.$stderr_buf} | ||
| 82 | else{$content = $stdout_buf."\n".$stderr_buf} | ||
| 83 | } | ||
| 84 | elsif(defined $stdout_buf){$content=$stdout_buf} | ||
| 85 | elsif(defined $stderr_buf){$content=$stderr_buf} | ||
| 86 | $content = "代码打印内容为空" if ( !defined $content or $content eq ""); | ||
| 87 | $client->reply_message($msg,$content,sub{$_[1]->from("bot")}); | ||
| 88 | }, | ||
| 89 | ); | ||
| 90 | } | ||
| 91 | }; | ||
| 92 | $client->on(receive_message=>$callback); | ||
| 93 | $client->on(send_message=>$callback); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub count_lines{ | ||
| 97 | my $data = shift; | ||
| 98 | my $count =()=$data=~/\r?\n/g; | ||
| 99 | return $count++; | ||
| 100 | } | ||
| 101 | |||
| 102 | 1; |