-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEEWSock.pm
207 lines (167 loc) · 4.9 KB
/
EEWSock.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package EEWSock;
# EEWSock.pm
# walkure at 3pf.jp
use strict;
use warnings;
use base qw/IO::Socket::INET/;
use Encode;
use Time::Piece;
use Time::HiRes;
use HTTP::Tiny;
use HTTP::Headers;
use Digest::MD5 qw(md5_hex);
use Socket qw(SOL_SOCKET SO_KEEPALIVE IPPROTO_TCP TCP_KEEPINTVL TCP_KEEPIDLE TCP_KEEPCNT);
sub configure
{
my ( $self, $args ) = @_;
*$self->{server_list} = $args->{ServerList} if defined $args->{ServerList};
*$self->{callback} = $args->{Callback} if defined $args->{Callback};
*$self->{header} = {};
*$self->{buffer} = '';
$self->get_server_list() unless defined *$self->{server_list};
my ($host,$port) = $self->choose_server();
print "+Choose $host:$port\n";
$args->{PeerAddr} = "$host:$port";
*$self->{timeout_log} = $args->{Logs}{Timeout};
*$self->{keepalive_log} = $args->{Logs}{KeepAlive};
print "+Timeout Log Enabled\n" if *$self->{timeout_log};
print "+KeepAlive Log Enabled\n" if *$self->{keepalive_log};
my $sock = $self->SUPER::configure($args);
# activate TCP Keep-Alive
$sock->setsockopt(SOL_SOCKET, SO_KEEPALIVE, 1);
# keep-alive packet interval 15secs.
$sock->setsockopt(IPPROTO_TCP, TCP_KEEPINTVL, 15);
# idle wait 4minutes
$sock->setsockopt(IPPROTO_TCP, TCP_KEEPIDLE, 60 * 3 );
$sock;
}
sub choose_server
{
my $self = shift;
my @server = @{*$self->{server_list}};
my $sum = scalar @server;
my ( $ip, $port ) = split /:/, $server[ int( rand($sum) ) ];
($ip,$port);
}
sub get_server_list
{
my $self = shift;
if(ref($self) eq 'EEWSock' && defined *$self->{server_list}){
print 'Cached '.scalar @{*$self->{server_list}}." servers\n";
return *$self->{server_list};
}
my $ua = HTTP::Tiny->new(agent => 'FastCaster/1.0 powered by weathernews.');
my @server;
my $res = $ua->get('http://lst10s-sp.wni.co.jp/server_list.txt');
if ( $res->{success}) {
@server = split /[\r\n]+/, $res->{content};
}
else {
die "Cannot get server list: $res->{status} $res->{reason} \n";
}
print 'Retrieved '.scalar @server." servers\n";
if(ref($self) eq 'EEWSock'){
*$self->{server_list} = \@server;
}else{
\@server;
}
}
sub wni_timestamp
{
my ($seconds,$microseconds) = Time::HiRes::gettimeofday;
sprintf '%s.%06d', localtime($seconds)->strftime('%Y/%m/%d %H:%M:%S'), $microseconds;
}
sub send_ack
{
my $self = shift;
my $now = wni_timestamp();
my $response = HTTP::Headers->new(
'Content-Type' => 'application/fast-cast',
'Server' => 'FastCaster/1.0.0 (Unix)',
'X-WNI-ID' => 'Response',
'X-WNI-Result' => 'OK',
'X-WNI-Protocol-Version' => '2.1',
'X-WNI-Time' => $now
);
my $msg = "HTTP/1.0 200 OK\n" . $response->as_string ."\n";
$self->syswrite($msg);
print scalar localtime." Send Request(Timeout) Waiting...\n" if *$self->{timeout_log};
}
sub logon
{
my ($self,$conf) = @_;
my $hash = defined $conf->{'passwd-md5'} ? $conf->{'passwd-md5'} : md5_hex($conf->{passwd});
my $now = wni_timestamp();
my $request = HTTP::Headers->new(
'User-Agent' => 'FastCaster/1.0 powered by weathernews.',
'Accept' => '*/*',
'Cache-Control' => 'no-cache',
'X-WNI-Account' => $conf->{user},
'X-WNI-Password' => $hash,
'X-WNI-Application-Version' => '2.2.4.0',
'X-WNI-Authentication-Method' => 'MDB_MWS',
'X-WNI-ID' => 'Login',
'X-WNI-Protocol-Version' => '2.1',
'X-WNI-Terminal-ID' => '211363088',
'X-WNI-Time' => $now
);
my $body = "GET /login HTTP/1.0\n" . $request->as_string . "\n" ;
$self->syswrite($body);
print scalar localtime." Send Request waiting...\n";
}
sub parse_body
{
my ($self,$body) = @_;
*$self->{buffer} .= $body;
unless(defined *$self->{size}){
my $i;
while (($i = index(*$self->{buffer},"\x0a")) >= 0){ #index for the head of string is 0
my $line = substr(*$self->{buffer},0,$i);
*$self->{buffer} = substr(*$self->{buffer},$i+1);
if($line =~ /GET \/ HTTP\/1.1/){
$self->send_ack();
*$self->{header} = {};
*$self->{buffer} = '';
next;
}
if($line =~ /:/){
my($name,$body) = split(/:\s*/,$line);
*$self->{header}{$name} = $body;
next;
}
unless(length $line){
my $id = *$self->{header}{'X-WNI-ID'};
if($id eq 'Data'){
if(defined *$self->{header}{'Content-Length'}){
print 'Begin Contents('.*$self->{header}{'Content-Length'}.")\n";
*$self->{size} = *$self->{header}{'Content-Length'} + 0;
#break from while
last;
}
}elsif($id eq 'Response'){
print 'Auth Status:'.*$self->{header}{'X-WNI-Result'}."\n";
}elsif($id eq 'Keep-Alive'){
print scalar localtime." Keep-Alive...\n" if *$self->{keepalive_log};
}else{
print "STATE:[$id]\n";
}
*$self->{size} = undef;
}
}
}
if(defined *$self->{size}){
if(*$self->{size} == length *$self->{buffer}){
if(defined *$self->{callback}){
*$self->{callback}->($self,*$self->{header}{'X-WNI-Data-MD5'},*$self->{buffer});
}
*$self->{size} = undef;
*$self->{buffer} = '';
}
}
}
sub set_callback
{
my($self,$func) = @_;
*$self->{callback} = $func;
}
1;