zlibproxy/app/controller/IndexController.php

224 lines
10 KiB
PHP
Raw Normal View History

2023-01-02 19:56:44 +08:00
<?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');
2024-06-21 20:30:44 +08:00
$tor=$request->cookie('tor', 'false');
2023-01-02 19:56:44 +08:00
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;
}
2024-06-21 20:30:44 +08:00
$host=getenv('public_url');
$httpquery=Http::timeout(60);
$onion='<svg style="margin-bottom: -4px;" t="1718896386758" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="9713" width="24" height="24"><path d="M512 597.333333C469.333333 597.333333 384 640 384 682.666667 384 768 512 768 512 768L512 725.333333C488.533333 725.333333 469.333333 706.133333 469.333333 682.666667 469.333333 659.2 488.533333 640 512 640L512 597.333333M512 810.666667C512 810.666667 341.333333 789.333333 341.333333 704 341.333333 576 469.333333 544 512 544L512 490.666667C469.333333 490.666667 298.666667 554.666667 298.666667 682.666667 298.666667 853.333333 512 853.333333 512 853.333333L512 810.666667M429.653333 299.946667 480.426667 322.56C498.773333 218.453333 547.84 149.333333 547.84 149.333333 529.493333 193.28 517.546667 229.546667 509.866667 258.133333 561.493333 151.466667 666.026667 85.333333 666.026667 85.333333 615.68 135.68 578.56 190.293333 553.386667 235.946667 620.8 164.266667 714.24 117.333333 714.24 117.333333 599.466667 190.72 547.84 307.2 535.04 339.626667L558.506667 343.04C558.506667 365.226667 558.506667 385.706667 569.173333 401.92 601.6 482.56 768 489.386667 768 682.666667 768 875.946667 596.053333 938.666667 504.746667 938.666667 413.44 938.666667 213.333333 897.28 213.333333 682.666667 213.333333 468.053333 424.533333 466.346667 462.08 380.586667 467.2 364.373333 429.653333 299.946667 429.653333 299.946667Z" p-id="9714" fill="#8a8a8a"></path></svg>';
$torstatus='<span>Off</span>';
if($tor=='true'){
$host=getenv('Zlibrary');
$httpquery=$httpquery->withProxy('socks5h://'.getenv('proxy'));
$onion='<svg style="margin-bottom: -4px;" t="1718896386758" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="9713" width="24" height="24"><path d="M512 597.333333C469.333333 597.333333 384 640 384 682.666667 384 768 512 768 512 768L512 725.333333C488.533333 725.333333 469.333333 706.133333 469.333333 682.666667 469.333333 659.2 488.533333 640 512 640L512 597.333333M512 810.666667C512 810.666667 341.333333 789.333333 341.333333 704 341.333333 576 469.333333 544 512 544L512 490.666667C469.333333 490.666667 298.666667 554.666667 298.666667 682.666667 298.666667 853.333333 512 853.333333 512 853.333333L512 810.666667M429.653333 299.946667 480.426667 322.56C498.773333 218.453333 547.84 149.333333 547.84 149.333333 529.493333 193.28 517.546667 229.546667 509.866667 258.133333 561.493333 151.466667 666.026667 85.333333 666.026667 85.333333 615.68 135.68 578.56 190.293333 553.386667 235.946667 620.8 164.266667 714.24 117.333333 714.24 117.333333 599.466667 190.72 547.84 307.2 535.04 339.626667L558.506667 343.04C558.506667 365.226667 558.506667 385.706667 569.173333 401.92 601.6 482.56 768 489.386667 768 682.666667 768 875.946667 596.053333 938.666667 504.746667 938.666667 413.44 938.666667 213.333333 897.28 213.333333 682.666667 213.333333 468.053333 424.533333 466.346667 462.08 380.586667 467.2 364.373333 429.653333 299.946667 429.653333 299.946667Z" p-id="9714" fill="#7A5EB7"></path></svg>';
$torstatus='<span style="color:green;">On</span>';
}
$url='http://'.$host.$path;
$auth=array('remix_userid'=>"$id",'remix_userkey'=>"$key",'siteLanguageV2'=>'zh','siteLanguage'=>'zh','selectedSiteMode'=>'books');
$return = $httpquery->withCookies($auth,$host)->get($url);
2023-01-02 19:56:44 +08:00
$back=$return->body();
if($return->header('Content-Type')=='text/html; charset=UTF-8'){
$ver=getenv('version');
2024-06-21 20:30:44 +08:00
$copyright=getenv('copyright');
$copyright_url=getenv('copyright_url');
2023-01-02 19:56:44 +08:00
$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;">
2024-06-21 20:30:44 +08:00
Zlibrary Proxy(v$ver) UserID:$id <a href='/login/switchtor?to=$path'>[$onion Tor $torstatus]</a> <a style="color: coral;" href="/login/logout">[退出账号]</a><a style="color: black;" href="$copyright_url" target="_blank_pmnet"> [$copyright]</a>
2023-01-02 19:56:44 +08:00
</div>
</center></body></html>
EOF;
$back=$front.$back;
}
2024-06-21 20:30:44 +08:00
$backhead=$return->headers();
$backhead['Content-Length']=strlen($back);
$response->withHeaders($backhead);
2023-01-02 19:56:44 +08:00
/**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')) ){
2024-06-21 20:30:44 +08:00
$redis->hset($rpath,'CT',json_encode($backhead));
2023-01-02 19:56:44 +08:00
$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');
2024-06-21 20:30:44 +08:00
$tor=$request->post('tor',false);
2023-01-02 19:56:44 +08:00
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' => '输入的验证码不正确']);
}
2024-06-21 20:30:44 +08:00
$url='https://'.getenv('public_login').'/rpc.php';
$response=Http::timeout(60);
if($tor=='true'){
$url='http://'.getenv('ZlibraryLogin').'/rpc.php';
$response=$response->withProxy('socks5h://'.getenv('proxy'));
}
$response = $response->post("$url", ['isModal' => true,'email'=>"$username",'password'=>"$password",'site_mode'=>'books','action'=>'login','redirectUrl'=>'','isSinglelogin'=>'1','isTorVersion'=>'1','gg_json_mode'=>'1']);
#print_r($response);
2023-01-02 19:56:44 +08:00
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]);
}
2024-06-21 20:30:44 +08:00
#print_r($return->response);
$rawparma=$return->response->params;
if($tor=='true'){
$rawparma=$return->response->priorityRedirectUrl;
}
$url=parse_url($rawparma)['query'];
#print_r($url);
2023-01-02 19:56:44 +08:00
$queryParts = explode('&', $url);
$params = array();
foreach ($queryParts as $param) {
$item = explode('=', $param);
$params[$item[0]] = $item[1];
}
2024-06-21 20:30:44 +08:00
return json(['code'=>200,'msg'=>'登陆成功','userid'=>$params['remix_userid']])->cookie('id', $params['remix_userid'],43200,'/')->cookie('key', $params['remix_userkey'],43200,'/')->cookie('tor', $tor,43200,'/');
2023-01-02 19:56:44 +08:00
}
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');
2024-06-21 20:30:44 +08:00
$tor=$request->cookie('tor', 'false');
2023-01-02 19:56:44 +08:00
if($key=='blank' || $id=='blank'){
return redirect('/login');
}
$path=$request->uri();
View::assign([
'id' => $id,
'key'=> $key,
'path'=> $path,
2024-06-21 20:30:44 +08:00
'tor'=>$tor,
2023-01-02 19:56:44 +08:00
]);
return view('index');
}
public function file(Request $request)
{
return response()->file(base_path() . '/app/controller/IndexController.php');
}
2024-06-21 20:30:44 +08:00
public function tor(Request $request)
{
$to=$request->input('to','/');
$tor=$request->cookie('tor','false');
if($tor=='true'){
$tor='false';
}else{
$tor='true';
}
return redirect($to)->cookie('tor', $tor,43200,'/');
}
2023-01-02 19:56:44 +08:00
}