CHips L MINI SHELL

CHips L pro

Current Path : /proc/2/root/usr/local/rvglobalsoft/rvmanager/auto/RVSInstaller/
Upload File :
Current File : //proc/2/root/usr/local/rvglobalsoft/rvmanager/auto/RVSInstaller/RVSDownload.pm

#!/usr/bin/perl
##LICENSE##

package RVSInstaller::RVSDownload;
use strict;
use Carp;
use Socket;
use File::Basename;
use vars qw ($startTime $UICONF);

$| = 1;

sub new {
    my ($class, %args) = @_;
    
    my $self = {%args};
    
    croak "__PACKAGE__ Constructor require 'programName' parameter to create object" if (!defined($self->{'programName'}));
    
    bless $self, $class;
    
    return $self;
}

sub Register {
    my $self = shift;
    my ($key, $value) = @_;
    if ( $key ne '' && $key ) {
       $self->{'OPTS'}->{$key} = $value;
    }
}

sub Connection{
    my $self = shift;
    my (%result, $setTimeout);
    $self->{ScreenControl} = RVSInstaller::ScreenControl->new(programName => $self->{'programName'});
    
    #if (defined $self->{'OPTS'}->{'OBJUI'}) {
    #    eval { require Curses::UI;};
    #    if (!$@) {
    #       $self->{'OPTS'}->{'OBJUI'} = Curses::UI->new( -clear_on_exit => 0, -debug =>1, -color_support => 1, -mouse_support => 0);
    #    }
    #}

    undef $self->{'OPTS'}->{'OBJUI'};
    $setTimeout = 15;
    if ($self->{'OPTS'}->{'SSL'} eq '' || $self->{'OPTS'}->{'URL'}=~/^https:\/\//i) {
        $self->{'OPTS'}->{'SSL'} = 0;
    }
    my ($url);
    $url = $self->{'OPTS'}->{'URL'};
    $url =~s/http:\/\///gi;
    $url =~s/https:\/\///gi;
    ($self->{'OPTS'}->{'ADDRESS'}, $self->{'OPTS'}->{'REQUEST'}) = split(/\//, $url,2);
    $self->{'OPTS'}->{'REQUEST'} = '/' . $self->{'OPTS'}->{'REQUEST'};

    ($self->{'OPTS'}->{'SITE'}, $self->{'OPTS'}->{'PORT'}) = split(/:/, $self->{'OPTS'}->{'ADDRESS'},2);
    
    if ( !$self->{'OPTS'}->{'PORT'} || $self->{'OPTS'}->{'PORT'} eq '') {
        if ( $self->{'OPTS'}->{'SSL'} || $self->{'OPTS'}->{'SSL'} eq '1') {
            $self->{'OPTS'}->{'PORT'} = 443;
        }
        else {
            $self->{'OPTS'}->{'PORT'} = 80;
        }
    }
    
    if ( $self->{'OPTS'}->{'SSL'} || $self->{'OPTS'}->{'SSL'} eq '1') {
        goto NONSSL;
# <-- This code SSL Download None Completed. -->
#      eval { 
#          use Net::SSLeay qw(die_now die_if_ssl_error);
#          Net::SSLeay::load_error_strings();
#          Net::SSLeay::SSLeay_add_ssl_algorithms();
#           Net::SSLeay::randomize();
#      };
#      if ($@) {
#          print "Net::SSLeay is not compiled.\n";
#         goto NONSSL;
#      }
#
#      $result{'Connect Type'} = 'SSL';
#      my ($page, @result, %headers);
#      
#        $self->{'OPTS'}->{'PORT'} = getservbyname ($self->{'OPTS'}->{'PORT'}, 'tcp') unless $self->{'OPTS'}->{'PORT'} =~ /^\d+$/;
#
#        my $dest_ip = gethostbyname ($self->{'OPTS'}->{'SITE'});
#        my $dest_serv_params  = sockaddr_in($self->{'OPTS'}->{'PORT'}, $dest_ip);
#
#        socket  (S, &AF_INET, &SOCK_STREAM, 0) or goto NONSSL;
#        connect (S, $dest_serv_params)  or goto NONSSL;
#        select (S); $| = 1; select (STDOUT);   
#        
#        my $ctx = Net::SSLeay::CTX_new() or goto NONSSL;
#        Net::SSLeay::CTX_set_options($ctx, &Net::SSLeay::OP_ALL);
#        my $ssl = Net::SSLeay::new($ctx) or goto NONSSL;
#        Net::SSLeay::set_fd($ssl, fileno(S));
#        my $res = Net::SSLeay::connect($ssl);
#    
#        $res = Net::SSLeay::write($ssl, "GET " . $self->{'OPTS'}->{'REQUEST'} . " HTTP/1.0\n\n");  
#        CORE::shutdown S, 1;
#        my ($got, $inheaders, $lastTime, $tmpSave, $lastSize, $beforTime, $lastTime, $ulastSize) ;
#        $inheaders = 1;
#
#        my ($reply);
#        while ($inheaders || $lastSize != $self->{'OPTS'}->{'totalSize'} ) {
#            
#
#            if ( $inheaders) {
#                $got = Net::SSLeay::read($ssl, 16384) or die "Unexpected EOF 1"; 
#                $lastTime = time();
#                $startTime = $lastTime;
#                
#               $inheaders = 0;
#               my ( @Header ) = split(/\n/, $got);
#               my (%HeaderData) =%{$self->SplitHeader(\@Header)};
#               if ($HeaderData{'HTTP'} ne '200 OK') {
#                    Net::SSLeay::free ($ssl);               
#                    Net::SSLeay::CTX_free ($ctx);
#                    close S;
#                   goto NONSSL;
#               }
#               $self->DisplayHeader(\%HeaderData);
#            }
#            else {
#                eval{
#                local $SIG{ALRM} = sub { die 'SSL read data has timed Out' ."\n"; };
#               alarm $setTimeout;
#                $got = Net::SSLeay::ssl_read_until($ssl, $reply, 230000) or die "Unexpected EOF 1"; 
#                $reply .= $got;
#                alarm 0; 
#                };
#                if (length($got) eq 0 ) {
#                    my $timeout = time() - $lastTime;
#                    if ($timeout > $setTimeout) {
#                        print "Time out\n";
#                        exit;
#                    }
#                }
#                else {
#                    if ( !$tmpSave ) {
#                        $lastTime = time();
#                        my ( $beforSize ) = 0;
#                        $tmpSave =1;
#                        $self->printLog("0%" . $self->printTab('0',30) . "[" . $self->printTab('0',length($self->{'OPTS'}->{'totalSize'})) . "0/" . $self->{'OPTS'}->{'totalSize'} . "]\n");
#                        open(SAVE, ">". $self->{'OPTS'}->{'saveas'});
#                        print SAVE $got;
#                        $beforTime = $lastTime;
#                        $lastTime = time();
#                        $lastSize = length($got);
#                        $self->DisplayPercent($beforSize, $lastSize, $self->{'OPTS'}->{'totalSize'},$beforTime, $lastTime );
#                    }
#                    else {
#                        
#                        print SAVE $got;
#                        $beforTime = $lastTime;
#                        $lastTime = time();
#                        my ($beforSize) = $lastSize;
#                        $lastSize += length($got);
#                        $self->DisplayPercent($beforSize, $lastSize, $self->{'OPTS'}->{'totalSize'}, $beforTime, $lastTime);
#                    }
#                }
#            }
#        }
#        close(SAVE);
#        Net::SSLeay::free($ssl);               
#        Net::SSLeay::CTX_free($ctx);
#        close S;
    } else {
NONSSL :
        # <-- NONE SSL Download (Socket) -->
        my ($urlDisplay);
        $urlDisplay = $self->{'OPTS'}->{'URL'};
        if ($urlDisplay =~/(.*?)download\.rvglobalsoft\.com(.*?)/) {
            $urlDisplay = $1 . 'download.rvglobalsoft.com';
        }
        #$self->printLog("-- " . gmtime() . " -- " . $urlDisplay ."\n");
        $self->printLog("Resolving " . $self->{'OPTS'}->{'SITE'} . "... ");
        my $ipaddr = inet_aton($self->{'OPTS'}->{'SITE'}) || $self->is_error("Unable to resolve " . $self->{'OPTS'}->{'SITE'} . "\n");
        #my $iaddr = gethostbyname($self->{'OPTS'}->{'SITE'}) || $self->is_error("Unable to resolve " . $self->{'OPTS'}->{'SITE'} . "\n");
        my ($ip1, $ip2, $ip3, $ip4) = unpack("C4", $ipaddr);
        my $siteIP = $ip1 . '.' . $ip2 . '.' . $ip3 . '.' . $ip4;
        $self->printLog($siteIP . "\n");
        
        my $proto = getprotobyname('tcp');
        my $port = getservbyname( 'http', 'tcp' );
        $self->printLog('Connecting to ' . $self->{'OPTS'}->{'SITE'} . '[' . $siteIP . ']:' . $port . '... ');
        
        socket(SOCK, AF_INET, SOCK_STREAM, $proto) || $self->is_error("socket error" . ": $!");
        
        my $sin = sockaddr_in($port, $ipaddr) || $self->is_error("sockaddr_in error" . ": $!");
        connect(SOCK, $sin) || $self->is_error("socket connect error" . ": $!");
        $self->printLog('connected.' . "\n");
        
        
        if ( !$self->{'OPTS'}->{'postdata'} || $self->{'OPTS'}->{'postdata'} eq '') {
           send SOCK, "GET " . $self->{'OPTS'}->{'REQUEST'} . " HTTP/1.0\r\nHost: " . $self->{'OPTS'}->{'SITE'} . "\r\n\r\n", 0;
        }
        else {
            send SOCK, "POST " . $self->{'OPTS'}->{'REQUEST'} . " HTTP/1.0\r\n", 0;
            send SOCK, "Host: " . $self->{'OPTS'}->{'SITE'} . "\r\n", 0;
            send SOCK, "Content-Type: application/x-www-form-urlencoded\r\n", 0;
            send SOCK, "Content-Length: " . length($self->{'OPTS'}->{'postdata'}) . "\r\n", 0;
            send SOCK, "Connection: Keep-Alive\r\n", 0;
            send SOCK, "Cache-Control: no-cache\r\n", 0;
            send SOCK, "\r\n", 0;
            send SOCK, $self->{'OPTS'}->{'postdata'}, 0;
        }
        
        my ( $inheaders, $lastTime, $tmpSave, $lastSize, $beforTime,  $ulastSize) ;
        $inheaders = 1;
        my (@header);
        open(SAVE, ">". $self->{'OPTS'}->{'saveas'});
        while  (my $got = <SOCK>) {
            if ($inheaders) {
                if ($got =~/^location/gi) {
                    my ($hKey, $urlRedirect) = split(/:/, $got, 2);
                    $self->{'OPTS'}->{'URL'} = $urlRedirect;
                    $self->Connection();
                    exit;
                }
                push(@header, $got);
                $got =~s/\n|\s|\r//g;
            }
            else {
                if ( !$tmpSave ) {
                    $tmpSave =1;
                
                    my ( $beforSize ) = 0;
                    $lastSize = length($got);
                    
                    $beforTime = $lastTime;
                    $self->printLog("Downloading has been completed 0%" . $self->printTab('0',30) . "[" . $self->printTab('0',length($self->{'OPTS'}->{'totalSize'})) . "0/" . $self->{'OPTS'}->{'totalSize'} . "]\n");
                    print $self->{'OPTS'}->{'totalSize'} . ' bytes.' . "\n0|" if ( !defined $ENV{'HTTP_USER_AGENT'} && $self->{'OPTS'}->{'Showdot'});
                    print SAVE $got;
                    $lastSize = length($got);
                    ($lastTime, $ulastSize) = $self->DisplayPercent($beforSize, $lastSize, 0,$self->{'OPTS'}->{'totalSize'}, $beforTime);
                }
                else {
                    print SAVE $got;
                    $beforTime = $lastTime;
                    $lastTime = time();

                    my ($beforSize) = $lastSize;
                    $lastSize += length($got);
                  
                    ($lastTime, $ulastSize) = $self->DisplayPercent($beforSize, $lastSize, $ulastSize,$self->{'OPTS'}->{'totalSize'}, $beforTime);
                }
            }
          
            if ( $got =~/^$/ and $inheaders) {
                my (%HeaderData) =%{$self->SplitHeader(\@header)};
                $self->DisplayHeader(\%HeaderData);
                if ($HeaderData{'HTTP'} ne '200 OK') {
                    $self->printLog("Exit with http code: ".$HeaderData{'HTTP'} . "\n");
                    exit;   
                }
                
                $lastTime = time();
                $startTime = $lastTime;
                $inheaders = 0;   
            }
        }
        close(SAVE);
        close(SOCK);
        chmod(0644, $self->{'OPTS'}->{'saveas'});
        print "=>|100 " if ( !defined $ENV{'HTTP_USER_AGENT'} && $self->{'OPTS'}->{'Showdot'});
        $self->printLog("\n");
        if (defined $self->{'OPTS'}->{'OBJUI'}) {
            $self->{'OPTS'}->{'OBJUI'}->noprogress;
        }
        if ( $lastSize eq $self->{'OPTS'}->{'totalSize'}) {
            $self->printLog('saved [' . $lastSize . '/' . $self->{'OPTS'}->{'totalSize'} . '] has been completed.' . "\n");
        }
        else {
            $self->printLog('saved [' . $lastSize . '/' . $self->{'OPTS'}->{'totalSize'} . '] has been  failed.' . "\n");
            
            if ( !defined $ENV{'HTTP_USER_AGENT'}) {
                $self->printLog(sprintf("
\e[1m\e[31m------------------------------------------\e[0m\n
Download speed is too low and timeout. Please download manually using this command:\n
wget %s -O %s \n\n
After successfully download it, run this command to continue installation:\n
perl /usr/local/rvglobalsoft/%s/auto%s.cgi\n
\e[1m\e[31m------------------------------------------\e[0m\n
", $self->{'OPTS'}->{'URL'}, $self->{'OPTS'}->{'saveas'}, $self->{'programName'}, $self->{'programName'}));
            } else {
                $self->{'OPTS'}->{'format'} = "$self->{ScreenControl}->DisplayBoxMsg(1,'', 'Downloading...', '\$1'); $self->{ScreenControl}->LogMsg('\$1' \. \"\\n\");$self->{ScreenControl}->DisplayMsg('\$1', '1', 'subMsg' . \$ACTION);";
                
                $self->printLog(sprintf("
------------------------------------------\n
Download speed is too low and timeout. Please download manually using this command:\n
wget %s -O %s \n\n
After successfully download it, run this command to continue installation:\n
perl /usr/local/rvglobalsoft/%s/auto%s.cgi\n
------------------------------------------\n
", $self->{'OPTS'}->{'URL'}, $self->{'OPTS'}->{'saveas'}, $self->{'programName'}, $self->{'programName'}));
            
            }
        }
    }
    
#    undef $self->{ScreenControl};
}

sub SplitHeader{
    my $self = shift;
    my ($allPageHeaders) = @_;
    my (@pageHeaders) = @{$allPageHeaders};
    my (%HeaderData);
    foreach (@pageHeaders) {
        if ( /^HTTP\/([\d\.]+)\s(.*)/ ) {
            $HeaderData{'HTTP'} = $2;
            $HeaderData{'HTTP'} =~s/^\s//gi;
            $HeaderData{'HTTP'} =~s/\s$//gi;
        }
        else {
            my ($key, $val) = split(/:/, $_, 2);
            $key =~s/\s$//gi;
            $key =~s/^\s//gi;
            $val =~s/\s$//gi;
            $val =~s/^\s//gi;
            if ( $key) {
                $HeaderData{lc($key)} = $val;
            }
        }
    }
    return \%HeaderData;
}

sub DisplayHeader{
    my $self = shift;
    my ($HeaderDataS) = $_[0];
    my (%HeaderData) = %{$HeaderDataS};

    if ($self->{'OPTS'}->{'saveas'} eq '') {
        my @strem = split(/;/, $HeaderData{'content-disposition'});
        my (%disposition);
        foreach ( @strem ) {
            my ($key, $val) = split(/=/, $_, 2);
            $val =~s/^\s//gi;
            $val =~s/\s$//gi;
            $val =~s/"|'|`//gi;
            $key =~s/^\s//gi;
            $key =~s/\s$//gi;
            $disposition{$key} = $val;
        }
        if ( $self->{'OPTS'}->{'saveas'} eq '') {
            if ( $disposition{'filename'} ) {
                $self->{'OPTS'}->{'saveas'} = $disposition{'filename'};
            }
            else {
                if ( $self->{'OPTS'}->{'REQUEST'}=~/(.*)\/(.*?)$/i) {
                    $self->{'OPTS'}->{'saveas'} = $2;
                }
                else {
                    $self->{'OPTS'}->{'saveas'} = $self->{'OPTS'}->{'REQUEST'};
                }
            }
        }
    }

    $self->printLog("HTTP request sent, awaiting response... " . $HeaderData{'HTTP'} . "\n");
    $self->printLog("Length: " . $HeaderData{'content-length'}  . " [" . $HeaderData{'content-type'} . "]\n");
    $self->{'OPTS'}->{'totalSize'} = $HeaderData{'content-length'};
    
    if (defined $self->{'OPTS'}->{'OBJUI'}) {
       $self->{'OPTS'}->{'OBJUI'}->progress(
            -max       => $self->{'OPTS'}->{'totalSize'},
            -message   =>"Downloading " . basename($self->{'OPTS'}->{'saveas'}) . " ... 0/" . $self->{'OPTS'}->{'totalSize'}, );
        $self->{'OPTS'}->{'OBJUI'}->setprogress(0, "Downloading " . basename($self->{'OPTS'}->{'saveas'}) . " ... 0/" . $self->{'OPTS'}->{'totalSize'},);
    }
}

sub DisplayPercent{
    my $self = shift;
    my ($beforSize, $lastSize, $ulastSize, $totalSize, $beforTime) = @_;
    my $beforPercent = 0;
    my $lastPercent = 0;
    if ($totalSize > 0) {
    	$beforPercent = ($beforSize * 100) / $totalSize;
        $lastPercent = ($lastSize *100) / $totalSize;
    } 
    
    if ( int($lastPercent) > int($beforPercent) ) {
        my ($nowTime) = time();
        my ($uTime) = $nowTime - $beforTime;
        my ($decSize) = $lastSize - $ulastSize;
        if (defined $self->{'OPTS'}->{'OBJUI'}) {
            $self->{'OPTS'}->{'OBJUI'}->setprogress($lastSize, "Downloading " . basename($self->{'OPTS'}->{'saveas'}) . " ... $lastSize/" . $totalSize);
        }
        print "=" if ( !defined $ENV{'HTTP_USER_AGENT'} && $self->{'OPTS'}->{'Showdot'});
        $self->printLog('Downloading has been completed ' . int($lastPercent) . "%" . $self->printTab(int($lastPercent),30) . "[" . $self->printTab($lastSize, length($totalSize)) . "$lastSize/$totalSize]\n");
        return $nowTime, $lastSize;
    } else {
        $self->printLog('Downloading has been completed ' . int($lastPercent) . "%" . $self->printTab(int($lastPercent),30) . "[" . $self->printTab($lastSize, length($totalSize)) . "$lastSize/$totalSize]\n", 1);
        if ( $self->{'OPTS'}->{'logfile'} ) {
            $self->printLog('Downloading has been completed ' . int($lastPercent) . "%" . $self->printTab(int($lastPercent),30) . "[" . $self->printTab($lastSize, length($totalSize)) . "$lastSize/$totalSize]\n");
        }
        return $beforTime, $ulastSize;
    }
}

sub printLog{
    my $self = shift;
    my ($msg, $skipPrint) = @_;
    if ( $self->{'OPTS'}->{'logfile'} ) {
        open(LOG, ">>" . $self->{'OPTS'}->{'logfile'});
        print LOG $msg;
        close(LOG);
    } else {
        if (defined $self->{'OPTS'}->{'OBJUI'}) {
            
        } else {
            if ( $self->{'OPTS'}->{'format'} ne '' && $self->{'OPTS'}->{'format'}) {
                my ($format) = $self->{'OPTS'}->{'format'};
                $format =~s/\$1/$msg/gi;
                $msg = $format;
            }
            if ( $self->{'OPTS'}->{'evalOutput'} eq 1) {
                if (!$skipPrint) {
                    $msg =~s/\r|\n|\t//gi;
                    eval($msg);
                }
            } else {
                print $msg;
            }
        }
    }
}

sub is_error{
    my $self = shift;
    my ($msg) = @_;
    $self->printLog($msg ."\n");
    exit;   
}

sub printTab{
    my $self = shift;
    my ($msg, $max) = @_;
    my $w = $max - length($msg);
    my ($i, $result);
    if (defined $self->{'OPTS'}->{'OBJUI'}) {
        return '    ';
    } else {
        for ( $i = 0; $i < $w; $i++) {
            $result .= " ";   
        }
    }
    return $result;
}
1;

Copyright 2K16 - 2K18 Indonesian Hacker Rulez