Skip to content

Commit

Permalink
Added new parameter expires(fonoster#27) and fixed fonoster#28
Browse files Browse the repository at this point in the history
  • Loading branch information
Fonoster Team committed Jan 20, 2019
1 parent e20da73 commit 3c73dce
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 62 deletions.
1 change: 1 addition & 0 deletions docs/api/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
1 change: 1 addition & 0 deletions etc/schemas/gateways_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"required": ["username", "secret"]
},
"expires": {"type": "integer"},
"host": {"type": "string"},
"transport": {"type": "string"},
"registries": {
Expand Down
6 changes: 3 additions & 3 deletions mod/core/processor/response_processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion mod/core/processor/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
86 changes: 35 additions & 51 deletions mod/registry/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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() {
Expand All @@ -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
Expand All @@ -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)
}
})
}
Expand Down
10 changes: 3 additions & 7 deletions mod/registry/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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))
}

0 comments on commit 3c73dce

Please sign in to comment.