function mta_decode($headers, $data) { $encode_types = get_content_encoding($headers); $types = explode(',', $encode_types); $res_data = $data; foreach ($types as $type) { if ($type == 'rc4') { $res_data = mta_rc4($res_data); } else { if ($type == 'gzip') { $header = unpack('Nlength/Lgzip', $res_data); if (intval($header['gzip']) === 0x88b1f) { $header = unpack('Nlength/H*body', $res_data); //$header['ori_buf'] = bin2hex($res_data); //$header['ori_len'] = strlen($res_data); $res_data = hex2bin($header['body']); } else { } $res_data = gzdecode($res_data); //jsondb_logger('nofity', 'gzip log', ['res'=>$res_data,'len'=>strlen($res_data),'header'=>$header]); } } } if (empty($res_data)) { jsondb_logger('notify', 'error ' . bin2hex($data)); } return json_decode($res_data); }
function mta_decode($headers, $data, $cb_fliter = null) { if (empty($data)) { return array('status' => 'error', 'error' => 'no data posted'); } $encode_types = get_content_encoding($headers); $types = explode(',', $encode_types); //jsondb_logger('notify', 'before: '.bin2hex($data)); $packed = false; $res_data = $data; foreach ($types as $type) { if ($type == 'rc4') { $res_data = mta_rc4($res_data); } elseif ($type == 'gzip') { $header = unpack('Nlength/Sgzip', $res_data); if (intval($header['gzip']) === 0x8b1f) { $header = unpack('Nlength/a*body', $res_data); $res_data = $header['body']; $packed = true; } $res_data = gzdecode($res_data); } } if (empty($res_data)) { jsondb_logger('notify', 'error ' . bin2hex($data)); return array('status' => 'error', 'error' => 'decryption error'); } $ori_data = json_decode($res_data); if ($cb_fliter) { $new_data = call_user_func($cb_fliter, $headers, $ori_data); if ($new_data) { $types = array_reverse($types); $res_data = json_encode($new_data); foreach ($types as $type) { if ($type == 'rc4') { $res_data = mta_rc4($res_data); //jsondb_logger('notify', 'after rc4: '.bin2hex($res_data)); } elseif ($type == 'gzip') { if ($packed) { $length = strlen($res_data); $res_data = gzencode($res_data); $res_data = pack('Na*', $length, $res_data); //jsondb_logger('notify', 'len2: '.$length); } else { $res_data = gzencode($res_data); } //jsondb_logger('notify', 'after zip: '.bin2hex($res_data)); } } return array('status' => 'ok', 'ori' => $ori_data, 'new' => $new_data, 'res' => $res_data); } } return array('status' => 'ok', 'ori' => $ori_data, 'new' => null, 'res' => null); }
function forward($cb_before = null, $cb_after = null, $url = '') { //生成url if ($url === '') { $url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; } else { if (!preg_match("/https?:/i", $url)) { if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") { $url = "https://" . $_SERVER['HTTP_HOST'] . "/" . ltrim($url, "/"); } else { $url = "http://" . $_SERVER['HTTP_HOST'] . "/" . ltrim($url, "/"); } } } //获取转发需要的头内容 $headers = get_request_headers(); //转发POST内容 $data_to_post = null; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (in_array(get_content_type($headers), array('application/x-www-form-urlencoded', 'multipart/form-data'))) { $data_to_post = $_POST; } else { //就抓出原始的post数据即可 $fp = fopen('php://input', 'r'); $post = stream_get_contents($fp); fclose($fp); $data_to_post = $post; } } if ($cb_before) { call_user_func_array($cb_before, [&$url, &$data_to_post, &$headers]); } //初始化curl选项 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_MAXREDIRS, 10); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_TIMEOUT, REQUEST_TIMEOUT); //将生成的头,设置在curl中 set_request_headers($ch, $headers); //设置POST数据 if ($data_to_post) { set_post($ch, $data_to_post); } //执行curl请求 //要防止长连接用这种方法 fixme $data = curl_exec($ch); $info = curl_getinfo($ch); curl_close($ch); //获取返回的body内容 $body = $info["size_download"] ? substr($data, $info["header_size"], $info["size_download"]) : ""; $headers_str = substr($data, 0, $info["header_size"]); $headers = get_response_headers($headers_str); if ($cb_after) { $encoding = get_content_encoding($headers); $body_str = $body; if ($encoding === 'deflate') { $body_str = gzinflate($body); } if ($encoding === 'gzip') { $body_str = gzdecode($body); } //调用过滤钩子,检测是否有修改内容 $old_md5 = md5($body_str); call_user_func_array($cb_after, [$info, &$headers, &$body_str]); $new_md5 = md5($body_str); //如果内容有修改,则需要从新打包,和计算内容长度 if ($old_md5 !== $new_md5) { switch ($encoding) { case 'deflate': $body = gzdeflate($body_str); break; case 'gzip': $body = gzencode($body_str); break; default: $body = $body_str; } //修正发出的内容长度 $headers = set_content_length($headers, strlen($body)); } } //转发返回的头内 set_response_headers($headers); //输出html内容到浏览器 echo $body; //函数返回结果给缓存使用 return [$headers, $body]; }