在运维工作中我们经常需要检测用户访问是否正常,一般来说,直接通过 DNS 客户端获取 A 记录就可以满足需要。不过如果我们可以获得具体连接的 IP 地址,那么就可以缩小问题的判断范围,因为 DNS 的 A 记录通常是有多个的。
AE::HTTP 模块可以返回 sock 给用户进行具体操作,我们可以通过 sock 接口很简单的获得对端的 IP 地址:
package Web::Checker::Util::HTTP;
use Moo;
use MooX::Types::MooseLike::Base qw/Str Num/;
use AnyEvent::HTTP;
use AnyEvent::Socket;
use AnyEvent;
use Time::HiRes qw/time/;
has peer => ( is => 'rw', isa => Str );
has reptime => ( is => 'rw', isa => Num );
has clength => ( is => 'rw', isa => Num );
has body => ( is => 'ro', isa => Str );
has proxy => ( is => 'ro', isa => Str, default => sub {undef} );
has cv => ( is => 'ro', default => sub { AnyEvent->condvar } );
sub get {
my ( $self, $url ) = @_;
$self->cv->begin;
my $begin = time;
http_get $url,
proxy => $self->proxy,
# 就是这里发挥了作用,默认应该是直接返回 body 字符串的
want_body_handle => 1,
sub {
my ( $hdl, $headers ) = @_;
my ( $port, $peer )
= AnyEvent::Socket::unpack_sockaddr getpeername $hdl->{fh};
$self->peer( AnyEvent::Socket::format_address $peer );
if ( $headers->{Status} =~ /^2/ ) {
my $end = time;
$self->reptime( $end - $begin );
$self->clength( $headers->{'content-length'} );
$self->cv->end;
}
};
$self->cv->recv;
}
1;
其实 AE::HTTP 还可以在 tcp_connect 的时候获取 sock,这时候就需要自己用 AnyEvent::Handle 写一遍 AnyEvent::HTTP::tcp_connect 已经写过的东西了(当然如果你本来就打算干点别的事情,那就是另外一回事情了)~~
mjnx.youku08.com
ejw3.youku08.com
uenx.youku08.com
frlu.youku08.com
cls6.youku08.com
mkxu.youku08.com
jg85.youku08.com
bju1.youku08.com
vlg5.youku08.com
oheg.youku08.com
qc4a.west186.com
wiqg.west186.com
x6q0.west186.com
ko59.west186.com
3p12.west186.com
b7du.west186.com
goop.west186.com
ithe.west186.com
1m6o.west186.com
1jju.west186.com
elgo.34flash.com
5krd.34flash.com
56x0.34flash.com
b8bs.34flash.com
fhkd.34flash.com
70q3.34flash.com
ax07.34flash.com
6qcp.34flash.com
5k4d.34flash.com
2cd6.34flash.com
flig.ba029.com
0r74.ba029.com
t0kj.ba029.com
1v0j.ba029.com
r6ev.ba029.com
0a09.ba029.com
kvb0.ba029.com
usq0.ba029.com
0jap.ba029.com
p0bf.ba029.com
42an.sokecanada.com
j0ms.sokecanada.com
eo0h.sokecanada.com
wadm.sokecanada.com
2lh2.sokecanada.com
tggq.sokecanada.com
27o5.sokecanada.com
g6g2.sokecanada.com
mk54.sokecanada.com
2xr7.sokecanada.com
khjh.17hainan.com
c6hk.17hainan.com
bc6k.17hainan.com
aood.17hainan.com
xd6r.17hainan.com
w1f1.17hainan.com
uvlh.17hainan.com
vxsu.17hainan.com
hc6f.17hainan.com
7b4a.17hainan.com
阅读(779) | 评论(0) | 转发(0) |