diff --git a/app/Http/Controllers/Api/TunnelController.php b/app/Http/Controllers/Api/TunnelController.php index 7cfeeb6..fcc0e27 100644 --- a/app/Http/Controllers/Api/TunnelController.php +++ b/app/Http/Controllers/Api/TunnelController.php @@ -3,7 +3,10 @@ namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; +use App\Models\Server; +use App\Models\Tunnel; use Illuminate\Http\Request; +use Illuminate\Support\Str; class TunnelController extends Controller { @@ -13,7 +16,7 @@ class TunnelController extends Controller public function index(Request $request) { return $this->success( - $request->user()->tunnels() + $request->user()->tunnels()->get() ); } @@ -22,7 +25,122 @@ public function index(Request $request) */ public function store(Request $request) { - // + $request->validate([ + 'name' => 'required', + 'protocol' => 'required', + 'local_address' => 'required', + 'server_id' => 'required', + ]); + + $data = $request->only([ + 'name', 'protocol', 'local_address', 'server_id', 'remote_port', 'custom_domain', + ]); + + if (!strpos($request->input('local_address'), ':')) { + return $this->error('本地地址必须包含端口号。'); + } + + $local_ip_port = explode(':', $request->input('local_address')); + + // port must be a number + if (!is_numeric($local_ip_port[1])) { + return $this->error('端口号必须是数字。'); + } + + // port must be a number between 1 and 65535 + if ($local_ip_port[1] < 1 || $local_ip_port[1] > 65535) { + return $this->error('本地地址端口号必须在 1 和 65535 之间。'); + } + + $server = Server::find($request->input('server_id')); + + if (is_null($server)) { + return $this->error('找不到服务器。'); + } + + if (Tunnel::where('server_id', $server->id)->count() > $server->max_tunnels) { + return $this->error('服务器无法开设更多隧道了。'); + } + + if ($request->input('protocol') == 'http' || $request->input('protocol') == 'https') { + // if (!auth()->user()->verified_at) { + // return failed('必须要先实名认证才能创建 HTTP(S) 隧道。'); + // } + + if ($request->filled('remote_port')) { + return $this->error('此协议不支持指定远程端口号。'); + } + + $data['remote_port'] = null; + + // 检测 域名格式 是否正确 + if (!preg_match('/^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/', $request->input('custom_domain'))) { + return $this->error('域名格式不正确。'); + } + + if ($request->has('custom_domain')) { + $custom_domain_search = Tunnel::where('server_id', $request->input('server_id'))->where('custom_domain', $request->input('custom_domain'))->where('protocol', $request->input('protocol'))->exists(); + if ($custom_domain_search) { + return $this->error('这个域名已经被使用了'); + } + } else { + return $this->error('必须提供域名。'); + } + + $data['custom_domain'] = Str::lower($request->input('custom_domain')); + + if (str_contains($request->input('custom_domain'), ',')) { + return $this->error('一次请求只能添加一个域名。'); + } + } elseif ($request->input('protocol') == 'tcp' || $request->input('protocol') == 'udp') { + if ($request->filled('custom_domain')) { + return $this->error('此协议不支持指定域名。'); + } + + $data['custom_domain'] = null; + $request->validate([ + 'remote_port' => "required|integer|max:$server->max_port|min:$server->min_port|bail", + ]); + + if ($request->input('remote_port') == $server->server_port || $request->input('remote_port') == $server->dashboard_port) { + return $this->error('无法使用这个远程端口。'); + } + + // 检查端口范围 + if ($request->input('remote_port') < $server->min_port || $request->input('remote_port') > $server->max_port) { + return $this->error('远程端口号必须在 ' . $server->min_port . ' 和 ' . $server->max_port . ' 之间。'); + } + + $remote_port_search = Tunnel::where('server_id', $server->id)->where('remote_port', $request->input('remote_port'))->where('protocol', strtolower($request->protocol))->exists(); + if ($remote_port_search) { + return $this->error('这个远程端口已经被使用了。'); + } + } elseif ($request->input('protocol') === 'stcp' || $request->input('protocol') === 'xtcp') { + $data['custom_domain'] = null; + $data['remote_port'] = null; + + $request->validate(['sk' => 'required|alpha_dash|min:3|max:15']); + + $data['sk'] = $request->input('sk'); + } else { + return $this->error('不支持的协议。'); + } + + $data['protocol'] = Str::lower($data['protocol']); + + $test_protocol = 'allow_' . $data['protocol']; + + if (!$server->$test_protocol) { + return $this->error('服务器不允许这个协议。'); + } + + $tunnel = auth('sanctum')->user()->tunnels()->create($data); + + + // 增加服务器的 tunnel 数量 + $server->increment('tunnels'); + + return $this->created($tunnel); } /** diff --git a/app/Models/User.php b/app/Models/User.php index 4e22adc..60ab3f0 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -4,6 +4,7 @@ // use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Sanctum\HasApiTokens; @@ -49,7 +50,7 @@ public function isAdmin() } // tunnels - public function tunnels() + public function tunnels(): HasMany { return $this->hasMany(Tunnel::class); } diff --git a/resources/js/plugins/router.js b/resources/js/plugins/router.js index 1a4d933..9b6f6aa 100644 --- a/resources/js/plugins/router.js +++ b/resources/js/plugins/router.js @@ -26,6 +26,14 @@ const routes = [ title: "创建隧道", }, }, + { + path: "/tunnels/:id", + name: "tunnels.show", + component: () => import("../views/Tunnels/Show.vue"), + meta: { + title: "隧道", + }, + }, { path: "/servers", name: "servers", @@ -52,6 +60,8 @@ const routes = [ title: "创建服务器", }, }, + + ]; diff --git a/resources/js/views/Servers/Index.vue b/resources/js/views/Servers/Index.vue index af301f0..b8c968f 100644 --- a/resources/js/views/Servers/Index.vue +++ b/resources/js/views/Servers/Index.vue @@ -6,9 +6,12 @@