Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial add

  • Loading branch information...
commit f66822ae3f730b6ddfe7025ed7e611e5f0e6663b 0 parents
@pdeschen authored
17 .project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>nodast</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.wst.jsdt.core.jsNature</nature>
+ </natures>
+</projectDescription>
3  .settings/.jsdtscope
@@ -0,0 +1,3 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+</classpath>
1  .settings/org.eclipse.wst.jsdt.ui.superType.container
@@ -0,0 +1 @@
+org.eclipse.wst.jsdt.launching.JRE_CONTAINER
1  .settings/org.eclipse.wst.jsdt.ui.superType.name
@@ -0,0 +1 @@
+Global
13 bin/nodast
@@ -0,0 +1,13 @@
+#!/usr/bin/env node
+
+var $nodeagi;
+try {
+ $nodeagi = require('nodeagi');
+}
+catch(exception) {
+ // load in place
+ if (!$nodeagi) {
+ $nodeagi = require('../lib/index.js');
+ }
+}
+$nodeagi.parse();
14 etc/nodast.js
@@ -0,0 +1,14 @@
+var config = {
+ listen : 9090,
+ upstreams : {
+ test : 'localhost:4573'
+ },
+ routes : {
+ '.*' : function() {
+ return ('agi://test/');
+ },
+ 'agi://192.168.129.170:9090/' : 'agi://test/'
+ }
+};
+
+exports.config = config;
0  lib/handler.js
No changes.
33 lib/index.js
@@ -0,0 +1,33 @@
+var sys = require('sys'), fs = require('fs'), path = require('path'), util = require('util');
+var cli = require('cli').enable('daemon');
+var $proxy = require('./proxy.js');
+var color = require("ansi-color").set;
+
+/**
+ * Parse the command line arguments and daemonize as necessary;
+ *
+ * @api public
+ */
+exports.parse = function() {
+
+ cli.enable('version');
+
+ cli.setApp($proxy.name, $proxy.version);
+
+ var options = cli.parse( {
+ test : [ 't', 'Test location', 'string' ]
+ });
+
+ // console.log(require('util').inspect(options, true, null));
+ if (options.test) {
+ var destination = require('./processor.js').test(options.test);
+ console.log(color('\t' + destination.hostname + ":" + destination.port,
+ "green"));
+ } else {
+ cli.main(function(args, options) {
+ $proxy.start();
+ });
+ }
+};
+
+exports.start = $proxy.start;
122 lib/processor.js
@@ -0,0 +1,122 @@
+var config = require('./../etc/nodast.js').config;
+
+var DEFAULT_CONTEXT = {
+ '$network_script' : '',
+ '$request' : 'agi://localhost:9090/',
+ '$channel' : 'unknown',
+ '$language' : 'en',
+ '$type' : 'SIP',
+ '$uniqueid' : '1306070695.16532',
+ '$version' : '1.6.2.6',
+ '$callerid' : 'unknown',
+ '$calleridname' : 'unknown',
+ '$callingpres' : '0',
+ '$callingani2' : '0',
+ '$callington' : '0',
+ '$callingtns' : '0',
+ '$dnid' : 'unknown',
+ '$rdnis' : 'unknown',
+ '$context' : 'default',
+ '$extension' : '',
+ '$priority' : '1',
+ '$enhanced' : '0.0',
+ '$accountcode' : 'unknown',
+ '$threadid' : '-1392870496'
+};
+
+var routes = function() {
+ var routes = config.routes;
+ var regexps = {};
+ // compile the location's regexp and map;
+ for ( var location in routes) {
+ new RegExp(location);
+ regexps[location] = routes[location];
+ }
+ return regexps;
+}();
+
+var resolv = function(url, context) {
+
+ if (context === undefined) {
+ context = DEFAULT_CONTEXT;
+ }
+ for ( var route in routes) {
+
+ if (new RegExp(route).test(url)) {
+
+ var groups = url.match(route);
+ var resolved = routes[route];
+
+ if (typeof resolved === 'function') {
+ for ( var index = 0; index < groups.length; index++) {
+ var group = groups[index];
+ context['$' + index] = group;
+ }
+ resolved = routes[route].call(context);
+ }
+ console.log(route + " => " + resolved);
+ return resolved;
+ }
+ }
+ return '';
+};
+
+var lookup = function(url, context) {
+
+ var resolved = resolv(url, context);
+
+ var upstreams = config.upstreams;
+ var orUpstreams = [];
+ for ( var upstream in upstreams) {
+ orUpstreams.push(upstream);
+ }
+
+ var upstreamRegexp = new RegExp('^agi:\/\/(' + orUpstreams.join('|') + ')?\/(.*)$');
+
+ if (upstreamRegexp.test(resolved)) {
+ var match = upstreamRegexp.exec(resolved);
+ var upstream = upstreams[match[1]].split(':');
+ return ( {
+ port : upstream[1],
+ hostname : upstream[0]
+ });
+ } else {
+ var regexp = /^agi:\/\/(.*)(:([0-9]*))?\/(.*)$/g;
+ var match = regexp.exec(resolved);
+
+ var destination = {
+ port : match[3],
+ hostname : match[1]
+ };
+
+ return (destination);
+ }
+
+ // return ( {
+ // port : config.$.port,
+ // hostname : config.$.hostname
+ // });
+};
+
+var parse = function(request) {
+ var context = DEFAULT_CONTEXT;
+ var params = request.split(/\n/);
+ for ( var int = 0; int < params.length; int++) {
+ var param = params[int];
+ var keyValue = param.split(': ');
+ context['$' + keyValue[0].replace('agi_', '')] = keyValue[1];
+ }
+ return context;
+};
+
+var process = function(request) {
+
+ var context = parse(request);
+
+ var destination = lookup(context.$request);
+
+ return destination;
+};
+
+exports.process = process;
+exports.test = lookup;
76 lib/proxy.js
@@ -0,0 +1,76 @@
+var sys = require("sys");
+var net = require("net");
+var color = require("ansi-color").set;
+var config = require('./../etc/nodast.js').config;
+
+var syslog = require('node-syslog').Syslog;
+
+syslog.init("nodast", syslog.LOG_PID | syslog.LOG_ODELAY, syslog.LOG_INFO);
+
+process.on('uncaughtException', function(error) {
+ syslog.log(syslog.LOG_ERROR, error);
+});
+
+var proxy = function() {
+
+ var handler = function(proxySocket) {
+
+ var buffer = [];
+
+ proxySocket.setEncoding('ascii');
+
+ var serviceSocket;
+ var connected = false;
+
+ proxySocket.on("data", function(data) {
+ console.log("<" + data);
+ if (!connected && serviceSocket === undefined) {
+ buffer.push(data);
+
+ if (data.indexOf('agi_request') == -1) {
+ return;
+ }
+
+ var destination = require('./processor.js').process(data);
+
+ syslog.log(syslog.LOG_INFO, 'request in for ' + destination.hostname + ':' + destination.port);
+
+ serviceSocket = new net.Socket();
+ serviceSocket.setEncoding('ascii');
+ serviceSocket.on("data", function(data) {
+ console.log(">" + data);
+ proxySocket.write(data);
+ });
+ serviceSocket.on("close", function(had_error) {
+ proxySocket.end();
+ });
+
+ serviceSocket.on('connect', function(socket) {
+ for ( var int = 0; int < buffer.length; int++) {
+ var line = buffer[int];
+ serviceSocket.write(line);
+ }
+ });
+
+ serviceSocket.connect(destination.port, destination.hostname);
+ } else {
+ serviceSocket.write(data);
+ }
+ });
+ proxySocket.on("close", function(had_error) {
+ serviceSocket.end();
+ });
+ };
+ var proxy = net.createServer();
+ proxy.on('connection', handler);
+ proxy.on('close', function() {
+ syslog.log(syslog.LOG_INFO, "terminating");
+ });
+ syslog.log(syslog.LOG_INFO, 'starting ' + exports.name + " v"
+ + exports.version + ' on ' + config.listen);
+ proxy.listen(config.listen);
+};
+
+exports.name = "nodast";
+exports.version = "0.0.1";
+exports.start = proxy;
21 test/request.txt
@@ -0,0 +1,21 @@
+agi_network_script: robot?hash=1097957525&callId=5508571880547773492
+agi_request: agi://10.9.8.18:9090/robot?hash=1097957525&callId=5508571880547773492
+agi_channel: SIP/pbx_2-00000890
+agi_language: en
+agi_type: SIP
+agi_uniqueid: 1306070695.16532
+agi_version: 1.6.2.6
+agi_callerid: unknown
+agi_calleridname: unknown
+agi_callingpres: 0
+agi_callingani2: 0
+agi_callington: 0
+agi_callingtns: 0
+agi_dnid: unknown
+agi_rdnis: unknown
+agi_context: default
+agi_extension:
+agi_priority: 1
+agi_enhanced: 0.0
+agi_accountcode: nuecho
+agi_threadid: -1392870496
5 test/test.js
@@ -0,0 +1,5 @@
+
+var request = require('fs').readFileSync(process.cwd() + '/request.txt', 'utf-8');
+var destination = require('../lib/processor.js').process(request);
+
+console.log(destination.hostname + " @ " + destination.port);
Please sign in to comment.
Something went wrong with that request. Please try again.