Advertisement
caitsith2

Nintendo Content server fallback script

May 15th, 2015
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.61 KB | None | 0 0
  1. <?php
  2.     error_reporting(E_ERROR | E_PARSE);
  3. //$host = "ccs.shop.wii.com";
  4. $host = $_SERVER["SERVER_NAME"];
  5. $page = $_SERVER["REQUEST_URI"];
  6. $cachedisabled = 0; //Set to 1 if not storing any contents locally.
  7. $logdisabled = 0;
  8.  
  9. $path = "logs";
  10. if(!file_exists($path))
  11.     mkdir($path,"0777",1);
  12.  
  13. if($logdisabled == 0)
  14. {
  15.     $logfh = fopen("$path/log.txt", "a");
  16.     //$save_string = implode("`|`", $_SERVER);
  17.     //fwrite($logfh,$save_string);
  18.     //$save_string = "\r\n";
  19.     //fwrite($logfh,$save_string);
  20.     fwrite($logfh,$_SERVER["REMOTE_ADDR"]);
  21.     fwrite($logfh,", ");
  22.     fwrite($logfh,$_SERVER["REQUEST_TIME"]);
  23.     fwrite($logfh,", \"");
  24.     fwrite($logfh,$_SERVER["HTTP_USER_AGENT"]);
  25.     fwrite($logfh,"\", \"");
  26.     fwrite($logfh,$_SERVER["HTTP_ACCEPT"]);
  27.     fwrite($logfh,",");
  28.     fwrite($logfh,$_SERVER["HTTP_ACCEPT_LANGUAGE"]);
  29.     fwrite($logfh,",");
  30.     fwrite($logfh,$_SERVER["HTTP_ACCEPT_ENCODING"]);
  31.     fwrite($logfh,",");
  32.     fwrite($logfh,$_SERVER["HTTP_ACCEPT_CHARSET"]);
  33.     fwrite($logfh,"\", \"");
  34.     fwrite($logfh,$_SERVER["REQUEST_METHOD"]);
  35.     fwrite($logfh," http://");
  36.     fwrite($logfh,$host);
  37.     fwrite($logfh,$_SERVER["REQUEST_URI"]);
  38.     fwrite($logfh,"\", ");
  39.     fwrite($logfh,$_SERVER["SERVER_PROTOCOL"]);
  40. }
  41.  
  42. $host = "ccs.cdn.c.shop.nintendowifi.net";
  43. list($blank,$ccs,$download,$title,$filename) = split("/",$page);
  44.  
  45. $hexchars = array("0","1","2","3","4","5","6","7","8","9","A","a","B","b","C","c","D","d","E","e","F","f");
  46. $emptytitle = str_replace($hexchars, "", $title);
  47. $uppertitle = str_replace("a","A",$title);
  48. $uppertitle = str_replace("b","B",$uppertitle);
  49. $uppertitle = str_replace("c","C",$uppertitle);
  50. $uppertitle = str_replace("d","D",$uppertitle);
  51. $uppertitle = str_replace("e","E",$uppertitle);
  52. $uppertitle = str_replace("f","F",$uppertitle); //Convert ALL HEX characters to UPPERCASE. (Greatly simplifies comparison)
  53.  
  54. if ( strcmp ( $ccs,"ccs" ) or strcmp ( $download,"download" ) or empty ( $filename ) or (strlen($title) != 16) or (!empty ($emptytitle)))
  55. {
  56.     header("HTTP/1.1 404 Not Found");
  57.     echo "404: File $page not found on this server<br>";
  58.     if($logdisabled == 0)
  59.     {
  60.         fwrite($logfh,", 404 -\r\n");
  61.         fclose($logfh);
  62.     }
  63.     die;
  64. }   //Request was not for Nintendo's Content server, this means the File definitely does NOT exist.
  65.  
  66. $path = "$ccs/$download/$title";
  67.  
  68. if(!file_exists($path))
  69.     mkdir($path,"0777",1);
  70. if(!file_exists("a/$path"))
  71.     mkdir("a/$path","0777",1);
  72. $myFile = $page;
  73. $fh = fopen("a/$path/$filename", "wb");
  74.  
  75. if(! $fh)
  76. {
  77.     header("HTTP/1.1 500 Internal Server Error");
  78.     echo "could not open local file for writing";
  79.     if($logdisabled == 0)
  80.     {
  81.         fwrite($logfh,", 500 -\r\n");
  82.         fclose($logfh);
  83.     }
  84.     $cachedisabled = 2;
  85. }   //We were unable to create the local file.
  86.  
  87.  
  88.  
  89. $fp = fsockopen( "$host", 80, &$errno, &$errdesc);
  90. if ( ! $fp )
  91. {
  92.     header("HTTP/1.1 503 Service Unavailable");
  93.     if($logdisabled == 0)
  94.     {
  95.         fwrite($logfh,", 503 -\r\n");
  96.         fclose($logfh);
  97.     }
  98.     die ( "Couldn't connect to $host:\nError: $errno\nDesc: $errdesc\n" );
  99. }   //Nintendo's File content server is down.
  100. $request = "GET $page HTTP/1.0\r\n";
  101. $request .= "Host: $host\r\n";
  102. $request .= "User-Agent: PHP test client\r\n\r\n";
  103. $page = array();
  104. fputs ( $fp, $request );
  105. $result = fgets( $fp, 1024 );
  106. $contentsize = 0;
  107. if(((strcmp($result,"HTTP/1.0 200 OK\r\n"))&&(strcmp($result,"HTTP/1.1 200 OK\r\n")))||($cachedisabled != 0))
  108. {
  109.     if($logdisabled == 0)
  110.     {
  111.         if($cachedisabled == 0)
  112.             fwrite($logfh,", Not Saving results - ");
  113.         else if ($cachedisabled == 2)
  114.             fwrite($logfh,", Local file failed to open - ");
  115.         else
  116.             fwrite($logfh,", Caching Disabled - ");
  117.         fwrite($logfh,$result);
  118.         fclose($logfh);
  119.     }
  120.     while ( strcmp ( $result , "\r\n" ) )
  121.     {
  122.         header($result);
  123.         $result = fgets( $fp, 1024 );
  124.     }
  125.     while ( ! feof( $fp ) )
  126.     {
  127.         $data = fgets( $fp, 1024 );
  128.         echo $data;
  129.     }
  130.     fclose( $fh );
  131.     fclose( $fp );
  132.     unlink("a/$path/$filename");
  133.     rmdir("a/$path/");
  134.     rmdir("a/ccs/download/");
  135.     rmdir("a/ccs/");
  136.     rmdir("a/");
  137.     die;
  138. }   //We connected to Nintendo's Server, but the result was NOT 200 OK, return the result, but don't save it locally.
  139. if($logdisabled == 0)
  140.     fwrite($logfh,", 200 ");
  141. while ( strcmp ( $result , "\r\n" ) )
  142. {
  143.     header($result);
  144.     $result = fgets ( $fp, 1024 );
  145.     //echo $result;
  146.     list($field,$value) = split(": ",$result);
  147.     if(!strcmp ( $field, "Content-Length" ))
  148.     {
  149.         //echo "Found the content length = $value<br>";
  150.         $contentsize = (int)$value;
  151.         if($logdisabled == 0)
  152.             fwrite($logfh, $contentsize);
  153.     }
  154.     //echo "<br>";
  155. }   //Otherwise, we did get 200 OK, cache the file locally, if and ONLY if the content size is known.
  156. //die;
  157. while ( ! feof( $fp ) )
  158. {
  159.     $data = fgets( $fp, 1024 );
  160.     echo $data;
  161.     fwrite( $fh, $data );
  162. }
  163.  
  164. fclose( $fh );
  165. fclose( $fp );
  166.  
  167. if (filesize("a/$path/$filename") != $contentsize)
  168. {
  169.     echo "<br><br><br>Content size mismatch<br>";
  170.     echo "Downloaded ";
  171.     echo filesize("a/$path/$filename");
  172.     if($logdisabled == 0)
  173.     {
  174.         fwrite($logfh," - Content size mismatch - downloaded ");
  175.         fwrite($logfh,filesize("a/$path/$filename"));
  176.         fwrite($logfh,"\r\n");
  177.         fclose($logfh);
  178.     }
  179.     echo " bytes<br>expected $contentsize bytes<br>deleting file $path/$filename<br>";
  180.     unlink("a/$path/$filename");
  181.     rmdir("a/$path/");
  182.     rmdir("a/ccs/download/");
  183.     rmdir("a/ccs/");
  184.     rmdir("a/");
  185.     die;
  186. }   //Content size mismatched, echo an error to the client before cutting it off.
  187.  
  188. rename("a/$path/$filename","$path/$filename");
  189. rmdir("a/$path/");
  190. rmdir("a/ccs/download/");
  191. rmdir("a/ccs/");
  192. rmdir("a/");
  193.  
  194. if($logdisabled == 0)
  195. {
  196.     fwrite($logfh,"\r\n");
  197.     fclose($logfh);
  198. }
  199. die;
  200. //Download completed, content size matched, save the file into permanent local cache.
  201.  
  202.  ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement