line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::BindSessionToIP; |
2
|
2
|
|
|
2
|
|
5866
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
15
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
5
|
2
|
|
|
2
|
|
376
|
use v5.10; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
93
|
|
6
|
2
|
|
|
2
|
|
21
|
use Data::Dumper; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
1183
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub register { |
9
|
2
|
|
|
2
|
1
|
91
|
my ( $self, $app, $conf ) = @_; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# On error callback |
12
|
2
|
|
|
|
|
3
|
my $on_error; |
13
|
2
|
100
|
66
|
|
|
19
|
if ( $conf->{on_error} && ref($conf->{on_error}) eq 'CODE' ) { |
14
|
1
|
|
|
|
|
3
|
$on_error = $conf->{on_error}; |
15
|
|
|
|
|
|
|
} else { |
16
|
1
|
|
|
1
|
|
4
|
$on_error = sub { shift->redirect_to('/'); }; |
|
1
|
|
|
|
|
5
|
|
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
$app->hook( |
20
|
|
|
|
|
|
|
before_routes => sub { |
21
|
10
|
|
|
10
|
|
288264
|
my ($c) = @_; |
22
|
|
|
|
|
|
|
|
23
|
10
|
|
100
|
|
|
61
|
my $stored_ip = $c->session('bind_session_to_ip.ip') // ''; |
24
|
10
|
|
|
|
|
329
|
my $req_ip = $c->remote_addr; |
25
|
|
|
|
|
|
|
|
26
|
10
|
100
|
|
|
|
3470
|
if ( $stored_ip ne $req_ip ) { |
27
|
4
|
100
|
|
|
|
29
|
if ($stored_ip) { |
28
|
2
|
|
|
|
|
50
|
$c->app->log->debug("BindSessionToIP: IP changed from [$stored_ip] to [$req_ip]"); |
29
|
|
|
|
|
|
|
|
30
|
2
|
|
|
|
|
117
|
$self->_destroy_session($c); |
31
|
2
|
|
|
|
|
8
|
$c->session('bind_session_to_ip.ip' => $req_ip); |
32
|
2
|
|
|
|
|
47
|
return $on_error->($c); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
2
|
|
|
|
|
12
|
$c->session('bind_session_to_ip.ip' => $req_ip); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
8
|
|
|
|
|
79
|
return 1; |
39
|
|
|
|
|
|
|
}, |
40
|
2
|
|
|
|
|
24
|
); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub _destroy_session { |
45
|
2
|
|
|
2
|
|
6
|
my ( $self, $c ) = @_; |
46
|
2
|
|
|
|
|
11
|
my $session = $c->session; |
47
|
|
|
|
|
|
|
|
48
|
2
|
|
|
|
|
34
|
foreach my $key (keys %$session) { |
49
|
4
|
|
|
|
|
13
|
$session->{$key} = ''; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
1; |
54
|
|
|
|
|
|
|
__END__ |