/*## (c) SECURITY EXPLORATIONS    2018 poland                                #*/
/*##     http://www.security-explorations.com                                #*/

/* THIS SOFTWARE IS PROTECTED BY DOMESTIC AND INTERNATIONAL COPYRIGHT LAWS    */
/* UNAUTHORISED COPYING OF THIS SOFTWARE IN EITHER SOURCE OR BINARY FORM IS   */
/* EXPRESSLY FORBIDDEN. ANY USE, INCLUDING THE REPRODUCTION, MODIFICATION,    */
/* DISTRIBUTION, TRANSMISSION, RE-PUBLICATION, STORAGE OR DISPLAY OF ANY      */
/* PART OF THE SOFTWARE, FOR COMMERCIAL OR ANY OTHER PURPOSES REQUIRES A      */
/* VALID LICENSE FROM THE COPYRIGHT HOLDER.                                   */

/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS    */
/* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,*/
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    */
/* SECURITY EXPLORATIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF  */
/* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE     */
/* SOFTWARE.                                                                  */

import java.lang.*;
import java.net.*;
import java.io.*;
import java.util.*;

public class Utils {
 private static String do_get(String s_url) {
  String result=null;

  try {
   URL url=new URL(s_url);
   HttpURLConnection conn=(HttpURLConnection)url.openConnection();

   int code=conn.getResponseCode();

   if (code==200) {
    InputStream in=conn.getInputStream();
    result="";

    BufferedReader b=new BufferedReader(new InputStreamReader(in,"utf-8"));
    String line=b.readLine();

    while(line!=null) {
     result=result+line;
     line=b.readLine();
    }

    b.close();
    in.close();
   }
  } catch(Throwable e) {
   e.printStackTrace();
  }

  return result;
 }

 public static String get_uuid(String host,int port) throws Throwable {
  String url="http://"+host+":"+port+"/upnpdev";

  String page=do_get(url);

  if (page!=null) {
   int idx=page.indexOf("<li>uuid:");

   if (idx>0) {
    int idx2=page.indexOf("<ul>",idx);

    if (idx2>0) {
     String uuid=page.substring(idx+9,idx2);
     return uuid;
    }
   }
  }

  return null;
 }

 public static String get_box_type(String host,int port,String uuid) throws Throwable {
  String url="http://"+host+":"+port+"/upnpdev/pres/uuid_"+uuid+"/00";

  String page=do_get(url);

  if (page!=null) {
   int idx=page.indexOf("Model name</td>");

   if (idx>0) {
    int idx2=page.indexOf("<td class=\"infovalue\">",idx);

    if (idx2>0) {
     idx2+=22;

     int idx3=page.indexOf("</td>",idx2);

     if (idx3>0) {
      String box_type=page.substring(idx2,idx3);
      return box_type;
     }
    }
   }
  }

  return null;
 }

 public static int discover_box_type(InetAddress remote_host) {
  int box_type=-1;

  try {
   System.out.println("Trying to manually detect box type");

   String host=remote_host.getHostAddress();
   int port=8080;

   String uuid=get_uuid(host,port);

   System.out.println("remote uuid: "+uuid);

   String boxtype=get_box_type(host,port,uuid);
   System.out.println("remote BOX TYPE: "+boxtype);

   if (boxtype.startsWith("ITI-2851")) {
    box_type=Payload.BOX_2851;
   } else {
    box_type=Payload.BOX_2849_2850;
   }
  } catch(Throwable t) {}

  return box_type;
 }
}
