Skip to content

Commit

Permalink
now its alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
mabels committed Apr 28, 2011
1 parent 74dbb4e commit 65fe596
Show file tree
Hide file tree
Showing 6 changed files with 590 additions and 130 deletions.
2 changes: 1 addition & 1 deletion conf-dispatcher/couch-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ function CouchClient(url) {
headers["Content-Type"] = "application/json";
}
getServerFromPool(uri, function (err, server) {
console.log(path+"***************************************"+require('util').inspect(server))
var request = server.request(method, path, headers);
if (body) {
request.write(body, 'utf8');
}
request.end();

request.on('response', function (response) {
response.setEncoding('utf8');
var body = "";
Expand Down
156 changes: 93 additions & 63 deletions conf-dispatcher/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var http = require('http');
var url = require('url');
var util = require('util');
var fs = require('fs');

var CouchClient = require('./couch-client');
Expand All @@ -8,15 +9,15 @@ var callStreamie = function(token, fn) {
var streamie = http.request({
host: 'streamie.org',
//host: '172.29.29.222',
//port: 8080,
port: 80,
path: '/user_info',
//port: 8080,
port: 80,
path: '/user_info',
method: 'GET',
headers: {
'Host': 'streamie.org',
'Cookie': 'token='+token
}
}, function(res) {
headers: {
'Host': 'streamie.org',
'Cookie': 'token='+token
}
}, function(res) {
res.setEncoding('utf8')
res.on('data', function(doc) {
try {
Expand All @@ -27,24 +28,23 @@ var callStreamie = function(token, fn) {
console.error('callStreamie:exception:'+e)
}
})
}).end()
}).end()
}

var getMacAddress = function(address, fn) {
fs.readFile('/proc/net/arp', 'utf8', function(err, data) {
if (err) {
console.error('can not read /proc/net/arp:'+err)
return
}
var lines = data.toString().split("\n")

for(var i = lines.length-1; i >= 0; --i) {
var line = lines[i].split(/\s+/)
if (line[0] == address) {
return fn(line[3])
if (err) {
console.error('can not read /proc/net/arp:'+err)
return
}
}
fn()
var lines = data.toString().split("\n")
for(var i = lines.length-1; i >= 0; --i) {
var line = lines[i].split(/\s+/)
if (line[0] == address) {
return fn(line[3])
}
}
fn()
})
}

Expand All @@ -53,30 +53,37 @@ var updateStreamie = function(mac, req, ret) {
if (err) { console.error('couchdb:get:failure:'+err) }
//console.log('mac:'+mac+":"+JSON.stringify(ret)+":"+JSON.stringify(doc))
//console.log(req)
var client = {
ipv4: req.headers['x-real-ip'] || req.socket.remoteAddress,
hwaddr: mac,
useragent: req.headers['user-agent'],
created_at: new Date()
};
if (!doc) {
console.log('NEW-DOC');
doc = {
_id: ret.user_id,
twitter: ret,
clients: [{
ipv4: req.headers['x-real-ip'] || req.socket.remoteAddress,
hwaddr: mac,
useragent: req.headers['user-agent'],
created_at: new Date()
}]
clients: [client]
}
} else {
console.log('UPDATE-DOC');
doc.twitter = ret
delete doc.completed
var found = false;
for(var i = doc.clients.length-1; i >= 0; --i) {
var clients = doc.clients[i]
if (clients.ipv4 == req.headers['x-real-ip'] || req.socket.remoteAddress) {
clients.hwaddr = mac
clients.useragent = req.headers['user-agent']
clients.created_at = new Date()
delete clients.iptabled
var r_ip = req.headers['x-real-ip'] || req.socket.remoteAddress;
if (doc.clients[i].ipv4 == r_ip) {
console.log('UPDATE-IPUPDATE');
doc.clients[i] = client;
found = true;
break;
}
}
if (!found) {
console.log('UPDATE-ADDCLIENT');
doc.clients.push(client);
}
}
//console.log(JSON.stringify(doc))
streamie.save(doc, function(err, doc) {
Expand Down Expand Up @@ -113,48 +120,71 @@ streamie.request('PUT', '/streamie', function(err, result) {
res.end('Weg hier \n');
}).listen(8124, "127.0.0.1");

var updateIPTables = function(id) {
var iptables = function(para, fn) {
var iptables = require('child_process').spawn('sudo', ['/sbin/iptables'].concat(para))
iptables.on('exit', function(code) {
console.log('iptables:'+para.join(' ')+"=>"+code);
fn(code);
});
}

var updateIPTables = function(id, rev) {
streamie.get(id, function(err, doc) {
if (err) { console.error('couchdb:get:failure:'+err) }
if (doc.completed) { return }
if (doc._rev != rev) { return }
if (doc.completed && doc.completed.pid == process.pid) { return }
for(var i = doc.clients.length-1; i >= 0; --i) {
var client = doc.clients[i]
if (!client.iptabled) {
console.log('iptables -D INPUT -i eth0 -m mac --mac-source '+client.hwaddr+' -s '+client.ipv4+' -j ACCEPT')
console.log('iptables -A INPUT -i eth0 -m mac --mac-source '+client.hwaddr+' -s '+client.ipv4+' -j ACCEPT')
client.iptabled = new Date()
}

// $IPTABLES -t mangle -I FREE_MACS -i $CONF_IF -p all -m mac
// --mac-source c8:bc:c8:4f:d4:66 -s 10.205.0.100 -j MARK --set-mark 0x1205
var iptable = [];
iptable.push('-t');
iptable.push('mangle');
iptable.push('-I');
iptable.push('FREE_MACS');
iptable.push('-p');
iptable.push('all');
iptable.push('-m');
iptable.push('mac');
iptable.push('--mac-source');
iptable.push(client.hwaddr);
iptable.push('-s');
iptable.push(client.ipv4);
iptable.push('-j');
iptable.push('MARK');
iptable.push('--set-mark');
iptable.push('0x1205');
iptables(iptable, function(code) {
client.iptabled = { date: new Date(), exitcode: code, rev: doc._rev }
});
}
doc.completed = new Date()
doc.completed = { date: new Date(), pid: process.pid, rev: doc._rev };
streamie.save(doc, function(err, doc) {
if (err) {
console.log('updateIPTables failed:'+err)
updateIPTables(id)
updateIPTables(id, rev)
}
})
})
}

// var _streamie = CouchClient('http://localhost:5984/streamie')
fs.readFile('streamie.json', 'utf8', function(err, data) {
since = data || '0'
if (err) { since = '0' }
console.log('CHANGES:'+since)
streamie.changes(since, function(err, changes) {
if (err) {
console.error('couchdb:changes:failure:'+err)
return
}
fs.writeFile('streamie.json', changes.seq+'', 'utf8', function(err) {
if (err) {
console.error('couchdb:write:failure:'+err)
return
}
if (changes.deleted) { return }
updateIPTables(changes.id)
})
})
})
iptables(['-t', 'mangle', '-F', 'FREE_MACS'], function(code) {
iptables(['-t', 'mangle', '-A', 'FREE_MACS', '-j', 'RETURN'], function(code) {
streamie.changes(0, function(err, changes) {
if (err) {
console.error('couchdb:changes:failure:'+err)
return
}
if (changes.deleted) {
console.log('DELETE:'+util.inspect(changes));
return
}
for(var i in changes.changes) {
updateIPTables(changes.id, changes.changes[i].rev )
}
})
})
})
})

console.log('Server running at http://127.0.0.1:8124/');
2 changes: 1 addition & 1 deletion conf-dispatcher/streamie.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16
18
Loading

0 comments on commit 65fe596

Please sign in to comment.