feat(app): 添加主要域名解析功能并优化 CURL 请求

- 新增 getMainDomain 函数,用于解析主机的主要域名
- 添加 curl_client 函数,实现更灵活的 CURL 请求
- 更新 composer.json,添加 jeremykendall/php-domain-parser 依赖
- 将版本号从 1.7.2升级到 1.7.3
This commit is contained in:
coolxitech 2024-12-21 19:40:41 +08:00
parent 7a6ea3ddf1
commit 188afd27f7
4 changed files with 15777 additions and 2 deletions

View File

@ -5,6 +5,10 @@ use app\model\Config;
use think\facade\Db;
use GuzzleHttp\Client;
use think\facade\Request;
use Pdp\Rules;
use Pdp\Domain;
use GuzzleHttp\Exception\RequestException;
function get_curl(string $url, $post = 0, $referer = 0, $cookie = 0, $header = 0, $ua = 0, $nobody = 0)
{
@ -288,6 +292,16 @@ function convert_second($s)
}
}
function getMainDomain($host)
{
$publicSuffixList = Rules::fromPath(app()->getBasePath() . 'data' . DIRECTORY_SEPARATOR . 'public_suffix_list.dat');
if (filter_var($host, FILTER_VALIDATE_IP)) return $host;
$domain_check = Domain::fromIDNA2008($host);
$result = $publicSuffixList->resolve($domain_check);
$domain_parse = $result->suffix()->toString();
return $domain_parse ?: $host;
}
function check_proxy($url, $proxy_server, $proxy_port, $type, $proxy_user, $proxy_pwd)
{
if ($type == 'https') {
@ -326,3 +340,88 @@ function check_proxy($url, $proxy_server, $proxy_port, $type, $proxy_user, $prox
throw new Exception('HTTP状态码异常' . $httpCode);
}
}
function curl_client($url, $data = null, $referer = null, $cookie = null, $headers = null, $proxy = false, $method = 'GET', $timeout = 5): array
{
$client = new Client([
'timeout' => $timeout,
'verify' => false, // 禁用 SSL 验证
]);
$options = [
'headers' => [
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36',
],
'curl' => [], // 用于设置额外的 cURL 配置
];
if ($headers) {
$options['headers'] = array_merge($options['headers'], $headers);
}
if ($data) {
if (strtoupper($method) === 'POST') {
$options['form_params'] = $data; // 表单提交
} else {
$options['body'] = $data; // 其他方法用 body 提交
}
}
if ($cookie) {
$options['headers']['Cookie'] = $cookie;
}
if ($referer) {
$options['headers']['Referer'] = $referer;
}
if ($proxy) {
$proxy_server = config_get('proxy_server');
$proxy_port = intval(config_get('proxy_port'));
$proxy_user = config_get('proxy_user');
$proxy_pwd = config_get('proxy_pwd');
$proxy_type = strtolower(config_get('proxy_type'));
$proxy_url = "$proxy_server:$proxy_port";
$options['curl'][CURLOPT_PROXY] = $proxy_url;
// 设置代理类型
if ($proxy_type === 'socks4') {
$options['curl'][CURLOPT_PROXYTYPE] = CURLPROXY_SOCKS4;
} elseif ($proxy_type === 'socks5') {
$options['curl'][CURLOPT_PROXYTYPE] = CURLPROXY_SOCKS5;
} else {
$options['curl'][CURLOPT_PROXYTYPE] = CURLPROXY_HTTP;
}
// 设置代理认证
if ($proxy_user && $proxy_pwd) {
$options['curl'][CURLOPT_PROXYUSERPWD] = "$proxy_user:$proxy_pwd";
}
}
try {
$response = $client->request($method, $url, $options);
return [
'code' => $response->getStatusCode(),
'redirect_url' => $response->getHeaderLine('Location'),
'header' => $response->getHeaders(),
'body' => $response->getBody()->getContents(),
];
} catch (RequestException $e) {
if ($e->hasResponse()) {
return [
'code' => $e->getResponse()->getStatusCode(),
'redirect_url' => $e->getResponse()->getHeaderLine('Location'),
'header' => $e->getResponse()->getHeaders(),
'body' => $e->getResponse()->getBody()->getContents(),
];
}
throw new Exception('HttpClient error: ' . $e->getMessage());
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
{
"name": "coolxitech/dnsmgr",
"description": "聚合DNS管理系统",
"version": "1.7.2",
"version": "1.7.3",
"type": "project",
"keywords": [
"thinkphp",
@ -49,7 +49,8 @@
"topthink/think-view": "^2.0",
"topthink/think-captcha": "^3.0",
"guzzlehttp/guzzle": "^7.9",
"coolxitech/cloudflare-sdk": "dev-master"
"coolxitech/cloudflare-sdk": "dev-master",
"jeremykendall/php-domain-parser": "^6.0"
},
"require-dev": {
"symfony/var-dumper": "^4.2",

Binary file not shown.

After

Width:  |  Height:  |  Size: 163 KiB