#!/usr/local/bin/node

var sys = require('sys');
var fs  = require('fs');

/*
------------------------------
 Process réalisant le tail -f
------------------------------
*/
function Spawn() {
  var spawn = require('child_process').spawn;
  var tail = spawn("tail", ["-f", filename]);
  return tail;
}

/*
------------------------------
 Le serveur http
------------------------------
*/
function Server() {
  var http = require("http");
  http.createServer(function(req,res){
    res.writeHead(200,{'Content-Type': 'text/plain'});
    tail.stdout.addListener('data', function (data) {
      res.write(data);
    });
  sys.puts("Listen on 8080");
  }).listen(8080);
}

var filename = process.ARGV[2];
if (!filename)return sys.puts("Usage: ./tail.js filename");
var tail = Spawn();
Server();
#!/usr/local/bin/node

var sys = require('sys');
var fs  = require('fs');

/*
------------------------------
 Chargement des IPs autorisées
------------------------------
*/
function IPLoad() {
  sys.puts("Updating iplist.");
  iplist = fs.readFileSync('./iplist.txt').toString().split('\n')
           .filter(function(ip) { return ip.length });
}

/*
------------------------------
 Process réalisant le tail -f
------------------------------
*/
function Spawn() {
  var spawn = require('child_process').spawn;
  var tail = spawn("tail", ["-f", filename]);
  return tail;
}

/*
------------------------------
 Le serveur http
------------------------------
*/
function Server() {
  var http = require("http");
  http.createServer(function(req,res){
    var allowed_ip = false;
    for(i in iplist) {
      sys.puts("loop");
      if (iplist[i] == req.connection.remoteAddress) {
        allowed_ip = true;
        break;
      }
    }
    if(allowed_ip == true) {
      res.writeHead(200,{'Content-Type': 'text/plain'});
      tail.stdout.addListener('data', function (data) {
        res.write(data);});
      }
    else {
      res.writeHead(401);
      res.write("Forbidden !!");
      res.end();
    }
    sys.puts("Listen open port 8080");
  }).listen(8080);
}

var filename = process.ARGV[2];
var iplist;
if (!filename)return sys.puts("Usage: ./tail.js filename");
IPLoad();
/*
------------------------------
 Process réalisant la mise
 une observation des 
 modifications
 du fichier iplist.txt
------------------------------
*/
fs.watchFile('./iplist.txt', function(c,p) { IPLoad(); });
var tail = Spawn();
Server();