|
Line 0
Link Here
|
|
|
1 |
#------------------------------------------------------------------------------ |
| 2 |
# Copyright (C) 2014, Jasper Lievisse Adriaanse <jasper@openbsd.org> |
| 3 |
# |
| 4 |
# Permission to use, copy, modify, and distribute this software for any |
| 5 |
# purpose with or without fee is hereby granted, provided that the above |
| 6 |
# copyright notice and this permission notice appear in all copies. |
| 7 |
# |
| 8 |
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 |
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 |
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 |
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 |
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 |
# |
| 16 |
#------------------------------------------------------------------------------ |
| 17 |
|
| 18 |
package Portscout::SiteHandler::GitHub; |
| 19 |
|
| 20 |
use JSON qw(decode_json); |
| 21 |
use LWP::UserAgent; |
| 22 |
|
| 23 |
use Portscout::Const; |
| 24 |
use Portscout::Config; |
| 25 |
|
| 26 |
use strict; |
| 27 |
|
| 28 |
require 5.006; |
| 29 |
|
| 30 |
|
| 31 |
#------------------------------------------------------------------------------ |
| 32 |
# Globals |
| 33 |
#------------------------------------------------------------------------------ |
| 34 |
|
| 35 |
push @Portscout::SiteHandler::sitehandlers, __PACKAGE__; |
| 36 |
|
| 37 |
our %settings; |
| 38 |
|
| 39 |
|
| 40 |
#------------------------------------------------------------------------------ |
| 41 |
# Func: new() |
| 42 |
# Desc: Constructor. |
| 43 |
# |
| 44 |
# Args: n/a |
| 45 |
# |
| 46 |
# Retn: $self |
| 47 |
#------------------------------------------------------------------------------ |
| 48 |
|
| 49 |
sub new |
| 50 |
{ |
| 51 |
my $self = {}; |
| 52 |
my $class = shift; |
| 53 |
|
| 54 |
$self->{name} = 'GitHub'; |
| 55 |
|
| 56 |
bless ($self, $class); |
| 57 |
return $self; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
#------------------------------------------------------------------------------ |
| 62 |
# Func: CanHandle() |
| 63 |
# Desc: Ask if this handler (package) can handle the given site. |
| 64 |
# |
| 65 |
# Args: $url - URL of site. |
| 66 |
# |
| 67 |
# Retn: $res - true/false. |
| 68 |
#------------------------------------------------------------------------------ |
| 69 |
|
| 70 |
sub CanHandle |
| 71 |
{ |
| 72 |
my $self = shift; |
| 73 |
|
| 74 |
my ($url) = @_; |
| 75 |
|
| 76 |
return ($url =~ /^https?:\/\/([^\/.]+\.)?github\.com\/(.*?)\/tar.gz/); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
#------------------------------------------------------------------------------ |
| 81 |
# Func: GetFiles() |
| 82 |
# Desc: Extract a list of files from the given URL. In the case of GitHub, |
| 83 |
# we are actually pulling the files from the project's Atom feed and |
| 84 |
# extract the release url, containing the tag it was based on. |
| 85 |
# |
| 86 |
# Args: $url - URL we would normally fetch from. |
| 87 |
# \%port - Port hash fetched from database. |
| 88 |
# \@files - Array to put files into. |
| 89 |
# |
| 90 |
# Retn: $success - False if file list could not be constructed; else, true. |
| 91 |
#------------------------------------------------------------------------------ |
| 92 |
|
| 93 |
sub GetFiles |
| 94 |
{ |
| 95 |
my $self = shift; |
| 96 |
|
| 97 |
my ($url, $port, $files) = @_; |
| 98 |
my $projname; |
| 99 |
|
| 100 |
if ($url =~ /https:\/\/github\.com\/(.*?)\/archive\//) { |
| 101 |
$projname = $1; |
| 102 |
} elsif ($url =~ /https:\/\/github.com\/downloads\/(.*)\//) { |
| 103 |
$projname = $1; |
| 104 |
} |
| 105 |
|
| 106 |
if ($projname) { |
| 107 |
my ($query, $ua, $response, $items, $json); |
| 108 |
|
| 109 |
# First check if there's a latest releases endpoint |
| 110 |
$query = 'https://api.github.com/repos/' . $projname . '/releases/latest'; |
| 111 |
|
| 112 |
_debug("GET $query"); |
| 113 |
$ua = LWP::UserAgent->new; |
| 114 |
$ua->agent(USER_AGENT); |
| 115 |
$ua->timeout($settings{http_timeout}); |
| 116 |
|
| 117 |
$response = $ua->request(HTTP::Request->new(GET => $query)); |
| 118 |
|
| 119 |
if (!$response->is_success || $response->status_line !~ /^2/) { |
| 120 |
_debug('GET failed: ' . $response->status_line); |
| 121 |
# Project didn't do any releases, so let's try tags instead. |
| 122 |
$query = 'https://api.github.com/repos/' . $projname . '/tags'; |
| 123 |
_debug("GET $query"); |
| 124 |
$ua = LWP::UserAgent->new; |
| 125 |
$ua->agent(USER_AGENT); |
| 126 |
$ua->timeout($settings{http_timeout}); |
| 127 |
|
| 128 |
$response = $ua->request(HTTP::Request->new(GET => $query)); |
| 129 |
|
| 130 |
if (!$response->is_success || $response->status_line !~ /^2/) { |
| 131 |
_debug('GET failed: ' . $response->status_line); |
| 132 |
return 0; |
| 133 |
} |
| 134 |
|
| 135 |
$json = decode_json($response->decoded_content); |
| 136 |
foreach my $tag (@$json) { |
| 137 |
my $tag_url = $tag->{tarball_url}; |
| 138 |
push(@$files, $tag_url); |
| 139 |
} |
| 140 |
|
| 141 |
_debug('Found ' . scalar @$files . ' files'); |
| 142 |
return 1; |
| 143 |
} |
| 144 |
|
| 145 |
$json = decode_json($response->decoded_content); |
| 146 |
push(@$files, $json->{tarball_url}); |
| 147 |
|
| 148 |
_debug('Found ' . scalar @$files . ' files'); |
| 149 |
} else { |
| 150 |
return 0; |
| 151 |
} |
| 152 |
|
| 153 |
return 1; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
#------------------------------------------------------------------------------ |
| 158 |
# Func: _debug() |
| 159 |
# Desc: Print a debug message. |
| 160 |
# |
| 161 |
# Args: $msg - Message. |
| 162 |
# |
| 163 |
# Retn: n/a |
| 164 |
#------------------------------------------------------------------------------ |
| 165 |
|
| 166 |
sub _debug |
| 167 |
{ |
| 168 |
my ($msg) = @_; |
| 169 |
|
| 170 |
$msg = '' if (!$msg); |
| 171 |
|
| 172 |
print STDERR "(" . __PACKAGE__ . ") $msg\n" if ($settings{debug}); |
| 173 |
} |
| 174 |
|
| 175 |
1; |