|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * Looking Glass - An easy to deploy Looking Glass |
| 5 | + * Copyright (C) 2014-2023 Guillaume Mazoyer <guillaume@mazoyer.eu> |
| 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, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | + */ |
| 21 | + |
| 22 | +require_once('router.php'); |
| 23 | +require_once('includes/command_builder.php'); |
| 24 | +require_once('includes/utils.php'); |
| 25 | + |
| 26 | +final class Mikrotik7 extends Router { |
| 27 | + protected function build_bgp($parameter, $routing_instance = false) { |
| 28 | + if (!is_valid_ip_address($parameter)) { |
| 29 | + throw new Exception('The parameter is not an IP address.'); |
| 30 | + } |
| 31 | + |
| 32 | + $cmd = new CommandBuilder(); |
| 33 | + $cmd->add('routing route print'); |
| 34 | + |
| 35 | + if ($this->config['bgp_detail']) { |
| 36 | + $cmd->add('detail'); |
| 37 | + } |
| 38 | + |
| 39 | + $cmd->add('where', quote($parameter).' in dst-address'); |
| 40 | + |
| 41 | + if ($routing_instance !== false) { |
| 42 | + $cmd->add('and routing-table='.$routing_instance); |
| 43 | + } |
| 44 | + |
| 45 | + // Remove blackhole routes |
| 46 | + if (!$this->config['blackhole']) { |
| 47 | + $cmd->add('and !blackhole'); |
| 48 | + } |
| 49 | + if (!$this->config['filtered']) { |
| 50 | + $cmd->add('and !filtered'); |
| 51 | + } |
| 52 | + if (!$this->config['disabled']) { |
| 53 | + $cmd->add('and !disabled'); |
| 54 | + } |
| 55 | + |
| 56 | + return array($cmd); |
| 57 | + } |
| 58 | + |
| 59 | + protected function build_aspath_regexp($parameter, $routing_instance = false) { |
| 60 | + $cmd = new CommandBuilder(); |
| 61 | + $cmd->add('routing route print'); |
| 62 | + |
| 63 | + if ($this->config['bgp_detail']) { |
| 64 | + $cmd->add('detail'); |
| 65 | + } |
| 66 | + |
| 67 | + $cmd->add('where', 'bgp.as-path~'.quote($parameter)); |
| 68 | + |
| 69 | + if ($routing_instance !== false) { |
| 70 | + $cmd->add('and routing-table='.$routing_instance); |
| 71 | + } |
| 72 | + |
| 73 | + // Remove blackhole routes |
| 74 | + if (!$this->config['blackhole']) { |
| 75 | + $cmd->add('and !blackhole'); |
| 76 | + } |
| 77 | + if (!$this->config['filtered']) { |
| 78 | + $cmd->add('and !filtered'); |
| 79 | + } |
| 80 | + if (!$this->config['disabled']) { |
| 81 | + $cmd->add('and !disabled'); |
| 82 | + } |
| 83 | + |
| 84 | + return array($cmd); |
| 85 | + } |
| 86 | + |
| 87 | + protected function build_as($parameter, $routing_instance = false) { |
| 88 | + $cmd = new CommandBuilder(); |
| 89 | + $cmd->add('routing route print'); |
| 90 | + |
| 91 | + if ($this->config['bgp_detail']) { |
| 92 | + $cmd->add('detail'); |
| 93 | + } |
| 94 | + |
| 95 | + $cmd->add('where', 'bgp.as-path~'.quote($parameter.'\$')); |
| 96 | + |
| 97 | + if ($routing_instance !== false) { |
| 98 | + $cmd->add('and routing-table='.$routing_instance); |
| 99 | + } |
| 100 | + |
| 101 | + // Remove blackhole routes |
| 102 | + if (!$this->config['blackhole']) { |
| 103 | + $cmd->add('and !blackhole'); |
| 104 | + } |
| 105 | + if (!$this->config['filtered']) { |
| 106 | + $cmd->add('and !filtered'); |
| 107 | + } |
| 108 | + if (!$this->config['disabled']) { |
| 109 | + $cmd->add('and !disabled'); |
| 110 | + } |
| 111 | + |
| 112 | + return array($cmd); |
| 113 | + //return $commands; |
| 114 | + } |
| 115 | + |
| 116 | + protected function build_ping($parameter, $routing_instance = false) { |
| 117 | + if (!is_valid_destination($parameter)) { |
| 118 | + throw new Exception('The parameter is not an IP address or a hostname.'); |
| 119 | + } |
| 120 | + |
| 121 | + $cmd = new CommandBuilder(); |
| 122 | + $cmd->add('ping count=10'); |
| 123 | + |
| 124 | + if (match_hostname($parameter)) { |
| 125 | + $cmd->add('address=[:resolv '.quote($parameter).']'); |
| 126 | + } else { |
| 127 | + $cmd->add('address='.quote($parameter)); |
| 128 | + } |
| 129 | + |
| 130 | + if ($this->has_source_interface_id()) { |
| 131 | + if (is_valid_ip_address($this->get_source_interface_id())) { |
| 132 | + $cmd->add('src-address='); |
| 133 | + if (match_ipv6($parameter)) { |
| 134 | + $cmd->add($this->get_source_interface_id('ipv6')); |
| 135 | + } else { |
| 136 | + $cmd->add($this->get_source_interface_id('ipv4')); |
| 137 | + } |
| 138 | + } else { |
| 139 | + $cmd->add('interface='.$this->get_source_interface_id()); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + if ($routing_instance !== false) { |
| 144 | + $cmd->add('vrf='.$routing_instance); |
| 145 | + } |
| 146 | + |
| 147 | + return array($cmd); |
| 148 | + } |
| 149 | + protected function build_traceroute($parameter, $routing_instance = false) { |
| 150 | + if (!is_valid_destination($parameter)) { |
| 151 | + throw new Exception('The parameter is not an IP address or a hostname.'); |
| 152 | + } |
| 153 | + |
| 154 | + $cmd = new CommandBuilder(); |
| 155 | + $cmd->add('tool traceroute count=10 use-dns=yes'); |
| 156 | + |
| 157 | + if (match_hostname($parameter)) { |
| 158 | + $cmd->add('address=[:resolv '.quote($parameter).']'); |
| 159 | + } else { |
| 160 | + $cmd->add('address='.quote($parameter)); |
| 161 | + } |
| 162 | + |
| 163 | + if ($this->has_source_interface_id()) { |
| 164 | + if (is_valid_ip_address($this->get_source_interface_id())) { |
| 165 | + $cmd->add('src-address='); |
| 166 | + if (match_ipv6($parameter)) { |
| 167 | + $cmd->add($this->get_source_interface_id('ipv6')); |
| 168 | + } else { |
| 169 | + $cmd->add($this->get_source_interface_id('ipv4')); |
| 170 | + } |
| 171 | + } else { |
| 172 | + $cmd->add('interface='.$this->get_source_interface_id()); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + if ($routing_instance !== false) { |
| 177 | + $cmd->add('vrf='.$routing_instance); |
| 178 | + } |
| 179 | + |
| 180 | + return array($cmd); |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +// End of mikrotik7.php |
0 commit comments