-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheil.pm
82 lines (67 loc) · 2.04 KB
/
eil.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use Net::DNS::Nameserver;
use Net::DNS::Resolver;
use Net::DNS;
use POSIX qw/strftime/;
use Encode;
use JSON;
use File::Slurp qw/slurp/;
use Data::Dumper;
our $DOM = 'www.qq.com';
our $EIL_CODE = 0xFDF0;
my $whitelist= slurp('eil_whitelist.json');
our $EIL_W = decode_json( $whitelist ); #whitelist
sub is_exists_eil {
my ($query) = @_;
my $opt = $query->edns;
return unless ($opt);
my $eil_val = $opt->{option}{$EIL_CODE};
return $eil_val;
}
sub read_eil_val {
my ($eil_val, $is_guess) = @_;
my ( $country_code, $area_code, $isp ) = $eil_val =~ /^(.{2})(.{6})(.{4})/;
$country_code =~ s/^\s+|\s+$//g;
$area_code =~ s/^\s+|\s+$//g;
$isp =~ s/^\s+|\s+$//g;
my $eil = {
country_code => $country_code,
area_code => $area_code,
isp => $isp,
};
return $is_guess ? guess_eil($eil) : strict_eil($eil) ;
}
sub strict_eil {
my ($eil) = @_;
my ( $c, $a, $i ) = @{$eil}{ 'country_code', 'area_code', 'isp' };
return unless ( exists $EIL_W->{$c} );
return if ( $a ne '*' and $a ne '' and ! exists $EIL_W->{$c}{area}{$a} );
return if ( $i ne '*' and $i ne '' and ! exists $EIL_W->{$c}{isp}{$i} );
return $eil;
}
sub guess_eil {
my ($eil) = @_;
my ( $c, $a, $i ) = @{$eil}{ 'country_code', 'area_code', 'isp' };
$eil->{'country_code'} = '' unless ( exists $EIL_W->{$c} );
$eil->{'area_code'} = ''
unless ( exists $EIL_W->{$c}{area}{$a} );
$eil->{'isp'} = '' unless ( exists $EIL_W->{$c}{isp}{$i} );
return $eil;
}
sub gen_eil_opt {
my ( $country_code, $area_code, $isp ) = @_;
my $eil_opt = new Net::DNS::RR(
type => 'OPT',
flags => 0,
rcode => 0,
);
my $eil_val = gen_eil_val( $country_code, $area_code, $isp );
$eil_opt->option( $EIL_CODE => $eil_val );
return $eil_opt;
}
sub gen_eil_val {
my ( $country_code, $area_code, $isp ) = @_;
$country_code = uc($country_code);
$isp = uc($isp);
my $eil_val = pack( 'A2A6A4', $country_code, $area_code, $isp );
return $eil_val;
}