diff --git a/atv.js b/atv.js
index caeb1d2..7337750 100755
--- a/atv.js
+++ b/atv.js
@@ -2,6 +2,10 @@
var utils = require("./utils");
+var localIp = getIPAddress();
+ console.log(localIp);
+
+
function getIPAddress() {
var interfaces = require('os').networkInterfaces();
for (var devName in interfaces) {
@@ -13,58 +17,9 @@ function getIPAddress() {
return alias.address;
}
}
-
return '0.0.0.0';
}
-var LOCAL_IP = getIPAddress();
-//const LOCAL_IP = "192.168.2.4";
-
-console.log("localIP address obtained:"+LOCAL_IP);
-console.log("Starting ATVBrowser - Let the hacking begin :)");
-utils.startDnsProxy(LOCAL_IP);
-utils.startWebServer(LOCAL_IP);
-
-var PlexAPI = require("plex-api");
-var client = new PlexAPI(LOCAL_IP);
-
-var result = client.query("/", function (error, result) {
- if (error) {
- throw new Error("Could not connect to server");
- }
-
- result.attributes; // MediaContainer attributes
- result.directory; // array of child Directory items
- console.log("attributes: "+result.attributes);
- console.log("directory: "+result.directory);
-
- console.log(result);
-
- return result;
-
-});
-
-//console.log(result);
-
-/*
-//setup the XML server
-var express = require('express');
-var app = express.createServer();
-
-app.get('/index.xml', function(req, res){
- res.render('index.ejs');
-});
-
-app.get('/assets/appletv/application.js', function(req,res) {
- res.sendfile('assets/appletv/application.js');
-});
-
-app.get('/assets/appletv/us/js/application.js', function(req,res) {
- res.sendfile('assets/appletv/us/js/application.js');
-});
-
-app.get('/assets/media.js', function(req,res) {
- res.sendfile('assets/media.js');
-});
-app.listen(3000);
-*/
\ No newline at end of file
+utils.startDnsProxy(localIp);
+utils.startWebServer(localIp);
+console.log("Starting ATVBrowser - Let the hacking begin :)");
\ No newline at end of file
diff --git a/utils.js b/utils.js
index d6894b9..27fab59 100755
--- a/utils.js
+++ b/utils.js
@@ -1,3 +1,25 @@
+var xml;
+var localIp = getIPAddress();
+var allKeys;
+var PlexAPI = require("plex-api");
+var AppleIndexHeader = '';
+var AppleIndexFooter = '';
+
+function getIPAddress() {
+ var interfaces = require('os').networkInterfaces();
+ for (var devName in interfaces) {
+ var iface = interfaces[devName];
+
+ for (var i = 0; i < iface.length; i++) {
+ var alias = iface[i];
+ if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal)
+ return alias.address;
+ }
+ }
+ return '0.0.0.0';
+}
+
+var jsontemplate = require("./node_modules/json-template/json-template").jsontemplate;
require('http').request({
hostname: 'www.whatismyip.org',
@@ -86,14 +108,72 @@ function startWebServer(localIp) {
var server = http.createServer(function(request, response) {
var pathname = url.parse(request.url).pathname;
console.log(request.url);
+
if (pathname.charAt(pathname.length - 1) == "/") {
- pathname += "index.html";
- }
+ response.writeHead(200, {'Content-Type': 'text/plain'});
+ response.write('hello');
+ }
+
+ var directories = xml.directory;
+ var length = directories.length;
+
+
+ if (pathname == "/index.xml") {
+
+ response.writeHead(200, {'Content-Type': 'text/xml'});
+ response.write(AppleIndexHeader);
+
+ for (var i = 0; i < length; i++) {
+ console.log(directories[i].attributes);
+ response.write(''+directories[i].attributes.thumb+''+directories[i].attributes.thumb+''+directories[i].attributes.title+'');
+ }
+
+ response.write(AppleIndexFooter);
+ response.end();
+ }
+
+
+
+
var realPath = path.join("assets", path.normalize(pathname.replace(/\.\./g, "")));
console.log("realPath: "+ realPath);
console.log("WEB: " + pathname);
- fs.stat(realPath, function(err, stats) {
+ //to read the media and application.js files
+ if (pathname == "/media.js") {
+ fs.readFile('./assets/media.js', 'utf8', function (err,data) {
+ if (err)
+ throw err;
+ if (data)
+ response.writeHead(200, {'Content-Type': 'application/javascript'});
+ response.write(data.toString('utf8'));
+ response.end();
+ });
+ }
+
+ if (pathname == "/appletv/application.js") {
+ fs.readFile('./assets/appletv/application.js', 'utf8', function (err,data) {
+ if (err)
+ throw err;
+ if (data)
+ response.writeHead(200, {'Content-Type': 'application/javascript'});
+ response.write(data.toString('utf8'));
+ response.end();
+ });
+ }
+
+ if (pathname == "/appletv/us/js/application.js") {
+ fs.readFile('./assets/appletv/us/js/application.js', 'utf8', function (err,data) {
+ if (err)
+ throw err;
+ if (data)
+ response.writeHead(200, {'Content-Type': 'application/javascript'});
+ response.write(data.toString('utf8'));
+ response.end();
+ });
+ }
+
+ /*fs.stat(realPath, function(err, stats) {
if (err) {
response.writeHead(404, {'Content-Type': 'text/plain'});
response.write("This request URL " + pathname + " was not found on this server.");
@@ -131,14 +211,14 @@ function startWebServer(localIp) {
}
} else {
var raw = fs.createReadStream(realPath);
+ raw.pipe(response);
response.writeHead(200, "OK");
- raw.pipe(response);
}
}
- });
+ });*/
+
});
server.listen(80);
- console.log("WebServer listening on " + localIp + ":80");
}
function resolveDNSDomain(msg) {
@@ -239,6 +319,7 @@ function getMsg(tag, domain, ip) {
}
function startDnsProxy(localIp) {
+
var dgram = require("dgram");
dgram.createSocket("udp4", function(msg, rinfo) {
@@ -265,5 +346,29 @@ function startDnsProxy(localIp) {
console.log("DnsProxy binding on " + localIp + ":53");
}
+
+var client = new PlexAPI(localIp);
+//first run query
+queryPlex("/library/sections");
+
+function queryPlex(key) {
+ client.query(key, function (error, result) {
+ if (error) {
+ throw new Error("Could not connect to server");
+ }
+ //result.attributes; // MediaContainer attributes
+ //result.directory; // array of child Directory items
+ xml = result;
+ parseXML();
+ });
+}
+
+function parseXML() {
+ //console.log(xml);
+ allKeys = xml.directory;
+ console.log("allKeys:"+allKeys);
+}
+
+
exports.startWebServer = startWebServer;
-exports.startDnsProxy = startDnsProxy;
+exports.startDnsProxy = startDnsProxy;
\ No newline at end of file