Browse Source

subs: add Surfboard Shadowsocks support

Signed-off-by: Beta Soft <betaxab@gmail.com>
Beta Soft 4 years ago
parent
commit
c326d0ab5c
2 changed files with 81 additions and 19 deletions
  1. 12 19
      app/Http/Controllers/Client/ClientController.php
  2. 69 0
      app/Utils/Surfboard.php

+ 12 - 19
app/Http/Controllers/Client/ClientController.php

@@ -8,6 +8,7 @@ use App\Utils\Clash;
 use App\Utils\QuantumultX;
 use App\Utils\Shadowrocket;
 use App\Utils\Surge;
+use App\Utils\Surfboard;
 use App\Utils\URLSchemes;
 use Illuminate\Http\Request;
 use App\Models\Server;
@@ -38,7 +39,7 @@ class ClientController extends Controller
                     die($this->clash($user, $servers['shadowsocks'], $servers['vmess'], $servers['trojan']));
                 }
                 if (strpos($_SERVER['HTTP_USER_AGENT'], 'surfboard') !== false) {
-                    die($this->surfboard($user, $servers['vmess']));
+                    die($this->surfboard($user, $servers['shadowsocks'], $servers['vmess']));
                 }
                 if (strpos($_SERVER['HTTP_USER_AGENT'], 'surge') !== false) {
                     die($this->surge($user, $servers['vmess'], $servers['trojan']));
@@ -155,29 +156,21 @@ class ClientController extends Controller
         return $config;
     }
 
-    private function surfboard($user, $vmess = [])
+    private function surfboard($user, $shadowsocks = [], $vmess = [])
     {
         $proxies = '';
         $proxyGroup = '';
+
+        foreach ($shadowsocks as $item) {
+            // [Proxy]
+            $proxies .= Surfboard::buildShadowsocks($user->uuid, $item);
+            // [Proxy Group]
+            $proxyGroup .= $item->name . ', ';
+        }
+
         foreach ($vmess as $item) {
             // [Proxy]
-            $proxies .= $item->name . ' = vmess, ' . $item->host . ', ' . $item->port . ', username=' . $user->uuid;
-            if ($item->tls) {
-                $tlsSettings = json_decode($item->tlsSettings);
-                $proxies .= ', tls=' . ($item->tls ? "true" : "false");
-                if (isset($tlsSettings->allowInsecure)) {
-                  $proxies .= ', skip-cert-verify=' . ($tlsSettings->allowInsecure ? "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=host:' . $wsSettings->headers->Host;
-                }
-            }
-            $proxies .= "\r\n";
+            $proxies .= Surfboard::buildVmess($user->uuid, $item);
             // [Proxy Group]
             $proxyGroup .= $item->name . ', ';
         }

+ 69 - 0
app/Utils/Surfboard.php

@@ -0,0 +1,69 @@
+<?php
+
+namespace App\Utils;
+
+
+class Surfboard
+{
+    public static function buildShadowsocks($password, $server)
+    {
+        $config = [
+            "{$server->name}=custom",
+            "{$server->host}",
+            "{$server->port}",
+            "{$server->cipher}",
+            "password={$password}",
+            "https://raw.githubusercontent.com/Hackl0us/proxy-tool-backup/master/SSEncrypt.module",
+            "tfo=true",
+            "udp-relay=true"
+        ];
+        $config = array_filter($config);
+        $uri = implode(',', $config);
+        $uri .= "\r\n";
+        return $uri;
+    }
+
+    public static function buildVmess($uuid, $server)
+    {
+        $config = [
+            "{$server->name}=vmess",
+            "{$server->host}",
+            "{$server->port}",
+            "username={$uuid}",
+            "tfo=true",
+            "udp-relay=false"
+        ];
+        if ($server->network === 'tcp') {
+            if ($server->tls) {
+                $tlsSettings = json_decode($server->tlsSettings);
+                array_push($config, $server->tls ? 'tls=true' : 'tls=false');
+                if (isset($tlsSettings->allowInsecure)) {
+                    array_push($config, $tlsSettings->allowInsecure ? 'skip-cert-verify=true' : 'skip-cert-verify=false');
+                }
+                if (isset($tlsSettings->serverName)) {
+                    array_push($config, "obfs-host={$tlsSettings->serverName}");
+                }
+            }
+        }
+
+        if ($server->network === 'ws') {
+            array_push($config, 'ws=true');
+            if ($server->tls) {
+                $tlsSettings = json_decode($server->tlsSettings);
+                array_push($config, $server->tls ? 'tls=true' : 'tls=false');
+                if (isset($tlsSettings->allowInsecure)) {
+                    array_push($config, $tlsSettings->allowInsecure ? 'skip-cert-verify=true' : 'skip-cert-verify=false');
+                }
+            }
+            if ($server->networkSettings) {
+                $wsSettings = json_decode($server->networkSettings);
+                if (isset($wsSettings->path)) array_push($config, "ws-path={$wsSettings->path}");
+                if (isset($wsSettings->headers->Host)) array_push($config, "ws-headers=host:{$wsSettings->headers->Host}");
+            }
+        }
+
+        $uri = implode(',', $config);
+        $uri .= "\r\n";
+        return $uri;
+    }
+}