#!/usr/bin/perl
##LICENSE##
package RVSInstaller::Connection;
use strict;
#use warnings;
use Carp;
use Socket;
use File::Basename;
use vars qw( $VERSION $THIS_PATH);
$VERSION = "0.01";
sub new {
my ($class, %args) = @_;
my $self = {%args};
croak "__PACKAGE__ Constructor require 'programName' parameter to create object" if (!defined($self->{'programName'}));
bless $self, $class;
my $Config = RVSInstaller::Config(programName => $self->programName);
$THIS_PATH = $Config->{'installConf'}->{'thisPath'};
return $self;
}
sub UrlConnection{
my $self = shift;
my ($site, $port, $authorization, $sslMode, $request, $timeout) = @_;
$timeout = $timeout ? $timeout : '30';
my %result = ();
if ( $sslMode && -f $THIS_PATH . '/.skipsslconnection') {
$result{'Err'} = 'Skip SSL Connection';
return \%result;
}
if ($sslMode) {
$port = $port ? $port : '443';
my $page = '';
my $SSLresult = '';
my @headers = ();
eval { require Net::SSLeay; };
if ($@) {
$result{'Err'} = 'Net::SSLeay' . " is not compiled";
return \%result;
}
eval {
local $SIG{ALRM} = sub { $result{'Err'} = "Connection Timeout"; return \%result;};
alarm $timeout;
($page, $SSLresult, @headers) = Net::SSLeay::get_https($site, $port, $request) or sub { $result{'Err'} = 'Net::SSLeay::get_https : Error'; return \%result;};
alarm 0;
};
if ( $SSLresult =~m/^HTTP\/([\d\.]+)\s+200\b/ ) {
$result{'Page'} = $page;
}
else {
$result{'Err'} = $SSLresult;
}
return \%result;
}
else {
NONESSL:
$site =~s/^http:\/\///gi;
$site =~s/^https:\/\///gi;
my $poto = getprotobyname('tcp');
my $ipaddr = inet_aton($site);
if ( !defined($ipaddr) ) {
$result{'Err'} = "Get protocal by hostname failed" . '.';
return \%result;
}
socket(SOCKET, AF_INET, SOCK_STREAM, $poto) || sub { $result{'Err'} = "socket error" . ": $!"; return \%result; };
$port = getservbyname( 'http', 'tcp' );
my $sin = sockaddr_in($port, $ipaddr);
connect (SOCKET, $sin) || sub { $result{'Err'} = "Cannot create socket connection to" . ' ' . $site . '.'; return \%result; };
my ($postData, @postDatas, $DATA, $lenData);
$DATA ='';
($request, $postData) = split(/\?/, $request, 2);
@postDatas = split(/&/,$postData);
foreach (@postDatas) {
my ($uKey, $uvalue) = split(/=/, $_ ,2);
if ($DATA && $DATA ne '') {
$DATA .= '&';
}
$DATA .= $uKey . '=' . $self->URLEncode($uvalue);
}
$lenData = length($DATA);
if ( $lenData <= 1800) {
# <-- Data request lower 1800 char send by get -->
send SOCKET, "GET " . $request . '?' . $DATA . " HTTP/1.0\r\nHost: $site\r\n\r\n", 0;
}
else {
# <-- Data request more then 1800 char send by post -->
send SOCKET, "POST $request HTTP/1.0\n", 0;
send SOCKET, "Host: $site\n", 0;
send SOCKET, "Content-Type: application/x-www-form-urlencoded\n", 0;
send SOCKET, "Content-Length: $lenData\n", 0;
send SOCKET, "Connection: Keep-Alive\n", 0;
send SOCKET, "Cache-Control: no-cache\n", 0;
send SOCKET, "\n", 0;
send SOCKET, $DATA, 0;
}
my $inheaders = 1;
my $ch_line = 0;
my $httpStatus;
$result{'Page'} = '';
while (my $data = <SOCKET>) {
if($inheaders) {
$data=~s/\n//g;
$data=~s/\s$//g;
if ( $data =~m/^HTTP\/([\d\.]+)\s(.*)/ ) {
$httpStatus = $2;
$httpStatus =~s/^\s//gi;
$httpStatus =~s/\s$//gi;
}
if ( $data =~m/^$/ && $inheaders) {
$inheaders = 0;
}
}
else {
$result{'Page'} .= $data;
}
}
close (SOCKET);
if ( $result{'Page'}) {
return \%result;
}
else {
$result{'Err'} = "Connection" . ' ' . $httpStatus . '.';
return \%result;
}
}
}
sub URLEncode{
my $self = shift;
my $theURL = $_[0];
$theURL =~ s/([\W])/"%" . uc(sprintf("%2.2x",ord($1)))/eg;
return $theURL;
}
1;
__END__
Copyright 2K16 - 2K18 Indonesian Hacker Rulez