forwardinghosts.php 1.3 KB

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