<?php

namespace App\Helpers\Utility;

use Symfony\Component\Process\Process;

class IP {
  public static function get_interface($_interface = 'eth0') {
    $command = new Process([ 'ip', 'addr', 'show', $_interface ]);

    $command->run();
    
    if($command->isSuccessful()) {
      $matches = [];
      preg_match('/((?<=(inet ))|(?<=(inet6 )))(((\d{1,3}\.){3}\d{1,3})|([0-9a-f]{0,4}\:){0,7}[0-9a-f]{0,4})/', $command->getOutput(), $matches);
      
      return $matches[0];
    }

    return null;
  }

  public static function get_external() {
    $command = new Process([ 'curl', 'ifconfig.me/ip' ]);

    $command->run();
    
    if($command->isSuccessful()) {
      return $command->getOutput();
    }

    return null;
  }
}

?>