belongsTo(Server::class); } public function user(): BelongsTo { return $this->belongsTo(User::class); } public function getConfig() { return (new TunnelController)->generateConfig($this); } public function close() { if ($this->run_id) { $frp = new Frp($this->server); $closed = $frp->close($this->run_id); if ($closed) { $cache_key = 'frpTunnel_data_' . $this->client_token; Cache::forget($cache_key); $cache_key = 'frpTunnel_data_' . $this->client_token . '_lock'; Cache::put($cache_key, 1, 30); $this->run_id = null; $this->saveQuietly(); } return true; } return false; } protected static function boot() { parent::boot(); static::creating(function (self $tunnel) { $tunnel->client_token = Str::random(18); }); static::updated(function (self $tunnel) { if ($tunnel->locked_reason) { $tunnel->close(); } }); static::deleted(function (self $tunnel) { $tunnel->close(); }); } }