Skip to content

Commit b00b435

Browse files
authored
Merge pull request #655 from JxQg/jxq_dev
Feat: Support VLESS node output for Loon protocol, improve buildVless method template
2 parents cf552f6 + 777bbb5 commit b00b435

File tree

1 file changed

+74
-51
lines changed

1 file changed

+74
-51
lines changed

app/Protocols/Loon.php

Lines changed: 74 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Loon extends AbstractProtocol
1414
Server::TYPE_VMESS,
1515
Server::TYPE_TROJAN,
1616
Server::TYPE_HYSTERIA,
17+
Server::TYPE_VLESS,
1718
];
1819

1920
protected $protocolRequirements = [
@@ -42,6 +43,9 @@ public function handle()
4243
if ($item['type'] === Server::TYPE_HYSTERIA) {
4344
$uri .= self::buildHysteria($item['password'], $item, $user);
4445
}
46+
if ($item['type'] === Server::TYPE_VLESS) {
47+
$uri .= self::buildVless($item['password'], $item);
48+
}
4549
}
4650
return response($uri)
4751
->header('content-type', 'text/plain')
@@ -176,58 +180,77 @@ public static function buildTrojan($password, $server)
176180
return $uri;
177181
}
178182

179-
public static function buildVless($uuid, $server)
180-
{
181-
$protocol_settings = $server['protocol_settings'];
182-
$config = [
183-
"{$server['name']}=vless",
184-
$server['host'],
185-
$server['port'],
186-
$uuid,
187-
'fast-open=false',
188-
'udp=true',
189-
'alterId=0'
190-
];
191-
switch ((int) data_get($protocol_settings, 'tls')) {
192-
case 1:
193-
$config[] = 'over-tls=true';
194-
$tlsSettings = data_get($protocol_settings, 'tls_settings', []);
195-
if ($tlsSettings) {
196-
$config[] = 'skip-cert-verify=' . (data_get($tlsSettings, 'allow_insecure') ? 'true' : 'false');
197-
if ($serverName = data_get($tlsSettings, 'server_name')) {
198-
$config[] = "tls-name={$serverName}";
199-
}
200-
}
201-
break;
202-
case 2:
203-
return '';
204-
}
205-
$network_settings = data_get($protocol_settings, 'network_settings', []);
206-
switch ((string) data_get($network_settings, 'network')) {
207-
case 'tcp':
208-
$config[] = 'transport=tcp';
209-
if ($headerType = data_get($network_settings, 'header.type')) {
210-
$config = collect($config)->map(function ($item) use ($headerType) {
211-
return $item === 'transport=tcp' ? "transport={$headerType}" : $item;
212-
})->toArray();
213-
}
214-
if ($paths = data_get($network_settings, 'header.request.path')) {
215-
$config[] = 'path=' . $paths[array_rand($paths)];
216-
}
217-
break;
218-
case 'ws':
219-
$config[] = 'transport=ws';
220-
if ($path = data_get($network_settings, 'path')) {
221-
$config[] = "path={$path}";
222-
}
183+
public static function buildVless($password, $server)
184+
{
185+
$protocol_settings = data_get($server, 'protocol_settings', []);
223186

224-
if ($host = data_get($network_settings, 'headers.Host')) {
225-
$config[] = "host={$host}";
226-
}
227-
break;
228-
}
229-
return implode(',', $config) . "\r\n";
230-
}
187+
$config = [
188+
"{$server['name']}=VLESS",
189+
"{$server['host']}",
190+
"{$server['port']}",
191+
"{$password}",
192+
"alterId=0",
193+
"udp=true"
194+
];
195+
196+
// flow
197+
if ($flow = data_get($protocol_settings, 'flow')) {
198+
$config[] = "flow={$flow}";
199+
}
200+
201+
// TLS/Reality
202+
switch (data_get($protocol_settings, 'tls')) {
203+
case 1:
204+
$config[] = "over-tls=true";
205+
$config[] = "skip-cert-verify=" . (data_get($protocol_settings, 'tls_settings.allow_insecure', false) ? "true" : "false");
206+
if ($serverName = data_get($protocol_settings, 'tls_settings.server_name')) {
207+
$config[] = "sni={$serverName}";
208+
}
209+
break;
210+
case 2:
211+
$config[] = "over-tls=true";
212+
$config[] = "skip-cert-verify=" . (data_get($protocol_settings, 'reality_settings.allow_insecure', false) ? "true" : "false");
213+
if ($serverName = data_get($protocol_settings, 'reality_settings.server_name')) {
214+
$config[] = "sni={$serverName}";
215+
}
216+
if ($pubkey = data_get($protocol_settings, 'reality_settings.public_key')) {
217+
$config[] = "public-key={$pubkey}";
218+
}
219+
if ($shortid = data_get($protocol_settings, 'reality_settings.short_id')) {
220+
$config[] = "short-id={$shortid}";
221+
}
222+
break;
223+
default:
224+
$config[] = "over-tls=false";
225+
break;
226+
}
227+
228+
// network
229+
switch (data_get($protocol_settings, 'network')) {
230+
case 'ws':
231+
$config[] = "transport=ws";
232+
if ($path = data_get($protocol_settings, 'network_settings.path')) {
233+
$config[] = "path={$path}";
234+
}
235+
if ($host = data_get($protocol_settings, 'network_settings.headers.Host')) {
236+
$config[] = "host={$host}";
237+
}
238+
break;
239+
case 'grpc':
240+
$config[] = "transport=grpc";
241+
if ($serviceName = data_get($protocol_settings, 'network_settings.serviceName')) {
242+
$config[] = "grpc-service-name={$serviceName}";
243+
}
244+
break;
245+
default:
246+
$config[] = "transport=tcp";
247+
break;
248+
}
249+
250+
$config = array_filter($config);
251+
$uri = implode(',', $config) . "\r\n";
252+
return $uri;
253+
}
231254

232255
public static function buildHysteria($password, $server, $user)
233256
{

0 commit comments

Comments
 (0)