Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 7 additions & 52 deletions atv.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
*/
utils.startDnsProxy(localIp);
utils.startWebServer(localIp);
console.log("Starting ATVBrowser - Let the hacking begin :)");
119 changes: 112 additions & 7 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
var xml;
var localIp = getIPAddress();
var allKeys;
var PlexAPI = require("plex-api");
var AppleIndexHeader = '<?xml version="1.0" encoding="UTF-8"?><atv><body><scroller id="com.sample.movie-shelf"><items><shelf id="shelf_1" columnCount="2"><sections><shelfSection><items>';
var AppleIndexFooter = '</items></shelfSection></sections></shelf></items></scroller></body></atv>';

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',
Expand Down Expand Up @@ -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('<moviePoster id="shelf_item_1" accessibilityLabel="'+directories[i].attributes.title+'" featured="true" onSelect="atv.loadURL(\'http://trailers.apple.com/'+directories[i].attributes.key+'.xml\');" onPlay="atv.loadURL(\'http://trailers.apple.com/'+directories[i].attributes.key+'.xml\');"><image>'+directories[i].attributes.thumb+'</image><defaultImage>'+directories[i].attributes.thumb+'</defaultImage><title>'+directories[i].attributes.title+'</title></moviePoster>');
}

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.");
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -239,6 +319,7 @@ function getMsg(tag, domain, ip) {
}

function startDnsProxy(localIp) {

var dgram = require("dgram");

dgram.createSocket("udp4", function(msg, rinfo) {
Expand All @@ -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;