Advertisement
Guest User

cbpolicyd-proxy.pl

a guest
Apr 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.34 KB | None | 0 0
  1.  
  2. #!/usr/bin/perl
  3.  
  4. use strict;
  5. use warnings;
  6.  
  7. use Sys::Syslog qw(:DEFAULT setlogsock);
  8. use IO::Socket;
  9.  
  10. # backend (cbpolicyd)
  11. my $backend_host = "127.0.0.1";
  12. my $backend_port = "10031";
  13.  
  14. # syslog
  15. my $syslog_socktype = 'unix'; # inet, unix, stream, console
  16. my $syslog_facility="mail";
  17. my $syslog_options="pid";
  18.  
  19. setlogsock $syslog_socktype;
  20. openlog "cbpolicyd-proxy", $syslog_options, $syslog_facility;
  21.  
  22. # Unbuffer standard output.
  23. select((select(STDOUT), $| = 1)[0]);
  24.  
  25. my @lines = ();
  26. while (<STDIN>) {
  27.         my $line = $_;
  28.         chomp $line;
  29.         if ($line eq '') {
  30.                 my $sock = IO::Socket::INET->new(PeerAddr => $backend_host, PeerPort => $backend_port, Proto => "tcp", Type => SOCK_STREAM);
  31.                 if (defined($sock)) {
  32.                         $sock->autoflush();
  33.                         print $sock join("\n", @lines). "\n\n";
  34.                         my @result = <$sock>;
  35.                         print STDOUT join("", @result);
  36.                         close($sock);
  37.                 } else {
  38.                         syslog "warning", "returning DUNNO because of socket error: $@";
  39.                         print $@;
  40.                         print STDOUT "action=DUNNO\n\n";
  41.                 }
  42.                 @lines = ();
  43.         } else {
  44.                 push @lines, $line;
  45.         }
  46. }
  47.  
  48. exit 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement