<pre name="code" class="python">use LWP::UserAgent; use URI::Escape; use Net::Ping; use JSON qw(encode_json); use Socket; use Net::SMTP; use LWP; use LWP::Simple; use LWP::UserAgent; use HTTP::Cookies; use HTTP::Headers; use HTTP::Response; use Encode; use URI::Escape; use URI::URL; use File::Temp qw/tempfile/; use AE; my $api = 'https://login.weixin.qq.com/jslogin'; my $now = time(); my @query_string = ( appid => 'wx782c26e4c19acffb', redirect_uri => 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage', fun => 'new', lang => 'zh_CN', _ => $now, ); my $ua = LWP::UserAgent->new; $ua->timeout(5); $ua->env_proxy; $ua->agent("Mozilla/8.0"); my $cookie_jar = HTTP::Cookies->new( file => 'lwp_cookies.txt', autosave => 1, ignore_discard => 1 ); $ua->cookie_jar($cookie_jar); $host = "https://login.weixin.qq.com/jslogin?appid=wx782c26e4c19acffb&redirect_uri=https%3A%2F%2Fwx.qq.com%2Fcgi-bin%2Fmmwebwx-bin%2Fwebwxnewloginpage&fun=new&lang=zh_CN&_=$now"; my $response = $ua->get($host); $ua->default_headers; if ( $response->is_success ) { print $response->decoded_content; # or whatever $r = $response->decoded_content; print " "; } else { die $response->status_line; } print "$r is $r "; if ( $r =~ /window.QRLogin.code = 200; window.QRLogin.uuid = "(.*?)"/g ) { $uuid = $1; print "$uuid is $uuid "; } my $api = "https://login.weixin.qq.com/qrcode/$uuid"; my $response = $ua->get($api); $ua->default_headers; if ( $response->is_success ) { $r = $response->decoded_content; # or whatever print " "; } else { die $response->status_line; } my ( $fh, $filename ) = tempfile( "weixin_qrcode_XXXX", SUFFIX => ".jpg", DIR => '/tmp' ); binmode $fh; print $fh $r; close $fh; print "登录二维码已经下载到本地 [ $filename ] "; #https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid=YaZ1MmzJ3Q==&tip=0&r=-$now&_=1453360034431 my $show_tip = 1; sub login { while (1) { my $api = "https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?loginicon=true&uuid=$uuid&tip=$show_tip&r=-$now&_=$now"; my $response = $ua->get($api); $ua->default_headers; if ( $response->is_success ) { $r = $response->decoded_content; print "$r is $r "; next unless defined $r; my %data = $r =~ /window.(.+?)=(.+?);/g; if ( $data{code} == 201 ) { print "手机微信扫码成功,请在手机微信上点击 [登录] 按钮... "; my $show_tip = 0; next; } elsif ( $data{code} == 200 ) { print "正在进行登录... "; foreach ($r) { if ( $_ =~ /window.redirect_uri="(.*?)"/ ) { $api = $1; print "$api is $api "; my $response = $ua->get($api); $ua->default_headers; } } foreach ($api) { if ( $_ =~ /https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage?ticket=(.*?)&uuid=(.*?)&lang=(.*?)&scan=(.*)/ ) { print "$1 is $1 "; print "$2 is $2 "; print "$3 is $3 "; print "$4 is $4 "; $ticket=$1; $user = $4; print "$user is $user "; return 1; } } } else { die $response->status_line; } } } } &login(); sub get_ticket { my $api = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage?ticket=$ticket&uuid=$uuid&lang=zh_CN&scan=$user&fun=new&version=v2&lang=zh_CN"; my $response = $ua->get($api); $ua->default_headers; if ( $response->is_success ) { print $response->decoded_content; # or whatever $r = $response->decoded_content; if ( $r =~ /<error.*<skey>(.*?)</skey><wxsid>(.*?)</wxsid><wxuin>(.*?)</wxuin><pass_ticket>(.*?)</pass_ticket.*/ ) { $Skey = $1; $wxsid = $2; $wxuin = $3; $pass_ticket = $4; print "$Skey is $Skey "; print "$wxsid is $wxsid "; print "$wxuin is $wxuin "; print "$pass_ticket is $pass_ticket "; $Skey_x = uri_escape($Skey); use POSIX; my $a = "e"; for ( my $b = 0 ; 15 > $b ; $b++ ) { $a .= POSIX::floor( 10 * rand() ); } $DeviceID = $a; } } } sub RUN { print "启动全局事件循环... "; AE::cv->recv; } sub get_init { @chatroom_id = (); #my $response= $browser->post("https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit", [ "r" => "-$now","lang" => "zh_CN","pass_ticket"=>"$pass_ticket"]); #多加了 $login_url = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r=-$now&lang=zh_CN&pass_ticket=$pass_ticket"; my $post = { BaseRequest => { Uin => $wxuin, Sid => $wxsid, Skey => $Skey, DeviceID => $DeviceID, } }; use JSON qw(encode_json); $json_string = encode_json($post); my $req = HTTP::Request->new( 'POST' => $login_url, [ 'r' => "-$now", 'lang' => 'zh_CN', 'pass_ticket' => "$pass_ticket" ] ); $req->referer("https://wx.qq.com/?&lang=zh_CN"); $req->content_type('application/json; charset=UTF-8') ; #post请求,如果有发送参数,必须要有这句 $req->content("$json_string"); #发送post的参数 my $res = $ua->request($req); #print "-------------init----------------- "; #print $res->status_line." "; #print $res->as_string();#获取的是原始内容,包括响应头,响应正文 print $res->content(); #获取的是响应正文 $d = $res->content(); ##导入 encode_json decode_json use JSON qw/encode_json decode_json/; $d = encode_utf8($d); $d = decode_json($d); use Data::Dumper; my $var = Dumper($d); print $var; foreach $m ( @{ $d->{ContactList} } ) { #print "---------------- "; #print $m->{UserName}; if ( $m->{UserName} =~ /@@/ ) { push( @chatroom_id, $m->{UserName} ); } print " "; }; print "@chatroom_id is @chatroom_id "; use Data::Dumper; $m = $d->{SyncKey}; @xa = $m->{List}; print "$m is $m "; my $var = Dumper($m); print $var; print "@xa is @xa "; my $var = Dumper(@xa); print $var; $count = $m->{Count}; #print "$count is $count "; for ( $i = 0 ; $i <= $count - 1 ; $i++ ) { #print $m->{List}->[$i]->{Val}; #print $m->{List}->[$i]->{Key}; $synckey .= "$m->{List}->[$i]->{Key}\_$m->{List}->[$i]->{Val}|"; }; $synckey =~ s/|$//; $synckey = uri_escape($synckey); print "$synckey is $synckey "; #print "SyncKey---------------- "; $chat_num = $d->{Count}; $name=$d->{User}->{NickName}; print "$name is $name "; $UserName= $d->{User}->{UserName};print $UserName ; print " "; print "-------------init----------------- "; } sub get_friend { use JSON qw(encode_json); $url = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact?lang=zh_CN&pass_ticket=$pass_ticket&seq=0&skey=$skey"; my $response = $ua->get($url); $ua->default_headers; if ( $response->is_success ) { print "-------------friend----------------- "; # print $response->decoded_content; # or whatever $d = $response->decoded_content; } use JSON qw/encode_json decode_json/; $d = encode_utf8($d); $d = decode_json($d); foreach $m ( @{ $d->{MemberList} } ) { #print $m->{UserName}; ###### push( @chatroom_id, $m->{UserName} ); # print " "; } } sub get_chatroom { #$login_url="https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxbatchgetcontact?type=ex&r=$now&pass_ticket=$pass_ticket"; $login_url = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxbatchgetcontact?type=ex&r=$now&lang=zh_CN&pass_ticket=$pass_ticket"; print "@chatroom_id is @chatroom_id "; my $post = { BaseRequest => { Uin => $wxuin, Sid => $wxsid, Skey => $Skey, DeviceID => $DeviceID, }, Count => @chatroom_id + 0, List => [ map { { UserName => $_, ChatRoomId => "" } } @chatroom_id ], }; use JSON qw(encode_json); $json_string = encode_json($post); $req = HTTP::Request->new( 'POST' => $login_url, [ 'lang' => 'zh_CN', 'pass_ticket' => "$pass_ticket", 'r' => "$now", 'type' => 'ex' ] ); $req->referer("https://wx.qq.com/?&lang=zh_CN"); $req->content_type('application/json; charset=UTF-8') ; #post请求,如果有发送参数,必须要有这句 $req->content("$json_string"); #发送post的参数 print "--------chatromm------------------ "; $res = $ua->request($req); #print $res->status_line." "; #print $res->as_string();#获取的是原始内容,包括响应头,响应正文 print $res->content();#获取的是响应正文 $d=$res->content(); use JSON qw/encode_json decode_json/; $d = encode_utf8($d); $d = decode_json($d); print "MemberList array "; print "--------chatromm------------------ "; }; sub send_sms { ($msg,$ToUserName,$FromUserName)=@_; print "$msg is $msg "; print "$FromUserName is $FromUserName "; print "$ToUserName is $ToUserName "; $now= time(); $url ="https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg?pass_ticket=$pass_ticket"; my @query_string = ( sid => $wxsid, skey => $Skey, r => $now, pass_ticket => $pass_ticket ); my $t = $now; my $post = { BaseRequest => { Uin => $wxuin, Sid => $wxsid, Skey => $Skey, DeviceID => $DeviceID, }, Msg => { ClientMsgId => $t, Content => decode("utf8",$msg), FromUserName => $FromUserName, LocalID => $t, ToUserName => $ToUserName, Type => 1, }, }; use JSON qw(encode_json); $json_string = encode_json($post); my $req = HTTP::Request->new( 'POST' => $url, [ 'pass_ticket' => "$pass_ticket" ] ); # $req->referer("https://wx.qq.com/?&lang=zh_CN"); $req->content_type('application/json; charset=UTF-8') ; #post请求,如果有发送参数,必须要有这句 $req->content("$json_string"); #发送post的参数 my $res = $ua->request($req); print $res->as_string(); print $res->content();#获取的是响应正文 print " "; }; sub get_sms{ if ($recm =~/s*@s*env-*s*/){ $recm =~ s/s+//; $gp =(split(/@/,"$recm"))[1]; use DBI; my $dbName = 'oadb'; my $dbUser = 'query'; my $dbUserPass = 'query'; @arr2=""; my $dbh = DBI->connect("dbi:Oracle:$dbName", $dbUser, $dbUserPass) or die "can't connect to database " ; my $hostSql = qq{select IP,INFO,ENV from machine_info where ENV='$gp' }; my ($a1, $a2, $a3,$a4,$a5,$a6,$a7,$a8,$a9); my $selStmt = $dbh->prepare($hostSql); $selStmt->bind_columns(undef, $a1, $a2, $a3); $selStmt->execute(); while( $selStmt->fetch() ){ push (@arr2, "$a1 $a2 $a3 " ); }; &send_sms("@arr2",$FromUserName,$UserName);}}; sub get_synccheck { $now= time(); $url = "https://webpush.weixin.qq.com/cgi-bin/mmwebwx-bin/synccheck?r=$now&skey=$Skey_x&sid=$wxsid&uin=$wxuin&deviceid=$DeviceID&synckey=$synckey&_=$now"; my $response = $ua->get($url); $ua->default_headers; if ( $response->is_success ) { $r = $response->decoded_content; } ##window.synccheck={retcode:"0",selector:"0"} if ( $r =~ /window.*selector:"(.*?)"}/ ) { $mss = $1; # print "$mss is $1 "; } if ( $mss != 0 ) { $sync_url = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsync?sid=$wxsid&skey=$Skey&lang=zh_CN&pass_ticket=$pass_ticket"; my @query_string = ( sid => $wxsid, skey => $Skey, r => $now, pass_ticket => $pass_ticket ); my $post = { BaseRequest => { Uin => $wxuin, Sid => $wxsid }, SyncKey => $m, rr => $now }; use JSON qw(encode_json); $json_string = encode_json($post); my $req = HTTP::Request->new( 'POST' => $sync_url, [ 'lang' => 'zh_CN', 'pass_ticket' => "$pass_ticket", 'sid' => $wxsid, 'skey' => $Skey ] ); $req->referer("https://wx.qq.com/?&lang=zh_CN"); $req->content_type('application/json; charset=UTF-8') ; #post请求,如果有发送参数,必须要有这句 $req->content("$json_string"); #发送post的参数 my $res = $ua->request($req); print "-------------syncinfo----------------- "; #print $res->status_line." "; #print $res->as_string();#获取的是原始内容,包括响应头,响应正文 #print $res->content();#获取的是响应正文 $d = $res->content(); use JSON qw/encode_json decode_json/; $d = encode_utf8($d); $d = decode_json($d); $m = $d->{SyncKey}; $synckey = ""; #$xa=$d->{SyncKey}->{List}; $count = $m->{Count}; #print "$count is $count "; for ( $i = 0 ; $i <= $count - 1 ; $i++ ) { #print $m->{List}->[$i]->{Val}; #print $m->{List}->[$i]->{Key}; $synckey .= "$m->{List}->[$i]->{Key}\_$m->{List}->[$i]->{Val}|"; }; ###$j每个元素,元素内容为引用 foreach $j (@{$d->{AddMsgList}}) { use Data::Dumper; my $var= Dumper($j); # my $var=encode("gbk",decode("utf8","$var")); if ("$j->{MsgType}" == 1){ print "$message is $j->{Content} "; print " "; ##@d91e48886e46846a143e4dc4f0430ae464469faf3d20c1fc108f34936906789f:<br/>那jiandan #@734f2197ad7f69b5d6bdc4e7051eead7a95b191d9204861f7232bb496ef456f2:<br/>jj $recm= $j->{Content}; if ($recm =~/^(@.+):<br/>(.*)/g){ $recm=$2; print "$recm is $recm "; }; $synckey =~ s/|$//; $synckey = uri_escape($synckey); print "$synckey is $synckey "; $FromUserName=$j->{FromUserName}; $ToUserName=$j->{ToUserName}; print "1--------$FromUserName is $FromUserName "; print "2--------$ToUserName is $ToUserName "; if ($FromUserName ne $UserName) { print "hihi "; &get_sms(); } }else {print "now not support message $j->{MsgType} ";} }; }} sub get_event { use AnyEvent; ##定义watch my $t = AnyEvent->timer( after => 0, interval => 5, cb => &get_synccheck ); ##不要再每秒打印时间 ##undef $t; my $cv = AnyEvent->condvar; $cv->recv; } &get_ticket(); &get_init(); &get_friend(); &get_chatroom(); &get_event(); print "now is blocking "; #&RUN();