rules: add surge/surfboard support [1/2]

Signed-off-by: Beta Soft <betaxab@gmail.com>
This commit is contained in:
Beta Soft
2020-03-28 10:10:30 +08:00
parent 40e5ee62b1
commit 1c6907fe33
2 changed files with 868 additions and 0 deletions

51
app/Http/Controllers/Client/ClientController.php Executable file → Normal file
View File

@ -38,6 +38,12 @@ class ClientController extends Controller
if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'clash') !== false) {
die($this->clash($user, $server));
}
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Surfboard') !== false) {
die($this->surge($user, $server));
}
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Surge') !== false) {
die($this->surge($user, $server));
}
}
die($this->origin($user, $server));
}
@ -89,6 +95,51 @@ class ClientController extends Controller
return base64_encode($uri);
}
private function surge($user, $server)
{
$proxies = '';
$proxyGroup = '';
foreach ($server as $item) {
// [Proxy]
$proxies .= $item->name . ' = vmess, ' . $item->host . ', ' . $item->port . ', username=' . $user->v2ray_uuid . ', tls=' . ($item->tls ? "true" : "false");
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=' . $wsSettings->headers->Host;
}
}
$proxies .= '\r\n';
// [Proxy Group]
$proxyGroup .= $item->name . ', ';
}
try {
$rules = '';
foreach (glob(base_path() . '/resources/rules/' . '*.surge.conf') as $file) {
$rules = file_get_contents("$file");
}
} catch (\Exception $e) {}
// Subscription link
$subsURL = 'http';
if ($_SERVER['HTTPS'] == 'on') {
$subsURL .= 's';
}
$subsURL .= '://';
if ($_SERVER['SERVER_PORT'] != ('80' || '443')) {
$subsURL .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
} else {
$subsURL .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
}
$rules = str_replace('{subs_link}',$subsURL,$rules);
$rules = str_replace('{proxies}',$proxies,$rules);
$rules = str_replace('{proxy_group}',rtrim($proxyGroup, ', '),$rules);
return $rules;
}
private function clash($user, $server)
{
$proxy = [];