| Total Complexity | 141 |
| Total Lines | 1032 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Network often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Network, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Network extends Injectable |
||
| 21 | { |
||
| 22 | |||
| 23 | public static function startSipDump(): void |
||
| 24 | { |
||
| 25 | $config = new MikoPBXConfig(); |
||
| 26 | $use = $config->getGeneralSettings('USE_PCAP_SIP_DUMP'); |
||
| 27 | if ($use !== '1') { |
||
| 28 | return; |
||
| 29 | } |
||
| 30 | |||
| 31 | Processes::killByName('pcapsipdump'); |
||
| 32 | $log_dir = System::getLogDir() . '/pcapsipdump'; |
||
| 33 | Util::mwMkdir($log_dir); |
||
| 34 | |||
| 35 | $network = new Network(); |
||
| 36 | $arr_eth = $network->getInterfacesNames(); |
||
| 37 | $pcapsipdumpPath = Util::which('pcapsipdump'); |
||
| 38 | foreach ($arr_eth as $eth) { |
||
| 39 | $pid_file = "/var/run/pcapsipdump_{$eth}.pid"; |
||
| 40 | Processes::mwExecBg( |
||
| 41 | $pcapsipdumpPath . ' -T 120 -P ' . $pid_file . ' -i ' . $eth . ' -m \'^(INVITE|REGISTER)$\' -L ' . $log_dir . '/dump.db' |
||
| 42 | ); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Имена всех подключенных сетевых интерфейсов. |
||
| 48 | */ |
||
| 49 | public function getInterfacesNames() |
||
| 50 | { |
||
| 51 | // Универсальная команда для получения всех PCI сетевых интерфейсов. |
||
| 52 | $lsPath = Util::which('ls'); |
||
| 53 | $grepPath = Util::which('grep'); |
||
| 54 | $awkPath = Util::which('awk'); |
||
| 55 | Processes::mwExec("{$lsPath} -l /sys/class/net | {$grepPath} devices | {$grepPath} -v virtual | {$awkPath} '{ print $9 }'", $names); |
||
| 56 | |||
| 57 | return $names; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Up loopback. |
||
| 62 | **/ |
||
| 63 | public function loConfigure() |
||
| 64 | { |
||
| 65 | if (Util::isSystemctl()) { |
||
| 66 | return; |
||
| 67 | } |
||
| 68 | $busyboxPath = Util::which('busybox'); |
||
| 69 | $ifconfigPath = Util::which('ifconfig'); |
||
| 70 | Processes::mwExec("{$busyboxPath} {$ifconfigPath} lo 127.0.0.1"); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Generates resolv.conf |
||
| 75 | **/ |
||
| 76 | public function resolvConfGenerate(): void |
||
| 77 | { |
||
| 78 | $resolv_conf = ''; |
||
| 79 | $data_hostname = self::getHostName(); |
||
| 80 | if (trim($data_hostname['domain']) !== '') { |
||
| 81 | $resolv_conf .= "domain {$data_hostname['domain']}\n"; |
||
| 82 | } |
||
| 83 | |||
| 84 | $resolv_conf .= "nameserver 127.0.0.1\n"; |
||
| 85 | |||
| 86 | $named_dns = []; |
||
| 87 | $dns = $this->getHostDNS(); |
||
| 88 | foreach ($dns as $ns) { |
||
| 89 | if (trim($ns) === '') { |
||
| 90 | continue; |
||
| 91 | } |
||
| 92 | $named_dns[] = $ns; |
||
| 93 | $resolv_conf .= "nameserver {$ns}\n"; |
||
| 94 | } |
||
| 95 | if (count($dns) === 0) { |
||
| 96 | $resolv_conf .= "nameserver 4.4.4.4\n"; |
||
| 97 | $named_dns[] .= "8.8.8.8"; |
||
| 98 | } |
||
| 99 | |||
| 100 | if (Util::isSystemctl()) { |
||
| 101 | $s_resolv_conf = "[Resolve]\n" |
||
| 102 | . "DNS=127.0.0.1\n"; |
||
| 103 | if (trim($data_hostname['domain']) !== '') { |
||
| 104 | $s_resolv_conf .= "Domains={$data_hostname['domain']}\n"; |
||
| 105 | } |
||
| 106 | file_put_contents('/etc/systemd/resolved.conf', $s_resolv_conf); |
||
| 107 | $systemctlPath = Util::which('systemctl'); |
||
| 108 | Processes::mwExec("{$systemctlPath} restart systemd-resolved"); |
||
| 109 | } else { |
||
| 110 | file_put_contents('/etc//resolv.conf', $resolv_conf); |
||
| 111 | } |
||
| 112 | |||
| 113 | $this->generatePdnsdConfig($named_dns); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Возвращает имя сервера в виде ассоциативного массива. |
||
| 118 | * |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | public static function getHostName(): array |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Возвращает массив DNS серверов из настроек. |
||
| 140 | * |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | public function getHostDNS(): array |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Настройка кэширующего DNS сервера. |
||
| 162 | * |
||
| 163 | * @param $named_dns |
||
| 164 | */ |
||
| 165 | public function generatePdnsdConfig($named_dns): void |
||
| 166 | { |
||
| 167 | $tempDir = $this->di->getShared('config')->path('core.tempDir'); |
||
| 168 | $cache_dir = $tempDir . '/pdnsd/cache'; |
||
| 169 | Util::mwMkdir($cache_dir); |
||
| 170 | |||
| 171 | $conf = 'global {' . "\n" . |
||
| 172 | ' perm_cache=10240;' . "\n" . |
||
| 173 | ' cache_dir="' . $cache_dir . '";' . "\n" . |
||
| 174 | ' pid_file = /var/run/pdnsd.pid;' . "\n" . |
||
| 175 | ' run_as="nobody";' . "\n" . |
||
| 176 | ' server_ip = 127.0.0.1;' . "\n" . |
||
| 177 | ' status_ctl = on;' . "\n" . |
||
| 178 | ' query_method=udp_tcp;' . "\n" . |
||
| 179 | ' min_ttl=15m;' . "\n" . |
||
| 180 | ' max_ttl=1w;' . "\n" . |
||
| 181 | ' timeout=10;' . "\n" . |
||
| 182 | ' neg_domain_pol=on;' . "\n" . |
||
| 183 | ' run_as=root;' . "\n" . |
||
| 184 | ' daemon=on;' . "\n" . |
||
| 185 | '}' . "\n" . |
||
| 186 | 'server {' . "\n" . |
||
| 187 | ' label = "main";' . "\n" . |
||
| 188 | ' ip = ' . implode(', ', $named_dns) . ';' . "\n" . |
||
| 189 | ' interface=lo;' . "\n" . |
||
| 190 | ' uptest=if;' . "\n" . |
||
| 191 | ' interval=10m;' . "\n" . |
||
| 192 | ' purge_cache=off;' . "\n" . |
||
| 193 | '}'; |
||
| 194 | |||
| 195 | $pdnsdConfFile = '/etc/pdnsd.conf'; |
||
| 196 | $savedConf = ''; |
||
| 197 | if(file_exists($pdnsdConfFile)){ |
||
| 198 | $savedConf = file_get_contents($pdnsdConfFile); |
||
| 199 | } |
||
| 200 | if($savedConf != $conf){ |
||
| 201 | file_put_contents($pdnsdConfFile, $conf); |
||
| 202 | } |
||
| 203 | $pdnsdPath = Util::which('pdnsd'); |
||
| 204 | $pid = Processes::getPidOfProcess($pdnsdPath); |
||
| 205 | if (!empty($pid) && $savedConf === $conf) { |
||
| 206 | // Выполним дополнительную проверку, работает ли сервер. |
||
| 207 | $resultResolve = gethostbynamel('lic.miko.ru'); |
||
| 208 | if($resultResolve !== false){ |
||
| 209 | // Ничего делать не нужно. Конфиг не изменился. Рестарт не требуется. |
||
| 210 | return; |
||
| 211 | } |
||
| 212 | // Выполним reload сервера DNS. |
||
| 213 | } |
||
| 214 | |||
| 215 | if (!empty($pid)) { |
||
| 216 | // Завершаем процесс. |
||
| 217 | $busyboxPath = Util::which('busybox'); |
||
| 218 | Processes::mwExec("{$busyboxPath} kill '$pid'"); |
||
| 219 | } |
||
| 220 | if (Util::isSystemctl()) { |
||
| 221 | $systemctlPath = Util::which('systemctl'); |
||
| 222 | Processes::mwExec("{$systemctlPath} restart pdnsd"); |
||
| 223 | } else { |
||
| 224 | Processes::mwExec("{$pdnsdPath} -c /etc/pdnsd.conf -4"); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Configures LAN interface |
||
| 230 | * |
||
| 231 | * @return int |
||
| 232 | */ |
||
| 233 | public function lanConfigure(): int |
||
| 234 | { |
||
| 235 | if (Util::isSystemctl()) { |
||
| 236 | $this->lanConfigureSystemCtl(); |
||
| 237 | $this->openVpnConfigure(); |
||
| 238 | |||
| 239 | return 0; |
||
| 240 | } |
||
| 241 | $busyboxPath = Util::which('busybox'); |
||
| 242 | $vconfigPath = Util::which('vconfig'); |
||
| 243 | $killallPath = Util::which('killall'); |
||
| 244 | |||
| 245 | $networks = $this->getGeneralNetSettings(); |
||
| 246 | $arr_commands = []; |
||
| 247 | $arr_commands[] = "{$killallPath} udhcpc"; |
||
| 248 | $eth_mtu = []; |
||
| 249 | foreach ($networks as $if_data) { |
||
| 250 | if ($if_data['disabled'] === '1') { |
||
| 251 | continue; |
||
| 252 | } |
||
| 253 | |||
| 254 | $if_name = $if_data['interface']; |
||
| 255 | $if_name = escapeshellcmd(trim($if_name)); |
||
| 256 | if (empty($if_name)) { |
||
| 257 | continue; |
||
| 258 | } |
||
| 259 | |||
| 260 | $data_hostname = self::getHostName(); |
||
| 261 | $hostname = $data_hostname['hostname']; |
||
| 262 | |||
| 263 | if ($if_data['vlanid'] > 0) { |
||
| 264 | // Переопределяем имя интерфейса. |
||
| 265 | $arr_commands[] = "{$vconfigPath} set_name_type VLAN_PLUS_VID_NO_PAD"; |
||
| 266 | // Добавляем новый интерфейс. |
||
| 267 | $arr_commands[] = "{$vconfigPath} add {$if_data['interface_orign']} {$if_data['vlanid']}"; |
||
| 268 | } |
||
| 269 | // Отключаем интерфейс. |
||
| 270 | $arr_commands[] = "{$busyboxPath} ifconfig $if_name down"; |
||
| 271 | $arr_commands[] = "{$busyboxPath} ifconfig $if_name 0.0.0.0"; |
||
| 272 | |||
| 273 | $gw_param = ''; |
||
| 274 | if (trim($if_data['dhcp']) === '1') { |
||
| 275 | /* |
||
| 276 | * -t - количество попыток. |
||
| 277 | * -T - таймаут попытки. |
||
| 278 | * -v - включить отладку. |
||
| 279 | * -S - логи в syslog. |
||
| 280 | * -q - Exit after obtaining lease |
||
| 281 | * -n - Exit if lease is not obtained |
||
| 282 | */ |
||
| 283 | $pid_file = "/var/run/udhcpc_{$if_name}"; |
||
| 284 | $pid_pcc = Processes::getPidOfProcess($pid_file); |
||
| 285 | if ( ! empty($pid_pcc) && file_exists($pid_file)) { |
||
| 286 | // Завершаем старый процесс. |
||
| 287 | $killPath = Util::which('kill'); |
||
| 288 | $catPath = Util::which('cat'); |
||
| 289 | system("{$killPath} `{$catPath} {$pid_file}` {$pid_pcc}"); |
||
| 290 | } |
||
| 291 | $udhcpcPath = Util::which('udhcpc'); |
||
| 292 | $nohupPath = Util::which('nohup'); |
||
| 293 | |||
| 294 | // Получаем IP и дожидаемся завершения процесса. |
||
| 295 | $workerPath = '/etc/rc/udhcpc.configure'; |
||
| 296 | $options = '-t 6 -T 5 -q -n'; |
||
| 297 | $arr_commands[] = "{$udhcpcPath} {$options} -i {$if_name} -x hostname:{$hostname} -s {$workerPath}"; |
||
| 298 | // Старутем новый процесс udhcpc в фоне. |
||
| 299 | $options = '-t 6 -T 5 -S -b -n'; |
||
| 300 | $arr_commands[] = "{$nohupPath} {$udhcpcPath} {$options} -p {$pid_file} -i {$if_name} -x hostname:{$hostname} -s {$workerPath} 2>&1 &"; |
||
| 301 | /* |
||
| 302 | udhcpc - утилита произведет настройку интерфейса |
||
| 303 | - произведет конфигурацию /etc/resolv.conf |
||
| 304 | Дальнейшая настройка маршрутов будет произволиться в udhcpcConfigureRenewBound(); |
||
| 305 | и в udhcpcConfigureDeconfig(). Эти методы будут вызваны скриптом WorkerUdhcpcConfigure.php. |
||
| 306 | // man udhcp |
||
| 307 | // http://pwet.fr/man/linux/administration_systeme/udhcpc/ |
||
| 308 | |||
| 309 | */ |
||
| 310 | } else { |
||
| 311 | $ipaddr = trim($if_data['ipaddr']); |
||
| 312 | $subnet = trim($if_data['subnet']); |
||
| 313 | $gateway = trim($if_data['gateway']); |
||
| 314 | if (empty($ipaddr)) { |
||
| 315 | continue; |
||
| 316 | } |
||
| 317 | // Это короткое представление маск /24 /32. |
||
| 318 | try { |
||
| 319 | $calc_subnet = new SubnetCalculator($ipaddr, $subnet); |
||
| 320 | $subnet = $calc_subnet->getSubnetMask(); |
||
| 321 | } catch (Throwable $e) { |
||
| 322 | echo "Caught exception: $ipaddr $subnet", $e->getMessage(), "\n"; |
||
| 323 | continue; |
||
| 324 | } |
||
| 325 | |||
| 326 | $ifconfigPath = Util::which('ifconfig'); |
||
| 327 | $arr_commands[] = "{$busyboxPath} {$ifconfigPath} $if_name $ipaddr netmask $subnet"; |
||
| 328 | |||
| 329 | if ("" != trim($gateway)) { |
||
| 330 | $gw_param = "gw $gateway"; |
||
| 331 | } |
||
| 332 | |||
| 333 | $routePath = Util::which('route'); |
||
| 334 | $arr_commands[] = "{$busyboxPath} {$routePath} del default $if_name"; |
||
| 335 | |||
| 336 | /** @var LanInterfaces $if_data */ |
||
| 337 | $if_data = LanInterfaces::findFirst("id = '{$if_data['id']}'"); |
||
| 338 | $is_inet = ($if_data !== null) ? $if_data->internet : 0; |
||
| 339 | // Добавляем маршруты по умолчанию. |
||
| 340 | if ($is_inet == 1) { |
||
| 341 | // ТОЛЬКО, если этот интерфейс для интернет, создаем дефолтный маршрут. |
||
| 342 | $arr_commands[] = "{$busyboxPath} {$routePath} add default $gw_param dev $if_name"; |
||
| 343 | } |
||
| 344 | // Поднимаем интерфейс. |
||
| 345 | $arr_commands[] = "{$busyboxPath} {$ifconfigPath} $if_name up"; |
||
| 346 | |||
| 347 | $eth_mtu[] = $if_name; |
||
| 348 | } |
||
| 349 | } |
||
| 350 | $out = null; |
||
| 351 | Processes::mwExecCommands($arr_commands, $out, 'net'); |
||
| 352 | $this->hostsGenerate(); |
||
| 353 | |||
| 354 | foreach ($eth_mtu as $eth) { |
||
| 355 | Processes::mwExecBg("/etc/rc/networking.set.mtu '{$eth}'"); |
||
| 356 | } |
||
| 357 | |||
| 358 | $firewall = new IptablesConf(); |
||
| 359 | $firewall->applyConfig(); |
||
| 360 | |||
| 361 | // Дополнительные "ручные" маршруты. |
||
| 362 | Util::fileWriteContent('/etc/static-routes', ''); |
||
| 363 | $arr_commands = []; |
||
| 364 | $out = []; |
||
| 365 | $grepPath = Util::which('grep'); |
||
| 366 | $awkPath = Util::which('awk'); |
||
| 367 | $catPath = Util::which('cat'); |
||
| 368 | Processes::mwExec( |
||
| 369 | "{$catPath} /etc/static-routes | {$grepPath} '^rout' | {$busyboxPath} {$awkPath} -F ';' '{print $1}'", |
||
| 370 | $arr_commands |
||
| 371 | ); |
||
| 372 | Processes::mwExecCommands($arr_commands, $out, 'rout'); |
||
| 373 | |||
| 374 | $this->openVpnConfigure(); |
||
| 375 | |||
| 376 | return 0; |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * For OS systemctl (Debian). |
||
| 381 | * Configures LAN interface |
||
| 382 | */ |
||
| 383 | public function lanConfigureSystemCtl(): void |
||
| 384 | { |
||
| 385 | $networks = $this->getGeneralNetSettings(); |
||
| 386 | $busyboxPath = Util::which('busybox'); |
||
| 387 | $grepPath = Util::which('grep'); |
||
| 388 | $awkPath = Util::which('awk'); |
||
| 389 | $catPath = Util::which('cat'); |
||
| 390 | $systemctlPath = Util::which('systemctl'); |
||
| 391 | $modprobePath = Util::which('modprobe'); |
||
| 392 | Processes::mwExec("{$systemctlPath} stop networking"); |
||
| 393 | Processes::mwExec("{$modprobePath} 8021q"); |
||
| 394 | foreach ($networks as $if_data) { |
||
| 395 | $if_name = trim($if_data['interface']); |
||
| 396 | if ('' == $if_name) { |
||
| 397 | continue; |
||
| 398 | } |
||
| 399 | $conf_file = "/etc/network/interfaces.d/{$if_name}"; |
||
| 400 | if ($if_data['disabled'] == 1) { |
||
| 401 | $ifdownPath = Util::which('ifdown'); |
||
| 402 | Processes::mwExec("{$ifdownPath} eth0"); |
||
| 403 | if (file_exists($if_name)) { |
||
| 404 | unlink($conf_file); |
||
| 405 | } |
||
| 406 | continue; |
||
| 407 | } |
||
| 408 | $subnet = trim($if_data['subnet']); |
||
| 409 | $ipaddr = trim($if_data['ipaddr']); |
||
| 410 | $gateway = trim($if_data['gateway']); |
||
| 411 | |||
| 412 | $result = ['']; |
||
| 413 | if (file_exists('/etc/static-routes')) { |
||
| 414 | $command = "{$catPath} /etc/static-routes " . |
||
| 415 | "| {$grepPath} '^rout' " . |
||
| 416 | "| {$busyboxPath} awk -F ';' '{print $1}' " . |
||
| 417 | "| {$grepPath} '{$if_name}\$' " . |
||
| 418 | "| {$awkPath} -F 'dev {$if_name}' '{ print $1 }'"; |
||
| 419 | Processes::mwExec($command, $result); |
||
| 420 | } |
||
| 421 | $routs_add = ltrim(implode("\npost-up ", $result)); |
||
| 422 | $routs_rem = ltrim(implode("\npre-down ", $result)); |
||
| 423 | |||
| 424 | |||
| 425 | if ($if_data['vlanid'] > 0) { |
||
| 426 | // Пока только статика. |
||
| 427 | $lan_config = "auto {$if_data['interface_orign']}.{$if_data['vlanid']}\n" . |
||
| 428 | "iface {$if_data['interface_orign']}.{$if_data['vlanid']} inet static \n" . |
||
| 429 | "address {$ipaddr}\n" . |
||
| 430 | "netmask {$subnet}\n" . |
||
| 431 | "gateway {$gateway}\n" . |
||
| 432 | "dns-nameservers 127.0.0.1\n" . |
||
| 433 | "vlan_raw_device {$if_data['interface_orign']}\n" . |
||
| 434 | "{$routs_add}\n" . |
||
| 435 | "{$routs_rem}\n"; |
||
| 436 | } elseif (trim($if_data['dhcp']) === '1') { |
||
| 437 | $lan_config = "auto {$if_name}\n" . |
||
| 438 | "iface {$if_name} inet dhcp\n" . |
||
| 439 | "{$routs_add}\n" . |
||
| 440 | "{$routs_rem}\n"; |
||
| 441 | } else { |
||
| 442 | if (empty($ipaddr)) { |
||
| 443 | continue; |
||
| 444 | } |
||
| 445 | try { |
||
| 446 | $calc_subnet = new SubnetCalculator($ipaddr, $subnet); |
||
| 447 | $subnet = $calc_subnet->getSubnetMask(); |
||
| 448 | } catch (Throwable $e) { |
||
| 449 | echo "Caught exception: $ipaddr $subnet", $e->getMessage(), "\n"; |
||
| 450 | continue; |
||
| 451 | } |
||
| 452 | $lan_config = "auto {$if_name}\n" . |
||
| 453 | "iface {$if_name} inet static\n" . |
||
| 454 | "address {$ipaddr}\n" . |
||
| 455 | "netmask {$subnet}\n" . |
||
| 456 | "gateway {$gateway}\n" . |
||
| 457 | "dns-nameservers 127.0.0.1\n" . |
||
| 458 | "{$routs_add}\n" . |
||
| 459 | "{$routs_rem}\n"; |
||
| 460 | } |
||
| 461 | file_put_contents("/etc/network/interfaces.d/{$if_name}", $lan_config); |
||
| 462 | } |
||
| 463 | $systemctlPath = Util::which('systemctl'); |
||
| 464 | Processes::mwExec("{$systemctlPath} start networking"); |
||
| 465 | $this->hostsGenerate(); |
||
| 466 | |||
| 467 | $firewall = new IptablesConf(); |
||
| 468 | $firewall->applyConfig(); |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Получение настроек интерфейсов LAN. |
||
| 473 | * |
||
| 474 | * @return array |
||
| 475 | */ |
||
| 476 | public function getGeneralNetSettings(): array |
||
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Преобразует сетевую маску в CIDR представление. |
||
| 538 | * |
||
| 539 | * @param $net_mask |
||
| 540 | * |
||
| 541 | * @return int |
||
| 542 | */ |
||
| 543 | public function netMaskToCidr($net_mask): int |
||
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Включаем интерфейс по его имени. |
||
| 557 | * |
||
| 558 | * @param $name |
||
| 559 | */ |
||
| 560 | public function enableLanInterface($name): void |
||
| 574 | } |
||
| 575 | } |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Удаляем интерфейс по его имени. |
||
| 579 | * |
||
| 580 | * @param $name |
||
| 581 | */ |
||
| 582 | public function disableLanInterface($name): void |
||
| 589 | } |
||
| 590 | } |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Добавляем в базу данных сведения о новом интерфейсе. |
||
| 594 | * |
||
| 595 | * @param $name |
||
| 596 | * @param bool $general |
||
| 597 | * |
||
| 598 | * @return mixed |
||
| 599 | */ |
||
| 600 | private function addLanInterface($name, $general = false) |
||
| 601 | { |
||
| 602 | $data = new LanInterfaces(); |
||
| 603 | $data->name = $name; |
||
| 604 | $data->interface = $name; |
||
| 605 | $data->dhcp = '1'; |
||
| 606 | $data->internet = ($general === true) ? '1' : '0'; |
||
| 607 | $data->disabled = '0'; |
||
| 608 | $data->vlanid = '0'; |
||
| 609 | $data->hostname = 'mikopbx'; |
||
| 610 | $data->domain = ''; |
||
| 611 | $data->topology = 'private'; |
||
| 612 | $data->primarydns= ''; |
||
| 613 | $data->save(); |
||
| 614 | |||
| 615 | return $data->toArray(); |
||
| 616 | } |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Настройка hosts |
||
| 620 | */ |
||
| 621 | public function hostsGenerate(): void |
||
| 625 | } |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Setup hostname |
||
| 629 | **/ |
||
| 630 | public function hostnameConfigure(): void |
||
| 631 | { |
||
| 632 | $data = Network::getHostName(); |
||
| 633 | $hosts_conf = "127.0.0.1 localhost\n" . |
||
| 634 | "127.0.0.1 {$data['hostname']}\n"; |
||
| 635 | if ( ! empty($data['domain'])) { |
||
| 636 | $hosts_conf .= "127.0.0.1 {$data['hostname']}.{$data['domain']}\n"; |
||
| 637 | } |
||
| 638 | Util::fileWriteContent('/etc/hosts', $hosts_conf); |
||
| 639 | |||
| 640 | $hostnamePath = Util::which('hostname'); |
||
| 641 | Processes::mwExec($hostnamePath . ' ' . escapeshellarg("{$data['hostname']}")); |
||
| 642 | } |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Настройка OpenVPN. Если в кастомизации системных файлов определн конфиг, то сеть поднимется. |
||
| 646 | */ |
||
| 647 | public function openVpnConfigure() |
||
| 663 | } |
||
| 664 | } |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Configures LAN interface FROM udhcpc (renew_bound) |
||
| 668 | */ |
||
| 669 | public function udhcpcConfigureRenewBound(): void |
||
| 770 | } |
||
| 771 | |||
| 772 | /** |
||
| 773 | * For OS systemctl (Debian). |
||
| 774 | * Configures LAN interface FROM dhcpc (renew_bound). |
||
| 775 | */ |
||
| 776 | public function udhcpcConfigureRenewBoundSystemCtl(): void |
||
| 836 | } |
||
| 837 | |||
| 838 | /** |
||
| 839 | * Сохранение настроек сетевого интерфейса. |
||
| 840 | * |
||
| 841 | * @param $data |
||
| 842 | * @param $name |
||
| 843 | */ |
||
| 844 | public function updateIfSettings($data, $name): void |
||
| 845 | { |
||
| 846 | /** @var LanInterfaces $res */ |
||
| 847 | $res = LanInterfaces::findFirst("interface = '$name' AND vlanid=0"); |
||
| 848 | if ($res === null || !$this->settingsIsChange($data, $res->toArray()) ) { |
||
| 849 | return; |
||
| 850 | } |
||
| 851 | foreach ($data as $key => $value) { |
||
| 852 | $res->writeAttribute($key, $value); |
||
| 853 | } |
||
| 854 | $res->save(); |
||
| 855 | } |
||
| 856 | |||
| 857 | /** |
||
| 858 | * Сохранение DNS настроек сетевого интерфейса. |
||
| 859 | * |
||
| 860 | * @param $data |
||
| 861 | * @param $name |
||
| 862 | */ |
||
| 863 | public function updateDnsSettings($data, $name): void |
||
| 864 | { |
||
| 865 | /** @var LanInterfaces $res */ |
||
| 866 | $res = LanInterfaces::findFirst("interface = '$name' AND vlanid=0"); |
||
| 867 | if ($res === null || !$this->settingsIsChange($data, $res->toArray())) { |
||
| 868 | return; |
||
| 869 | } |
||
| 870 | foreach ($data as $key => $value) { |
||
| 871 | $res->writeAttribute($key, $value); |
||
| 872 | } |
||
| 873 | if (empty($res->primarydns) && !empty($res->secondarydns)) { |
||
| 874 | $res->primarydns = $res->secondarydns; |
||
| 875 | $res->secondarydns = ''; |
||
| 876 | } |
||
| 877 | $res->save(); |
||
| 878 | } |
||
| 879 | |||
| 880 | /** |
||
| 881 | * Сравнение двух массивов. |
||
| 882 | * @param array $data |
||
| 883 | * @param array $dbData |
||
| 884 | * @return bool |
||
| 885 | */ |
||
| 886 | private function settingsIsChange(array $data, array $dbData):bool{ |
||
| 887 | $isChange = false; |
||
| 888 | foreach ($data as $key => $value){ |
||
| 889 | if(!isset($dbData[$key]) || $value === $dbData[$key]){ |
||
| 890 | continue; |
||
| 891 | } |
||
| 892 | $isChange = true; |
||
| 893 | } |
||
| 894 | var_dump($isChange); |
||
|
|
|||
| 895 | return $isChange; |
||
| 896 | } |
||
| 897 | |||
| 898 | /** |
||
| 899 | * Возвращает имя интерфейса по его id. |
||
| 900 | * |
||
| 901 | * @param $id_net |
||
| 902 | * |
||
| 903 | * @return string |
||
| 904 | */ |
||
| 905 | public function getInterfaceNameById($id_net): string |
||
| 906 | { |
||
| 907 | $res = LanInterfaces::findFirstById($id_net); |
||
| 908 | if ($res !== null && $res->interface !== null) { |
||
| 909 | return $res->interface; |
||
| 910 | } |
||
| 911 | |||
| 912 | return ''; |
||
| 913 | } |
||
| 914 | |||
| 915 | /** |
||
| 916 | * Возвращает список включеннх веб интерейсов |
||
| 917 | * |
||
| 918 | * @return array |
||
| 919 | */ |
||
| 920 | public function getEnabledLanInterfaces(): array |
||
| 921 | { |
||
| 922 | /** @var \MikoPBX\Common\Models\LanInterfaces $res */ |
||
| 923 | $res = LanInterfaces::find('disabled=0'); |
||
| 924 | |||
| 925 | return $res->toArray(); |
||
| 926 | } |
||
| 927 | |||
| 928 | /** |
||
| 929 | * Configures LAN interface FROM udhcpc (deconfig) |
||
| 930 | */ |
||
| 931 | public function udhcpcConfigureDeconfig(): void |
||
| 946 | } |
||
| 947 | |||
| 948 | /** |
||
| 949 | * Сохранение настроек сетевого интерфейса. |
||
| 950 | * |
||
| 951 | * @param $data |
||
| 952 | */ |
||
| 953 | public function updateNetSettings($data): void |
||
| 954 | { |
||
| 955 | $res = LanInterfaces::findFirst("internet = '1'"); |
||
| 956 | $update_inet = false; |
||
| 957 | if ($res === null) { |
||
| 958 | $res = LanInterfaces::findFirst(); |
||
| 959 | $update_inet = true; |
||
| 960 | } |
||
| 961 | |||
| 962 | if ($res !== null) { |
||
| 963 | foreach ($data as $key => $value) { |
||
| 964 | $res->$key = $value; |
||
| 965 | } |
||
| 966 | if ($update_inet === true) { |
||
| 967 | $res->internet = 1; |
||
| 968 | } |
||
| 969 | $res->save(); |
||
| 970 | } |
||
| 971 | } |
||
| 972 | |||
| 973 | /** |
||
| 974 | * Возвращает массив с информацией по сетевым интерфейсам. |
||
| 975 | * |
||
| 976 | * @return array |
||
| 977 | */ |
||
| 978 | public function getInterfaces(): array |
||
| 988 | } |
||
| 989 | |||
| 990 | /** |
||
| 991 | * Сбор информации по сетевому интерфейсу. |
||
| 992 | * |
||
| 993 | * @param $name |
||
| 994 | * |
||
| 995 | * @return array |
||
| 996 | */ |
||
| 997 | public function getInterface($name): array |
||
| 1052 | } |
||
| 1053 | |||
| 1054 | } |
||
| 1055 |