2018-06-26 18:34:11 +02:00
|
|
|
import {AllServers} from "../src/Server";
|
2016-12-01 23:18:18 +01:00
|
|
|
/* Functions to deal with manipulating IP addresses*/
|
|
|
|
|
|
|
|
//Generate a random IP address
|
|
|
|
//Will not return an IP address that already exists in the AllServers array
|
2017-08-30 19:44:29 +02:00
|
|
|
function createRandomIp() {
|
2017-07-13 18:54:29 +02:00
|
|
|
var ip = createRandomByte(99) +'.' +
|
|
|
|
createRandomByte(9) +'.' +
|
|
|
|
createRandomByte(9) +'.' +
|
|
|
|
createRandomByte(9);
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2016-12-01 23:18:18 +01:00
|
|
|
//If the Ip already exists, recurse to create a new one
|
|
|
|
if (ipExists(ip)) {
|
|
|
|
return createRandomIp();
|
|
|
|
}
|
|
|
|
return ip;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Returns true if the IP already exists in one of the game's servers
|
2017-08-30 19:44:29 +02:00
|
|
|
function ipExists(ip) {
|
2016-12-01 23:18:18 +01:00
|
|
|
for (var property in AllServers) {
|
|
|
|
if (AllServers.hasOwnProperty(property)) {
|
|
|
|
if (property == ip) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-30 19:44:29 +02:00
|
|
|
function createRandomByte(n=9) {
|
2017-07-13 18:54:29 +02:00
|
|
|
return Math.round(Math.random()*n);
|
2016-12-01 23:18:18 +01:00
|
|
|
}
|
|
|
|
|
2017-08-30 19:44:29 +02:00
|
|
|
function isValidIPAddress(ipaddress) {
|
|
|
|
if (/^(25[0-6]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-6]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-6]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-6]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress))
|
|
|
|
{
|
2016-12-01 23:18:18 +01:00
|
|
|
return true;
|
2017-08-30 19:44:29 +02:00
|
|
|
}
|
2017-06-09 06:35:03 +02:00
|
|
|
return false;
|
2017-08-30 19:44:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export {createRandomIp, ipExists, isValidIPAddress};
|