Test Setup Failed
Push — master ( 76a7d8...21c3b6 )
by Nikita
07:04 queued 14s
created

DedicatedServersController::downloadLogs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Gameap\Http\Controllers\Admin;
4
5
use Gameap\Helpers\OsHelper;
6
use Gameap\Http\Controllers\AuthController;
7
use Gameap\Http\Requests\Admin\DedicatedServerRequest;
8
use Gameap\Models\ClientCertificate;
9
use Gameap\Models\DedicatedServer;
10
use Gameap\Repositories\DedicatedServersRepository;
11
use Gameap\Services\Daemon\CertificateService;
12
use Gameap\Services\Daemon\DebugService;
13
use Illuminate\Support\Facades\Cache;
14
use Illuminate\Support\Facades\Storage;
15
use Illuminate\Support\Str;
16
use Knik\Gameap\GdaemonStatus;
17
use RuntimeException;
18
use ZipArchive;
19
20
class DedicatedServersController extends AuthController
21
{
22
    /**
23
     * The DedicatedServersRepository instance.
24
     *
25
     * @var \Gameap\Repositories\DedicatedServersRepository
26
     */
27
    protected $repository;
28
29
    /** @var DebugService */
30
    protected $debugService;
31
32
    public function __construct(
33
        DedicatedServersRepository $repository,
34
        DebugService $downloadDebugService
35
    ) {
36
        parent::__construct();
37
38
        $this->repository           = $repository;
39
        $this->debugService = $downloadDebugService;
40
    }
41
42
    /**
43
     * Display a listing of the resource.
44
     *
45
     * @return \Illuminate\View\View
46
     */
47
    public function index()
48
    {
49
        $dedicatedServers = $this->repository->getAll();
50
51
        return view('admin.dedicated_servers.list', [
52
            'dedicatedServers' => $dedicatedServers,
53
        ]);
54
    }
55
56
    /**
57
     * Show the form for creating a new resource.
58
     *
59
     * @return \Illuminate\View\View
60
     */
61
    public function create()
62
    {
63
        // Add auto setup token
64
        $autoSetupToken = env('DAEMON_SETUP_TOKEN');
65
66
        if (empty($autoSetupToken)) {
67
            $autoSetupToken = Str::random(24);
68
            Cache::put('gdaemonAutoSetupToken', $autoSetupToken, 300);
69
        }
70
71
        $clientCertificates = ClientCertificate::all(['id', 'fingerprint'])->pluck('fingerprint', 'id');
72
        return view('admin.dedicated_servers.create', compact('clientCertificates', 'autoSetupToken'));
73
    }
74
75
    /**
76
     * Store a newly created resource in storage.
77
     *
78
     * @param  \Gameap\Http\Requests\Admin\DedicatedServerRequest  $request
79
     * @return \Illuminate\Http\RedirectResponse
80
     */
81
    public function store(DedicatedServerRequest $request)
82
    {
83
        $attributes = $request->all();
84
85
        if ($request->hasFile('gdaemon_server_cert')) {
86
            $attributes['gdaemon_server_cert'] = $request->file('gdaemon_server_cert')->store(
87
                'certs/server',
88
                'local'
89
            );
90
        }
91
92
        $this->repository->store($attributes);
93
94
        return redirect()->route('admin.dedicated_servers.index')
95
            ->with('success', __('dedicated_servers.create_success_msg'));
96
    }
97
98
    /**
99
     * Display the specified resource.
100
     *
101
     * @param \Gameap\Models\DedicatedServer $dedicatedServer
102
     * @param GdaemonStatus $gdaemonStatus
103
     * @return \Illuminate\View\View
104
     */
105
    public function show(DedicatedServer $dedicatedServer, GdaemonStatus $gdaemonStatus)
106
    {
107
        $gdaemonStatus->setConfig($dedicatedServer->gdaemonSettings());
108
109
        try {
110
            $gdaemonVersion = $gdaemonStatus->version();
111
            $baseInfo       = $gdaemonStatus->infoBase();
112
        } catch (RuntimeException $e) {
113
            $gdaemonVersion = [];
114
            $baseInfo       = [];
115
        }
116
117
        return view(
118
            'admin.dedicated_servers.view',
119
            compact(
120
                'dedicatedServer',
121
                'gdaemonVersion',
122
                'baseInfo'
123
            )
124
        );
125
    }
126
127
    /**
128
     * Show the form for editing the specified resource.
129
     *
130
     * @param  \Gameap\Models\DedicatedServer  $dedicatedServer
131
     * @return \Illuminate\View\View
132
     */
133
    public function edit(DedicatedServer $dedicatedServer)
134
    {
135
        $clientCertificates = ClientCertificate::all(['id', 'fingerprint'])->pluck('fingerprint', 'id');
136
        return view('admin.dedicated_servers.edit', compact('dedicatedServer', 'clientCertificates'));
137
    }
138
139
    /**
140
     * Update the specified resource in storage.
141
     *
142
     * @param  \Gameap\Http\Requests\Admin\DedicatedServerRequest  $request
143
     * @param  \Gameap\Models\DedicatedServer  $dedicatedServer
144
     * @return \Illuminate\Http\RedirectResponse
145
     */
146
    public function update(DedicatedServerRequest $request, DedicatedServer $dedicatedServer)
147
    {
148
        $attributes = $request->all();
149
        
150
        if ($request->hasFile('gdaemon_server_cert')) {
151
            $attributes['gdaemon_server_cert'] = $request->file('gdaemon_server_cert')->store(
152
                'certs/server',
153
                'local'
154
            );
155
156
            $certificateFile = Storage::disk('local')
157
                ->getDriver()
158
                ->getAdapter()
159
                ->applyPathPrefix($dedicatedServer->gdaemon_server_cert);
160
            
161
            if (file_exists($certificateFile)) {
162
                unlink($certificateFile);
163
            }
164
        }
165
        
166
        $this->repository->update($dedicatedServer, $attributes);
167
168
        return redirect()->route('admin.dedicated_servers.index')
169
            ->with('success', __('dedicated_servers.update_success_msg'));
170
    }
171
172
    /**
173
     * Remove the specified resource from storage.
174
     *
175
     * @param  \Gameap\Models\DedicatedServer  $dedicatedServer
176
     * @return \Illuminate\Http\RedirectResponse
177
     */
178
    public function destroy(DedicatedServer $dedicatedServer)
179
    {
180
        $this->repository->destroy($dedicatedServer);
181
182
        return redirect()->route('admin.dedicated_servers.index')
183
            ->with('success', __('dedicated_servers.delete_success_msg'));
184
    }
185
186
    public function logsZip(DedicatedServer $dedicatedServer)
187
    {
188
        try {
189
            $zipPath = $this->debugService->downloadLogs($dedicatedServer);
190
        } catch (RuntimeException $exception) {
191
            return redirect()->route('admin.dedicated_servers.show', [$dedicatedServer->id])
192
                ->with('error', $exception->getMessage());
193
        }
194
195
        return response()->download($zipPath, "logs.zip");
196
    }
197
198
    public function certificatesZip(DedicatedServer $dedicatedServer)
0 ignored issues
show
Unused Code introduced by
The parameter $dedicatedServer is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

198
    public function certificatesZip(/** @scrutinizer ignore-unused */ DedicatedServer $dedicatedServer)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
199
    {
200
        $key                     = CertificateService::generateKey();
201
        $csr                     = CertificateService::generateCsr($key);
202
        $serverSignedCertificate = CertificateService::signCsr($csr);
203
204
        $zipFilePath = OsHelper::tempFile();
205
        $zip = new ZipArchive();
206
        $zip->open($zipFilePath, ZipArchive::CREATE);
207
        $zip->addFromString("server.key", $key);
208
        $zip->addFromString("server.crt", $serverSignedCertificate);
209
        $zip->addFromString("ca.crt", CertificateService::getRootCert());
210
        $zip->addFromString(
211
            "README.md",
212
            "* Move this files to certs directory (For linux: /etc/gameap-daemon/certs/)\n" .
213
            "* Edit gameap-daemon configuration, set `ca_certificate_file`, `certificate_chain_file` and `private_key_file`"
214
        );
215
        $zip->close();
216
217
        return response()->download($zipFilePath, "certificates.zip");
218
    }
219
}
220