the forums at degreez.net

It is currently Fri Nov 17, 2017 1:03 pm

All times are UTC - 7 hours [ DST ]




Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: webseed help
PostPosted: Sun Jan 04, 2009 7:41 pm 
Offline

Joined: Sun Jan 04, 2009 7:15 pm
Posts: 3
I'm trying to create a .torrent file which enables webseeding. I've been searching the web, and almost everything I've found is over two years old. It's also "experimental" discussions, as if there is no complete method. I've found that there are two competing methods, either of which I would be fine with. (Can I enable both in the same torrent file?)

The newest info I can find is in this forum from just a few months ago:
http:/ /forums.degreez. net/viewtopic.php?t=7456

However, the BitTornado download page webseed.zip file isn't working (403 error). And I can't find any useful utilities which enable the GetRight webseeding method.

I'm trying to create multi-file torrents which are compatible with other torrent programs. I can run PHP on my http server. What should I do? Are any of these methods actually reliably working yet?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2009 9:06 pm 
Offline

Joined: Sun Mar 07, 2004 10:05 am
Posts: 1212
http://download.bittornado.com/download ... d-0.9a.zip


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2009 11:50 pm 
Offline

Joined: Sun Jan 04, 2009 7:15 pm
Posts: 3
If you're interested, I've made some changes to the code. Mainly they are clarity improvements in the form of better error checking, and speed improvements by avoiding DB access if the user isn't interested in throttling. So there are two config vars which let the user enable/disable the speed and upload limits.

I also removed a bunch of debug code, and a SQL query which failed every time by default.


Code:
diff -u webseed-0.9a/addseed.php webseed-0.9a.new/addseed.php
--- webseed-0.9a/addseed.php   2003-10-13 12:42:20.000000000 -0600
+++ webseed-0.9a.new/addseed.php   2009-01-05 17:44:57.000000000 -0700
@@ -1,5 +1,4 @@
 <?php
-
 require_once("config.php");
 
 if ($username == "USERNAME" || $password == "PASSWORD")
@@ -11,7 +10,7 @@
 
 
 ?>
-<HTML><HEAD><TITLE>Tortune device for DeHackEd and TheSHAD0W</TITLE></HEAD>
+<HTML><HEAD><TITLE>Torture device for DeHackEd and TheSHAD0W</TITLE></HEAD>
 <BODY>
 <H1>Add to Web-seed</H1>
 <FORM METHOD="POST" ENCTYPE="multipart/form-data">
@@ -30,8 +29,8 @@
 BitTorrent adds an extra directory based on what the torrent file says is
 used. This script does not. Oh, and use forward-slashes <TT>/</TT> as a
 separator.
-<P>
-</BODY></HTML>
+<P>The current working directory is:<br>
+<tt><?php echo getcwd(); ?></tt>
 <?php
 
 error_reporting(E_ALL);
@@ -58,8 +57,10 @@
 
    if (strlen($_FILES["torrent"]["tmp_name"]) > 0)
    {
-      is_uploaded_file($_FILES["torrent"]["tmp_name"]) or die("File upload error 1\n");
-      $fd = fopen($_FILES["torrent"]["tmp_name"], "rb") or die("File upload error 2\n");
+      is_uploaded_file($_FILES["torrent"]["tmp_name"])
+         or die("File upload error 1\n");
+      $fd = fopen($_FILES["torrent"]["tmp_name"], "rb")
+         or die("File upload error 2\n");
       $alltorrent = fread($fd, filesize($_FILES["torrent"]["tmp_name"]));
       fclose($fd);
    }
@@ -69,7 +70,11 @@
    
    $info_hash = sha1(BEncode($info));
    
