| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Mojolicious::Plugin::Ident; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 8 |  |  | 8 |  | 218631 | use strict; | 
|  | 8 |  |  |  |  | 15 |  | 
|  | 8 |  |  |  |  | 246 |  | 
| 4 | 8 |  |  | 8 |  | 29 | use warnings; | 
|  | 8 |  |  |  |  | 11 |  | 
|  | 8 |  |  |  |  | 177 |  | 
| 5 | 8 |  |  | 8 |  | 83 | use v5.10; | 
|  | 8 |  |  |  |  | 18 |  | 
|  | 8 |  |  |  |  | 334 |  | 
| 6 | 8 |  |  | 8 |  | 430 | use Mojo::Base 'Mojolicious::Plugin'; | 
|  | 8 |  |  |  |  | 7461 |  | 
|  | 8 |  |  |  |  | 77 |  | 
| 7 | 8 |  |  | 8 |  | 3001 | use AnyEvent; | 
|  | 8 |  |  |  |  | 4301 |  | 
|  | 8 |  |  |  |  | 171 |  | 
| 8 | 8 |  |  | 8 |  | 3986 | use AnyEvent::Ident::Client; | 
|  | 8 |  |  |  |  | 35594 |  | 
|  | 8 |  |  |  |  | 214 |  | 
| 9 | 8 |  |  | 8 |  | 518 | use Mojo::Exception; | 
|  | 8 |  |  |  |  | 1966 |  | 
|  | 8 |  |  |  |  | 80 |  | 
| 10 | 8 |  |  | 8 |  | 3801 | use Mojolicious::Plugin::Ident::Response; | 
|  | 8 |  |  |  |  | 15 |  | 
|  | 8 |  |  |  |  | 5986 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | # ABSTRACT: Mojolicious plugin to interact with a remote ident service | 
| 13 |  |  |  |  |  |  | our $VERSION = '0.31'; # VERSION | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | sub register | 
| 17 |  |  |  |  |  |  | { | 
| 18 | 7 |  |  | 7 | 1 | 263 | my($self, $app, $conf) = @_; | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 7 |  |  |  |  | 26 | Mojolicious::Plugin::Ident::Response->_setup; | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 7 |  | 100 |  |  | 60 | my $default_timeout = $conf->{timeout} // 2; | 
| 23 | 7 |  | 50 |  |  | 24 | my $port = $conf->{port} // 113; | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | $app->helper(ident => sub { | 
| 26 | 13 |  |  | 13 |  | 66640 | my $callback; | 
| 27 | 13 | 100 |  |  |  | 125 | $callback = pop if ref($_[-1]) eq 'CODE'; | 
| 28 | 13 |  |  |  |  | 29 | my($controller, $tx, $timeout) = @_; | 
| 29 | 13 |  | 66 |  |  | 198 | $tx //= $controller->tx; | 
| 30 | 13 |  | 66 |  |  | 110 | $timeout //= $default_timeout; | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 13 | 100 |  |  |  | 38 | if($callback) | 
| 33 |  |  |  |  |  |  | { | 
| 34 |  |  |  |  |  |  | AnyEvent::Ident::Client | 
| 35 |  |  |  |  |  |  | ->new( response_class => 'Mojolicious::Plugin::Ident::Response', hostname => $tx->remote_address, port => $port ) | 
| 36 |  |  |  |  |  |  | ->ident($tx->remote_port, $tx->local_port, sub { | 
| 37 | 7 |  |  |  |  | 14352 | my $ident_response = shift; | 
| 38 | 7 |  |  |  |  | 38 | $ident_response->{remote_address} = $tx->remote_address; | 
| 39 | 7 |  |  |  |  | 550 | $callback->($ident_response) | 
| 40 |  |  |  |  |  |  | } | 
| 41 | 7 |  |  |  |  | 191 | ); | 
| 42 | 7 |  |  |  |  | 5490 | return; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 6 |  |  |  |  | 190 | my $done = AnyEvent->condvar; | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | my $w = AnyEvent->timer(after => $timeout // $default_timeout, cb => sub { | 
| 48 | 1 |  |  |  |  | 994726 | $done->croak("ident timeout"); | 
| 49 | 6 |  | 33 |  |  | 107 | }); | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 6 |  |  |  |  | 96 | my $ident_response; | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | AnyEvent::Ident::Client | 
| 54 |  |  |  |  |  |  | ->new( response_class => 'Mojolicious::Plugin::Ident::Response', hostname => $tx->remote_address, port => $port ) | 
| 55 |  |  |  |  |  |  | ->ident($tx->remote_port, $tx->local_port, sub { | 
| 56 | 5 |  |  |  |  | 4359 | $ident_response = shift; | 
| 57 | 5 |  |  |  |  | 32 | $ident_response->{remote_address} = $tx->remote_address; | 
| 58 | 5 |  |  |  |  | 379 | $done->send(1); | 
| 59 |  |  |  |  |  |  | } | 
| 60 | 6 |  |  |  |  | 30 | ); | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 6 |  |  |  |  | 3694 | $done->recv; | 
| 63 | 5 |  |  |  |  | 208 | undef $w; | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 5 | 100 |  |  |  | 54 | if($ident_response->is_success) | 
| 66 |  |  |  |  |  |  | { | 
| 67 | 3 |  |  |  |  | 59 | return $ident_response; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  | else | 
| 70 |  |  |  |  |  |  | { | 
| 71 | 2 |  |  |  |  | 19 | my $error = 'ident error: ' . $ident_response->error_type; | 
| 72 | 2 |  |  |  |  | 48 | $controller->app->log->error($error); | 
| 73 | 2 |  |  |  |  | 90 | die Mojo::Exception->new($error); | 
| 74 |  |  |  |  |  |  | } | 
| 75 | 7 |  |  |  |  | 98 | }); | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | $app->helper(ident_same_user => sub { | 
| 78 | 15 |  |  | 15 |  | 533928 | my $callback; | 
| 79 | 15 | 100 |  |  |  | 83 | $callback = pop if ref($_[-1]) eq 'CODE'; | 
| 80 | 15 |  |  |  |  | 36 | my $controller = shift; | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 15 | 100 |  |  |  | 50 | if($callback) | 
| 83 |  |  |  |  |  |  | { | 
| 84 | 10 |  |  |  |  | 18 | my($tx, $timeout) = @_; | 
| 85 | 10 |  | 33 |  |  | 252 | $tx //= $controller->tx; | 
| 86 | 10 |  | 33 |  |  | 111 | $timeout //= $default_timeout; | 
| 87 |  |  |  |  |  |  |  | 
| 88 | 10 | 100 |  |  |  | 50 | if(defined $controller->session('ident_same_user')) | 
| 89 |  |  |  |  |  |  | { | 
| 90 |  |  |  |  |  |  | Mojo::IOLoop->timer(0 => sub { | 
| 91 | 4 |  |  |  |  | 7738 | $callback->($controller->session('ident_same_user')); | 
| 92 | 4 |  |  |  |  | 2845 | }); | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  | else | 
| 95 |  |  |  |  |  |  | { | 
| 96 |  |  |  |  |  |  | $controller->ident($tx, $timeout, sub { | 
| 97 | 6 |  |  |  |  | 12 | my $res = shift; | 
| 98 | 6 | 100 |  |  |  | 38 | if($res->is_success) | 
| 99 |  |  |  |  |  |  | { | 
| 100 | 4 |  |  |  |  | 82 | my $same_user = $res->same_user; | 
| 101 | 4 |  |  |  |  | 27 | $controller->session('ident_same_user' => $same_user); | 
| 102 | 4 |  |  |  |  | 632 | $callback->($same_user); | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  | else | 
| 105 |  |  |  |  |  |  | { | 
| 106 | 2 |  |  |  |  | 17 | $callback->(0); | 
| 107 |  |  |  |  |  |  | } | 
| 108 | 6 |  |  |  |  | 2126 | }); | 
| 109 |  |  |  |  |  |  | } | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  | else | 
| 112 |  |  |  |  |  |  | { | 
| 113 | 5 |  | 66 |  |  | 22 | $controller->session('ident_same_user') // do { | 
| 114 | 3 |  |  |  |  | 771 | my $same_user = eval { $controller->ident(@_)->same_user }; | 
|  | 3 |  |  |  |  | 24 |  | 
| 115 | 3 | 100 |  |  |  | 154 | return if $@; | 
| 116 | 2 |  |  |  |  | 8 | $controller->session('ident_same_user' => $same_user); | 
| 117 | 2 |  |  |  |  | 255 | $same_user; | 
| 118 |  |  |  |  |  |  | }; | 
| 119 |  |  |  |  |  |  | } | 
| 120 | 7 |  |  |  |  | 712 | }); | 
| 121 |  |  |  |  |  |  | } | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | 1; | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | __END__ |