improvement of ipv4 regex and test cases (#73)

* improvement of ipv4 regex and test cases

* require utils test

* change example

---------

Co-authored-by: Markus Frindt <m.frindt@cognigy.com>
This commit is contained in:
Markus Frindt
2024-08-21 11:47:06 -04:00
committed by GitHub
co-authored by Markus Frindt
parent 496d6c7a71
commit b190d8a312
5 changed files with 104 additions and 38 deletions
+2 -1
View File
@@ -7,6 +7,7 @@ const {
} = require('./config');
const assert = require('assert');
const short = require('short-uuid');
const {isValidIPv4} = require('./utils');
const DEFAULT_EXPIRES = (parseInt(JAMBONES_REGBOT_DEFAULT_EXPIRES_INTERVAL) || 3600);
const MIN_EXPIRES = (parseInt(JAMBONES_REGBOT_MIN_EXPIRES_INTERVAL) || 30);
const MAX_INITIAL_DELAY = 15;
@@ -124,7 +125,7 @@ class Regbot {
}
this.logger.debug(`sending REGISTER for ${this.aor}`);
const isIPv4 = /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/.test(this.ipv4);
const isIPv4 = isValidIPv4(this.ipv4);
const proxy = `sip:${this.ipv4}${isIPv4 ? `:${this.port}` : ''};transport=${transport}`;
this.logger.debug({isIPv4}, `sending via proxy ${proxy}`);
+15 -1
View File
@@ -27,9 +27,23 @@ async function addSipGatewayToBlacklist(client, logger, sip_gateway_sid, expired
}
}
/* Regex pattern to match valid IPv4 addresses (0.0.0.0 to 255.255.255.255) */
const ipv4Pattern = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
/**
* Checks if the given input string represents a valid IPv4 address.
*
* @param {string} ip - The string to be validated.
* @returns {boolean} - `true` if the input is a valid IPv4 address, `false` otherwise.
*/
function isValidIPv4(ip) {
return ipv4Pattern.test(ip);
}
module.exports = {
isUacBehindNat,
getSipProtocol,
addSipGatewayToBlacklist,
NAT_EXPIRES: 30
NAT_EXPIRES: 30,
isValidIPv4,
};