| 1 | package Webqq::Client::Plugin::Perlcode; | ||
| 2 | use File::Temp qw/tempfile/; | ||
| 3 | use Webqq::Client::Util qw(console_stderr); | ||
| 4 | use File::Path qw/mkpath rmtree/; | ||
| 5 | use IPC::Run qw(run timeout start pump finish harness); | ||
| 6 | use POSIX qw(strftime); | ||
| 7 | |||
| 8 | if($^O !~ /linux/){ | ||
| 9 | console_stderr "Webqq::Client::App::Perlcode只能运行在linux系统上\n"; | ||
| 10 | exit; | ||
| 11 | } | ||
| 12 | chomp(my $PERL_COMMAND = `/bin/env which perl`); | ||
| 13 | mkpath "/tmp/webqq/log/",{owner=>"nobody",group=>"nobody",mode=>0555}; | ||
| 14 | mkpath "/tmp/webqq/bin/",{owner=>"nobody",group=>"nobody",mode=>0555}; | ||
| 15 | mkpath "/tmp/webqq/src/",{owner=>"nobody",group=>"nobody",mode=>0555}; | ||
| 16 | chown +(getpwnam("nobody"))[2,3],"/tmp/webqq/"; | ||
| 17 | chown +(getpwnam("nobody"))[2,3],"/tmp/webqq/log"; | ||
| 18 | chown +(getpwnam("nobody"))[2,3],"/tmp/webqq/bin"; | ||
| 19 | chown +(getpwnam("nobody"))[2,3],"/tmp/webqq/src"; | ||
| 20 | |||
| 21 | open LOG,">>/tmp/webqq/log/exec.log" or die $!; | ||
| 22 | sub call{ | ||
| 23 | my ($client,$msg,$perl_path) = @_; | ||
| 24 | return 1 if time - $msg->{msg_time} > 10; | ||
| 25 | $PERL_COMMAND = $perl_path if defined $perl_path; | ||
| 26 | if($msg->{content} =~/(?:>>>)(.*?)(?:__END__|$)/s or $msg->{content} =~/perl\s+-e\s+'([^']+)'/s){ | ||
| 27 | $msg->{allow_plugin} = 0; | ||
| 28 | my $doc = ''; | ||
| 29 | my $code = $1; | ||
| 30 | $code=~s/^\s+|\s+$//g; | ||
| 31 | $code=~s/CORE:://g; | ||
| 32 | $code=~s/CORE::GLOBAL:://g; | ||
| 33 | if($code){ | ||
| 34 | $code = q#use feature qw(say);BEGIN{use File::Path;use BSD::Resource;setrlimit(RLIMIT_NOFILE,10,10);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;use POSIX qw(setuid setgid);{my($u,$g)= (getpwnam("nobody"))[2,3];mkpath('/tmp/webqq/bin/',{owner=>$u,group=>$g,mode=>0555}) unless -e '/tmp/webqq/bin';chdir '/tmp/webqq/bin';chroot '/tmp/webqq/bin' or die "chroot fail: $!";chdir "/";setuid($u);setgid($g);%ENV=();}# . $code; | ||
| 35 | my ($fh, $filename) = tempfile("webqq_perlcode_XXXXXXXX",SUFFIX =>".pl",DIR => "/tmp/webqq/src"); | ||
| 36 | print $code,"\n",$filename,"\n" if $client->{debug}; | ||
| 37 | print $fh $code; | ||
| 38 | close $fh; | ||
| 39 | chomp(my $syntax_check = `$PERL_COMMAND -Ttc '$filename' 2>&1`); | ||
| 40 | if($syntax_check =~/syntax OK/){ | ||
| 41 | my $out_and_err = ''; | ||
| 42 | my $h; | ||
| 43 | eval{ | ||
| 44 | my ($line,$len) = (0,0); | ||
| 45 | my @cmd = ($PERL_COMMAND,"-Tt",$filename); | ||
| 46 | $h= harness | ||
| 47 | \@cmd,'>&',\$out_and_err,timeout(5) or $doc="@灰灰 run perlcode fail"; | ||
| 48 | while($len<=200 and $line <=10){ | ||
| 49 | $h->pump; | ||
| 50 | $out_and_err=~s/\Q$filename\E/CODE/g; | ||
| 51 | $len = length($out_and_err); | ||
| 52 | $line = ()=$out_and_err=~m/\n/g; | ||
| 53 | select undef,undef,undef,0.01; | ||
| 54 | } | ||
| 55 | $h->kill_kill; | ||
| 56 | }; | ||
| 57 | |||
| 58 | if($@=~/^IPC::Run: timeout on timer/){ | ||
| 59 | $doc .= "代码执行结果:\n". &truncate($out_and_err) . "\n(代码执行超时)" ; | ||
| 60 | $h->kill_kill; | ||
| 61 | } | ||
| 62 | elsif($@=~/^process ended prematurely/){ | ||
| 63 | $doc = "代码执行结果:\n". &truncate($out_and_err); | ||
| 64 | } | ||
| 65 | else{ $doc = "代码执行结果:\n". &truncate($out_and_err);} | ||
| 66 | } | ||
| 67 | |||
| 68 | else{$doc = "代码语法检查错误:\n" . $syntax_check;} | ||
| 69 | $doc=~s/\Q$filename\E/CODE/g; | ||
| 70 | unlink $filename; | ||
| 71 | print LOG strftime("%Y-%m-%d %H:%M:%S",localtime()),"\n",$code,"\n",$doc,"\n"; | ||
| 72 | |||
| 73 | $client->reply_message($msg,$doc) if $doc; | ||
| 74 | } | ||
| 75 | return 0; | ||
| 76 | } | ||
| 77 | |||
| 78 | return 1; | ||
| 79 | } | ||
| 80 | sub truncate { | ||
| 81 | my $out_and_err = shift; | ||
| 82 | my $is_truncated = 0; | ||
| 83 | if(length($out_and_err)>200){ | ||
| 84 | $out_and_err = substr($out_and_err,0,200); | ||
| 85 | $is_truncated = 1; | ||
| 86 | } | ||
| 87 | my @l =split /\n/,$out_and_err,11; | ||
| 88 | if(@l>10){ | ||
| 89 | $out_and_err = join "\n",@l[0..9]; | ||
| 90 | $is_truncated = 1; | ||
| 91 | } | ||
| 92 | return $out_and_err. ($is_truncated?"\n(已截断)":""); | ||
| 93 | } | ||
| 94 | 1; |