Permalink
Browse files

v0.4 of torrage codebase.

  • Loading branch information...
0 parents commit 049d8c66da7d0a7b214904e27ebb4f6eb48a128c @torrage committed Jul 5, 2010
BIN 404.ghtml
Binary file not shown.
23 404.php
@@ -0,0 +1,23 @@
+<?php
+ include_once dirname( __FILE__ ) . '/inc/settings.inc.php';
+
+ print_head();
+?>
+ <h2>Torrage is a free service for caching torrent files online.</h2>
+ <p>404 - File not found.</p>
+ <p>The torrent you are looking for can not be found on the system, if you want to, you can <a href="/">add it to the cache</a>.</p>
+ <p>
+ <b>Note:</b> HEX values A-F must be in uppercase in torrent URL's
+ <br />
+ <br /><i>Example:</i>
+ <ul>
+ <li><b>OK:</b> <?=getProto();?><?=$SETTINGS['torrstoredns'];?>/torrent/<b>640FE84C613C17F663551D218689A64E8AEBEABE</b>.torrent</li>
+ <br />
+ <li><b>ERROR:</b> <?=getProto();?><?=$SETTINGS['torrstoredns'];?>/torrent/<b><?= strtolower('640FE84C613C17F663551D218689A64E8AEBEABE'); ?></b>.torrent</li>
+ </ul>
+ </p>
+ <br />
+ <br />
+<?php
+ print_foot();
+
@@ -0,0 +1,14 @@
+<?php
+
+ include_once dirname( __FILE__ ) . '/../inc/settings.inc.php';
+
+ ini_set( 'soap.wsdl_cache_enabled', '0' );
+
+ $torrent = base64_encode( file_get_contents( 'test.torrent' ) );
+
+ $client = new SoapClient( getProto() . $SETTINGS['torrstoredns'] . '/api/torrage.wsdl' );
+ $infoHash = $client->cacheTorrent( $torrent );
+
+ echo $infoHash . "\n";
+
+ // URL will be http(s)://torrage.com/torrent/$infoHash.torrent
@@ -0,0 +1,17 @@
+<?php
+ include_once dirname( __FILE__ ) . '/../inc/settings.inc.php';
+
+ function cacheTorrent( $torrent )
+ {
+ $torrent = base64_decode( $torrent );
+ $fp = tempnam( '/tmp', 'soapApi' );
+ file_put_contents( $fp, $torrent );
+ $toReturn = handle_upload( $fp );
+ unlink( $fp );
+ return $toReturn;
+ }
+
+ ini_set( 'soap.wsdl_cache_enabled', '0' ); // disabling WSDL cache
+ $server = new SoapServer( 'torrage.wsdl' );
+ $server->addFunction( 'cacheTorrent' );
+ $server->handle();
@@ -0,0 +1,14 @@
+<?php
+
+ include_once dirname( __FILE__ ) . '/../inc/settings.inc.php';
+
+ ini_set( 'soap.wsdl_cache_enabled', '0' );
+
+ $torrent = base64_encode( file_get_contents( 'test.torrent' ) );
+
+ $client = new SoapClient( getProto() . $SETTINGS['torrstoredns'] . '/api/torrage.wsdl' );
+ $infoHash = $client->cacheTorrent( $torrent );
+
+ echo $infoHash . "\n";
+ // URL will be http(s)://torrage.com/torrent/$infoHash.torrent
+
Binary file not shown.
@@ -0,0 +1,48 @@
+<?xml version='1.0' encoding='UTF-8' ?>
+<definitions name='Cache'
+ targetNamespace='urn:Torrage'
+ xmlns:tns='urn:Torrage'
+ xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
+ xmlns:xsd='http://www.w3.org/2001/XMLSchema'
+ xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/'
+ xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
+ xmlns='http://schemas.xmlsoap.org/wsdl/'>
+
+<message name='cacheTorrentRequest'>
+ <part name='torrent' type='xsd:string'/>
+</message>
+
+<message name='cacheTorrentResponse'>
+ <part name='Result' type='xsd:string'/>
+</message>
+
+<portType name='CachePortType'>
+ <operation name='cacheTorrent'>
+ <input message='tns:cacheTorrentRequest'/>
+ <output message='tns:cacheTorrentResponse'/>
+ </operation>
+</portType>
+
+<binding name='CacheBinding' type='tns:CachePortType'>
+ <soap:binding style='rpc'
+ transport='http://schemas.xmlsoap.org/soap/http'/>
+
+ <operation name='cacheTorrent'>
+ <soap:operation soapAction='urn:xmethods-delayed-quotes#cacheTorrent'/>
+ <input>
+ <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
+ encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
+ </input>
+ <output>
+ <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes'
+ encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/>
+ </output>
+ </operation>
+</binding>
+
+<service name='CacheService'>
+ <port name='CachePort' binding='CacheBinding'>
+ <soap:address location='http://torrage.com/api/index.php'/>
+ </port>
+</service>
+</definitions>
@@ -0,0 +1,68 @@
+<?php
+ include_once dirname( __FILE__ ) . '/inc/main.inc.php';
+
+ print_head();
+?>
+<h1>API</h1>
+<p>People that are interested in using our service for automated caching of their newly created .torrent files or caching massive amounts of older files, can do so by using one our APIs.<br />
+This page contains some documentation on the APIs but also some example code in different languages. If you have some code for any additional language we do not cover, please give us some working example code over e-mail.</p>
+<p><i>All files will be cached at http://torrage.com/torrent/&lt;infoHash&gt;.torrent</i></p>
+<p><b>Note:</b> HEX values A-F must be in uppercase in torrent URL's</p>
+
+<h2>SOAP API</h2>
+<p>The SOAP API is probably the most easy to use in a modern scripting/programming language. The WSDL offers one simply function; <b>cacheTorrent()</b>. The function returns the info hash of the torrent on success, or a three digit error code if there was an error.<br />
+The SOAP WSDL is located at <a href="/api/torrage.wsdl"><?=getProto();?><?=$SETTINGS['torrstoredns'];?>/api/torrage.wsdl</a>.</p>
+<h3>PHP</h3>
+<p>Below is example code in PHP to cache "my.torrent". <i>You would need to compile PHP --with-soap</i>.</p>
+<pre>
+&lt;?php
+ $client = new SoapClient( '<?=getProto();?><?=$SETTINGS['torrstoredns'];?>/api/torrage.wsdl' );
+ $infoHash = $client->cacheTorrent( base64_encode( file_get_contents( 'my.torrent' ) ) );
+?&gt;
+</pre>
+<h3>PERL</h3>
+<p>Perl code to cache "my.torrent". <i>Requires SOAP::Lite (libsoap-lite-perl in debian)</i></p>
+<pre>#!/usr/bin/perl
+
+use MIME::Base64 ();
+use SOAP::Lite ();
+
+open( FILE, 'my.torrent' ) or die "$!";
+while( read( FILE, $buf, 60*57 ) ) { $tor .= MIME::Base64::encode( $buf ); }
+close( FILE );
+
+$infoHash = SOAP::Lite-&gt;service( '<?=getProto();?><?=$SETTINGS['torrstoredns'];?>/api/torrage.wsdl' )-&gt;cacheTorrent( $tor );
+
+print $infoHash;
+</pre>
+<h2>HTTP POST</h2>
+<p>If you don't have support for SOAP there is a normal HTTP POST interface. Here we show some example code for that as well.</p>
+
+<h3>PHP</h3>
+<p>Below is example code to cache "my.torrent". This feature requires the <i>pecl_http</i> extension.</p>
+<pre>
+&lt;?php
+ $files = array(
+ array(
+ 'name' => 'torrent', // Don't change
+ 'type' => 'application/x-bittorrent',
+ 'file' => 'my.torrent' // Full path for file to upload
+ )
+ );
+
+ $http_resp = http_post_fields( '<?=getProto();?><?=$SETTINGS['torrstoredns'];?>/autoupload.php', array(), $files );
+ $tmp = explode( "\r\n", $http_resp );
+ $infoHash = substr( $tmp[count( $tmp ) - 1], 0, 40 );
+ unset( $tmp, $http_resp, $files );
+?&gt;
+</pre>
+<h2>libTorrage</h2>
+<p>We've also made a small client in C that you can build if you want to script on command line or use the code in your own C project.<br />
+The file is available to download here: <a href="/source/libtorrage-0.1.1.tar.gz">libtorrage-0.1.1.tar.gz</a>.</p>
+
+<h3>Sample usage</h3>
+<pre>
+./torragecache <?=getProto();?><?=$SETTINGS['torrstoredns'];?>/autoupload.php my.torrent
+</pre>
+<?php
+ print_foot();
@@ -0,0 +1,36 @@
+<?php
+ include_once dirname( __FILE__ ) . '/inc/main.inc.php';
+
+ set_time_limit( 600 );
+ ini_set( 'upload_max_filesize', 6 * 1048576 );
+ error_reporting( 0 );
+
+ upload_error_handler( handle_upload( $_FILES['torrent']['tmp_name'] ) );
+
+ function upload_error_handler( $error )
+ {
+ global $SETTINGS;
+
+ switch( $error )
+ {
+ case TORRAGE_FILE_NOT_FOUND:
+ header( 'X-Torrage-Error-Msg: File empty.' );
+ die( "Error $error: File not found.\n" );
+ case TORRAGE_FILE_EMPTY:
+ header( 'X-Torrage-Error-Msg: File empty.' );
+ die( "Error $error: File empty.\n" );
+ case TORRAGE_FILE_INVALID:
+ header( 'X-Torrage-Error-Msg: Broken torrent file, please recreate it and try again.' );
+ die( "Error $error: Broken torrent file, please recreate it and try again.\n" );
+ case TORRAGE_FILE_UNKNOWN:
+ header( 'X-Torrage-Error-Msg: Error ' . $error );
+ die( "Error $error" );
+ case TORRAGE_FILE_ERROR:
+ header( 'X-Torrage-Error-Msg: Something didn\'t work, please try again later!' );
+ die( "Error $error: Something did'nt work, please try again later!\n" );
+ }
+
+ header( "X-Torrage-Infohash: $error" );
+ echo "$error\n";
+ exit( 0 );
+ }
@@ -0,0 +1,14 @@
+<?php
+
+ include_once dirname( __FILE__ ) . '/../inc/settings.inc.php';
+
+ ini_set( 'soap.wsdl_cache_enabled', '0' );
+
+ $torrent = base64_encode( file_get_contents( 'test.torrent' ) );
+
+ $client = new SoapClient( getProto() . $SETTINGS['torrstoredns'] . '/api/torrage.wsdl' );
+ $infoHash = $client->cacheTorrent( $torrent );
+
+ echo $infoHash . "\n";
+
+ // URL will be http(s)://torrage.com/torrent/$infoHash.torrent
@@ -0,0 +1,9 @@
+#!/bin/bash
+# cron setup
+# @daily /var/data/torrage.com/www/cron/daily.sh
+
+yesterday="$(date -d "yesterday" +%Y%m%d)"
+
+sort $yesterday.txt | uniq | sort > $yesterday.sort;
+rm $yesterday.txt;
+mv $yesterday.sort $yesterday.txt;
@@ -0,0 +1,11 @@
+#!/bin/bash
+# cron setup
+# @monthly /var/data/torrage.com/www/cron/monthly.sh
+
+lastmonth="$(date -d "5 days ago" +%Y%m)"
+
+sort ${lastmonth}.txt ${lastmonth}??.txt | uniq | sort > ${lastmonth}.sort;
+rm ${lastmonth}.txt;
+mv ${lastmonth}.sort $lastmonth.txt;
+
+rm ${lastmonth}??.txt;
@@ -0,0 +1,4 @@
+To create the static 404 error page do the following:
+
+php 404.php | gzip -9 > 404.ghtml
+
@@ -0,0 +1,85 @@
+# Special config for using the torrage system with lighttpd
+# You will of course also need to add your php handler.
+
+#
+#
+#
+#
+#
+# Lighttpd 1.5
+#
+#
+#
+#
+#
+
+server.modules = (
+ "mod_rewrite",
+ "mod_setenv",
+)
+
+server.error-handler-404 = "/404.ghtml"
+
+url.rewrite-once = (
+ "/torrent/([0-9A-F]{2,2})([0-9A-F]{2,2})([0-9A-F]{36,36}).*" => "/t/$1/$2/$3.torrent",
+)
+
+$HTTP["url"] =~ "\.torrent" {
+ setenv.add-response-header = ( "Content-Encoding" => "gzip" )
+ mimetype.assign = ( ".torrent" => "application/x-bittorrent" )
+}
+$HTTP["url"] =~ "\.ghtml" {
+ setenv.add-response-header = ( "Content-Encoding" => "gzip" )
+}
+
+#
+#
+#
+#
+#
+# Lighttpd 2.0
+#
+#
+#
+#
+#
+
+module_load (
+ "mod_status",
+ "mod_fastcgi",
+ "mod_dirlist",
+ "mod_rewrite",
+ "mod_redirect",
+ "mod_expire",
+ "mod_deflate",
+ "mod_cache_disk_etag",
+);
+
+do_deflate {
+ if req.is_handled {
+ if response.header["Content-Type"] =~ "^(.*/javascript|text/.*)(;|$)" {
+ deflate;
+ }
+ }
+}
+
+php {
+ fastcgi "unix:/var/run/lighttpd/php-fastcgi.socket";
+ do_deflate;
+}
+
+pregzipped {
+ header.add "Content-Encoding" => "gzip";
+ header.add "Vary" => "Accept-Encoding";
+}
+
+if req.host =~ "^(.+\.)?torrage.com" {
+ rewrite "/torrent/([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{36}).*" => "/t/$1/$2/$3.torrent";
+ docroot "/var/data/torrage.com/www";
+ index ( "index.php" );
+ if req.path == "/sync/" { dirlist; do_deflate; }
+ if !physical.exists { rewrite "/404.ghtml"; docroot "/var/data/torrage.com/www"; static; set_status 404; }
+ else if req.path =$ ".torrent" { pregzipped; }
+ else if req.path =$ ".php" { php; }
+ if req.path =$ ".ghtml" { pregzipped; }
+}
@@ -0,0 +1,5 @@
+In the file api/torrage.wsdl you will need to change the following line to match your setup.
+
+ <soap:address location='http://torrage.com/api/index.php'/>
+
+The SOAP API is the preferred one for most servers, so please make sure this works as it is supposed to :-)
@@ -0,0 +1,14 @@
+#
+# Change paths and ip's to match your setup, save the file as /etc/stunnel.conf
+# start stunnel with the following command:
+#
+# ulimit -n 131072 ; stunnel /etc/stunnel.conf
+#
+
+debug=0
+
+[http]
+CAfile=/var/data/torrage.com/ssl/cafile.ca
+cert=/var/data/torrage.com/ssl/torrage_com.pem
+connect=212.63.212.212:80
+accept=212.63.212.212:443
Oops, something went wrong.

0 comments on commit 049d8c6

Please sign in to comment.