From 3c73dce81652d64375ba0a1e322ef9831434dd2d Mon Sep 17 00:00:00 2001 From: Fonoster Team Date: Sat, 19 Jan 2019 23:15:06 -0500 Subject: [PATCH] Added new parameter expires(#27) and fixed #28 --- docs/api/resources.md | 1 + etc/schemas/gateways_schema.json | 1 + mod/core/processor/response_processor.js | 6 +- mod/core/processor/test.js | 2 +- mod/registry/registry.js | 86 ++++++++++-------------- mod/registry/test.js | 10 +-- 6 files changed, 44 insertions(+), 62 deletions(-) diff --git a/docs/api/resources.md b/docs/api/resources.md index e3d6c9af0..7a91a5881 100644 --- a/docs/api/resources.md +++ b/docs/api/resources.md @@ -192,6 +192,7 @@ The parameters for `restful_data_provider` are: | spec.credentials.secret | Gateway secret | Yes | | spec.host | Gateway host | Yes | | spec.transport | Transport protocol | Yes | +| spec.expires | Requested lifespan of the registration in seconds. Defaults to `3600` | No | | spec.registries.[*] | Additional registries for ingress calls | No | **Example** diff --git a/etc/schemas/gateways_schema.json b/etc/schemas/gateways_schema.json index 5929b9e84..c91eb54a7 100644 --- a/etc/schemas/gateways_schema.json +++ b/etc/schemas/gateways_schema.json @@ -33,6 +33,7 @@ }, "required": ["username", "secret"] }, + "expires": {"type": "integer"}, "host": {"type": "string"}, "transport": {"type": "string"}, "registries": { diff --git a/mod/core/processor/response_processor.js b/mod/core/processor/response_processor.js index e3ae7925d..c0ac0fb43 100644 --- a/mod/core/processor/response_processor.js +++ b/mod/core/processor/response_processor.js @@ -49,13 +49,13 @@ export default class ResponseProcessor { storeInRegistry(response) { const fromURI = response.getHeader(FromHeader.NAME).getAddress().getURI() const expiresHeader = response.getHeader(ExpiresHeader.NAME) - const expires = expiresHeader != null? expiresHeader.getExpires() : 300 - this.registry.storeRegistry(fromURI.getUser(), fromURI.getHost(), expires) + const expires = expiresHeader != null? expiresHeader.getExpires() : 3600 + this.registry.storeRegistry(fromURI, expires) } removeFromRegistry(response) { const fromURI = response.getHeader(FromHeader.NAME).getAddress().getURI() - this.registry.removeRegistry(fromURI.getHost()) + this.registry.removeRegistry(fromURI.toString()) } handleAuthChallenge(event) { diff --git a/mod/core/processor/test.js b/mod/core/processor/test.js index 0956231b7..c7de8f0da 100644 --- a/mod/core/processor/test.js +++ b/mod/core/processor/test.js @@ -98,7 +98,7 @@ function getRequest(from, to) { const toAddress = addressFactory.createAddress('sip:' + to) const toHeader = headerFactory.createToHeader(toAddress, null) - const expireHeader = headerFactory.createExpiresHeader(300) + const expireHeader = headerFactory.createExpiresHeader(3600) const contactAddress = addressFactory.createAddress('sip:' + from + ':' + port) const contactHeader = headerFactory.createContactHeader(contactAddress) const userAgentHeader = headerFactory.createUserAgentHeader(userAgent) diff --git a/mod/registry/registry.js b/mod/registry/registry.js index a2c6356f9..41386afa6 100644 --- a/mod/registry/registry.js +++ b/mod/registry/registry.js @@ -18,11 +18,10 @@ var cseq = 0 export default class Registry { - constructor(sipProvider, dataAPIs, expires = 300, checkExpiresTime = .5) { + constructor(sipProvider, dataAPIs) { this.dataAPIs = dataAPIs.GatewaysAPI this.gatewaysAPI = dataAPIs.GatewaysAPI - this.expires = expires - this.checkExpiresTime = checkExpiresTime + this.checkExpiresTime = .5 this.sipProvider = sipProvider this.config = getConfig() this.messageFactory = SipFactory.getInstance().createMessageFactory() @@ -33,7 +32,7 @@ export default class Registry { this.registry = new HashMap() } - getHostAddress(transport, received, rport) { + getLPAddress(transport, received, rport) { try { const lp = this.sipProvider.getListeningPoint(transport) const host = this.config.spec.externAddr? this.config.spec.externAddr : lp.getIPAddress() @@ -45,90 +44,73 @@ export default class Registry { } } - requestChallenge(username, gwRef, peerHost, transport, received, rport) { - const address = this.getHostAddress(transport, received, rport) - const host = address.host - const port = address.port - const request = this.messageFactory.createRequest('REGISTER sip:' + peerHost + ' SIP/2.0\r\n\r\n') - const fromAddress = this.addressFactory.createAddress('sip:' + username + '@' + peerHost) - const contactAddress = this.addressFactory.createAddress('sip:' + username + '@' + host + ':' + port) - const viaHeader = this.headerFactory.createViaHeader(host, port, transport, null) - - let headers = [] + requestChallenge(username, gwRef, gwHost, transport, received, rport, expires) { + const contactAddr = this.getLPAddress(transport, received, rport) + const viaAddr = this.getLPAddress(transport) + const request = this.messageFactory.createRequest('REGISTER sip:' + gwHost + ' SIP/2.0\r\n\r\n') + const fromAddress = this.addressFactory.createAddress('sip:' + username + '@' + gwHost) + const contactAddress = this.addressFactory.createAddress('sip:' + username + '@' + contactAddr.host + ':' + contactAddr.port) + const viaHeader = this.headerFactory.createViaHeader(viaAddr.host, viaAddr.port, transport, null) + const headers = [] viaHeader.setRPort() headers.push(viaHeader) - headers.push(this.headerFactory.createMaxForwardsHeader(70)) headers.push(this.sipProvider.getNewCallId()) + headers.push(this.headerFactory.createExpiresHeader(expires)) + headers.push(this.headerFactory.createMaxForwardsHeader(70)) headers.push(this.headerFactory.createCSeqHeader(cseq++, Request.REGISTER)) headers.push(this.headerFactory.createFromHeader(fromAddress, new SipUtils().generateTag())) headers.push(this.headerFactory.createToHeader(fromAddress, null)) headers.push(this.headerFactory.createContactHeader(contactAddress)) headers.push(this.headerFactory.createUserAgentHeader(this.userAgent)) - headers.push(this.headerFactory.createHeader('X-Gateway-Ref', gwRef)) headers.push(this.headerFactory.createAllowHeader('INVITE')) headers.push(this.headerFactory.createAllowHeader('ACK')) headers.push(this.headerFactory.createAllowHeader('BYE')) headers.push(this.headerFactory.createAllowHeader('CANCEL')) headers.push(this.headerFactory.createAllowHeader('REGISTER')) headers.push(this.headerFactory.createAllowHeader('OPTIONS')) + headers.push(this.headerFactory.createHeader('X-Gateway-Ref', gwRef)) headers.forEach(header => request.addHeader(header)) - this.sendRequest(request, peerHost) + this.sendRequest(request, gwHost) } - sendRequest(request, peerHost) { + sendRequest(request, gwHost) { try { const clientTransaction = this.sipProvider.getNewClientTransaction(request) clientTransaction.sendRequest() } catch(e) { - this.handleChallengeException(e, peerHost) + this.handleChallengeException(e, gwHost) } LOG.debug(request) } - handleChallengeException(e, peerHost) { - this.registry.remove(peerHost) + handleChallengeException(e, gwHost) { + this.registry.remove(gwHost) if(e instanceof javax.sip.TransactionUnavailableException || e instanceof javax.sip.SipException) { - LOG.warn('Unable to register with Gateway -> ' + peerHost + '. (Verify your network status)') + LOG.warn('Unable to register with Gateway -> ' + gwHost + '. (Verify your network status)') } else { LOG.warn(e) } } - storeRegistry(username, host, expires = 300) { + storeRegistry(gwURI, expires) { // Re-register before actual time expiration let actualExpires = expires - 2 * 60 * this.checkExpiresTime const reg = { - username: username, - host: host, - ip: InetAddress.getByName(host).getHostAddress(), + username: gwURI.getUser(), + host: gwURI.getHost(), + ip: InetAddress.getByName(gwURI.getHost()).getHostAddress(), expires: actualExpires, registeredOn: Date.now(), regOnFormatted: moment(new Date(Date.now())).fromNow() } - this.registry.put(host, reg) - } - - removeRegistry (host) { - this.registry.remove(host) + this.registry.put(gwURI.toString(), reg) } - hasHost(host) { - return this.registry.get(host) != null - } - - hasIp(ip) { - const iterator = this.registry.values().iterator() - - while(iterator.hasNext()) { - const reg = iterator.next() - if (reg.ip.equals(ip)) { - return true - } - } - return false + removeRegistry (gwURIStr) { + this.registry.remove(gwURIStr) } listAsJSON() { @@ -143,8 +125,8 @@ export default class Registry { return s } - isExpired (host) { - const reg = this.registry.get(host) + isExpired (gwURIStr) { + const reg = this.registry.get(gwURIStr) if (reg == null) { return true @@ -165,20 +147,22 @@ export default class Registry { if (response.status == Status.OK) { response.result.forEach (function(gateway) { - if (myRegistry.isExpired(gateway.spec.host)) { + const gwURIStr = 'sip:' + gateway.spec.credentials.username + '@' + gateway.spec.host + const expires = gateway.spec.expires? gateway.spec.expires : 3600 + if (myRegistry.isExpired(gwURIStr)) { LOG.debug('Register with ' + gateway.metadata.name + ' using ' + gateway.spec.credentials.username + '@' + gateway.spec.host) myRegistry.requestChallenge(gateway.spec.credentials.username, - gateway.metadata.ref, gateway.spec.host, gateway.spec.transport) + gateway.metadata.ref, gateway.spec.host, gateway.spec.transport, null, null, expires) } let registries = gateway.spec.registries if (registries != undefined) { registries.forEach (function(h) { - if (myRegistry.isExpired(gateway.spec.host)) { + if (myRegistry.isExpired(gwURIStr)) { LOG.debug('Register with ' + gateway.metadata.name + ' using ' + gateway.spec.credentials.username + '@' + h) - myRegistry.requestChallenge(gateway.spec.credentials.username, gateway.metadata.ref, h, gateway.spec.transport) + myRegistry.requestChallenge(gateway.spec.credentials.username, gateway.metadata.ref, h, gateway.spec.transport, null, null, expires) } }) } diff --git a/mod/registry/test.js b/mod/registry/test.js index ddc1a8812..34ad8a3f4 100644 --- a/mod/registry/test.js +++ b/mod/registry/test.js @@ -7,6 +7,8 @@ import Registry from 'registry/registry' import GatewaysAPI from 'data_api/gateways_api' const InetAddress = Packages.java.net.InetAddress +const SipFactory = Packages.javax.sip.SipFactory +const addressFactory = SipFactory.getInstance().createAddressFactory() const dataAPIs = { GatewaysAPI: new GatewaysAPI() @@ -18,12 +20,6 @@ export let testGroup = { name: "Registry Module" } // Warning: This will fill if there is not Internet connection testGroup.store_registry = function () { const registry = new Registry(null, dataAPIs) - registry.storeRegistry('29121', 'sanjose2.voip.ms', 200) + registry.storeRegistry(addressFactory.createSipURI('29121', 'sanjose2.voip.ms'), 200) assertTrue(registry.listAsJSON().length == 1) - assertTrue(registry.hasHost('sanjose2.voip.ms')) - assertFalse(registry.hasHost('atlanta.voip.ms')) - - const hostAddress = InetAddress.getByName('sanjose2.voip.ms').getHostAddress() - assertTrue(registry.hasIp(hostAddress)) } -