-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathproxy.js
More file actions
80 lines (64 loc) · 2.18 KB
/
proxy.js
File metadata and controls
80 lines (64 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var util = require('util'), net = require("net"), color = require("ansi-color").set, syslog =
require('node-syslog');
var dir = process.env.npm_package_config_location || './../etc/';
var config = {
location : dir,
base : require(dir + 'nodast.js').config
};
syslog.init("nodast", syslog.LOG_PID | syslog.LOG_ODELAY, syslog.LOG_INFO);
process.on('uncaughtException', function (error) {
syslog.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);
// should log into own log file instead...
// 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.base.listen);
proxy.listen(config.base.listen);
};
exports.name = "nodast";
exports.version = "0.0.1";
exports.start = proxy;
exports.config = config;