Использование пула IP алресов для отправки почтовых сообщений в Postfix. Предлагаю несколько возможных реализаций
Вариант 1 с использование IPTABLES
1 2 3 4 5 6 7 8 9 10 |
iptables -t nat -I POSTROUTING -m state --state NEW -p tcp --dport 25 -o eth0 -m statistic --mode nth --every 8 --packet 0 -j SNAT --to-source x.x.x.1 iptables -t nat -I POSTROUTING -m state --state NEW -p tcp --dport 25 -o eth0 -m statistic --mode nth --every 8 --packet 0 -j SNAT --to-source x.x.x.2 iptables -t nat -I POSTROUTING -m state --state NEW -p tcp --dport 25 -o eth0 -m statistic --mode nth --every 8 --packet 0 -j SNAT --to-source x.x.x.3 iptables -t nat -I POSTROUTING -m state --state NEW -p tcp --dport 25 -o eth0 -m statistic --mode nth --every 8 --packet 0 -j SNAT --to-source x.x.x.4 iptables -t nat -I POSTROUTING -m state --state NEW -p tcp --dport 25 -o eth0 -m statistic --mode nth --every 8 --packet 0 -j SNAT --to-source x.x.x.5 iptables -t nat -I POSTROUTING -m state --state NEW -p tcp --dport 25 -o eth0 -m statistic --mode nth --every 8 --packet 0 -j SNAT --to-source x.x.x.6 iptables -t nat -I POSTROUTING -m state --state NEW -p tcp --dport 25 -o eth0 -m statistic --mode nth --every 8 --packet 0 -j SNAT --to-source x.x.x.7 iptables -t nat -I POSTROUTING -m state --state NEW -p tcp --dport 25 -o eth0 -m statistic --mode nth --every 8 --packet 0 -j SNAT --to-source x.x.x.8 |
Вариант 2 с использованием скрипта на Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# cd /etc/postfix # vi random.pl #!/usr/bin/perl -w # author: Hari Hendaryanto <hari.h -at- csmcom.com> use strict; use warnings; use Sys::Syslog qw(:DEFAULT setlogsock); # # our transports array, we will define this in master.cf as transport services # our @array = ( 'rotate1:', 'rotate2:', 'rotate3:', 'rotate4:', 'rotate5:' ); # # Initalize and open syslog. # openlog('postfix/randomizer','pid','mail'); # # Autoflush standard output. # select STDOUT; $|++; while (<>) { chomp; # randomizing transports array my $random_smtp = int(rand(scalar(@array))); if (/^get\s(.+)$/i) { print "200 $array[$random_smtp]\n"; syslog("info","Using: %s Transport Service", $random_smtp); next; } print "200 smtp:"; } |
Make it executable
1 2 3 4 |
# chmod 755 random.pl |
master.cf parts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
Run the scripts via postfix spawn daemon service. 127.0.0.1:2527 inet n n n - 0 spawn user=nobody argv=/etc/postfix/random.pl add 5 smtp client services called rotate1, rotate2, rotate3, rotate4, rotate5, that bind to its own ip address and has uniq syslog/helo name. # random smtp rotate1 unix - - n - - smtp -o syslog_name=postfix-rotate1 -o smtp_helo_name=smtp1.example.com -o smtp_bind_address=1.2.3.1 rotate2 unix - - n - - smtp -o syslog_name=postfix-rotate2 -o smtp_helo_name=smtp2.example.com -o smtp_bind_address=1.2.3.2 rotate3 unix - - n - - smtp -o syslog_name=postfix-rotate3 -o smtp_helo_name=smtp3.example.com -o smtp_bind_address=1.2.3.3 rotate4 unix - - n - - smtp -o syslog_name=postfix-rotate4 -o smtp_helo_name=smtp4.example.com -o smtp_bind_address=1.2.3.4 rotate5 unix - - n - - smtp -o syslog_name=postfix-rotate5 -o smtp_helo_name=smtp5.example.com -o smtp_bind_address=1.2.3.5 Before we actually implement our randomize transport, let’s make sure that the setting actually work. Reload postfix # postfix reload Run this query fiew times, and you’ll see the perl script will return “random answer” transport # postmap -q "whatever" tcp:127.0.0.1:2527 rotate1: # postmap -q "whatever" tcp:127.0.0.1:2527 rotate5: |
And so on..
Note on “whatever”, since the script acted in “catch-all” mode as i’ve mentioned earlier, what ever postfix transport_maps client asked. it will be answered with random values such as rotate1, rotate2, rotate3, rotate4, rotate5 in randomized fashion.
main.cf parts
Add these lines
1 2 3 4 5 |
transport_maps = tcp:[127.0.0.1]:2527 127.0.0.1:2527_time_limit = 3600s |
Reload postfix that’s it. example log would be like these and that’s indicate that randomizer is working.
1 2 3 4 5 6 7 |
Month date 12:26:53 host postfix-rotate1/smtp[4252]: A1CA68480A4: to=<xxx@example.com>, relay=mx.example.com.com[xx.xx.xxx.xx]:25], delay=3.6, delays=0.69/0.01/0.81/2, dsn=2.0.0, status=sent (250 ok dirdel) --snip-- Month date 12:27:06 host postfix-rotate5/smtp[4253]: 41C2E8480A4: to=<xxx@example.net>, relay=mx.example.net[xx.xxx.xxx.xxx]:25], delay=6, delays=0.14/0.01/0.85/5, dsn=2.0.0, status=sent (250 ok dirdel) --snip-- Month date 12:27:22 host postfix-rotate3/smtp[4277]: 4BA9F8480A4: to=<xxx@example.org>, relay=mx.example.org[xx.xxx.xx.xxx]:25], delay=7.9, delays=0.85/0.02/0.61/6.4, dsn=2.0.0, status=sent (250 ok dirdel) |