diff options
author | Alexander Sulfrian <alex@spline.inf.fu-berlin.de> | 2016-06-21 02:42:25 +0200 |
---|---|---|
committer | Alexander Sulfrian <alex@spline.inf.fu-berlin.de> | 2016-06-21 02:42:25 +0200 |
commit | 339312c92a71a0943ae34aab27b1026bb3479d0d (patch) | |
tree | cea49efa50ec436b517a25dc1d1ff90f575e2f18 /Spline/Srs.pm | |
parent | d9ba41806745b830fd229b4cbcd8b5386203af27 (diff) | |
download | srs-339312c92a71a0943ae34aab27b1026bb3479d0d.tar.gz srs-339312c92a71a0943ae34aab27b1026bb3479d0d.tar.bz2 srs-339312c92a71a0943ae34aab27b1026bb3479d0d.zip |
Make subclasses of Net::Server
Spline::Socketmap is now a subclasses of Net::Server and
Spline::Socketmap::Srs is a subclass of Spline::Socketmap. This way the
Srs stuff is completly independent of the postfix-socketmap protocol
handling.
The config is located in the instances and configurable with Net::Server
command line options or config file.
Diffstat (limited to 'Spline/Srs.pm')
-rw-r--r-- | Spline/Srs.pm | 108 |
1 files changed, 47 insertions, 61 deletions
diff --git a/Spline/Srs.pm b/Spline/Srs.pm index e799245..597e3ed 100644 --- a/Spline/Srs.pm +++ b/Spline/Srs.pm @@ -8,37 +8,44 @@ use Mail::SRS; use base 'Exporter'; our @EXPORT = qw( ); our @EXPORT_OK = qw( - config_set - config_get check_exclude - srs_forward - srs_reverse ); -my $config = { - alias => '', - excludes => [], - secret => '', - max_age => 49, - hash_length => 5, - hash_min => 5, - ignore_time => 0, - srsalt_fallback => 0, -}; - -my $srs = undef; - -sub config_set($$) { - my ($key, $value) = @_; - return unless defined $config->{$key}; - - $config->{$key} = $value; - $srs = undef; -} +sub new { + my $class = shift; + my $opts = shift; + + my $active_secret; + if (ref $opts->{secret} eq 'ARRAY') { + $active_secret = $opts->{secret}->[0]; + } + else { + $active_secret = $opts->{secret}; + } + + if (length($active_secret // '') < 20) { + die 'You need to configure a suitable secret'; + } + + if (!$opts->{alias}) { + die 'You need to configure an alias domain'; + } -sub config_get($) { - my $key = shift; - return $config->{$key}; + my $self = { + alias => $opts->{alias} // '', + excludes => $opts->{excludes} // [], + srsalt_fallback => $opts->{srsalt_fallback} // 0, + srs => new Mail::SRS( + Secret => $opts->{secret} // '', + MaxAge => $opts->{max_age} // 49, + HashLength => $opts->{hash_length} // 5, + HashMin => $opts->{hash_min} // 5, + IgnoreTimestamp => $opts->{ignore_time} // 0, + ), + }; + bless $self, $class; + + return $self; } sub check_exclude($@) { @@ -72,31 +79,29 @@ sub replace_srsalt_chars($) { return $addr; } -sub srs_forward($) { +sub forward($) { + my $self = shift; my $addr = shift; - return $addr if check_exclude($addr, $config->{excludes}); - - check_configured(); - return $srs->forward($addr, $config->{alias}); + return $addr if check_exclude($addr, $self->{excludes}); + return $self->{srs}->forward($addr, $self->{alias}); } -sub srs_reverse($) { +sub reverse($) { + my $self = shift; my $addr = shift; - check_configured(); - - if ($config->{srsalt_fallback}) { + if ($self->{srsalt_fallback}) { my $result; eval { - $result = $srs->reverse($addr); + $result = $self->{srs}->reverse($addr); }; my $err = $@; if ($err) { if ($err =~ m/Invalid hash/) { my $fallback = replace_srsalt_chars($addr); - return $srs->reverse($fallback); + return $self->{srs}->reverse($fallback); } die $err; @@ -105,38 +110,19 @@ sub srs_reverse($) { return $result; } - return $srs->reverse($addr); -} - -sub check_configured() { - return if defined $srs; - - if (length($config->{secret}) < 20) { - die 'You need to configure a suitable secret'; - } - - if (!$config->{alias}) { - die 'You need to configure an alias domain'; - } - - $srs = new Mail::SRS( - Secret => $config->{secret}, - MaxAge => $config->{max_age}, - HashLength => $config->{hash_length}, - HashMin => $config->{hash_min}, - IgnoreTimestamp => $config->{ignore_time}, - ); + return $self->{srs}->reverse($addr); } sub handle($$) { + my $self = shift; my ($map, $key) = @_; my $result; if ($map eq 'forward') { - $result = srs_forward($key); + $result = $self->forward($key); } elsif ($map eq 'reverse') { - $result = srs_reverse($key); + $result = $self->reverse($key); } else { return "PERM Invalid request"; |