| 1 | package Geo::Mirror; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | our $VERSION = '1.45'; | ||
| 6 | |||
| 7 | use constant PI => 3.14159265358979323846; | ||
| 8 | |||
| 9 | our ( %lat, %lon, $geo_ip_pkg ); | ||
| 10 | |||
| 11 | BEGIN { | ||
| 12 | if ( eval { require Geo::IP } ) { | ||
| 13 | $geo_ip_pkg = "Geo::IP"; | ||
| 14 | } | ||
| 15 | else { | ||
| 16 | require Geo::IP::PurePerl; | ||
| 17 | $geo_ip_pkg = "Geo::IP::PurePerl"; | ||
| 18 | } | ||
| 19 | $geo_ip_pkg->import('GEOIP_STANDARD'); | ||
| 20 | } | ||
| 21 | |||
| 22 | { | ||
| 23 | local $_; | ||
| 24 | while (<DATA>) { | ||
| 25 | my ( $country, $lat, $lon ) = split(':'); | ||
| 26 | |||
| 27 | $lat{$country} = $lat; | ||
| 28 | $lon{$country} = $lon; | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | sub new { | ||
| 33 | my ( $class, %h ) = @_; | ||
| 34 | my $self = \%h; | ||
| 35 | bless $self, $class; | ||
| 36 | $self->_init; | ||
| 37 | return $self; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub _init { | ||
| 41 | my $self = shift; | ||
| 42 | my $mirror_file = $self->{mirror_file}; | ||
| 43 | local $_; | ||
| 44 | open MIRROR, "$mirror_file" or die $!; | ||
| 45 | while (<MIRROR>) { | ||
| 46 | my ( $url, $country, $fresh ) = split(' '); | ||
| 47 | push @{ $self->{mirror}->{$country} }, $url; | ||
| 48 | $self->{fresh}{$url} = $fresh || 0; | ||
| 49 | } | ||
| 50 | close MIRROR; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub _random_mirror { | ||
| 54 | my ( $self, $country, $fresh ) = @_; | ||
| 55 | |||
| 56 | my @mirror = grep { $self->{fresh}{$_} >= $fresh } | ||
| 57 | @{ $self->{mirror}->{$country} }; | ||
| 58 | |||
| 59 | my $num = scalar @mirror or return; | ||
| 60 | |||
| 61 | return $mirror[ rand($num) ]; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub find_mirror_by_country { | ||
| 65 | my ( $self, $country, $fresh ) = @_; | ||
| 66 | my $url; | ||
| 67 | |||
| 68 | $fresh ||= 0; | ||
| 69 | |||
| 70 | if ( exists $self->{mirror}->{$country} ) { | ||
| 71 | $url = $self->_random_mirror( $country, $fresh ); | ||
| 72 | } | ||
| 73 | |||
| 74 | if ( !$url ) { | ||
| 75 | my $nearby = $self->{nearby_cache}->{$country} | ||
| 76 | ||= $self->_find_nearby_countries($country); | ||
| 77 | foreach my $new_country (@$nearby) { | ||
| 78 | $url = $self->_random_mirror( $new_country, $fresh ) and last; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | return $url; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub find_mirror_by_addr { | ||
| 86 | my ( $self, $addr, $fresh ) = @_; | ||
| 87 | |||
| 88 | unless ( $self->{gi} ) { | ||
| 89 | if ( $self->{database_file} ) { | ||
| 90 | $self->{gi} | ||
| 91 | = $geo_ip_pkg->open( $self->{database_file}, GEOIP_STANDARD ); | ||
| 92 | } | ||
| 93 | else { | ||
| 94 | $self->{gi} = $geo_ip_pkg->new(GEOIP_STANDARD); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | # default to US if country not found | ||
| 99 | my $country = lc( $self->{gi}->country_code_by_addr($addr) ) || 'us'; | ||
| 100 | $country = 'us' if $country eq '--'; | ||
| 101 | return $self->find_mirror_by_country( $country, $fresh ); | ||
| 102 | } | ||
| 103 | |||
| 104 | sub _find_nearby_countries { | ||
| 105 | my ( $self, $country ) = @_; | ||
| 106 | |||
| 107 | my %distance = map { ( $_, $self->_calculate_distance( $country, $_ ) ) } | ||
| 108 | keys %{ $self->{mirror} }; | ||
| 109 | delete $distance{$country}; | ||
| 110 | |||
| 111 | [ sort { $distance{$a} <=> $distance{$b} } keys %distance ]; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub _calculate_distance { | ||
| 115 | my ( $self, $country1, $country2 ) = @_; | ||
| 116 | |||
| 117 | my $lat_1 = $lat{$country1}; | ||
| 118 | my $lat_2 = $lat{$country2}; | ||
| 119 | my $lon_1 = $lon{$country1}; | ||
| 120 | my $lon_2 = $lon{$country2}; | ||
| 121 | |||
| 122 | # Convert all the degrees to radians | ||
| 123 | $lat_1 *= PI / 180; | ||
| 124 | $lon_1 *= PI / 180; | ||
| 125 | $lat_2 *= PI / 180; | ||
| 126 | $lon_2 *= PI / 180; | ||
| 127 | |||
| 128 | # Find the deltas | ||
| 129 | my $delta_lat = $lat_2 - $lat_1; | ||
| 130 | my $delta_lon = $lon_2 - $lon_1; | ||
| 131 | |||
| 132 | # Find the Great Circle distance | ||
| 133 | my $temp = sin( $delta_lat / 2.0 )**2 | ||
| 134 | + cos($lat_1) * cos($lat_2) * sin( $delta_lon / 2.0 )**2; | ||
| 135 | return atan2( sqrt($temp), sqrt( 1 - $temp ) ); | ||
| 136 | } | ||
| 137 | |||
| 138 | 1; | ||
| 139 | |||
| 140 | =head1 NAME | ||
| 141 | |||
| 142 | Geo::Mirror - Find closest Mirror | ||
| 143 | |||
| 144 | =head1 SYNOPSIS | ||
| 145 | |||
| 146 | use Geo::Mirror; | ||
| 147 | |||
| 148 | my $gm = Geo::Mirror->new(mirror_file => '/path/to/mirrors.txt'); | ||
| 149 | |||
| 150 | my $mirror = $gm->closest_mirror_by_country('us', $freshness); # $freshness optional, see description below | ||
| 151 | my $mirror = $gm->closest_mirror_by_addr('24.24.24.24', $freshness); # $freshness optional, see description below | ||
| 152 | |||
| 153 | =head1 DESCRIPTION | ||
| 154 | |||
| 155 | This module finds the closest mirror for an IP address. It uses L<Geo::IP> | ||
| 156 | to identify the country that the IP address originated from. If | ||
| 157 | the country is not represented in the mirror list, then it finds the | ||
| 158 | closest country using a latitude/longitude table. | ||
| 159 | |||
| 160 | The mirror file should be a space separate list of URL, Country, and mirror freshness. | ||
| 161 | The mirror freshness is optional and can be used by user | ||
| 162 | request a mirror with a minimum freshness. | ||
| 163 | |||
| 164 | =head1 AUTHOR | ||
| 165 | |||
| 166 | Copyright (c) 2004, MaxMind LLC, support@maxmind.com | ||
| 167 | |||
| 168 | All rights reserved. This package is free software; you can redistribute it | ||
| 169 | and/or modify it under the same terms as Perl itself. | ||
| 170 | |||
| 171 | =cut | ||
| 172 | |||
| 173 | __DATA__ |