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