forwardinghosts.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. try {
  29. foreach ($redis->hGetAll('WHITELISTED_FWD_HOST') as $host => $source) {
  30. if (in_net($_GET['host'], $host)) {
  31. echo '200 PERMIT';
  32. exit;
  33. }
  34. }
  35. echo '200 DUNNO';
  36. }
  37. catch (RedisException $e) {
  38. echo '200 DUNNO';
  39. exit;
  40. }
  41. ?>