/
main.pl
executable file
·176 lines (155 loc) · 6.12 KB
/
main.pl
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
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.010;
use autodie;
use Carp qw(croak);
use Data::Dumper;
use Encode qw(decode);
use FindBin;
use Getopt::Long ();
use HTTP::CookieJar;
use HTTP::Tiny;
use JSON ();
use Text::Trim qw(trim);
use URI;
use Web::Scraper;
use YAML ();
my $CONFIG_FILE = "${FindBin::RealBin}/config.yml";
my $USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64; rv:36.0) Gecko/20100101 Firefox/36.0';
my $BASE_URL = 'https://shadowx.co';
my $BASE_URI = URI->new( $BASE_URL );
sub build_uri_path {
my ( $uri, $path ) = @_;
my $uri_clone = $uri->clone;
$uri_clone->path( $path );
return $uri_clone;
}
sub get_token {
my ( $http ) = @_;
my $login_page_response = $http->get( build_uri_path( $BASE_URI, '/auth/login' ) );
croak sprintf( 'Cannot get login page! (%d: %s)', $login_page_response->{status}, $login_page_response->{reason} ) if not $login_page_response->{success};
my $scraper = scraper {
process_first 'input[name="_token"]', 'token' => '@value';
};
my $res = $scraper->scrape( decode( 'utf8', $login_page_response->{content} ) );
return $res->{token};
}
sub login {
my ( $http, $account ) = @_;
my $token = get_token( $http );
my $login_response = $http->post_form( build_uri_path( $BASE_URI, '/auth/login' ), {
email => $account->{email},
password => $account->{password},
_token => $token,
}, {
headers => {
Referer => build_uri_path( $BASE_URI, '/auth/login' ),
},
} );
croak sprintf( 'Cannot login! (%d: %s)', $login_response->{status}, $login_response->{reason} ) if $login_response->{status} != 302;
}
sub checkin {
my ( $http ) = @_;
my $checkin_response = $http->get( build_uri_path( $BASE_URI, '/user/checkin' ), {
headers => {
'X-Requested-With' => 'XMLHttpRequest',
Referer => build_uri_path( $BASE_URI, '/user' ),
}
} );
croak sprintf( 'Cannot checkin! (%d: %s)', $checkin_response->{status}, $checkin_response->{reason} ) if not $checkin_response->{success};
return JSON->new->utf8->decode( $checkin_response->{content} );
}
sub get_account_info {
my ( $http ) = @_;
my $account_info_response = $http->get( build_uri_path( $BASE_URI, '/user' ) );
croak sprintf( 'Cannot get account info! (%d: %s)', $account_info_response->{status}, $account_info_response->{reason} ) if not $account_info_response->{success};
my $scraper = scraper {
process '.box', 'box[]' => scraper {
process_first '.box-title', 'title' => [ 'TEXT', sub { trim } ];
process '.box-body p:not(:empty)', 'body[]' => [ 'TEXT', sub { trim } ];
};
};
my $res = $scraper->scrape( decode( 'utf8', $account_info_response->{content} ) );
return $res->{box};
}
sub get_node_info {
my ( $http ) = @_;
my $node_info_response = $http->get( build_uri_path( $BASE_URI, '/user/node' ) );
croak sprintf( 'Cannot get node info! (%d: %s)', $node_info_response->{status}, $node_info_response->{reason} ) if not $node_info_response->{success};
my $scraper = scraper {
process '.content-wrapper .col-md-6 > .box', 'node_lists[]' => scraper {
process_first '.box-title', 'name' => [ 'TEXT', sub { trim } ];
process '.nav-tabs-custom', 'nodes[]' => scraper {
process_first '.header' , 'name' => [ 'TEXT', sub { trim } ];
process_first '.tab-content code', 'address' => [ 'TEXT', sub { trim } ];
process_first '.tab-content .bg-orange', 'status' => [ 'TEXT', sub { trim } ];
process_first '.tab-content .bg-green', 'method' => [ 'TEXT', sub { trim } ];
process_first '.tab-content p:last-child', 'description' => [ 'TEXT', sub { trim } ];
};
};
};
my $res = $scraper->scrape( decode( 'utf8', $node_info_response->{content} ) );
return $res->{node_lists};
}
sub get_sys_info {
my ( $http ) = @_;
my $sys_info_response = $http->get( build_uri_path( $BASE_URI, '/user/sys' ) );
croak sprintf( 'Cannot get sys info! (%d: %s)', $sys_info_response->{status}, $sys_info_response->{reason} ) if not $sys_info_response->{success};
my $scraper = scraper {
process '.box-body > p', 'sys[]' => [ 'TEXT', sub { trim } ];
};
my $res = $scraper->scrape( decode( 'utf8', $sys_info_response->{content} ) );
return $res->{sys};
}
sub main {
binmode *STDIN, ':encoding(utf8)';
binmode *STDOUT, ':encoding(utf8)';
binmode *STDERR, ':encoding(utf8)';
my $getopt_parser = Getopt::Long::Parser->new;
my %option_of;
croak 'Cannot parse options!' if not $getopt_parser->getoptionsfromarray(
\@_,
\%option_of,
'checkin!',
'account-info!',
'node-info!',
'sys-info!'
);
my $config = YAML::LoadFile( $CONFIG_FILE );
my $cookie_jar = HTTP::CookieJar->new;
my $http = HTTP::Tiny->new(
agent => $USER_AGENT,
cookie_jar => $cookie_jar
);
login( $http, $config->{account} );
if ( defined $option_of{checkin} and $option_of{checkin} == 1 ) {
say checkin( $http )->{msg};
}
if ( defined $option_of{'account-info'} and $option_of{'account-info'} == 1 ) {
my $account_info = get_account_info( $http );
for my $box ( @{ $account_info } ) {
say $box->{title};
map { say $_ } @{ $box->{body} };
print "\n";
}
}
if ( defined $option_of{'node-info'} and $option_of{'node-info'} == 1 ) {
my $node_info = get_node_info( $http );
my @fields = qw/name description address method status/;
for my $node_list ( @{ $node_info } ) {
next if not defined $node_list->{nodes};
say $node_list->{name};
for my $node ( @{ $node_list->{nodes} } ) {
map { say $node->{ $_ } } @fields;
print "\n";
}
}
}
if ( defined $option_of{'sys-info'} and $option_of{'sys-info'} == 1 ) {
my $sys_info = get_sys_info( $http );
map { say $_ } @{ $sys_info };
}
}
main( @ARGV );