Issues (2963)

app/Http/Controllers/Ajax/NetCommand.php (1 issue)

1
<?php
2
/**
3
 * NetCommand.php
4
 *
5
 * -Description-
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 *
20
 * @link       https://www.librenms.org
21
 *
22
 * @copyright  2019 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace App\Http\Controllers\Ajax;
27
28
use App\Http\Controllers\Controller;
29
use Illuminate\Http\Request;
30
use Illuminate\Support\Str;
31
use LibreNMS\Config;
32
use Symfony\Component\HttpFoundation\StreamedResponse;
33
use Symfony\Component\Process\Process;
34
35
class NetCommand extends Controller
36
{
37
    public function run(Request $request)
38
    {
39
        $this->validate($request, [
40
            'cmd' => 'in:whois,ping,tracert,nmap',
41
            'query' => 'ip_or_hostname',
42
        ]);
43
44
        ini_set('allow_url_fopen', '0');
45
46
        switch ($request->get('cmd')) {
47
            case 'whois':
48
                $cmd = [Config::get('whois', 'whois'), $request->get('query')];
49
                break;
50
            case 'ping':
51
                $cmd = [Config::get('ping', 'ping'), '-c', '5', $request->get('query')];
52
                break;
53
            case 'tracert':
54
                $cmd = [Config::get('mtr', 'mtr'), '-r', '-c', '5', $request->get('query')];
55
                break;
56
            case 'nmap':
57
                if (! $request->user()->isAdmin()) {
58
                    return response('Insufficient privileges');
59
                } else {
60
                    $cmd = [Config::get('nmap', 'nmap'), $request->get('query')];
61
                }
62
                break;
63
            default:
64
                return response('Invalid command');
65
        }
66
67
        $proc = new Process($cmd);
68
        $proc->setTimeout(240);
69
70
        //stream output
71
        return (new StreamedResponse(
72
            function () use ($proc, $request) {
73
                // a bit dirty, bust browser initial cache
74
                $ua = $request->header('User-Agent');
75
                if (Str::contains($ua, ['Chrome', 'Trident'])) {
0 ignored issues
show
It seems like $ua can also be of type array; however, parameter $haystack of Illuminate\Support\Str::contains() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

75
                if (Str::contains(/** @scrutinizer ignore-type */ $ua, ['Chrome', 'Trident'])) {
Loading history...
76
                    $char = "\f"; // line feed
77
                } else {
78
                    $char = '';
79
                }
80
                echo str_repeat($char, 4096);
81
                echo PHP_EOL; // avoid first line mess ups due to line feed
82
83
                $proc->run(function ($type, $buffer) {
84
                    echo $buffer;
85
                    ob_flush();
86
                    flush();
87
                });
88
            },
89
            200,
90
            [
91
                'Content-Type' => 'text/plain; charset=utf-8',
92
                'X-Accel-Buffering' => 'no',
93
            ]
94
        ))->send();
95
    }
96
}
97