| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,179 @@ |
| 1 |
+<?php |
|
| 2 |
+//Used for HTTP seeding |
|
| 3 |
+//Requires information in torrent file for client to use |
|
| 4 |
+ |
|
| 5 |
+header("Content-Type: text/plain");
|
|
| 6 |
+ |
|
| 7 |
+//error_log("One");
|
|
| 8 |
+if (!isset($_GET["info_hash"]) || !isset($_GET["piece"])) |
|
| 9 |
+ reject("400 Bad Request");
|
|
| 10 |
+ |
|
| 11 |
+if (get_magic_quotes_gpc()) |
|
| 12 |
+ $info_hash=stripslashes($_GET["info_hash"]); |
|
| 13 |
+else |
|
| 14 |
+ $info_hash=$_GET["info_hash"]; |
|
| 15 |
+ |
|
| 16 |
+$piece = $_GET["piece"]; |
|
| 17 |
+//error_log("Two");
|
|
| 18 |
+ |
|
| 19 |
+if (!is_numeric($piece) || strlen($info_hash) != 20) |
|
| 20 |
+ reject("400 Bad Request");
|
|
| 21 |
+ |
|
| 22 |
+$info_hash = bin2hex($info_hash); |
|
| 23 |
+ |
|
| 24 |
+//error_log("Info hash=$info_hash, piece numnber=$piece");
|
|
| 25 |
+ |
|
| 26 |
+require_once("config.php");
|
|
| 27 |
+ |
|
| 28 |
+//change from KB to bytes |
|
| 29 |
+$max_upload_rate = $GLOBALS["max_upload_rate"] * 1024; |
|
| 30 |
+ |
|
| 31 |
+function Lock($hash, $time = 0) |
|
| 32 |
+{
|
|
| 33 |
+ $results = mysql_query("SELECT GET_LOCK('$hash', $time)");
|
|
| 34 |
+ $string = mysql_fetch_row($results); |
|
| 35 |
+ if (strcmp($string[0], "1") == 0) |
|
| 36 |
+ {
|
|
| 37 |
+ //error_log("Got lock $hash");
|
|
| 38 |
+ return true; |
|
| 39 |
+ } |
|
| 40 |
+ //error_log("Failed to lock $hash");
|
|
| 41 |
+ return false; |
|
| 42 |
+} |
|
| 43 |
+ |
|
| 44 |
+function Unlock($hash) |
|
| 45 |
+{
|
|
| 46 |
+ mysql_query("SELECT RELEASE_LOCK('$hash')");
|
|
| 47 |
+} |
|
| 48 |
+ |
|
| 49 |
+function reject($error = "503 Service Temporarily Unavailable", $message="") |
|
| 50 |
+{
|
|
| 51 |
+ header("HTTP/1.0 $error");
|
|
| 52 |
+ echo $message; |
|
| 53 |
+ die; |
|
| 54 |
+} |
|
| 55 |
+ |
|
| 56 |
+mysql_connect($dbhost, $dbuser, $dbpass) or die; |
|
| 57 |
+mysql_select_db($database) or die; |
|
| 58 |
+ |
|
| 59 |
+if (!Lock("WebSeedLock", 2))
|
|
| 60 |
+ reject(); |
|
| 61 |
+ |
|
| 62 |
+$result = mysql_query("SELECT (UNIX_TIMESTAMP() - started) FROM ".$prefix."speedlimit");
|
|
| 63 |
+$row = mysql_fetch_row($result); |
|
| 64 |
+ |
|
| 65 |
+// If nothing has happened for a little while, do NOT |
|
| 66 |
+// let that average enable massive bursts. |
|
| 67 |
+if ($row[0] > 180) |
|
| 68 |
+ mysql_query("UPDATE ".$prefix."speedlimit SET started=UNIX_TIMESTAMP()-1, total_uploaded=total_uploaded+uploaded, uploaded=0");
|
|
| 69 |
+ |
|
| 70 |
+$result = mysql_query("SELECT uploaded / (UNIX_TIMESTAMP() - started) FROM ".$prefix."speedlimit");
|
|
| 71 |
+$row = mysql_fetch_row($result); |
|
| 72 |
+ |
|
| 73 |
+if ((float)($row[0]) > $max_upload_rate) |
|
| 74 |
+{
|
|
| 75 |
+ $result = mysql_query("SELECT (uploaded/". $max_upload_rate . "+started) - UNIX_TIMESTAMP() FROM ".$prefix."speedlimit");
|
|
| 76 |
+ $row = mysql_fetch_row($result); |
|
| 77 |
+ reject("503 Service Temporarily Unavailable", (int)$row[0] + mt_rand(1,30));
|
|
| 78 |
+} |
|
| 79 |
+ |
|
| 80 |
+$result = mysql_query("SELECT seeds FROM ".$prefix."summary WHERE info_hash=$info_hash");
|
|
| 81 |
+if ($result) |
|
| 82 |
+{
|
|
| 83 |
+ //error_log("Doing PHPBT check");
|
|
| 84 |
+ $row = mysql_fetch_assoc($result); |
|
| 85 |
+ if ($row["seeds"] > 5) //if there are seeds available, don't use HTTP seeding |
|
| 86 |
+ reject(); |
|
| 87 |
+} |
|
| 88 |
+if (mysql_num_rows($result) == 0) //hash isn't even in database! |
|
| 89 |
+{
|
|
| 90 |
+ //reject em! |
|
| 91 |
+ reject(); |
|
| 92 |
+} |
|
| 93 |
+ |
|
| 94 |
+Unlock("WebSeedLock");
|
|
| 95 |
+ |
|
| 96 |
+// Max uploads check |
|
| 97 |
+for ($lockno=0; $lockno < $GLOBALS["max_uploads"]; $lockno++) |
|
| 98 |
+ if (Lock("WebSeed--$lockno", 0))
|
|
| 99 |
+ break; |
|
| 100 |
+//error_log("Lockno=$lockno");
|
|
| 101 |
+if ($lockno == $GLOBALS["max_uploads"]) |
|
| 102 |
+ reject(); |
|
| 103 |
+ |
|
| 104 |
+ |
|
| 105 |
+// Get to work! |
|
| 106 |
+$result = mysql_query("SELECT ".$prefix."summary.piecelength, ".$prefix."summary.numpieces FROM ".$prefix."summary WHERE info_hash=\"$info_hash\"");
|
|
| 107 |
+if (!$result) |
|
| 108 |
+ reject("500 Internal Server Error");
|
|
| 109 |
+ |
|
| 110 |
+$config = mysql_fetch_assoc($result); |
|
| 111 |
+if (!$config) |
|
| 112 |
+ reject("403 Forbidden");
|
|
| 113 |
+ |
|
| 114 |
+$result = mysql_query("SELECT * FROM ".$prefix."webseedfiles WHERE info_hash=\"$info_hash\" ORDER BY fileorder");
|
|
| 115 |
+ |
|
| 116 |
+if ($config["numpieces"] < $piece || $piece < 0) |
|
| 117 |
+ reject("400 Bad Request");
|
|
| 118 |
+ |
|
| 119 |
+ |
|
| 120 |
+// Data to return, and accounting. |
|
| 121 |
+$xmit = ""; |
|
| 122 |
+$xmitbytes = 0; |
|
| 123 |
+ |
|
| 124 |
+while ($row = mysql_fetch_assoc($result)) |
|
| 125 |
+{
|
|
| 126 |
+ if (!($piece >= $row["startpiece"] && $piece <= $row["endpiece"])) |
|
| 127 |
+ continue; |
|
| 128 |
+ |
|
| 129 |
+ $offset = ($row["startpiece"] == $piece) ? 0 : (($piece - $row["startpiece"])*$config["piecelength"] - $row["startpieceoffset"]); |
|
| 130 |
+ $fd = fopen($row["filename"], "rb") or reject("500 Internal Server Error");
|
|
| 131 |
+ if (fseek($fd, $offset) != 0) |
|
| 132 |
+ reject("500 Internal Server Error");
|
|
| 133 |
+ $data = fread($fd, $config["piecelength"]-$xmitbytes); |
|
| 134 |
+ if ($data === false) |
|
| 135 |
+ reject("500 Internal Server Error");
|
|
| 136 |
+ $xmit .= $data; |
|
| 137 |
+ $xmitbytes += strlen($data); |
|
| 138 |
+ if ($xmitbytes == $config["piecelength"]) |
|
| 139 |
+ break; |
|
| 140 |
+ fclose($fd); |
|
| 141 |
+} |
|
| 142 |
+ |
|
| 143 |
+ |
|
| 144 |
+// Header is most likely already: 200 Ok |
|
| 145 |
+ |
|
| 146 |
+//error_log("Send length: $xmitbytes == ".strlen($xmit));
|
|
| 147 |
+ |
|
| 148 |
+if (isset($_GET["ranges"])) |
|
| 149 |
+{
|
|
| 150 |
+ $myxmit = ""; |
|
| 151 |
+ $ranges = explode(",", $_GET["ranges"]);
|
|
| 152 |
+ foreach ($ranges as $blocks) |
|
| 153 |
+ {
|
|
| 154 |
+ $startstop = explode("-", $blocks);
|
|
| 155 |
+ if (!is_numeric($startstop[0]) || !is_numeric($startstop[1])) |
|
| 156 |
+ reject("400 Bad Request");
|
|
| 157 |
+ if (isset($startstop[2])) |
|
| 158 |
+ reject("400 Bad Request");
|
|
| 159 |
+ $start = $startstop[0]; |
|
| 160 |
+ $stop = $startstop[1]; |
|
| 161 |
+ if ($start > $stop) |
|
| 162 |
+ reject("400 Bad Request");
|
|
| 163 |
+ $myxmit .= substr($xmit, $start, $stop-$start+1); |
|
| 164 |
+ } |
|
| 165 |
+ header("Content-Length: ".strlen($myxmit));
|
|
| 166 |
+ mysql_query("UPDATE ".$prefix."speedlimit SET uploaded=uploaded+".strlen($myxmit));
|
|
| 167 |
+ echo $myxmit; |
|
| 168 |
+} |
|
| 169 |
+else |
|
| 170 |
+{
|
|
| 171 |
+ mysql_query("UPDATE ".$prefix."speedlimit SET uploaded=uploaded+$xmitbytes");
|
|
| 172 |
+ header("Content-Length: $xmitbytes");
|
|
| 173 |
+ echo $xmit; |
|
| 174 |
+} |
|
| 175 |
+ |
|
| 176 |
+Unlock("WebSeed--$lockno");
|
|
| 177 |
+exit; |
|
| 178 |
+ |
|
| 179 |
+?> |