-   mysql_query("INSERT INTO phpseed_torrents (info_hash, piecelength, numpieces) values (\"$info_hash\", ".$info["piece length"].", ". (strlen($info["pieces"]) / 20) . ")");
+   if (!mysql_query("INSERT INTO phpseed_torrents ".
+         "(info_hash, piecelength, numpieces) values ".
+         "(\"$info_hash\", ".$info["piece length"].", ".
+         (strlen($info["pieces"]) / 20) . ")"))
+      trigger_error("Failed Query ".mysql_error(), E_USER_ERROR);
 
    if (isset($info["files"])) // Multi-file
    {
@@ -93,7 +98,6 @@
          while (true)
          {
             $sub = min($info["piece length"]-$piecelen, $filesize);
-//            error_log("Sub: $sub");
             $piecelen += $sub;
             $filesize -= $sub;
             
@@ -103,38 +107,51 @@
             {
                $pieceno++;
                $piecelen = 0;
-//               error_log("Restarted piece length");
             }
             if ($piecelen > $info["piece length"])
-            {
                die("Logic error in script. Please report to the author.");
-            }
-
-
          }
+         
+         //Construct file name
          $filename = $fsbase;
          if (isset($info["files"][$fileno]["path"][1]))
          {
-            $filename .= $file["path"][0];
+            $filename .= $info["files"][$fileno]["path"][0];
             for ($i=1; isset($info["files"][$fileno]["path"][$i]); $i++)
                $filename .= "/".$info["files"][$fileno]["path"][$i];
          }
          else
             $filename .= $info["files"][$fileno]["path"][0];
+         
+         //Error checking
+         if(!file_exists($filename))
+            echo '<p>Warning: File doesn\'t exist: '.$filename.'</p>';
+         
+         if(file_exists($filename) &&
+               filesize($filename) != $info["files"][$fileno]["length"])
+            echo '<p>Warning: File sizes don\'t match: '.$filename.'</p>';
+         
+         //Insert file info
          $filename = mysql_real_escape_string($filename);         
-         mysql_query("INSERT INTO phpseed_files (info_hash,filename,startpiece,endpiece, startpieceoffset, fileorder) values (\"$info_hash\", \"$filename\", $startpiece, $pieceno, $startoffset, $fileno)");
+         if (!mysql_query("INSERT INTO phpseed_files ".
+               "(info_hash,filename,startpiece,endpiece, ".
+               "startpieceoffset, fileorder) values ".
+               "(\"$info_hash\", \"$filename\", $startpiece, ".
+               "$pieceno, $startoffset, $fileno)"))
+            trigger_error("Failed Query ".mysql_error(), E_USER_ERROR);
          $fileno++;
-      }
-      // end of nasty work
+      } // end of nasty work
    } // end of multi-file section
    else
    {
-      mysql_query("INSERT INTO phpseed_files (info_hash,filename,startpiece,endpiece, startpieceoffset, fileorder) values (\"$info_hash\", \"".mysql_real_escape_string($fsbase)."\", 0, ". (strlen($array["info"]["pieces"])/20 - 1).", 0, 0)");
+      if (!mysql_query("INSERT INTO phpseed_files ".
+            "(info_hash,filename,startpiece,endpiece, ".
+            "startpieceoffset, fileorder) values ".
+            "(\"$info_hash\", \"".mysql_real_escape_string($fsbase).
+            "\", 0, ". (strlen($array["info"]["pieces"])/20 - 1).", 0, 0)"))
+         trigger_error("Failed Query ".mysql_error(), E_USER_ERROR);
    }
-
-
 } // end of "WORK ON POST" section
 
-
-
-?>
\ No newline at end of file
+?>
+</BODY></HTML>
\ No newline at end of file
diff -u webseed-0.9a/config.php webseed-0.9a.new/config.php
--- webseed-0.9a/config.php   2003-10-13 10:27:02.000000000 -0600
+++ webseed-0.9a.new/config.php   2009-01-19 00:19:47.000000000 -0700
@@ -4,23 +4,27 @@
 // Max upload rate is in bytes, not kilobytes,
 // so 50kb/sec is 50000
 
+$GLOBALS["enable_speedlimit"] = true;
 $GLOBALS["max_upload_rate"] = 15000 ;
+
+$GLOBALS["enable_uploadlimit"] = true;
 $GLOBALS["max_uploads"] = 2 ;
 
diff -u webseed-0.9a/seed.php webseed-0.9a.new/seed.php
--- webseed-0.9a/seed.php   2003-09-05 14:55:00.000000000 -0600
+++ webseed-0.9a.new/seed.php   2009-01-19 13:57:28.000000000 -0700
@@ -1,5 +1,4 @@
 <?php
