mirror of
https://github.com/jambonz/sbc-sip-sidecar.git
synced 2026-10-04 02:04:20 +00:00
fix #29
This commit is contained in:
@@ -12,13 +12,26 @@ const regbots = [];
|
|||||||
const carriers = [];
|
const carriers = [];
|
||||||
const gateways = [];
|
const gateways = [];
|
||||||
|
|
||||||
|
function pickRelevantCarrierProperties(c) {
|
||||||
|
return {
|
||||||
|
voip_carrier_sid: c.voip_carrier_sid,
|
||||||
|
requires_register: c.requires_register,
|
||||||
|
is_active: c.is_active,
|
||||||
|
register_username: c.register_username,
|
||||||
|
register_password: c.register_password,
|
||||||
|
register_sip_realm: c.register_sip_realm,
|
||||||
|
register_from_user: c.register_from_user,
|
||||||
|
register_from_domain: c.register_from_domain,
|
||||||
|
register_public_ip_in_contact: c.register_public_ip_in_contact
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
class Regbot {
|
class Regbot {
|
||||||
constructor(logger, opts) {
|
constructor(logger, opts) {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
|
|
||||||
['ipv4', 'port', 'username', 'password', 'sip_realm'].forEach((prop) => this[prop] = opts[prop]);
|
['ipv4', 'port', 'username', 'password', 'sip_realm'].forEach((prop) => this[prop] = opts[prop]);
|
||||||
|
|
||||||
logger.debug({ opts }, 'Regbot');
|
|
||||||
this.voip_carrier_sid = opts.voip_carrier_sid;
|
this.voip_carrier_sid = opts.voip_carrier_sid;
|
||||||
this.username = opts.username;
|
this.username = opts.username;
|
||||||
this.password = opts.password;
|
this.password = opts.password;
|
||||||
@@ -35,11 +48,13 @@ class Regbot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
start(srf) {
|
start(srf) {
|
||||||
|
this.logger.info(`starting regbot ${this.username}@${this.sip_realm}`);
|
||||||
assert(!this.timer);
|
assert(!this.timer);
|
||||||
this.register(srf);
|
this.register(srf);
|
||||||
}
|
}
|
||||||
|
|
||||||
stop() {
|
stop() {
|
||||||
|
this.logger.info(`stopping regbot ${this.username}@${this.sip_realm}`);
|
||||||
clearTimeout(this.timer);
|
clearTimeout(this.timer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,6 +75,7 @@ class Regbot {
|
|||||||
try {
|
try {
|
||||||
const contactAddress = this.use_public_ip_in_contact ?
|
const contactAddress = this.use_public_ip_in_contact ?
|
||||||
`${this.username}@${srf.locals.sbcPublicIpAddress}` : this.aor;
|
`${this.username}@${srf.locals.sbcPublicIpAddress}` : this.aor;
|
||||||
|
this.logger.debug(`sending REGISTER for ${this.aor}`);
|
||||||
const req = await srf.request(`sip:${this.aor}`, {
|
const req = await srf.request(`sip:${this.aor}`, {
|
||||||
method: 'REGISTER',
|
method: 'REGISTER',
|
||||||
proxy: `sip:${this.ipv4}:${this.port}`,
|
proxy: `sip:${this.ipv4}:${this.port}`,
|
||||||
@@ -76,7 +92,7 @@ class Regbot {
|
|||||||
req.on('response', (res) => {
|
req.on('response', (res) => {
|
||||||
if (res.status !== 200) {
|
if (res.status !== 200) {
|
||||||
this.status = 'fail';
|
this.status = 'fail';
|
||||||
this.logger.info(`Regbot: got ${res.status} registering to ${this.sip_realm} at ${this.ipv4}:${this.port}`);
|
this.logger.info(`${this.aor}: got ${res.status} registering to ${this.ipv4}:${this.port}`);
|
||||||
this.timer = setTimeout(this.register.bind(this, srf), 30 * 1000);
|
this.timer = setTimeout(this.register.bind(this, srf), 30 * 1000);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -101,7 +117,7 @@ class Regbot {
|
|||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.logger.error({ err }, `Regbot Error registering to ${this.ipv4}:${this.port}`);
|
this.logger.error({ err }, `${this.aor}: Error registering to ${this.ipv4}:${this.port}`);
|
||||||
this.timer = setTimeout(this.register.bind(this, srf), 60 * 1000);
|
this.timer = setTimeout(this.register.bind(this, srf), 60 * 1000);
|
||||||
updateVoipCarriersRegisterStatus(this.voip_carrier_sid, JSON.stringify({
|
updateVoipCarriersRegisterStatus(this.voip_carrier_sid, JSON.stringify({
|
||||||
status: 'fail',
|
status: 'fail',
|
||||||
@@ -219,17 +235,18 @@ const updateCarrierRegbots = async(logger, srf) => {
|
|||||||
let hasChanged = false;
|
let hasChanged = false;
|
||||||
const gws = [];
|
const gws = [];
|
||||||
const cs = (await lookupAllVoipCarriers())
|
const cs = (await lookupAllVoipCarriers())
|
||||||
.filter((c) => c.requires_register);
|
.filter((c) => c.requires_register && c.is_active)
|
||||||
|
.map((c) => pickRelevantCarrierProperties(c));
|
||||||
if (JSON.stringify(cs) !== JSON.stringify(carriers)) hasChanged = true;
|
if (JSON.stringify(cs) !== JSON.stringify(carriers)) hasChanged = true;
|
||||||
for (const c of cs) {
|
for (const c of cs) {
|
||||||
try {
|
try {
|
||||||
const arr = (await lookupSipGatewaysByCarrier(c.voip_carrier_sid))
|
const arr = (await lookupSipGatewaysByCarrier(c.voip_carrier_sid))
|
||||||
.filter((gw) => gw.outbound && gw.is_active)
|
.filter((gw) => gw.outbound && gw.is_active)
|
||||||
.map((gw) => {
|
.map((gw) => {
|
||||||
gw.carrier = c;
|
gw.carrier = pickRelevantCarrierProperties(c);
|
||||||
return gw;
|
return gw;
|
||||||
});
|
});
|
||||||
Array.prototype.push.apply(gws, arr);
|
gws.push(...arr);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error({ err }, 'updateCarrierRegbots Error retrieving gateways');
|
logger.error({ err }, 'updateCarrierRegbots Error retrieving gateways');
|
||||||
}
|
}
|
||||||
@@ -264,9 +281,8 @@ const updateCarrierRegbots = async(logger, srf) => {
|
|||||||
});
|
});
|
||||||
regbots.push(rb);
|
regbots.push(rb);
|
||||||
rb.start(srf);
|
rb.start(srf);
|
||||||
logger.info({ regbot: rb.toJSON() }, 'Starting regbot');
|
|
||||||
}
|
}
|
||||||
debug(`updateCarrierRegbots: we have ${regbots.length} regbots`);
|
logger.debug(`updateCarrierRegbots: we have started ${regbots.length} regbots`);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error({ err }, 'updateCarrierRegbots Error');
|
logger.error({ err }, 'updateCarrierRegbots Error');
|
||||||
|
|||||||
Reference in New Issue
Block a user