forked from GB28181/VAG.Node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGB28181Server.js
254 lines (205 loc) · 7.84 KB
/
GB28181Server.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
const digest = require("./sip/digest");
const SIP = require("./sip/sip");
const context = require("./core/ctx");
const Logger = require("./core/logger");
const NodeSipSession = require("./GB28181Session");
// GB28181 SIP服务器
class NodeSIPServer {
constructor(config) {
this.listen = config.GB28181.sipServer.listen || 5060;
this.host = config.GB28181.sipServer.host || "127.0.0.1";// SIP服务器主机地址
this.defaultPassword = config.GB28181.sipServer.password || "12345678";
this.id=config.GB28181.sipServer.serial||"42000000402000000001";
this.domain = config.GB28181.sipServer.realm || "4200000040";// SIP服务器域
this.config = config;
// 临时用户信息
this.userinfo = {};
// 会话
this.dialogs = {};
}
run() {
// 监听端口,接收消息
this.uas = SIP.create({ publicAddress: this.host, port: this.listen }, (request, remote) => {
switch (request.method) {
// 当前域 注册/注销 REGISTER
case "REGISTER":
context.nodeEvent.emit("register", request, remote);
break;
// 上级域/媒体接收者 INVITE
case "INVITE":
context.nodeEvent.emit("invite", request, remote);
break;
// 当前域 MESSAGE 消息
case "MESSAGE":
context.nodeEvent.emit("message", request, remote);
break;
// 上级域/媒体接收者 ACK/BYE Step(11/13)
case "ACK":
context.nodeEvent.emit("ack", request, remote);
break;
case "BYE":
context.nodeEvent.emit("bye", request, remote);
break;
default:
this.uas.send(SIP.makeResponse(request, 405, "Method not allowed"));
break;
}
// 未指定方法的回复
if (!request.method) {
let userid = SIP.parseUri(request.headers.from.uri).user;
switch (request.status) {
case 100:
Logger.info(`[${userid}] Retrying`);
break;
case 400:
Logger.error(`[${userid}] bad request`);
break;
case 481:
Logger.error(`[${userid}] Call/Transaction Does Not Exist`);
break;
default:
Logger.error(`[${userid}] unknow status=${request.status} request${request}`);
break;
}
}
});
// 注册&注销 请求
context.nodeEvent.on("register", (request, remote) => {
this.auth(request, remote);
});
// 处理消息
context.nodeEvent.on("message", (request) => {
this.uas.send(SIP.makeResponse(request, 200, "OK"));
let userid = SIP.parseUri(request.headers.from.uri).user;
// 处理消息
if (context.sessions.has(userid)) {
let session = context.sessions.get(userid);
session.onMessage(request);
}
});
// 处理摄像头主动发bye信令,推流的服务器有问题or录像机回放完
context.nodeEvent.on("bye", (request) => {
this.uas.send(SIP.makeResponse(request, 200, "OK"));
let userid = SIP.parseUri(request.headers.from.uri).user;
for (var [key, value] of context.sessions) {
// 录像机里的通道摄像机
value.catalog.devicelist.forEach((item) => {
if (item.DeviceID === userid) {
let session = context.sessions.get(key);
session.onBye(request);
}
});
// 摄像机
if (key === userid) {
let session = context.sessions.get(userid);
session.onBye(request);
}
}
});
Logger.log(`Node Media GB28181 Sip-Server started on port: ${this.listen}`);
}
stop() {
// 服务器下线
this.uas.destroy();
}
// 身份验证
auth(request, remote) {
let serverInfo = SIP.parseUri(request.uri);
if (!request.headers || !request.headers.to || !request.headers.from || !serverInfo.user) {
this.uas.send(SIP.makeResponse(request, 400, "missing to header."));
return;
}
else if (!request.headers.to.uri) {
this.uas.send(SIP.makeResponse(request, 400, "missing username on to header."));
return;
}
else if (!request.headers.contact || request.headers.contact.length == 0) {
this.uas.send(SIP.makeResponse(request, 400, "missing contact header."));
return;
}
let toInfo = SIP.parseUri(request.headers.to.uri);
let fromInfo = SIP.parseUri(request.headers.from.uri);
// 用户 标识
let userId = fromInfo.user;
Logger.log(`[${userId}] register protocol=${remote.protocol} ip=${remote.address} port=${remote.port} `);
// 安全性检查
if (toInfo.user.length != 20 || fromInfo.user.length != 20 || toInfo.user != fromInfo.user || serverInfo.user != this.id) {
Logger.log(`[${userId}] check fail. `, toInfo.user, fromInfo.user, serverInfo);
return;
}
// 判断是否携带鉴权字段
if (!this.userinfo[userId] || !request.headers.authorization) {
// 会话标识
this.userinfo[userId] = { realm: this.domain };
Logger.log(`[${userId}] auth start. `);
this.uas.send(digest.challenge(this.userinfo[userId], SIP.makeResponse(request, 401, "Authentication Required.")));
return;
}
else {
if (request.headers.authorization && !digest.authenticateRequest(this.userinfo[userId], request, { password: this.defaultPassword })) {
this.uas.send(digest.challenge(this.userinfo[userId], SIP.makeResponse(request, 406, "Not Acceptable.")));
Logger.log(`[${userId}] auth fail. `);
return;
}
delete this.userinfo[userId];
Logger.log(`[${userId}] auth success. `);
}
// 注册有效期
let expires = request.headers.expires;
// 注册/保存会话信息
if (parseInt(expires) != 0) {
// 新的会话
if (context.sessions.has(userId)) {
let session = context.sessions.get(userId);
// 在注册有效期内,刷新注册还是重新注册
// 重新注册
if (session.request.headers["call-id"] != request.headers["call-id"]) {
// 重新注册是否需要断开之前的会话
let dialogs = [];
if (context.dialogs)
context.dialogs.forEach(dialog => {
if (dialog.deviceId === userId)
dialogs.push(dialog);
});
// 删除对话
dialogs.forEach(dialog => {
let index = context.dialogs.indexOf(dialog);
if (index > -1) {
context.dialogs.splice(index, 1);
}
});
session.request = request;
}
// 刷新过期时间
session.expires = expires || this.expires;
Logger.log(`[${userId}] device register refresh.`);
}
else {
// 新的设备加入
let session = new NodeSipSession(this.config, userId, { request: request, info: remote }, this.uas);
session.expires = expires || this.expires;
session.run();
}
}
else {
// 注销
if (context.sessions.has(userId))
context.nodeEvent.emit("logout", context.sessions.get(userId));
}
// 通过验证,增加 Date 字段,上下级和设备之间校时功能
this.uas.send(SIP.makeResponse(request, 200, "OK", { headers: { date: this.getNowTimeParse(), expires: (expires || this.expires) } }));
}
// 获取当前时间
getNowTimeParse() {
const time = new Date();
const YYYY = time.getFullYear();
const MM = time.getMonth() < 9 ? "0" + (time.getMonth() + 1) : (time.getMonth() + 1);
const DD = time.getDate() < 10 ? "0" + time.getDate() : time.getDate();
const hh = time.getHours() < 10 ? "0" + time.getHours() : time.getHours();
const mm = time.getMinutes() < 10 ? "0" + time.getMinutes() : time.getMinutes();
const ss = time.getSeconds() < 10 ? "0" + time.getSeconds() : time.getSeconds();
const ms = time.getMilliseconds();
return `${YYYY}-${MM}-${DD}T${hh}:${mm}:${ss}.${ms}`;
}
}
module.exports = NodeSIPServer;