-
 /* I would like to take this opportunity to say that TheSHAD0W is a sick,
    twisted and evil person for doing this. Normally I would approve of that,
    but since I'm writing this code, I would like to make my point known.
@@ -8,7 +7,6 @@
 */
 header("Content-Type: text/plain");
 
-//error_log("One");
 if (!isset($_GET["info_hash"]) || !isset($_GET["piece"]))
    reject("400 Bad Request");
 
@@ -17,35 +15,34 @@
 else
    $info_hash=$_GET["info_hash"];
 
-$piece = $_GET["piece"];
-//error_log("Two");
+$piece = (int) $_GET["piece"];
 
 if (!is_numeric($piece) || strlen($info_hash) != 20)
    reject("400 Bad Request");
 
 $info_hash = bin2hex($info_hash);
 
-//error_log("Info hash=$info_hash, piece numnber=$piece");
-
 require_once("config.php");
 
 function Lock($hash, $time = 0)
 {
-        $results = mysql_query("SELECT GET_LOCK('$hash', $time)");
-        $string = mysql_fetch_row($results);
-        if (strcmp($string[0], "1") == 0)
-        {
-//      error_log("Got lock $hash");
-           return true;
+   $results = mysql_query("SELECT GET_LOCK('$hash', $time)");
+   if (!$results)
+   {
+      trigger_error("Failed Query");
+      reject("500 Internal Server Error");
    }
-//   error_log("Failed to lock $hash");
-        return false;
-
+   $string = mysql_fetch_row($results);
+   return (strcmp($string[0], "1") == 0);
 }
 
 function Unlock($hash)
 {
-        mysql_query("SELECT RELEASE_LOCK('$hash')");
+   if (!mysql_query("SELECT RELEASE_LOCK('$hash')"))
+   {
+      trigger_error("Failed Query");
+      reject("500 Internal Server Error");
+   }
 }
 
 function reject($error = "503 Service Temporarily Unavailable", $message="")
@@ -58,58 +55,77 @@
 mysql_connect($dbhost, $dbuser, $dbpass) or die;
 mysql_select_db($database) or die;
 
-if (!Lock("WebSeedLock", 2))
-   reject();
-
-$result = mysql_query("SELECT (UNIX_TIMESTAMP() - started) FROM phpseed_speedlimit");
-$row = mysql_fetch_row($result);
-
-// If nothing has happened for a little while, do NOT
-// let that average enable massive bursts.
-if ($row[0] > 180)
-   mysql_query("UPDATE phpseed_speedlimit SET started=UNIX_TIMESTAMP()-1, total_uploaded=total_uploaded+uploaded, uploaded=0");
+if($GLOBALS["enable_speedlimit"])
+   checkspeed();
 
-$result = mysql_query("SELECT uploaded / (UNIX_TIMESTAMP() - started) FROM phpseed_speedlimit");
-$row = mysql_fetch_row($result);
-
-if ((float)($row[0]) > $GLOBALS["max_upload_rate"])
+function checkspeed()
 {
-   $result = mysql_query("SELECT (uploaded/".$GLOBALS["max_upload_rate"]. "+started) - UNIX_TIMESTAMP() FROM phpseed_speedlimit");
+   if (!Lock("WebSeedLock", 2))
+      reject();
+
+   $result = mysql_query("SELECT (UNIX_TIMESTAMP() - started) FROM phpseed_speedlimit");
+   if (!$result)
+   {
+      trigger_error("Failed Query");
+      reject("500 Internal Server Error");
+   }
    $row = mysql_fetch_row($result);
-   reject("503 Service Temporarily Unavailable", (int)$row[0] + mt_rand(1,30));
+   
+   // If nothing has happened for a little while, do NOT
+   // let that average enable massive bursts.
+   if ($row[0] > 180)
+      mysql_query("UPDATE phpseed_speedlimit SET started=UNIX_TIMESTAMP()-1, total_uploaded=total_uploaded+uploaded, uploaded=0");
+   
+   $result = mysql_query("SELECT uploaded / (UNIX_TIMESTAMP() - started) FROM phpseed_speedlimit");
+   if (!$result)
+   {
+      trigger_error("Failed Query");
+      reject("500 Internal Server Error");
+   }
+   $row = mysql_fetch_row($result);
+   
+   if ((float)($row[0]) > $GLOBALS["max_upload_rate"])
+   {
+      $result = mysql_query("SELECT (uploaded/".
+            $GLOBALS["max_upload_rate"]. "+started) - ".
+            "UNIX_TIMESTAMP() FROM phpseed_speedlimit");
+      if (!$result)
+      {
+         trigger_error("Failed Query");
+         reject("500 Internal Server Error");
+      }
+      $row = mysql_fetch_row($result);
+      reject("503 Service Temporarily Unavailable", (int)$row[0] + mt_rand(1,30));
+   }
+
+   Unlock("WebSeedLock");
 }
 
-$result = mysql_query("SELECT seeds FROM summary WHERE info_hash=$info_hash");
-if ($result)
+if($GLOBALS["enable_uploadlimit"])
 {
-//   error_log("Doing PHPBT check");
-   $row = mysql_fetch_assoc($result);
-   if ($row["seeds"] > 10)
+   // Max uploads check
+   for ($lockno=0; $lockno < $GLOBALS["max_uploads"]; $lockno++)
+      if (Lock("WebSeed--$lockno", 0))
+         break;
+   
+   if ($lockno == $GLOBALS["max_uploads"])
       reject();
 }
 
-Unlock("WebSeedLock");
-
-// Max uploads check
-for ($lockno=0; $lockno < $GLOBALS["max_uploads"]; $lockno++)
-   if (Lock("WebSeed--$lockno", 0))
-      break;
-// error_log("Lockno=$lockno");
-if ($lockno == $GLOBALS["max_uploads"])
-   reject();
-
 
 // Get to work!
-$result = mysql_query("SELECT * FROM phpseed_torrents WHERE info_hash=\"$info_hash\"");
+$result = mysql_query('SELECT * FROM phpseed_torrents '.
+      'WHERE info_hash="'.mysql_escape_string($info_hash).'"');
 if (!$result)
+{
+   trigger_error("Failed Query");
    reject("500 Internal Server Error");
+}
 
 $config = mysql_fetch_assoc($result);
 if (!$config)
    reject("403 Forbidden");
 
-$result = mysql_query("SELECT * FROM phpseed_files WHERE info_hash=\"$info_hash\" ORDER BY fileorder");
-//print_r($config);
 if ($config["numpieces"] < $piece || $piece < 0)
    reject("400 Bad Request");
 
@@ -117,13 +133,18 @@
 // Data to return, and accounting.
 $xmit = "";
 $xmitbytes = 0;
+$result = mysql_query('SELECT * FROM phpseed_files '.
+      'WHERE info_hash="'.mysql_escape_string($info_hash).'" AND '.
+         $piece.' >= startpiece AND '.$piece.' <= endpiece '.
+      'ORDER BY fileorder');
+if (!$result)
+{
+   trigger_error("Failed Query");
+   reject("500 Internal Server Error");
+}
 
 while ($row = mysql_fetch_assoc($result))
 {
-   if (!($piece >= $row["startpiece"] && $piece <= $row["endpiece"]))
-      continue;
-
-//   $offset = $row["startpieceoffset"] + $config["piecelength"] * ($piece - $row["startpiece"]);
    $offset = ($row["startpiece"] == $piece) ? 0 : (($piece - $row["startpiece"])*$config["piecelength"] - $row["startpieceoffset"]);
    $fd = fopen($row["filename"], "rb") or reject("500 Internal Server Error");
    if (fseek($fd, $offset) != 0)
@@ -140,10 +161,6 @@
 
 
 // Header is most likely already: 200 Ok
-//mysql_query("UPDATE phpseed_speedlimit SET uploaded=uploaded+$xmitbytes");
-
-//error_log("Send length: $xmitbytes == ".strlen($xmit));
-
 if (isset($_GET["ranges"]))
 {
    $myxmit = "";
@@ -151,9 +168,9 @@
    foreach ($ranges as $blocks)
    {
       $startstop = explode("-", $blocks);
-      if (!is_numeric($startstop[0]) || !is_numeric($startstop[1]))
-         reject("400 Bad Request");
-      if (isset($startstop[2]))
+      if (!is_numeric($startstop[0]) ||
+         !is_numeric($startstop[1]) ||
+         isset($startstop[2]))
          reject("400 Bad Request");
       $start = $startstop[0];
       $stop = $startstop[1];
@@ -162,17 +179,19 @@
       $myxmit .= substr($xmit, $start, $stop-$start+1);
    }
    header("Content-Length: ".strlen($myxmit));
-   mysql_query("UPDATE phpseed_speedlimit SET uploaded=uploaded+".strlen($myxmit));
+   if($GLOBALS["enable_speedlimit"])
+      mysql_query("UPDATE phpseed_speedlimit SET uploaded=uploaded+".strlen($myxmit));
    echo $myxmit;
 }
 else
 {
-   mysql_query("UPDATE phpseed_speedlimit SET uploaded=uploaded+$xmitbytes");
+   if($GLOBALS["enable_speedlimit"])
+      mysql_query("UPDATE phpseed_speedlimit SET uploaded=uploaded+$xmitbytes");
    header("Content-Length: $xmitbytes");
    echo $xmit;
 }
 
-Unlock("WebSeed--$lockno");
+if($GLOBALS["enable_uploadlimit"])
+   Unlock("WebSeed--$lockno");
 exit;
-
 ?>
\ No newline at end of file


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 25, 2009 4:50 pm 
Offline

Joined: Sat Mar 13, 2004 11:16 am
Posts: 60
Not that I'm complaining that someone took the time and effort to make the script a little bit better, but who in their right mind would disable upload speed/slot throttling?

Code:
-$result = mysql_query("SELECT * FROM phpseed_torrents WHERE info_hash=\"$info_hash\"");
+$result = mysql_query('SELECT * FROM phpseed_torrents '.
+      'WHERE info_hash="'.mysql_escape_string($info_hash).'"');

Most people think that escaping every last string is a good idea and I'm not sure if you consider the fact that I didn't use it a security risk. I say if you don't trust the output of sha1() or bin2hex() then you have bigger problems.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 25, 2009 8:21 pm 
Offline

Joined: Sun Jan 04, 2009 7:15 pm
Posts: 3
Quote:
Not that I'm complaining that someone took the time and effort to make the script a little bit better, but who in their right mind would disable upload speed/slot throttling?

It makes perfect sense for me; I'll try to explain. I'm on a Yahoo "unlimited" shared hosting plan ($20/month). I get unlimited bandwidth and disk space. However, as you use more, the connection slows down. For example, I'm using just under 20 GB of space, thus I can upload at 5 KB/s. You're also limited to whatever CPU resources they decide to give you (I'm not expecting much of that either). I've never found a speed that they start to throttle HTTP downloads.

I have a 9 GB torrent file as part of my business. It's a set of global images. I want people to download them directly from the hosting server, or via bittorrent for convenience and as a backup in case Yahoo decides they aren't *that* unlimited, but I only have a slow DSL line feeding the bittorrent. So when I was linked from Boing Boing (http: // www. boingboing. net/ 2009/01/01/free-high-res-images. html), I got several hundred people on the torrent, but it was a week (and several friends' DSL lines) before I managed to get all the pieces into the swarm. Ideally I would have the redundancy of bittorrent combined with the bandwidth of my Yahoo account.

So I finally invested a week into figuring out how to get web seeding working correctly on this rather limited shared account. Your script is great, but it has a boat load of unneeded SQL queries if bandwidth isn't the issue. I suspect that CPU time is the issue, and SQL queries are one of the best ways to sink a script, so I removed them. Thus my patch.

If I ever find that yahoo is rate limiting me, it will be trivial to enable the web seed code (but by that point it may be redundant). If Yahoo decides to take my account down, I'll still have bittorrent as a backup.

Quote:
I say if you don't trust the output of sha1() or bin2hex() then you have bigger problems.

:) I agree, I escaped it out of reflex because I wasn't sure that I had followed the code branches all the way back. After looking at the code some more I decided to leave it in, anyway. Rather be safe than sorry, as the saying goes. Feel free to do what you wish, though it's not much of a CPU hit compared to the advantage of another layer of added security/clarity.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 7 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group