1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* RoutesTablesController.php |
4
|
|
|
* |
5
|
|
|
* Route tables data for bootgrid display |
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 PipoCanaja |
23
|
|
|
* @author PipoCanaja |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace App\Http\Controllers\Table; |
27
|
|
|
|
28
|
|
|
use App\Models\Device; |
29
|
|
|
use App\Models\Route; |
30
|
|
|
use Illuminate\Database\Eloquent\Builder; |
31
|
|
|
use Illuminate\Http\Request; |
32
|
|
|
use LibreNMS\Util\IP; |
33
|
|
|
use LibreNMS\Util\Url; |
34
|
|
|
|
35
|
|
|
class RoutesTablesController extends TableController |
36
|
|
|
{ |
37
|
|
|
protected $ipCache = []; |
38
|
|
|
|
39
|
|
|
protected function rules() |
40
|
|
|
{ |
41
|
|
|
return [ |
42
|
|
|
'device_id' => 'nullable|integer', |
43
|
|
|
'searchby' => 'in:inetCidrRouteNextHop,inetCidrRouteDest', |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function filterFields($request) |
48
|
|
|
{ |
49
|
|
|
return [ |
50
|
|
|
'route.context_name' => 'context_name', |
51
|
|
|
'route.inetCidrRouteProto' => 'proto', |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected function sortFields($request) |
56
|
|
|
{ |
57
|
|
|
return [ |
58
|
|
|
'context_name', |
59
|
|
|
'inetCidrRouteDestType', |
60
|
|
|
'inetCidrRouteDest', |
61
|
|
|
'inetCidrRoutePfxLen', |
62
|
|
|
'inetCidrRouteNextHop', |
63
|
|
|
'inetCidrRouteIfIndex', |
64
|
|
|
'inetCidrRouteMetric1', |
65
|
|
|
'inetCidrRouteType', |
66
|
|
|
'inetCidrRouteProto', |
67
|
|
|
'created_at', |
68
|
|
|
'updated_at', |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Defines the base query for this resource |
74
|
|
|
* |
75
|
|
|
* @param \Illuminate\Http\Request $request |
76
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder |
77
|
|
|
*/ |
78
|
|
|
protected function baseQuery($request) |
79
|
|
|
{ |
80
|
|
|
$join = function ($query) { |
81
|
|
|
$query->on('ports.port_id', 'route.port_id'); |
82
|
|
|
}; |
83
|
|
|
$showAllRoutes = trim(\Request::get('showAllRoutes')); |
|
|
|
|
84
|
|
|
$showProtocols = trim(\Request::get('showProtocols')); |
85
|
|
|
if ($showProtocols == 'all') { |
86
|
|
|
$protocols = ['ipv4', 'ipv6']; |
87
|
|
|
} else { |
88
|
|
|
$protocols = [$showProtocols]; |
89
|
|
|
} |
90
|
|
|
if ($request->device_id && $showAllRoutes == 'false') { |
91
|
|
|
$query = Route::hasAccess($request->user()) |
92
|
|
|
->leftJoin('ports', $join) |
93
|
|
|
->where('route.device_id', $request->device_id) |
94
|
|
|
->whereIn('route.inetCidrRouteDestType', $protocols) |
95
|
|
|
->where('updated_at', Route::hasAccess($request->user()) |
96
|
|
|
->where('route.device_id', $request->device_id) |
97
|
|
|
->select('updated_at') |
98
|
|
|
->max('updated_at')); |
99
|
|
|
|
100
|
|
|
return $query; |
101
|
|
|
} |
102
|
|
|
if ($request->device_id && $showAllRoutes == 'true') { |
103
|
|
|
$query = Route::hasAccess($request->user()) |
104
|
|
|
->leftJoin('ports', $join) |
105
|
|
|
->where('route.device_id', $request->device_id) |
106
|
|
|
->whereIn('route.inetCidrRouteDestType', $protocols); |
107
|
|
|
|
108
|
|
|
return $query; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return Route::hasAccess($request->user()) |
112
|
|
|
->leftJoin('ports', $join); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $search |
117
|
|
|
* @param Builder $query |
118
|
|
|
* @param array $fields |
119
|
|
|
* @return Builder|\Illuminate\Database\Query\Builder |
120
|
|
|
*/ |
121
|
|
|
protected function search($search, $query, $fields = []) |
122
|
|
|
{ |
123
|
|
|
if ($search = trim(\Request::get('searchPhrase'))) { |
|
|
|
|
124
|
|
|
$searchLike = '%' . $search . '%'; |
125
|
|
|
|
126
|
|
|
return $query->where(function ($query) use ($searchLike) { |
127
|
|
|
return $query->where('route.inetCidrRouteNextHop', 'like', $searchLike) |
128
|
|
|
->orWhere('route.inetCidrRouteDest', 'like', $searchLike); |
129
|
|
|
}); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $query; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param Request $request |
137
|
|
|
* @param Builder $query |
138
|
|
|
* @return Builder |
139
|
|
|
*/ |
140
|
|
|
public function sort($request, $query) |
141
|
|
|
{ |
142
|
|
|
$sort = $request->get('sort'); |
143
|
|
|
if (isset($sort['inetCidrRouteIfIndex'])) { |
144
|
|
|
$query->orderBy('ifDescr', $sort['inetCidrRouteIfIndex']) |
145
|
|
|
->orderBy('inetCidrRouteIfIndex', $sort['inetCidrRouteIfIndex']); |
146
|
|
|
} |
147
|
|
|
// Simple fields to sort |
148
|
|
|
$s_fields = [ |
149
|
|
|
'inetCidrRouteDestType', |
150
|
|
|
'inetCidrRouteType', |
151
|
|
|
'inetCidrRouteMetric1', |
152
|
|
|
'inetCidrRoutePfxLen', |
153
|
|
|
'inetCidrRouteNextHop', |
154
|
|
|
'updated_at', |
155
|
|
|
'created_at', |
156
|
|
|
'context_name', |
157
|
|
|
'inetCidrRouteDest', |
158
|
|
|
]; |
159
|
|
|
foreach ($s_fields as $s_field) { |
160
|
|
|
if (isset($sort[$s_field])) { |
161
|
|
|
$query->orderBy($s_field, $sort[$s_field]); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $query; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param Route $route_entry |
170
|
|
|
*/ |
171
|
|
|
public function formatItem($route_entry) |
172
|
|
|
{ |
173
|
|
|
$item['updated_at'] = $route_entry->updated_at ? $route_entry->updated_at->diffForHumans() : $route_entry->updated_at; |
|
|
|
|
174
|
|
|
$item['created_at'] = $route_entry->created_at ? $route_entry->created_at->toDateTimeString() : $route_entry->created_at; |
175
|
|
|
$item['inetCidrRouteIfIndex'] = $route_entry->inetCidrRouteIfIndex == 0 ? 'Undefined' : $route_entry->inetCidrRouteIfIndex; |
176
|
|
|
$item['inetCidrRouteMetric1'] = $route_entry->inetCidrRouteMetric1; |
177
|
|
|
$item['inetCidrRoutePfxLen'] = $route_entry->inetCidrRoutePfxLen; |
178
|
|
|
$item['inetCidrRouteDestType'] = $route_entry->inetCidrRouteDestType; |
179
|
|
|
|
180
|
|
|
try { |
181
|
|
|
$obj_inetCidrRouteDest = IP::parse($route_entry->inetCidrRouteDest); |
182
|
|
|
$item['inetCidrRouteDest'] = $obj_inetCidrRouteDest->compressed(); |
183
|
|
|
} catch (\Exception $e) { |
184
|
|
|
$item['inetCidrRouteDest'] = $route_entry->inetCidrRouteDest; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$item['inetCidrRouteIfIndex'] = $route_entry->inetCidrRouteIfIndex == 0 ? 'Undefined' : 'IfIndex ' . $route_entry->inetCidrRouteIfIndex; |
188
|
|
|
if ($port = $route_entry->port()->first()) { |
189
|
|
|
$item['inetCidrRouteIfIndex'] = Url::portLink($port, htmlspecialchars($port->getShortLabel())); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
try { |
193
|
|
|
$obj_inetCidrRouteNextHop = IP::parse($route_entry->inetCidrRouteNextHop); |
194
|
|
|
$item['inetCidrRouteNextHop'] = $obj_inetCidrRouteNextHop->compressed(); |
195
|
|
|
} catch (\Exception $e) { |
196
|
|
|
$item['inetCidrRouteNextHop'] = $route_entry->inetCidrRouteNextHop; |
197
|
|
|
} |
198
|
|
|
$device = Device::findByIp($route_entry->inetCidrRouteNextHop); |
199
|
|
|
if ($device) { |
200
|
|
|
if ($device->device_id == $route_entry->device_id || in_array($route_entry->inetCidrRouteNextHop, ['127.0.0.1', '::1'])) { |
|
|
|
|
201
|
|
|
$item['inetCidrRouteNextHop'] = Url::deviceLink($device, 'localhost'); |
202
|
|
|
} else { |
203
|
|
|
$item['inetCidrRouteNextHop'] = $item['inetCidrRouteNextHop'] . '<br>(' . Url::deviceLink($device) . ')'; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$item['inetCidrRouteProto'] = $route_entry->inetCidrRouteProto; |
208
|
|
|
if ($route_entry->inetCidrRouteProto && $route_entry::$translateProto[$route_entry->inetCidrRouteProto]) { |
209
|
|
|
$item['inetCidrRouteProto'] = $route_entry::$translateProto[$route_entry->inetCidrRouteProto]; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$item['inetCidrRouteType'] = $route_entry->inetCidrRouteType; |
213
|
|
|
if ($route_entry->inetCidrRouteType && $route_entry::$translateType[$route_entry->inetCidrRouteType]) { |
214
|
|
|
$item['inetCidrRouteType'] = $route_entry::$translateType[$route_entry->inetCidrRouteType]; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$item['context_name'] = '[global]'; |
218
|
|
|
if ($route_entry->context_name != '') { |
219
|
|
|
$item['context_name'] = '<a href="' . Url::generate(['page' => 'routing', 'protocol' => 'vrf', 'vrf' => $route_entry->context_name]) . '">' . htmlspecialchars($route_entry->context_name) . '</a>'; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $item; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|