jka-core
Version:
Library for working with Jedi Academy servers
42 lines (41 loc) • 1.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getIpAndPort = exports.errors = void 0;
exports.errors = {
incorrectFormatErrorMessage: 'Parameter "server" has incorrect format!\nRequired format: "server_ipv4_or_domainname:port"\ne.g. 242.9.9.9:29071 or server.com:30001',
};
function getIpAndPort(server) {
const array = server.split(':');
if (array.length === 2) {
// TODO: Add domain checker
const [ip, port] = [array[0], parseInt(array[1])];
if (isValidIp(ip) && isValidPort(port))
return { ip, port };
else
throw new Error(exports.errors.incorrectFormatErrorMessage);
}
else {
throw new Error(exports.errors.incorrectFormatErrorMessage);
}
}
exports.getIpAndPort = getIpAndPort;
function isValidIp(candidate) {
if (typeof candidate !== 'string')
return false;
if (candidate === 'localhost')
return true;
// IPv4
if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(candidate))
return true;
// domains
if (/^(?:(?:(?:[a-zA-z\-]+)\:\/{1,3})?(?:[a-zA-Z0-9])(?:[a-zA-Z0-9\-\.]){1,61}(?:\.[a-zA-Z]{2,})+|\[(?:(?:(?:[a-fA-F0-9]){1,4})(?::(?:[a-fA-F0-9]){1,4}){7}|::1|::)\]|(?:(?:[0-9]{1,3})(?:\.[0-9]{1,3}){3}))(?:\:[0-9]{1,5})?$/.test(candidate))
return true;
return false;
}
function isValidPort(candidate) {
if (typeof candidate !== 'number' || isNaN(candidate))
return false;
if (!(candidate > 0 && candidate < 65535))
return false;
return true;
}