尝试增加ClashMeta的hy2订阅,调整hy数据库以储存混淆obfs

This commit is contained in:
root
2023-09-30 21:35:31 +09:00
parent 28ad50650e
commit 21c03fc1e1
7 changed files with 144 additions and 11 deletions

View File

@ -4,6 +4,7 @@ namespace App\Http\Controllers\V1\Admin\Server;
use App\Http\Controllers\Controller;
use App\Models\ServerHysteria;
use App\Utils\Helper;
use Illuminate\Http\Request;
class HysteriaController extends Controller
@ -24,10 +25,18 @@ class HysteriaController extends Controller
'rate' => 'required|numeric',
'up_mbps' => 'required|numeric|min:1',
'down_mbps' => 'required|numeric|min:1',
'obfs' => 'nullable',
'obfs_password' => 'nullable',
'server_name' => 'nullable',
'insecure' => 'required|in:0,1'
]);
if(isset($params['obfs'])) {
if(!isset($params['obfs_password'])) $params['obfs_password'] = Helper::getServerKey($request->input('created_at'), 16);
} else {
$params['obfs_password'] = null;
}
if ($request->input('id')) {
$server = ServerHysteria::find($request->input('id'));
if (!$server) {

View File

@ -28,6 +28,7 @@ class UniProxyController extends Controller
}
$this->nodeType = $request->input('node_type');
if ($this->nodeType === 'v2ray') $this->nodeType = 'vmess';
if ($this->nodeType === 'hysteria2') $this->nodeType = 'hysteria';
$this->nodeId = $request->input('node_id');
$this->serverService = new ServerService();
$this->nodeInfo = $this->serverService->getServer($this->nodeId, $this->nodeType);
@ -118,9 +119,14 @@ class UniProxyController extends Controller
'server_port' => $this->nodeInfo->server_port,
'server_name' => $this->nodeInfo->server_name,
'up_mbps' => $this->nodeInfo->up_mbps,
'down_mbps' => $this->nodeInfo->down_mbps,
'obfs' => Helper::getServerKey($this->nodeInfo->created_at, 16)
'down_mbps' => $this->nodeInfo->down_mbps
];
if ($this->nodeInfo->version == 1) {
$response['obfs'] = $this->nodeInfo->obfs_password ?? null;
} elseif ($this->nodeInfo->version == 2) {
$response['obfs'] = $this->nodeInfo->obfs ?? null;
$response['obfs-password'] = $this->nodeInfo->obfs_password ?? null;
}
break;
}
$response['base_config'] = [

View File

@ -52,6 +52,10 @@ class ClashMeta
array_push($proxy, self::buildTrojan($user['uuid'], $item));
array_push($proxies, $item['name']);
}
if ($item['type'] === 'hysteria') {
array_push($proxy, self::buildHysteria($user['uuid'], $item));
array_push($proxies, $item['name']);
}
}
$config['proxies'] = array_merge($config['proxies'] ? $config['proxies'] : [], $proxy);
@ -244,6 +248,38 @@ class ClashMeta
return $array;
}
public static function buildHysteria($password, $server)
{
$array = [];
$array['name'] = $server['name'];
$array['server'] = $server['host'];
$array['port'] = $server['port'];
$array['udp'] = true;
//Todo:完善客户端上下行
//$array['up'] = $server['up_mbps'];
//$array['down'] = $server['down_mbps'];
if (isset($server['server_name'])) $array['sni'] = $server['server_name'];
if ($server['version'] === 2) {
$array['type'] = 'hysteria2';
$array['password'] = $password;
if (isset($server['obfs'])){
$array['obfs'] = $server['obfs'];
$array['obfs-password'] = $server['obfs_password'];
}
} else {
$array['type'] = 'hysteria';
$array['auth_str'] = $password;
if (isset($server['obfs']) && isset($server['obfs_password'])){
$array['obfs'] = $server['obfs_password'];
}
$array['protocol'] = 'udp';
}
return $array;
}
private function isMatch($exp, $str)
{
return @preg_match($exp, $str);

View File

@ -36,6 +36,9 @@ class General
if ($item['type'] === 'trojan') {
$uri .= self::buildTrojan($user['uuid'], $item);
}
if ($item['type'] === 'hysteria') {
$uri .= self::buildHysteria($user['uuid'], $item);
}
}
return base64_encode($uri);
}
@ -147,7 +150,7 @@ class General
if (isset($kcpSettings['header']['type'])) $config['headerType'] = $kcpSettings['header']['type'];
if (isset($kcpSettings['seed'])) $config['path'] = Helper::encodeURIComponent($kcpSettings['seed']);
$output .= "&headerType={$config['headerType']}" . "&seed={$config['path']}";
}
}
if ((string)$server['network'] === 'ws') {
$wsSettings = $server['network_settings'];
if (isset($wsSettings['path'])) $config['path'] = Helper::encodeURIComponent($wsSettings['path']);
@ -164,11 +167,11 @@ class General
$quicSettings = $server['network_settings'];
if (isset($quicSettings['security'])) $config['quicSecurity'] = $quicSettings['security'];
if (isset($quicSettings['header']['type'])) $config['headerType'] = $quicSettings['header']['type'];
$output .= "&quicSecurity={$config['quicSecurity']}" . "&headerType={$config['headerType']}";
if ((string)$quicSettings['security'] !== 'none' && isset($quicSettings['key'])) $config['path'] = Helper::encodeURIComponent($quicSettings['key']);
$output .= "&key={$config['path']}";
}
if ((string)$server['network'] === 'grpc') {
@ -195,4 +198,33 @@ class General
return $uri;
}
public static function buildHysteria($password, $server)
{
$remote = filter_var($server['host'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ? '[' . $server['host'] . ']' : $server['host'];
$name = Helper::encodeURIComponent($server['name']);
if ($server['version'] == 2) {
$uri = "hysteria2://{$password}@{$remote}:{$server['port']}/?insecure={$server['insecure']}&sni={$server['server_name']}";
if (isset($server['obfs']) && isset($server['obfs_password'])) {
$uri .= "&obfs={$server['obfs']}&obfs-password={$server['obfs_password']}";
}
} else {
$uri = "hysteria://{$remote}:{$server['port']}/?";
$query = http_build_query([
'protocol' => 'udp',
'auth' => $password,
'insecure' => $server['insecure'],
'peer' => $server['server_name']
//'upmbps' => $server['up_mbps'],
//'downmbps' => $server['up_mbps']
]);
$uri .= $query;
if (isset($server['obfs']) && isset($server['obfs_password'])) {
$uri .= "&obfs={$server['obfs']}&obfsParam{$server['obfs_password']}";
}
}
$uri .= "#{$name}\r\n";
return $uri;
}
}