This commit is contained in:
2023-01-02 19:56:44 +08:00
commit e187878b7a
3244 changed files with 260239 additions and 0 deletions
+101
View File
@@ -0,0 +1,101 @@
<?php
namespace app\controller;
use Webman\Captcha\CaptchaBuilder;
use Respect\Validation\Validator as v;
use support\Request;
use yzh52521\EasyHttp\Response;
use yzh52521\EasyHttp\RequestException;
use yzh52521\EasyHttp\Http;
use support\Redis;
use support\View;
class File
{
public function check(Request $request)
{
$id=$request->cookie('id', 'blank');
$key=$request->cookie('key', 'blank');
$path=$request->input('path','blank');
if($key=='blank' || $id=='blank'){
return json(['code'=>509,'msg'=>'未登录或参数错误']);
}
$name=base64_encode($path);
$redis = Redis::connection('default');
if($redis->exists('BookCache_'.$name)){
if($redis->hget('BookCache_'.$name,'Status')=='OK'){
return json(['code'=>200, 'msg'=>'找到文件', 'size'=>$redis->hget('BookCache_'.$name,'Size'),'name'=>$redis->hget('BookCache_'.$name,'Name')]);
}else{
return json(['code'=>202,'msg'=>'文件下载中']);
}
}else {
return json(['code'=>201, 'msg'=>'未找到文件']);
}
}
public function download(Request $request)
{
$id=$request->cookie('id', 'blank');
$key=$request->cookie('key', 'blank');
$path=$request->input('path','blank');
if($key=='blank' || $id=='blank'){
return json(['code'=>509,'msg'=>'未登录或参数错误']);
}
$auth=array('remix_userid'=>"$id",'remix_userkey'=>"$key",'siteLanguageV2'=>'zh','selectedSiteMode'=>'books');
$url='http://'.getenv('Zlibrary')."$path";
$get=Http::withProxy('socks5h://'.getenv('proxy'))->withCookies($auth,getenv('Zlibrary'));
$head=$get->head($url);
$ContentLength=(int)$head->header('Content-Length');
$size=round(($ContentLength/1048576),2);
if($size>=30){
return json(['code'=>500,'msg'=>'文件太大了!']);
}
if($head->header('Content-Type')=='text/html; charset=UTF-8'){
return json(['code'=>404,'msg'=>'链接错误']);
}
$redis = Redis::connection('default');
$name=base64_encode($path);
$redis->hset('BookCache_'.$name,'Time',time());
$redis->hset('BookCache_'.$name,'Date',date("Y/m/d H:i:s"));
$redis->hset('BookCache_'.$name,'Status','Downloading');
if(!is_dir(base_path().'/books/'.date("Y/m/"))){
mkdir(base_path().'/books/'.date("Y/m/"),0777,true);
}
$filepath=base_path().'/books/'.date("Y/m/").$name.'.bin';
$return=$get->get($url)->body();
$file = fopen("$filepath","w");
fwrite($file,$return);
fclose($file);
$filerealname=explode('"',explode(";",$head->header('Content-Disposition'))[1])[1];
$redis->hset('BookCache_'.$name,'Path',$filepath);
$redis->hset('BookCache_'.$name,'Size',$size);
$redis->hset('BookCache_'.$name,'Name',$filerealname);
$redis->hset('BookCache_'.$name,'Status','OK');
return json(['code'=>200,'name'=>"$filerealname",'size'=>"$size"]);
}
public function get(Request $request)
{
$id=$request->cookie('id', 'blank');
$key=$request->cookie('key', 'blank');
$path=$request->input('path','blank');
if($key=='blank' || $id=='blank'){
return json(['code'=>509,'msg'=>'未登录或参数错误']);
}
$redis = Redis::connection('default');
$name=base64_encode($path);
if($redis->exists('BookCache_'.$name)){
if($redis->hget('BookCache_'.$name,'Status')=='OK'){
$redis->hset('BookCache_'.$name,'LastDown',time());
return response()->download($redis->hget('BookCache_'.$name,'Path'),$redis->hget('BookCache_'.$name,'Name'));
}else{
return json(['code'=>202,'msg'=>'文件仍在下载中']);
}
}else {
return json(['code'=>201, 'msg'=>'未找到文件']);
}
}
}
+173
View File
@@ -0,0 +1,173 @@
<?php
namespace app\controller;
use Webman\Captcha\CaptchaBuilder;
use Respect\Validation\Validator as v;
use support\Request;
use yzh52521\EasyHttp\Response;
use yzh52521\EasyHttp\RequestException;
use yzh52521\EasyHttp\Http;
use support\Redis;
use support\View;
class IndexController
{
public function index(Request $request)
{
$redis = Redis::connection('default');
$kf='ProxyCache_';
$response = response();
$id=$request->cookie('id', 'blank');
$key=$request->cookie('key', 'blank');
if($key=='blank' || $id=='blank'){
return redirect('/login');
}
$path=$request->uri();
if($path==''||$path=='/'){
$path='/?signAll=1&ts=1657';
}
$rpath=$kf.urlencode("$path");
if($redis->get($rpath)){
$response->withHeaders(json_decode($redis->hget($rpath,'CT')));
$response->withBody($redis->hget($rpath,'body'));
return $response;
}
$url='http://'.getenv('Zlibrary').$path;
$auth=array('remix_userid'=>"$id",'remix_userkey'=>"$key",'siteLanguageV2'=>'zh','selectedSiteMode'=>'books');
$return = Http::withProxy('socks5h://'.getenv('proxy'))->withCookies($auth,getenv('Zlibrary'))->get($url);
$response->withHeaders($return->headers());
$back=$return->body();
if($return->header('Content-Type')=='text/html; charset=UTF-8'){
$ver=getenv('version');
$front= <<<EOF
<html><body>
<center>
<div style="background-color: bisque;color: black;position:fixed!important;font-size:1.8rem;line-height:1.618;z-index:9999!important;text-align:-webkit-center;">
Zlibrary Proxy(v$ver) UserID:$id <a style="color: coral;" href="/login/logout">[退出账号]</a><a style="color: black;" href="//www.pmnet.gq" target="_blank_pmnet"> [PublicMirrorsNetwork]</a>
</div>
</center></body></html>
EOF;
$back=$front.$back;
}
/**Make Cache */
if($path=='/resources/build/global.js?0.495' || $path=='/resources/build/global.css?0.495' || $path=='/resources/ext/freewall.js' || preg_match("/^image*/",$return->header('Content-Type')) ){
$redis->hset($rpath,'CT',json_encode($return->headers()));
$redis->hset($rpath,'body',$back);
$redis->expire($rpath,43200);
}
$response->withBody($back);
return $response;
}
public function login(Request $request)
{
return view('login');
}
public function check(Request $request)
{
$username=$request->post('name','blank');
$password=$request->post('password','blank');
$code=$request->post('code','blank');
if($username=='blank' || $password=='blank' || $code=='blank'){
return json(['code'=>500, 'msg'=>'缺少参数']);
}
try {
$V=v::Email()->setName('用户名')->check($username);
} catch (ValidationException $exception) {
return json(['code'=>500, 'msg'=>$exception->getMessage()]);
}
try {
$V=v::stringType()->setName('密码')->noWhitespace()->check($password);
} catch (ValidationException $exception) {
return json(['code'=>500, 'msg'=>$exception->getMessage()]);
}
try {
$V=v::stringType()->length(5, 5)->setName('验证码')->noWhitespace()->check($code);
} catch (ValidationException $exception) {
return json(['code'=>500, 'msg'=>$exception->getMessage()]);
}
if (strtolower($code) !== $request->session()->get('captcha')) {
return json(['code' => 400, 'msg' => '输入的验证码不正确']);
}
$url='http://'.getenv('ZlibraryLogin').'/rpc.php';
$response = Http::withProxy('socks5h://'.getenv('proxy'))->post("$url", ['isModal' => true,'email'=>"$username",'password'=>"$password",'site_mode'=>'books','action'=>'login','redirectUrl'=>'','isSinglelogin'=>'1','isTorVersion'=>'1','gg_json_mode'=>'1']);
if(!$response->successful()){
return json(['code' => 501, 'msg' => '登陆失败,服务端错误']);
}
$return=$response->body();
$return=json_decode($return);
if(isset($return->response->validationError)){
return json(['code' => 502, 'msg' => 'Zlib登陆失败:'.$return->response->message]);
}
$url=parse_url($return->response->params)['query'];
$queryParts = explode('&', $url);
$params = array();
foreach ($queryParts as $param) {
$item = explode('=', $param);
$params[$item[0]] = $item[1];
}
return json(['code'=>200,'msg'=>'登陆成功','userid'=>$params['remix_userid']])->cookie('id', $params['remix_userid'],43200,'/')->cookie('key', $params['remix_userkey'],43200,'/');
}
public function code(Request $request)
{
// 初始化验证码类
$builder = new CaptchaBuilder;
// 生成验证码
$builder->build();
// 将验证码的值存储到session中
$request->session()->set('captcha', strtolower($builder->getPhrase()));
// 获得验证码图片二进制数据
$img_content = $builder->get();
// 输出验证码二进制数据
return response($img_content, 200, ['Content-Type' => 'image/jpeg']);
}
public function logout(Request $request)
{
$response = response();
$response->cookie('id','', -1,'/');
$response->cookie('key','', -1,'/');
$response->header('Location', '/login');
$response->withStatus(302);
return $response;
}
public function download(Request $request)
{
$id=$request->cookie('id', 'blank');
$key=$request->cookie('key', 'blank');
if($key=='blank' || $id=='blank'){
return redirect('/login');
}
$path=$request->uri();
View::assign([
'id' => $id,
'key'=> $key,
'path'=> $path,
]);
return view('index');
}
public function file(Request $request)
{
return response()->file(base_path() . '/app/controller/IndexController.php');
}
}