Advertisement
Guest User

Rapidshare Perl Script

a guest
May 12th, 2012
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 8.59 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. # Version 2.1.1 (13. July 2007)
  4.  
  5. # RapidShare AG OpenSource Perl Uploader V2 (with upload resume). For non-commercial use only. All rights reserved.
  6. # Included: Uploading to free, collector's and premium-zone. The MD5-check after uploads checks if the upload worked.
  7. # Upload resume can continue aborted downloads up to 24 hours after the download aborted. This means, incomplete files
  8. # will automatically be deleted from the RapidShare servers 24 hours after the first chunk arrived.
  9. # This is a PERL script written for experts and for coders wanting to know how to write own upload programs.
  10. # Tested under Linux and Linux only.
  11. # If you write your own upload-tools, please look at our rsapi.cgi calls. You need them to have fun.
  12. #
  13. # To upload a file, put this script on a machine with perl installed and use the following syntax:
  14. # perl rsapi.pl free mytestfile.rar        (this uploads mytestfile.rar as a free user)
  15. # perl rsapi.pl prem archive.rar 334 test  (this uploads archive.rar to the premium-zone of login 334 with password test)
  16. # perl rsapi.pl col a.rar testuser mypw    (this uploads a.rar to the collector's-zone of login testuser with password mypw)
  17.  
  18. use strict;
  19. use warnings;
  20. use Digest::MD5("md5_hex");
  21. use Fcntl;
  22. use IO::Socket;
  23.  
  24. my ($file, $zone, $login, $password, $uploadpath, $cursize, $size, $filecontent, $md5hex, $size2, $socket, $uploadserver, $killcode);
  25.  
  26.  
  27.  
  28. # This chapter sets some vars and parses some vars.
  29. $/ = undef;
  30. $SIG{PIPE} = 'IGNORE';
  31. $file = $ARGV[0] || die "Syntax: $0 <filename to upload> <free|prem|col> [login] [password]\n";
  32. $zone = $ARGV[1] || "";
  33. $login = $ARGV[2] || "";
  34. $password = $ARGV[3] || "";
  35. $uploadpath = "l3";
  36. $size = -s $file || die "File $file is empty or does not exist!\n";
  37.  
  38.  
  39.  
  40. # This chapter checks the file and calculates the MD5HEX of the existing local file.
  41. print "File $file has $size bytes. Calculating MD5HEX...\n";
  42. open(FH, $file) || die "Unable to open file: $!\n";
  43. $filecontent = <FH>;
  44. close(FH);
  45. $md5hex = md5_hex($filecontent);
  46. $size2 = length($filecontent);
  47. print "MD5HEX is $md5hex ($size2 bytes analyzed)\n";
  48. unless ($size == $size2) { die "Strange error: $size bytes found, but only $size2 bytes analyzed?\n" }
  49.  
  50.  
  51.  
  52. # This chapter finds out which upload server is free for uploading our file by fetching http://rapidshare.com/cgi-bin/rsapi.cgi?sub=nextuploadserver_v1
  53. if ($login and $password) { print "Trying to upload to your $zone account.\n" } else { print "Uploading as a free user.\n" }
  54. print "Getting upload server infos.\n";
  55. $socket = IO::Socket::INET->new(PeerAddr => "rapidshare.com:80") || die "Unable to open port: $!\n";
  56. print $socket qq|GET /cgi-bin/rsapi.cgi?sub=nextuploadserver_v1 HTTP/1.0\r\n\r\n|;
  57. ($uploadserver) = <$socket> =~ /\r\n\r\n(\d+)/;
  58. unless ($uploadserver) { die "Uploadserver invalid? Internal error!\n" }
  59. print "Uploading to rs$uploadserver$uploadpath.rapidshare.com\n";
  60.  
  61. $cursize = 0;
  62. while ($cursize < $size) { $cursize = &uploadchunk($file, $md5hex, $size, $cursize, "rs$uploadserver$uploadpath.rapidshare.com:80") }
  63.  
  64.  
  65.  
  66. sub uploadchunk {
  67.   my $file = shift || die;
  68.   my $md5hex = shift || die;
  69.   my $size = shift || die;
  70.   my $cursize = shift || 0;
  71.   my $fulluploadserver = shift || die;
  72.  
  73.   my ($uploaddata, $wantchunksize, $fh, $socket, $boundary, $contentheader, $contenttail, $contentlength, $header, $chunks, $chunksize,
  74. $bufferlen, $buffer, $result, $fileid, $complete, $resumed, $filename);
  75.  
  76.   if (-e "$file.uploaddata") {
  77.     print "Found .uploaddata! Overriding settings and trying to resume.\n";
  78.     open(I, "$file.uploaddata") or die "Unable to open file: $!\n";
  79.     ($fulluploadserver, $fileid, $killcode) = split(/\n/, <I>);
  80.     print "Uploadserver=$fulluploadserver\nFile-ID=$fileid\nKillcode=$killcode\n";
  81.     close(I);
  82.     print "Checking if RS gives an OK and the position...\n";
  83.     $socket = IO::Socket::INET->new(PeerAddr => "rapidshare.com:80") || die "Unable to open port: $!\n";
  84.     print $socket qq|GET /cgi-bin/rsapi.cgi?sub=checkincomplete_v1&fileid=$fileid&killcode=$killcode HTTP/1.0\r\n\r\n|;
  85.     $result = <$socket>;
  86.     unless ($result =~ /\r\n\r\n(\d+)/) { die "I can't resume the file. Please delete $file.uploaddata. RS said:\n$result\n" }
  87.     $cursize = $1;
  88.     print "All ok. The upload stopped at $cursize. Trying to resume.\n";
  89.     $resumed = 1;
  90.   }
  91.  
  92.   $wantchunksize = 1024000;
  93.  
  94.   if ($size > $wantchunksize) {
  95.     $chunks = 1;
  96.     $chunksize = $size - $cursize;
  97.     if ($chunksize > $wantchunksize) { $chunksize = $wantchunksize } else { $complete = 1 }
  98.   } else {
  99.     $chunks = 0;
  100.     $chunksize = $size;
  101.   }
  102.  
  103.   print "Upload chunk is $chunksize bytes starting at $cursize.\n";
  104.  
  105.   sysopen($fh, $file, O_RDONLY) || die "Unable to open file: $!\n";
  106.   $filename = $file =~ /[\/\\]([^\/\\]+)$/ ? $1 : $file;
  107.   $socket = IO::Socket::INET->new(PeerAddr => $fulluploadserver) || die "Unable to open socket: $!\n";
  108.   $boundary = "---------------------632865735RS4EVER5675865";
  109.   $contentheader .= qq|$boundary\r\nContent-Disposition: form-data; name="rsapi_v1"\r\n\r\n1\r\n|;
  110.  
  111.   if ($resumed) {
  112.     $contentheader .= qq|$boundary\r\nContent-Disposition: form-data; name="fileid"\r\n\r\n$fileid\r\n|;
  113.     $contentheader .= qq|$boundary\r\nContent-Disposition: form-data; name="killcode"\r\n\r\n$killcode\r\n|;
  114.     if ($complete) { $contentheader .= qq|$boundary\r\nContent-Disposition: form-data; name="complete"\r\n\r\n1\r\n| }
  115.   } else {
  116.     if ($zone eq "prem" and $login and $password) {
  117.       $contentheader .= qq|$boundary\r\nContent-Disposition: form-data; name="login"\r\n\r\n$login\r\n|;
  118.       $contentheader .= qq|$boundary\r\nContent-Disposition: form-data; name="password"\r\n\r\n$password\r\n|;
  119.     }
  120.  
  121.     if ($zone eq "col" and $login and $password) {
  122.       $contentheader .= qq|$boundary\r\nContent-Disposition: form-data; name="freeaccountid"\r\n\r\n$login\r\n|;
  123.       $contentheader .= qq|$boundary\r\nContent-Disposition: form-data; name="password"\r\n\r\n$password\r\n|;
  124.     }
  125.  
  126.     if ($chunks) { $contentheader .= qq|$boundary\r\nContent-Disposition: form-data; name="incomplete"\r\n\r\n1\r\n| }
  127.   }
  128.  
  129.   $contentheader .= qq|$boundary\r\nContent-Disposition: form-data; name="filecontent"; filename="$filename"\r\n\r\n|;
  130.   $contenttail = "\r\n$boundary--\r\n";
  131.   $contentlength = length($contentheader) + $chunksize + length($contenttail);
  132.  
  133.   if ($resumed) {
  134.     $header = qq|POST /cgi-bin/uploadresume.cgi HTTP/1.0\r\nContent-Type: multipart/form-data; boundary=$boundary\r\nContent-Length: $contentlength\r\n\r\n|;
  135.   } else {
  136.     $header = qq|POST /cgi-bin/upload.cgi HTTP/1.0\r\nContent-Type: multipart/form-data; boundary=$boundary\r\nContent-Length: $contentlength\r\n\r\n|;
  137.   }
  138.  
  139.   print $socket "$header$contentheader";
  140.  
  141.   sysseek($fh, $cursize, 0);
  142.   $bufferlen = sysread($fh, $buffer, $wantchunksize) || 0;
  143.   unless ($bufferlen) { die "Error while reading file: $!\n" }
  144.   print "Sending $bufferlen bytes.\n";
  145.   $cursize += $bufferlen;
  146.   print $socket $buffer;
  147.   print $socket $contenttail;
  148.   print "Server response:\n";
  149.   ($result) = <$socket> =~ /\r\n\r\n(.+)/s;
  150.   unless ($result) { die "Ooops! Did not receive any valid server results?\n" }
  151.   print $result . "\n";
  152.  
  153.   if ($resumed) {
  154.     if ($complete) {
  155.       if ($result =~ /^COMPLETE,(\w+)/) {
  156.         print "Upload completed! MD5HEX=$1 Checking MD5...\n";
  157.         if ($md5hex ne $1) { die "MD5-CHECK NOT PASSED! LOCAL=$md5hex REMOTE=$1\n" }
  158.         print "MD5-check passed. Upload OK! Saving status to rsapiuploads.txt\n";
  159.         open(O,">>rsapiuploads.txt") or die "Unable to save to rsapiuploads.txt: $!\n";
  160.         print O "Upload OK!\n\n";
  161.         close(O);
  162.         unlink("$file.uploaddata");
  163.       } else {
  164.         die "Unexpected server response!\n";
  165.       }
  166.     } else {
  167.       if ($result =~ /^CHUNK,(\d+)/) {
  168.         print "Chunk upload completed! Uploaded=$1\n";
  169.       } else {
  170.         die "Unexpected server response!\n";
  171.       }
  172.     }
  173.   } else {
  174.     if ($result =~ /files\/(\d+)/) { $fileid = $1 } else { die "Server result did not contain a file ID.\n" }
  175.     unless ($result =~ /File1\.3=(\d+)/ and $1 == $cursize) { die "Server did not save all data we sent.\n" }
  176.     unless ($result =~ /File1\.2=.+?killcode=(\d+)/) { die "Server did not send our killcode.\n" }
  177.  
  178.     $killcode = $1;
  179.  
  180.     open(O,">>rsapiuploads.txt") or die "Unable to save to rsapiuploads.txt: $!\n";
  181.     print O "Uploading $file. Download-links:\n$result";
  182.     close(O);
  183.  
  184.     if ($chunks) {
  185.       open(O, ">$file.uploaddata") or die "Unable to save upload server: $!\n";
  186.       print O "$fulluploadserver\n$fileid\n$killcode\n";
  187.       close(O);
  188.     }
  189.   }
  190.  
  191.   return $cursize;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement