-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.ts
82 lines (75 loc) · 2.36 KB
/
index.ts
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
81
82
/**
* Asterisk AGI (FastAGI) Server for Nodejs
* @module node-asteriskagi
* @license MIT
* @author Corey S. McFadden <[email protected]>
* @copyright This project is not affiliated with, endorsed by, or sponsored by Digium Inc. or Sangoma Technologies Corp, holders of the "Asterisk" trademark, which is used here for identification purposes only.
*/
import * as net from "net";
import events from "events";
import { AGIChannel } from "./channel";
class AGIServer extends events.EventEmitter {
public port: number = 4573;
private fAGI!: net.Server;
constructor(props: { port?: number }) {
super();
try {
this.port = props?.port || this.port;
this._bind();
} catch (err) {
console.log(`AGIServer error`, err);
}
}
/**
* Bind ports+listeners and signal ready.
*/
_bind() {
try {
this.fAGI = net.createServer((socket) => {
const remoteServer: string | false = socket?.remoteAddress
? socket.remoteAddress.split(":").pop() || false
: false;
let dataBuffer = "";
socket.on("data", async (data) => {
dataBuffer += data.toString();
if (dataBuffer.includes("\n\n")) {
socket.removeAllListeners("data");
const agiVariables = dataBuffer
.split("\n")
.reduce<{ [key: string]: string }>((acc, line) => {
if (line.startsWith("agi_")) {
const [key, value] = line
.split(":")
.map((item) => item.trim());
acc[key.substring(4)] = value;
}
return acc;
}, {});
// Hangup detection
socket.on("data", async (data) => {
if (data.toString().includes("HANGUP")) {
socket.removeAllListeners("data");
socket.end();
}
});
const call = new AGIChannel({
...agiVariables,
remoteServer,
socket,
});
this.emit("call", call);
socket.on("end", () => {
call.emit("hangup");
});
}
});
});
this.fAGI.listen(this.port, () => {
this.emit("ready", this.port);
});
} catch (err) {
console.log(`AGIServer`, err);
}
}
}
module.exports = AGIServer;