support trojan surge

This commit is contained in:
Tokumeikoi 2020-06-13 18:33:42 +08:00
parent 2fc77a38f2
commit bf915214dd
4 changed files with 56 additions and 95 deletions

View File

@ -6,6 +6,7 @@ use App\Http\Controllers\Controller;
use App\Services\ServerService; use App\Services\ServerService;
use App\Utils\Clash; use App\Utils\Clash;
use App\Utils\QuantumultX; use App\Utils\QuantumultX;
use App\Utils\Surge;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Models\Server; use App\Models\Server;
use App\Utils\Helper; use App\Utils\Helper;
@ -24,20 +25,21 @@ class ClientController extends Controller
$servers = $serverService->getAllServers($user); $servers = $serverService->getAllServers($user);
} }
if (isset($_SERVER['HTTP_USER_AGENT'])) { if (isset($_SERVER['HTTP_USER_AGENT'])) {
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Quantumult%20X') !== false) { $_SERVER['HTTP_USER_AGENT'] = strtolower($_SERVER['HTTP_USER_AGENT']);
if (strpos($_SERVER['HTTP_USER_AGENT'], 'quantumult%20x') !== false) {
die($this->quantumultX($user, $servers['vmess'], $servers['trojan'])); die($this->quantumultX($user, $servers['vmess'], $servers['trojan']));
} }
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'clash') !== false) { if (strpos($_SERVER['HTTP_USER_AGENT'], 'clash') !== false) {
die($this->clash($user, $servers['vmess'], $servers['trojan'])); die($this->clash($user, $servers['vmess'], $servers['trojan']));
} }
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Surfboard') !== false) { if (strpos($_SERVER['HTTP_USER_AGENT'], 'surfboard') !== false) {
die($this->surfboard($user, $servers['vmess'])); die($this->surfboard($user, $servers['vmess']));
} }
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Surge') !== false) { if (strpos($_SERVER['HTTP_USER_AGENT'], 'surge') !== false) {
die($this->surge($user, $servers['vmess'])); die($this->surge($user, $servers['vmess'], $servers['trojan']));
} }
die($this->origin($user, $servers['vmess'], $servers['trojan']));
} }
die($this->origin($user, $servers['vmess'], $servers['trojan']));
} }
private function quantumultX($user, $vmess = [], $trojan = []) private function quantumultX($user, $vmess = [], $trojan = [])
@ -64,29 +66,20 @@ class ClientController extends Controller
return base64_encode($uri); return base64_encode($uri);
} }
private function surge($user, $vmess = []) private function surge($user, $vmess = [], $trojan = [])
{ {
$proxies = ''; $proxies = '';
$proxyGroup = ''; $proxyGroup = '';
foreach ($vmess as $item) { foreach ($vmess as $item) {
// [Proxy] // [Proxy]
$proxies .= $item->name . ' = vmess, ' . $item->host . ', ' . $item->port . ', username=' . $user->uuid . ', tfo=true'; $proxies .= Surge::buildVmess($user->uuid, $item);
if ($item->tls) { // [Proxy Group]
$tlsSettings = json_decode($item->tlsSettings); $proxyGroup .= $item->name . ', ';
$proxies .= ', tls=' . ($item->tls ? "true" : "false"); }
if (isset($tlsSettings->allowInsecure)) {
$proxies .= ', skip-cert-verify=' . ($tlsSettings->allowInsecure ? "true" : "false"); foreach ($trojan as $item) {
} // [Proxy]
} $proxies .= Surge::buildTrojan($user->uuid, $item);
if ($item->network == 'ws') {
$proxies .= ', ws=true';
if ($item->networkSettings) {
$wsSettings = json_decode($item->networkSettings);
if (isset($wsSettings->path)) $proxies .= ', ws-path=' . $wsSettings->path;
if (isset($wsSettings->headers->Host)) $proxies .= ', ws-headers=host:' . $wsSettings->headers->Host;
}
}
$proxies .= "\r\n";
// [Proxy Group] // [Proxy Group]
$proxyGroup .= $item->name . ', '; $proxyGroup .= $item->name . ', ';
} }
@ -111,9 +104,9 @@ class ClientController extends Controller
$subsURL .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; $subsURL .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
} }
$config = str_replace('$subs_link',$subsURL,$config); $config = str_replace('$subs_link', $subsURL, $config);
$config = str_replace('$proxies',$proxies,$config); $config = str_replace('$proxies', $proxies, $config);
$config = str_replace('$proxy_group',rtrim($proxyGroup, ', '),$config); $config = str_replace('$proxy_group', rtrim($proxyGroup, ', '), $config);
return $config; return $config;
} }

36
app/Utils/Surge.php Normal file
View File

@ -0,0 +1,36 @@
<?php
namespace App\Utils;
class Surge
{
public static function buildVmess($uuid, $server)
{
$proxies = $server->name . ' = vmess, ' . $server->host . ', ' . $server->port . ', username=' . $uuid . ', tfo=true';
if ($server->tls) {
$tlsSettings = json_decode($server->tlsSettings);
$proxies .= ', tls=' . ($server->tls ? "true" : "false");
if (isset($tlsSettings->allowInsecure)) {
$proxies .= ', skip-cert-verify=' . ($tlsSettings->allowInsecure ? "true" : "false");
}
}
if ($server->network == 'ws') {
$proxies .= ', ws=true';
if ($server->networkSettings) {
$wsSettings = json_decode($server->networkSettings);
if (isset($wsSettings->path)) $proxies .= ', ws-path=' . $wsSettings->path;
if (isset($wsSettings->headers->Host)) $proxies .= ', ws-headers=host:' . $wsSettings->headers->Host;
}
}
$proxies .= "\r\n";
return $proxies;
}
public static function buildTrojan($password, $server)
{
$uri = "{$server->name} = trojan, {$server->host}, {$server->port}, password={$password}";
$uri .= "\r\n";
return $uri;
}
}

View File

@ -1,36 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}

View File

@ -1,32 +0,0 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePasswordResetsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('password_resets', function (Blueprint $table) {
$table->string('email')->index();
$table->string('token');
$table->timestamp('created_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('password_resets');
}
}