forwardinghosts.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. header('Content-Type: text/plain');
  3. ini_set('error_reporting', 0);
  4. $redis = new Redis();
  5. $redis->connect('redis-mailcow', 6379);
  6. function in_net($addr, $net) {
  7. $net = explode('/', $net);
  8. if (count($net) > 1) {
  9. $mask = $net[1];
  10. }
  11. $net = inet_pton($net[0]);
  12. $addr = inet_pton($addr);
  13. $length = strlen($net); // 4 for IPv4, 16 for IPv6
  14. if (strlen($net) != strlen($addr)) {
  15. return false;
  16. }
  17. if (!isset($mask)) {
  18. $mask = $length * 8;
  19. }
  20. $addr_bin = '';
  21. $net_bin = '';
  22. for ($i = 0; $i < $length; ++$i) {
  23. $addr_bin .= str_pad(decbin(ord(substr($addr, $i, $i+1))), 8, '0', STR_PAD_LEFT);
  24. $net_bin .= str_pad(decbin(ord(substr($net, $i, $i+1))), 8, '0', STR_PAD_LEFT);
  25. }
  26. return substr($addr_bin, 0, $mask) == substr($net_bin, 0, $mask);
  27. }
  28. if (isset($_GET['host'])) {
  29. try {
  30. foreach ($redis->hGetAll('WHITELISTED_FWD_HOST') as $host => $source) {
  31. if (in_net($_GET['host'], $host)) {
  32. echo '200 PERMIT';
  33. exit;
  34. }
  35. }
  36. echo '200 DUNNO';
  37. }
  38. catch (RedisException $e) {
  39. echo '200 DUNNO';
  40. exit;
  41. }
  42. } else {
  43. try {
  44. echo '240.240.240.240' . PHP_EOL;
  45. foreach ($redis->hGetAll('WHITELISTED_FWD_HOST') as $host => $source) {
  46. echo $host . PHP_EOL;
  47. }
  48. }
  49. catch (RedisException $e) {
  50. echo '240.240.240.240' . PHP_EOL;
  51. exit;
  52. }
  53. }
  54. ?>