DnsSrvDiscoverer.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Service discovery using DNS SRV records
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * @since 1.29
  24. */
  25. class DnsSrvDiscoverer {
  26. /**
  27. * @var string
  28. */
  29. private $domain;
  30. /**
  31. * @param string $domain
  32. */
  33. public function __construct( $domain ) {
  34. $this->domain = $domain;
  35. }
  36. /**
  37. * Fetch the servers with a DNS SRV request
  38. *
  39. * @return array
  40. */
  41. public function getServers() {
  42. $result = [];
  43. foreach ( $this->getDnsRecords() as $record ) {
  44. $result[] = [
  45. 'target' => $record['target'],
  46. 'port' => $record['port'],
  47. 'pri' => $record['pri'],
  48. 'weight' => $record['weight'],
  49. ];
  50. }
  51. return $result;
  52. }
  53. /**
  54. * Pick a server according to the priority fields.
  55. * Note that weight is currently ignored.
  56. *
  57. * @param array $servers from getServers
  58. * @return array|bool
  59. */
  60. public function pickServer( array $servers ) {
  61. if ( !$servers ) {
  62. return false;
  63. }
  64. $srvsByPrio = [];
  65. foreach ( $servers as $server ) {
  66. $srvsByPrio[$server['pri']][] = $server;
  67. }
  68. $min = min( array_keys( $srvsByPrio ) );
  69. if ( count( $srvsByPrio[$min] ) == 1 ) {
  70. return $srvsByPrio[$min][0];
  71. } else {
  72. // Choose randomly
  73. $rand = mt_rand( 0, count( $srvsByPrio[$min] ) - 1 );
  74. return $srvsByPrio[$min][$rand];
  75. }
  76. }
  77. /**
  78. * @param array $server
  79. * @param array $servers
  80. * @return array[]
  81. */
  82. public function removeServer( $server, array $servers ) {
  83. foreach ( $servers as $i => $srv ) {
  84. if ( $srv['target'] === $server['target'] && $srv['port'] === $server['port'] ) {
  85. unset( $servers[$i] );
  86. break;
  87. }
  88. }
  89. return array_values( $servers );
  90. }
  91. /**
  92. * @return array[]
  93. */
  94. protected function getDnsRecords() {
  95. return dns_get_record( $this->domain, DNS_SRV );
  96. }
  97. }