Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
proxy for the Transmission torrent client and daemon. Useful to control your seedbox from a cellular mobile broadband carrier subscription where connections to ports are limited.
<?php
$auth = "username:password";
$url = "http://hostname:9091/transmission/rpc";
ini_set ("display_errors", false);
ini_set ("log_errors", true);
error_reporting (E_ALL);
if (!isset ($_SERVER["PHP_AUTH_USER"]) || !isset ($_SERVER["PHP_AUTH_PW"])) {
header ('WWW-Authenticate: Basic realm="transmission-proxy"', true);
header ("{$_SERVER["SERVER_PROTOCOL"]} 401 Unauthorized", true, 401);
} elseif ($_SERVER["PHP_AUTH_USER"] != strtok ($auth, ":") || $_SERVER["PHP_AUTH_PW"] != strtok (":")) {
header ("{$_SERVER["SERVER_PROTOCOL"]} 403 Forbidden", true, 403);
} else {
$curl = curl_init ();
curl_setopt_array ($curl
, array (
CURLOPT_URL => $url,
CURLOPT_USERPWD => $auth,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CRLF => true,
CURLOPT_HEADER => true,
CURLINFO_HEADER_OUT => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => file_get_contents ("php://input"),
)
);
if (isset ($_SERVER['HTTP_X_TRANSMISSION_SESSION_ID'])) curl_setopt ($curl, CURLOPT_HTTPHEADER, array ("x-transmission-session-id: {$_SERVER['HTTP_X_TRANSMISSION_SESSION_ID']}"));
$response = curl_exec ($curl);
$code = curl_getinfo ($curl, CURLINFO_HTTP_CODE);
header ("{$_SERVER["SERVER_PROTOCOL"]} $code", true, $code);
if (preg_match ("/^x-transmission-session-id:\s*(.+?)$/im", substr ($response, 0, strpos ($response, "\r\n\r\n")), $reg)) header ("x-transmission-session-id: {$reg[1]}", true);
$pos = strpos ($response, "\r\n\r\n");
if ($pos !== false) echo substr ($response, $pos + 4);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment