Skip to content

Commit e64f8a6

Browse files
committed
change SIP Register management and callId generation #5 #4
1 parent a6935e7 commit e64f8a6

File tree

9 files changed

+268
-217
lines changed

9 files changed

+268
-217
lines changed

dist/stage.js

+80-63
Original file line numberDiff line numberDiff line change
@@ -10692,13 +10692,22 @@ module.exports = function (stage) {
1069210692

1069310693
'use strict';
1069410694

10695+
var byteToHex = function byteToHex(byte) {
10696+
return ('0' + byte.toString(16)).slice(-2);
10697+
};
10698+
10699+
var generateId = function generateId(len) {
10700+
var arr = new Uint8Array((len || 40) / 2);
10701+
window.crypto.getRandomValues(arr);
10702+
return [].map.call(arr, byteToHex).join("");
10703+
};
10704+
1069510705
/*
1069610706
*
1069710707
* DIGEST authenticate
1069810708
*
1069910709
*
1070010710
*/
10701-
1070210711
var stringify = function stringify(value) {
1070310712
return '"' + value + '"';
1070410713
};
@@ -10780,13 +10789,14 @@ module.exports = function (stage) {
1078010789
_createClass(authenticate, [{
1078110790
key: 'register',
1078210791
value: function register(message, type) {
10783-
/*if (transaction.sended){
10784-
console.log("WWW-Authenticate error")
10785-
return ;
10786-
}*/
1078710792
//console.log("AUTH REGISTER")
1078810793
//console.log(message);
1078910794
var head = message.authenticate;
10795+
if (!head) {
10796+
head = this.dialog.authenticate;
10797+
} else {
10798+
this.dialog.authenticate = head;
10799+
}
1079010800
this.realm = head.realm;
1079110801
this.nonce = head.nonce;
1079210802
this.cnonce = head.cnonce;
@@ -10850,7 +10860,8 @@ module.exports = function (stage) {
1085010860
CallId: /^(.*)@.*$/,
1085110861
algorithm: /= */,
1085210862
fromTo: /<sip:(.*)@(.*)>/,
10853-
fromToG: /(.*)?<sip:(.*)@(.*)>/
10863+
fromToG: /(.*)?<sip:(.*)@(.*)>/,
10864+
contact: /.*<(sips?:.*)>(.*)?$/
1085410865
};
1085510866

1085610867
var parsefromTo = function parsefromTo(type, value) {
@@ -10928,7 +10939,12 @@ module.exports = function (stage) {
1092810939
} else {
1092910940
_this[headName] = headValue;
1093010941
if (_this[func]) {
10931-
_this[headName] = _this[func](headValue);
10942+
try {
10943+
_this[headName] = _this[func](headValue);
10944+
} catch (e) {
10945+
_this.message.sip.logger("Parse : " + headName, "ERROR");
10946+
throw e;
10947+
}
1093210948
}
1093310949
}
1093410950
});
@@ -11017,43 +11033,29 @@ module.exports = function (stage) {
1101711033
this.message.method = res[1];
1101811034
return value;
1101911035
}
11020-
11021-
/*setContact (value){
11022-
var parseValue = value.replace(regContact,"$1");
11023-
console.log(parseValue)
11024-
var sp = parseValue.split(";");
11025-
var contact = sp.shift();
11026-
var tab = contact.split(":");
11027-
this.message.contact = tab[0];
11028-
this.message.rport = tab[1];
11029-
for (var i = 0 ; i < sp.length ;i++){
11030-
var res3 = sp[i].split("=");
11031-
//console.log(res3[0] +" : "+ res3[1] );
11032-
this["contact"+res3[0]] = res3[1];
11033-
}
11034-
return value;
11035-
}*/
11036-
1103711036
}, {
1103811037
key: 'setContact',
1103911038
value: function setContact(value) {
11040-
var regContact = /.*<(sips?:.*)>.*/g;
11041-
//console.log(value)
11042-
var parseValue = regContact.exec(value);
11043-
//console.log(parseValue)
11039+
var parseValue = regHeaders.contact.exec(value);
1104411040
if (parseValue) {
1104511041
this.message.contact = parseValue[1];
11042+
if (parseValue[2]) {
11043+
var clean = parseValue[2].replace(/^;(.*)/, "$1");
11044+
var sp = clean.split(";");
11045+
for (var i = 0; i < sp.length; i++) {
11046+
var res = sp[i].split("=");
11047+
if (!res) {
11048+
continue;
11049+
}
11050+
var name = res[0].toLowerCase();
11051+
if (name === "expires") {
11052+
this["contact-" + name] = res[1];
11053+
}
11054+
}
11055+
}
11056+
} else {
11057+
throw new Error("Contact parse error : " + value);
1104611058
}
11047-
/*if ( parseValue[2] ){
11048-
console.log(parseValue[2])
11049-
var clean = parseValue[2].replace("^;(.*)","$1")
11050-
var sp = clean.split(";");
11051-
for (var i = 0 ; i < sp.length ;i++){
11052-
var res3 = sp[i].split("=");
11053-
console.log(res3[0] +" : "+ res3[1] );
11054-
//this["contact"+res3[0]] = res3[1];
11055-
}
11056-
}*/
1105711059
return value;
1105811060
}
1105911061
}, {
@@ -11238,9 +11240,9 @@ module.exports = function (stage) {
1123811240
var rport = this.transaction.dialog.sip.rport;
1123911241
var ip = this.transaction.dialog.sip.publicAddress;
1124011242

11243+
this.header.via = "Via: " + this.transaction.dialog.sip.via + ";" + "branch=" + this.transaction.branch;
1124111244
//if ( rport ){
1124211245
//this.header.via = "Via: "+this.transaction.dialog.sip.version+"/"+this.transaction.dialog.sip.settings.transport+" " +ip+":"+rport+";"+"branch="+this.transaction.branch;
11243-
this.header.via = "Via: " + this.transaction.dialog.sip.via + ";" + "branch=" + this.transaction.branch;
1124411246
//}else{
1124511247
//this.header.via = "Via: "+this.transaction.dialog.sip.version+"/"+this.transaction.dialog.sip.settings.transport+" " +ip+":"+this["request-port"]+";"+"branch="+this.transaction.branch;
1124611248
//}
@@ -11711,7 +11713,7 @@ module.exports = function (stage) {
1171111713
}, {
1171211714
key: 'generateCallId',
1171311715
value: function generateCallId() {
11714-
return parseInt(Math.random() * 1000000000, 10);
11716+
return generateId() + "@nodefony";
1171511717
}
1171611718
}, {
1171711719
key: 'generateTag',
@@ -11953,10 +11955,15 @@ module.exports = function (stage) {
1195311955
}, {
1195411956
key: 'parseBody',
1195511957
value: function parseBody() {
11956-
if (this.split[1]) {
11957-
this.body = new bodySip(this, this.split[1]);
11958-
} else {
11959-
this.body = new bodySip(this, "");
11958+
try {
11959+
if (this.split[1]) {
11960+
this.body = new bodySip(this, this.split[1]);
11961+
} else {
11962+
this.body = new bodySip(this, "");
11963+
}
11964+
} catch (e) {
11965+
this.sip.logger("SIP parseBody Message :" + this.split[1], "ERROR");
11966+
throw e;
1196011967
}
1196111968
}
1196211969
}, {
@@ -11966,6 +11973,7 @@ module.exports = function (stage) {
1196611973
try {
1196711974
this.header = new headerSip(this, this.split[0]);
1196811975
} catch (e) {
11976+
this.sip.logger("SIP parseHeader Message :" + this.split[0], "ERROR");
1196911977
throw e;
1197011978
}
1197111979
} else {
@@ -12095,7 +12103,6 @@ module.exports = function (stage) {
1209512103
if (this.rport) {
1209612104
this["request-uri"] = "sip:" + this.userName + "@" + this.publicAddress + ":" + this.rport + ";transport=" + this.transportType;
1209712105
}
12098-
var transaction = null;
1209912106
switch (message.code) {
1210012107
case 401:
1210112108
case 407:
@@ -12117,24 +12124,22 @@ module.exports = function (stage) {
1211712124
}
1211812125
this.registered = message.code;
1211912126
}
12120-
12121-
delete this.authenticate;
12122-
this.authenticate = null;
12123-
this.authenticate = new authenticate(message.dialog, this.userName, this.settings.password);
12124-
transaction = this.authenticate.register(message, message.code === 407 ? "proxy" : null);
12125-
12127+
delete this.authenticateRegister;
12128+
this.authenticateRegister = null;
12129+
this.authenticateRegister = new authenticate(message.dialog, this.userName, this.settings.password);
12130+
this.authenticateRegister.register(message, message.code === 407 ? "proxy" : null);
1212612131
break;
1212712132
case 403:
1212812133
this.registered = message.code;
1212912134
//console.log("Forbidden (bad auth)")
12130-
delete this.authenticate;
12131-
this.authenticate = null;
12135+
delete this.authenticateRegister;
12136+
this.authenticateRegister = null;
1213212137
this.notificationsCenter.fire("onError", this, message);
1213312138
break;
1213412139
case 404:
1213512140
this.registered = message.code;
12136-
delete this.authenticate;
12137-
this.authenticate = null;
12141+
delete this.authenticateRegister;
12142+
this.authenticateRegister = null;
1213812143
this.notificationsCenter.fire("onError", this, message);
1213912144
break;
1214012145
case 200:
@@ -12150,14 +12155,19 @@ module.exports = function (stage) {
1215012155
this.notificationsCenter.fire("onRegister", this, message);
1215112156
}
1215212157
this.registered = message.code;
12158+
12159+
var expires = message.header["contact-expires"] || this.settings.expires;
12160+
expires = parseInt(expires, 10) * 900; // 10% (ms)
1215312161
this.registerInterval = setInterval(function () {
12154-
_this2.register(_this2.userName, _this2.settings.password);
12155-
}, this.settings.expires * 900);
12162+
_this2.authenticateRegister.register(message);
12163+
_this2.notificationsCenter.fire("onRenew", _this2, _this2.authenticateRegister, message);
12164+
//this.register(this.userName, this.settings.password);
12165+
}, expires);
1215612166
break;
1215712167
default:
1215812168
this.registered = message.code;
12159-
delete this.authenticate;
12160-
this.authenticate = null;
12169+
delete this.authenticateRegister;
12170+
this.authenticateRegister = null;
1216112171
//console.log(message);
1216212172
this.notificationsCenter.fire("on" + message.code, this, message);
1216312173
break;
@@ -12194,7 +12204,7 @@ module.exports = function (stage) {
1219412204
delete this.authenticate;
1219512205
this.authenticate = null;
1219612206
this.authenticate = new authenticate(message.dialog, this.userName, this.settings.password);
12197-
transaction = this.authenticate.register(message, message.code === 407 ? "proxy" : null);
12207+
var transaction = this.authenticate.register(message, message.code === 407 ? "proxy" : null);
1219812208
this.fire("onInitCall", message.dialog.toName, message.dialog, transaction);
1219912209
break;
1220012210
case 180:
@@ -12218,8 +12228,6 @@ module.exports = function (stage) {
1221812228
this.notificationsCenter.fire("onError", this, message);
1221912229
break;
1222012230
case 487:
12221-
// ACK !!
12222-
break;
1222312231
case 404:
1222412232
case 477:
1222512233
case 480:
@@ -12344,6 +12352,7 @@ module.exports = function (stage) {
1234412352
_this3.serverPort = _this3.settings.portServer;
1234512353

1234612354
_this3.authenticate = false;
12355+
_this3.authenticateRegister = null;
1234712356

1234812357
// REGISTER
1234912358
_this3.registerInterval = null;
@@ -17940,7 +17949,11 @@ module.exports = function (stage) {
1794017949
key: 'close',
1794117950
value: function close() {
1794217951
this.logger("WEBRTC CLOSE TRANSACTION : " + this.callId, "DEBUG");
17943-
this.RTCPeerConnection.close();
17952+
if (this.RTCPeerConnection) {
17953+
this.RTCPeerConnection.close();
17954+
} else {
17955+
this.logger("WEBRTC TRANSACTION ALREADY CLOSED : " + this.callId, "WARNING");
17956+
}
1794417957
this.webrtc.unListen("onKeyPress", this.sendDtmf);
1794517958
delete this.RTCPeerConnection;
1794617959
return this;
@@ -18191,6 +18204,10 @@ module.exports = function (stage) {
1819118204

1819218205
this.protocol.listen(this, "onTimeout", function (sip, message) {
1819318206
this.notificationsCenter.fire("onTimeout", message.method, 408, message);
18207+
var transac = this.transactions[message.callId];
18208+
if (transac) {
18209+
this.closeTransaction(transac, transac.to.name);
18210+
}
1819418211
});
1819518212

1819618213
this.protocol.listen(this, "onDecline", function (message) {

dist/stage.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/stage.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)