You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
192 lines
6.6 KiB
192 lines
6.6 KiB
<?php
|
|
|
|
namespace app\api\controller;
|
|
|
|
use app\Request;
|
|
|
|
use app\api\middleware\LcJWTAuth;
|
|
use Lcobucci\JWT\Configuration;
|
|
use Lcobucci\JWT\Signer\Hmac\Sha256;
|
|
use Lcobucci\JWT\Signer\Key\InMemory;
|
|
use DateTimeImmutable;
|
|
|
|
class Index extends ApiController
|
|
{
|
|
protected $middleware = [
|
|
LcJWTAuth::class => [
|
|
'except' => ['proxy', 'test']
|
|
]
|
|
];
|
|
//
|
|
public function index(Request $request)
|
|
{
|
|
$arr=[];
|
|
$head = $request->header('mkpwd');
|
|
if ($head == '7xopjJClRxTHhtAm') {
|
|
// make user password
|
|
$data = $request->post();
|
|
// 用户密码
|
|
$salt = isset($data['salt'])?$data['salt']:makeSalt(6);
|
|
|
|
$arr['encpass']=password($data['upass'].$salt);
|
|
$arr['salt']=$salt;
|
|
} else {
|
|
$arr = ["ver" => "00", "date" => time()];
|
|
}
|
|
$ss = json_encode($arr);
|
|
return $ss;
|
|
}
|
|
|
|
/**
|
|
* 生成随机字符串
|
|
* make salt
|
|
* @param int $len
|
|
*/
|
|
protected function makeSalt(int $len){
|
|
$ss = "abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
|
|
$salt = '';
|
|
for ($i = 0; $i < $len; $i++) {
|
|
$salt .= $ss[mt_rand(0, strlen($ss) - 1)];
|
|
}
|
|
return $salt;
|
|
}
|
|
|
|
public function test(){
|
|
// return time();
|
|
// throwError('密码错误');
|
|
$claims=[
|
|
"id" => 1,
|
|
"nick_name" => 'zhangsan'
|
|
];
|
|
$signer = new Sha256();
|
|
$key = InMemory::plainText(config('jwt.secret'));
|
|
|
|
//
|
|
$config = Configuration::forSymmetricSigner($signer,$key);
|
|
|
|
//$builder = new Builder();
|
|
// 设置发行时间和过期时间
|
|
$now = new DateTimeImmutable(); // 当前时间
|
|
|
|
// 设置发行时间和过期时间
|
|
$secondsToAdd = (int) config('jwt.token_ttl');
|
|
$expiresAt = $now->add(new \DateInterval('PT' . $secondsToAdd . 'S'));
|
|
|
|
// $token = $builder
|
|
// ->issuedAt($now) // iat: 发行时间
|
|
// ->expiresAt($expiresAt) // exp: 过期时间
|
|
// ->withIssuer('iss', 'xtt') // iss: 发行人
|
|
// ->withSubject('sub', 'xtoken') // sub: 主题
|
|
// ->withAudience('aud', 'ttc'); // aud: 受众
|
|
|
|
$token = $config->builder()
|
|
// 签发人
|
|
->issuedBy('https://douyin.xingtongworld.com/')
|
|
// 受众
|
|
->permittedFor('https://douyin.xingtongworld.com/')
|
|
// JWT ID 编号 唯一标识
|
|
->identifiedBy($claims['id'])
|
|
// 签发时间
|
|
->issuedAt($now)
|
|
// 在1分钟后才可使用
|
|
// ->canOnlyBeUsedAfter($now->modify('+1 minute'))
|
|
// 过期时间1小时
|
|
->expiresAt($now->modify('+1 hour'))
|
|
// 自定义uid 额外参数
|
|
->withClaim('uid', $claims['id'])
|
|
->withClaim('name',$claims['nick_name'])
|
|
// 自定义header 参数
|
|
// ->withHeader('foo', 'bar')
|
|
// 生成token
|
|
->getToken($config->signer(), $config->signingKey());
|
|
|
|
// base64
|
|
return $token->toString();
|
|
|
|
// foreach ($claims as $key => $value) {
|
|
// $token = $token->withClaim($key, $value);
|
|
// }
|
|
|
|
// return (string) $token->sign($signer, $key);
|
|
}
|
|
|
|
// make proxy
|
|
public function proxy(){
|
|
$ch = curl_init();
|
|
|
|
$params = [
|
|
// 账户密钥
|
|
"app_key" => "50f95964abbb4b10f9b5e4f1075149d0",
|
|
// 指定套餐,默认不指定
|
|
"pack" => "0",
|
|
// 获取代理IP数量
|
|
"num" => "20",
|
|
// 代理协议 http协议: 1
|
|
"xy" => "1",
|
|
// 格式 1:txt 2-json
|
|
"type" => "2",
|
|
// 支持自定义间隔符
|
|
"lb" => "\r\n",
|
|
// 是否去重,默认不去重,不去重=0,去重=99
|
|
"nr" => "0",
|
|
// 指定地区,默认全国,支持指定省或市,支持同时指定多个,英文符号 | 分隔
|
|
// 全国: 0
|
|
// 北京市: 110000
|
|
// 北京市+江苏省+杭州市: 110000|320000|330100
|
|
"area_id" => "0",
|
|
//
|
|
"isp"=>0,
|
|
];
|
|
|
|
// 设置请求地址
|
|
curl_setopt($ch, CURLOPT_URL, "https://api.wandoudl.com/api/ip/list-v2?" . http_build_query($params));
|
|
// https请求 不验证证书和hosts
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
|
|
// 响应内容返回而不是直接输出
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
// 请求超时时间 秒
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
|
// 发出网络请求
|
|
$output = curl_exec($ch);
|
|
// serilizer
|
|
file_put_contents("ips".date('YmdHis').".json",$output);
|
|
echo $output;
|
|
|
|
// if ($output === FALSE) {
|
|
// // 网络请求失败
|
|
// echo "CURL Fail:\terror:" . curl_error($ch) . "\terrno:" . curl_errno($ch), PHP_EOL;
|
|
// } else {
|
|
// // 判断是否获取失败,空返回
|
|
// if (empty($output)) {
|
|
// echo "Warning:\tEmpty Proxy Address List" . PHP_EOL;
|
|
// return;
|
|
// }
|
|
// // 判断是否获取失败,有错误信息
|
|
// if (strpos($output, "{") === 0) {
|
|
// $response = json_decode($output, true);
|
|
// echo "Error Response:\tcode:", $response["code"], "\tmsg:", $response["msg"], PHP_EOL;
|
|
// return;
|
|
// }
|
|
// // 使用自定义换行符分割
|
|
// $proxyAddrList = explode("\r\n", $output);
|
|
|
|
// echo "Obtain total proxy addresses:\t" . count($proxyAddrList), PHP_EOL;
|
|
// // 设置需要通过代理请求的网址
|
|
// curl_setopt($ch, CURLOPT_URL, "https://myip.ipip.net");
|
|
|
|
// foreach ($proxyAddrList as $proxyAddr) {
|
|
// // 设置代理地址,格式 http://ip:port
|
|
// curl_setopt($ch, CURLOPT_PROXY, "http://$proxyAddr");
|
|
// // 通过代理发出网络请求
|
|
// $output = curl_exec($ch);
|
|
// // 判断请求结果
|
|
// if ($output) {
|
|
// echo trim($output), PHP_EOL;
|
|
// } else {
|
|
// echo "Proxy Fail:\tproxy:", $proxyAddr, "\terror:", curl_error($ch), "\terrno:", curl_errno($ch), PHP_EOL;
|
|
// }
|
|
// }
|
|
// }
|
|
curl_close($ch);
|
|
}
|
|
}
|
|
|