| 1 | ########################################### | ||||||||||
| 2 | package SVN::Utils::ClientIP; | ||||||||||
| 3 | ########################################### | ||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | require Exporter; | |||||||||
| 7 | 2 | our @ISA = qw(Exporter); | |||||||||
| 8 | 2 | our @EXPORT_OK = qw(ssh_client_ip); | |||||||||
| 9 | 2 | our $VERSION = "0.02"; | |||||||||
| 10 | |||||||||||
| 11 | 2 | ▶ | use Proc::ProcessTable; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | ▶ | use Proc::Info::Environment; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | ########################################### | ||||||||||
| 15 | 0 | ▶ | sub ssh_client_ip { | ||||||||
|
Subroutine
ssh_client_ip: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 16 | ########################################### | ||||||||||
| 17 | |||||||||||
| 18 | 0 | my $finder = __PACKAGE__->new(); | |||||||||
| 19 | |||||||||||
| 20 | 0 | my($ip, $pid, $port) = $finder->ssh_client_ip_find(); | |||||||||
| 21 | |||||||||||
| 22 | 0 | ▶ | if( !defined $ip ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 0 | warn $finder->error(); | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | # only IP | ||||||||||
| 27 | 0 | return $ip; | |||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | ########################################### | ||||||||||
| 31 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 32 | ########################################### | ||||||||||
| 33 | 1 | my($class, %options) = @_; | |||||||||
| 34 | |||||||||||
| 35 | 1 | my $proc_info = Proc::Info::Environment->new(); | |||||||||
| 36 | 1 | my $proc_table = Proc::ProcessTable->new(); | |||||||||
| 37 | |||||||||||
| 38 | 1 | my %ppid_of = (); | |||||||||
| 39 | 1 | foreach my $proc ( @{$proc_table->table} ) { | |||||||||
| 40 | 10 | $ppid_of{ $proc->pid() } = $proc->ppid(); | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 1 | my $self = { | |||||||||
| 44 | proc_info => $proc_info, | ||||||||||
| 45 | ppid_of => \%ppid_of, | ||||||||||
| 46 | pid => $$, | ||||||||||
| 47 | ssh_client_var_name => "SSH_CLIENT", | ||||||||||
| 48 | error => undef, | ||||||||||
| 49 | %options | ||||||||||
| 50 | }; | ||||||||||
| 51 | |||||||||||
| 52 | 1 | bless $self, $class; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | ########################################### | ||||||||||
| 56 | 1 | ▶ | sub ssh_client_ip_find { | ||||||||
|
Subroutine
ssh_client_ip_find: called CC=6 SCAR=20.9
Pod
undocumented
| |||||||||||
| 57 | ########################################### | ||||||||||
| 58 | 1 | my($self) = @_; | |||||||||
| 59 | |||||||||||
| 60 | 1 | my $pid = $self->{pid}; | |||||||||
| 61 | |||||||||||
| 62 | 1 | ▶ | while( exists $self->{ppid_of}->{ $pid } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | |||||||||||
| 64 | 1 | $pid = $self->{ppid_of}->{ $pid }; | |||||||||
| 65 | |||||||||||
| 66 | 1 | ▶ | last if $pid == 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | |||||||||||
| 68 | 1 | my $env = $self->{proc_info}->env( $pid ); | |||||||||
| 69 | |||||||||||
| 70 | 1 | ▶ | if(! defined $env) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 0 | $self->error( $self->{proc_info}->error() ); | |||||||||
| 72 | 0 | return undef; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 1 | ▶ | if( exists $env->{ $self->{ssh_client_var_name} } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | my($ip, $pid, $port) = split /\s+/, | ||||||||||
| 77 | 1 | $env->{ $self->{ssh_client_var_name} }; | |||||||||
| 78 | |||||||||||
| 79 | 1 | ▶ | if( wantarray ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 0 | return ($ip, $pid, $port); | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 1 | return $ip; | |||||||||
| 84 | } | ||||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 0 | $self->error( "Can't find $self->{ssh_client_var_name} anywhere" ); | |||||||||
| 88 | 0 | return undef; | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | ########################################### | ||||||||||
| 92 | 0 | ▶ | sub error { | ||||||||
|
Subroutine
error: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 93 | ########################################### | ||||||||||
| 94 | 0 | my($self, $error) = @_; | |||||||||
| 95 | |||||||||||
| 96 | 0 | ▶ | if(defined $error) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 0 | $self->{error} = $error; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 0 | return $self->{error}; | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 2 | 1; | |||||||||
| 104 | |||||||||||
| 105 | __END__ | ||||||||||