Skip to content

Commit

Permalink
preselect ip minecraft scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTrunk committed Dec 28, 2023
1 parent a20972d commit b0c0665
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions src/services/domainService.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const { getCustomConfigs } = require('./application/custom');
const { getApplicationsToProcess } = require('./application/subset');
const { DOMAIN_TYPE } = require('./constants');
const { startCertRsync } = require('./rsync');
const { matchRule } = require('./serviceHelper');

let myIP = null;
let myFDMnameORip = null;
Expand Down Expand Up @@ -181,6 +182,28 @@ function filterMandatoryApps(apps) {
return appsInBucket;
}

async function selectIPforMinecraft(ips, app) {
// choose the ip address whose sum of digits is the lowest
let chosenIp = ips[0];
let chosenIpSum = ips[0].split(':')[0].split('.').reduce((a, b) => parseInt(a, 10) + parseInt(b, 10), 0);
for (const ip of ips) {
const sum = ip.split(':')[0].split('.').reduce((a, b) => parseInt(a, 10) + parseInt(b, 10), 0);
if (sum < chosenIpSum) {
chosenIp = ip;
chosenIpSum = sum;
}
}
const isOk = await applicationChecks.checkApplication(app, chosenIp);
if (isOk) {
return chosenIp;
}
const newIps = ips.filter((ip) => ip !== chosenIp);
if (newIps.length) {
return selectIPforMinecraft(newIps, app);
}
return null;
}

// periodically keeps HAproxy ans certificates updated every 4 minutes
async function generateAndReplaceMainApplicationHaproxyConfig() {
try {
Expand Down Expand Up @@ -226,11 +249,21 @@ async function generateAndReplaceMainApplicationHaproxyConfig() {
const appLocations = await fluxService.getApplicationLocation(app.name);
if (appLocations.length > 0) {
const appIps = [];
for (const location of appLocations) { // run coded checks for app
// hard code fix for minecraft
// if its minecraft, do check only on the main app, if ok continue
if (matchRule(app.name.toLowerCase(), config.minecraftApps)) {
// eslint-disable-next-line no-await-in-loop
const isOk = await applicationChecks.checkApplication(app, location.ip);
if (isOk && location.ip !== '144.76.73.6') { // hard fix
appIps.push(location.ip);
const selectedIP = await selectIPforMinecraft(appLocations, app);
if (selectedIP) { // hard fix
appIps.push(selectedIP);
}
} else {
for (const location of appLocations) { // run coded checks for app
// eslint-disable-next-line no-await-in-loop
const isOk = await applicationChecks.checkApplication(app, location.ip);
if (isOk && location.ip !== '144.76.73.6') { // hard fix
appIps.push(location.ip);
}
}
}
if (config.mandatoryApps.includes(app.name) && appIps.length < 1) {
Expand Down

0 comments on commit b0c0665

Please sign in to comment.