Xboard/app/Protocols/Loon.php

180 lines
6.2 KiB
PHP
Raw Normal View History

2023-11-17 01:44:01 -05:00
<?php
namespace App\Protocols;
2025-01-06 12:20:11 -05:00
use App\Contracts\ProtocolInterface;
class Loon implements ProtocolInterface
2023-11-17 01:44:01 -05:00
{
2025-01-06 12:20:11 -05:00
public $flags = ['loon'];
2023-11-17 01:44:01 -05:00
private $servers;
private $user;
public function __construct($user, $servers)
{
$this->user = $user;
$this->servers = $servers;
}
2025-01-06 12:20:11 -05:00
public function getFlags(): array
{
return $this->flags;
}
2023-11-17 01:44:01 -05:00
public function handle()
{
$servers = $this->servers;
$user = $this->user;
$uri = '';
foreach ($servers as $item) {
2025-01-06 12:20:11 -05:00
if (
$item['type'] === 'shadowsocks'
&& in_array(data_get($item['protocol_settings'], 'cipher'), [
2023-11-17 01:44:01 -05:00
'aes-128-gcm',
'aes-192-gcm',
'aes-256-gcm',
'chacha20-ietf-poly1305'
])
) {
$uri .= self::buildShadowsocks($item['password'], $item);
2023-11-17 01:44:01 -05:00
}
if ($item['type'] === 'vmess') {
$uri .= self::buildVmess($user['uuid'], $item);
}
if ($item['type'] === 'trojan') {
$uri .= self::buildTrojan($user['uuid'], $item);
}
if ($item['type'] === 'hysteria') {
$uri .= self::buildHysteria($user['uuid'], $item, $user);
}
2023-11-17 01:44:01 -05:00
}
return response($uri, 200)
2025-01-06 12:20:11 -05:00
->header('Subscription-Userinfo', "upload={$user['u']}; download={$user['d']}; total={$user['transfer_enable']}; expire={$user['expired_at']}");
2023-11-17 01:44:01 -05:00
}
public static function buildShadowsocks($password, $server)
{
2025-01-06 12:20:11 -05:00
$cipher = data_get($server['protocol_settings'], 'cipher');
2023-11-17 01:44:01 -05:00
$config = [
"{$server['name']}=Shadowsocks",
"{$server['host']}",
"{$server['port']}",
2025-01-06 12:20:11 -05:00
"{$cipher}",
2023-11-17 01:44:01 -05:00
"{$password}",
'fast-open=false',
'udp=true'
];
$config = array_filter($config);
$uri = implode(',', $config);
$uri .= "\r\n";
return $uri;
}
public static function buildVmess($uuid, $server)
{
2025-01-06 12:20:11 -05:00
$protocol_settings = $server['protocol_settings'];
2023-11-17 01:44:01 -05:00
$config = [
"{$server['name']}=vmess",
"{$server['host']}",
"{$server['port']}",
'auto',
"{$uuid}",
'fast-open=false',
'udp=true',
"alterId=0"
];
2025-01-06 12:20:11 -05:00
if (data_get($protocol_settings, 'tls')) {
if (data_get($protocol_settings, 'network') === 'tcp')
array_push($config, 'over-tls=true');
if (data_get($protocol_settings, 'tls_settings')) {
$tls_settings = data_get($protocol_settings, 'tls_settings');
if (data_get($tls_settings, 'allow_insecure'))
array_push($config, 'skip-cert-verify=' . ($tls_settings['allow_insecure'] ? 'true' : 'false'));
if (data_get($tls_settings, 'server_name'))
array_push($config, "tls-name={$tls_settings['server_name']}");
}
}
switch (data_get($server['protocol_settings'], 'network')) {
case 'tcp':
array_push($config, 'transport=tcp');
$tcpSettings = data_get($protocol_settings, 'network_settings');
if (data_get($protocol_settings, 'network_settings')['header']['type'])
2023-11-17 01:44:01 -05:00
$config = str_replace('transport=tcp', "transport={$tcpSettings['header']['type']}", $config);
2025-01-06 12:20:11 -05:00
if (data_get($tcpSettings, key: 'header.request.path')) {
$paths = data_get($tcpSettings, key: 'header.request.path');
$path = $paths[array_rand($paths)];
2023-12-17 20:40:35 -05:00
array_push($config, "path={$path}");
2025-01-06 12:20:11 -05:00
}
if (data_get($tcpSettings, key: 'header.request.headers.Host')) {
$hosts = data_get($tcpSettings, key: 'header.request.headers.Host');
2023-12-17 20:40:35 -05:00
$host = $hosts[array_rand($hosts)];
array_push($config, "host={$host}");
}
2025-01-06 12:20:11 -05:00
break;
case 'ws':
array_push($config, 'transport=ws');
$wsSettings = data_get($protocol_settings, 'network_settings');
if (data_get($wsSettings, key: 'path'))
2023-11-17 01:44:01 -05:00
array_push($config, "path={$wsSettings['path']}");
2025-01-06 12:20:11 -05:00
if (data_get($wsSettings, key: 'headers.Host'))
2023-11-17 01:44:01 -05:00
array_push($config, "host={$wsSettings['headers']['Host']}");
2025-01-06 12:20:11 -05:00
break;
2023-11-17 01:44:01 -05:00
}
$uri = implode(',', $config);
$uri .= "\r\n";
return $uri;
}
public static function buildTrojan($password, $server)
{
2025-01-06 12:20:11 -05:00
$protocol_settings = $server['protocol_settings'];
2023-11-17 01:44:01 -05:00
$config = [
"{$server['name']}=trojan",
"{$server['host']}",
"{$server['port']}",
"{$password}",
2025-01-06 12:20:11 -05:00
data_get($protocol_settings, 'server_name') ? "tls-name={$protocol_settings['server_name']}" : "",
2023-11-17 01:44:01 -05:00
'fast-open=false',
'udp=true'
];
if (!empty($server['allow_insecure'])) {
2025-01-06 12:20:11 -05:00
array_push($config, data_get($protocol_settings, 'tls_settings')['allow_insecure'] ? 'skip-cert-verify=true' : 'skip-cert-verify=false');
2023-11-17 01:44:01 -05:00
}
$config = array_filter($config);
$uri = implode(',', $config);
$uri .= "\r\n";
return $uri;
}
public static function buildHysteria($password, $server, $user)
{
2025-01-06 12:20:11 -05:00
$protocol_settings = $server['protocol_settings'];
if ($protocol_settings['version'] != 2) {
return;
}
$config = [
"{$server['name']}=Hysteria2",
$server['host'],
$server['port'],
$password,
2025-01-06 12:20:11 -05:00
$protocol_settings['tls']['server_name'] ? "sni={$protocol_settings['tls']['server_name']}" : "(null)"
];
2025-01-06 12:20:11 -05:00
if (data_get($protocol_settings, 'tls.allow_insecure'))
$config[] = "skip-cert-verify=true";
$config[] = "download-bandwidth=" . data_get($protocol_settings, 'bandwidth.download_bandwidth');
$config[] = "udp=true";
$config = array_filter($config);
$uri = implode(',', $config);
$uri .= "\r\n";
return $uri;
}
2023-11-17 01:44:01 -05:00
}