Merge masterIP and masterPort into masterURL

Merge the masterIP and masterPort configuration entries into a single
masterURL entry.  Fixes the problem with connecting to an HTTPS version
of the master server and also makes it easier to host the master server
behind a reverse proxy.

This breaks all known code interfacing Clusterio.
This commit is contained in:
Hornwitser committed 2019-09-09 12:44:09 +02:00
1 parent da99b39402
commit 98900e72dc
15 files changed
+68 -43

No files matched your search

+8 -3
View File
@@ -25,9 +25,14 @@ class UPSdisplay {
let UPS = Math.round(ticksInPeriod / (timePeriod/1000));
// console.log("UPS: " + UPS);
try{
needle.post(this.config.masterIP+':'+this.config.masterPort+'/api/editSlaveMeta', {instanceID: this.config.unique, password: this.config.clientPassword, meta: {UPS:UPS, tick:this.tick}}, {headers: {'x-access-token': this.config.masterAuthToken}}, function(err, resp) {
// success?
});
needle.post(
this.config.masterURL+'/api/editSlaveMeta',
{instanceID: this.config.unique, password: this.config.clientPassword, meta: {UPS:UPS, tick:this.tick}},
{headers: {'x-access-token': this.config.masterAuthToken}},
function(err, resp) {
// success?
}
);
} catch (err){
console.log(err);
}
+19 -8
View File
@@ -78,9 +78,14 @@ module.exports = class remoteCommands {
}
}
console.log("Recorded flows, copper plate since last time: " + payload["copper-plate"]);
needle.post(that.config.masterIP + ":" + that.config.masterPort + '/api/logStats', {timestamp: timestamp, instanceID: that.config.unique, data: payload}, needleOptionsWithTokenAuthHeader, function (err, response, body) {
// we did it, keep going
});
needle.post(
that.config.masterURL + '/api/logStats',
{timestamp: timestamp, instanceID: that.config.unique, data: payload},
needleOptionsWithTokenAuthHeader,
function (err, response, body) {
// we did it, keep going
}
);
}
oldTimestamp = timestamp;
oldFlowStats = totalFlows;
@@ -105,7 +110,7 @@ module.exports = class remoteCommands {
g[0] = g[0].replace("\u0000", "");
// console.log("exporting " + JSON.stringify(g));
// send our entity and count to the master for him to keep track of
needle.post(that.config.masterIP + ":" + that.config.masterPort + '/api/place', {
needle.post(that.config.masterURL + '/api/place', {
name: g[0],
count: g[1],
instanceName: instance, // name of instance
@@ -168,7 +173,7 @@ module.exports = class remoteCommands {
// request our items, one item at a time
for (let i = 0; i < Object.keys(preparedPackage).length; i++) {
// console.log(preparedPackage[Object.keys(preparedPackage)[i]]);
needle.post(that.config.masterIP + ":" + that.config.masterPort + '/api/remove', preparedPackage[Object.keys(preparedPackage)[i]], needleOptionsWithTokenAuthHeader, function (err, response, body) {
function callback(err, response, body) {
if (response && response.body && typeof response.body == "object") {
// buffer confirmed orders
confirmedOrders[confirmedOrders.length] = {name:response.body.name,count:response.body.count}
@@ -176,7 +181,13 @@ module.exports = class remoteCommands {
console.log(`Imported ${response.body.count} ${response.body.name} from master`);
}
}
});
}
needle.post(
that.config.masterURL + '/api/remove',
preparedPackage[Object.keys(preparedPackage)[i]],
needleOptionsWithTokenAuthHeader,
callback
);
}
// if we got some confirmed orders
// console.log("Importing " + confirmedOrders.length + " items! " + JSON.stringify(confirmedOrders));
@@ -195,7 +206,7 @@ module.exports = class remoteCommands {
// COMBINATOR SIGNALS ---------------------------------------------------------
// get inventory from Master and RCON it to our slave
setInterval(() => {
needle.get(that.config.masterIP + ":" + that.config.masterPort + '/api/inventory', function (err, response, body) {
needle.get(that.config.masterURL + '/api/inventory', function (err, response, body) {
if(err){
console.error("Unable to get JSON master/api/inventory, master might be unaccessible");
} else if (response && response.body) {
@@ -307,7 +318,7 @@ module.exports = class remoteCommands {
return new Promise((resolve, reject) => {
let instance = this.instances[instanceID];
if(!instance){
needle.get(this.config.masterIP+":"+this.config.masterPort+ '/api/slaves', { compressed: true }, (err, response) => {
needle.get(this.config.masterURL + '/api/slaves', { compressed: true }, (err, response) => {
if(err || response.statusCode != 200) {
console.log("Unable to get JSON master/api/slaves, master might be unaccessible");
} else if (response && response.body) {
+1 -1
View File
@@ -55,7 +55,7 @@ module.exports = class remoteCommands {
return new Promise((resolve, reject) => {
let instance = this.instances[instanceID];
if(!instance){
needle.get(this.config.masterIP+":"+this.config.masterPort+ '/api/slaves', { compressed: true }, (err, response) => {
needle.get(this.config.masterURL + '/api/slaves', { compressed: true }, (err, response) => {
if(err || response.statusCode != 200) {
console.log("Unable to get JSON master/api/slaves, master might be unaccessible");
} else if (response && response.body) {
@@ -60,7 +60,7 @@ function handleInventory(json, config, callback){
}
//console.log("Requesting: "+JSON.stringify(request));
// request the difference from master
needle.post(config.masterIP + ":" + config.masterPort + '/api/remove', request, {json:true,headers: {'x-access-token': config.masterAuthToken}}, function (err, response, body) {
function callback(err, response, body) {
if (!err && response && response.body && (typeof response.body == "object" || isJson(response.body))) {
// confirmed orders, already removed from master inventory
//response.body.name: response.body.count
@@ -70,7 +70,13 @@ function handleInventory(json, config, callback){
} else {
collectStacks(false);
}
});
}
needle.post(
config.masterURL + '/api/remove',
request,
{json:true,headers: {'x-access-token': config.masterAuthToken}},
callback
);
} else {
collectStacks(false);
}
@@ -87,9 +93,14 @@ function handleInventory(json, config, callback){
for(let i = 0; i < stackArray.length; i++){
if(stackArray[i].count > 0){
// console.log("Returning overflow: " + JSON.stringify(stackArray[i]));
needle.post(config.masterIP + ":" + config.masterPort + '/api/place', stackArray[i], {headers: {'x-access-token': config.masterAuthToken}}, function (err, resp, body) {
// console.log(body);
});
needle.post(
config.masterURL + '/api/place',
stackArray[i],
{headers: {'x-access-token': config.masterAuthToken}},
function (err, resp, body) {
// console.log(body);
}
);
}
}
});
@@ -127,7 +138,7 @@ function pollInventories(outputFile){
//return "/silent-command "+'local a="{"for b,c in pairs(game.players)do if c.connected then local d=game.players[b].get_inventory(defines.inventory.player_main).get_contents()local e=game.players[b].get_quickbar().get_contents()local f={}for g=1,game.players[b].force.character_logistic_slot_count do f[g]=game.players[b].character.get_request_slot(g)end;a=a..b..":{inventory:{"for h,i in pairs(d)do a=a.."['+"'"+'"..h.."'+"'"+']:"..i..","end;for h,i in pairs(e)do a=a.."['+"'"+'"..h.."'+"'"+']:"..i..","end;a=a.."},requestSlots:{"for h,i in pairs(f)do a=a.."['+"'"+'"..i["name"].."'+"'"+']:"..i["count"]..", "end;a=a.."}},"end end;a=a.."}"game.write_file("'+outputFile+'",a)'
return '/silent-command '+'local a='+"'"+'{"players":{'+"'"+'local b=false;for c,d in pairs(game.players)do if d.connected and d.character then if b then a=a..'+"'"+','+"'"+'else b=true end;local e=game.players[c].get_inventory(defines.inventory.player_main).get_contents()local f=game.players[c].get_quickbar().get_contents()local g={}local z={}for j,k in pairs(e)do if z[j] ~= nil then z[j] = z[j] + k else z[j] = k end end for j,k in pairs(f)do if z[j] ~= nil then z[j] = z[j] + k else z[j] = k end end for h=1,game.players[c].force.character_logistic_slot_count do g[h]=game.players[c].character.get_request_slot(h)end;a=a..'+"'"+'"'+"'"+'..c..'+"'"+'":{"inventory":{'+"'"+'local i=false;for j,k in pairs(z)do if i then a=a..'+"'"+','+"'"+'else i=true end;a=a..'+"'"+'"'+"'"+'..j..'+"'"+'":'+"'"+'..k end;a=a..'+"'"+'},"requestSlots":{'+"'"+'i=false;for j,k in pairs(g)do if i then a=a..'+"'"+','+"'"+'else i=true end;a=a..'+"'"+'"'+"'"+'..k['+"'"+'name'+"'"+']..'+"'"+'":'+"'"+'..k['+"'"+'count'+"'"+']end;a=a..'+"'"+'}}'+"'"+'end end;a=a.."}}"game.write_file("'+outputFile+'",a,true,0)'
/*
needle.post(config.masterIP+':'+config.masterPort+'/api/editSlaveMeta', {instanceID: config.unique, password: config.clientPassword, meta: {UPS:UPS}}, function(err, resp) {
needle.post(config.masterURL+'/api/editSlaveMeta', {instanceID: config.unique, password: config.clientPassword, meta: {UPS:UPS}}, function(err, resp) {
// success?
});
*/
@@ -12,8 +12,7 @@ const sinon = require("sinon");
describe("inventoryImports/interfacingFunctions.js", function(){
describe("handleInventory(inventory, config)", function(){
let config = {
masterIP:"localhost",
masterPort:8080,
masterURL:"http://localhost:8080",
masterAuthToken:"",
}
it("returns false if given invalid input", function(){
+3 -3
View File
@@ -50,7 +50,7 @@ class ResearchSync {
}
initial_request_own_data(callback) {
const url = `${this.config.masterIP}:${this.config.masterPort}/api/getSlaveMeta`
const url = `${this.config.masterURL}/api/getSlaveMeta`
const data = {
instanceID: this.config.unique,
password: this.config.clientPassword,
@@ -102,7 +102,7 @@ class ResearchSync {
}
request_cluster_data() {
const slaves_data_url = `${this.config.masterIP}:${this.config.masterPort}/api/slaves`
const slaves_data_url = `${this.config.masterURL}/api/slaves`
needle.get(slaves_data_url, {compressed:true}, this.sync_researches.bind(this))
}
@@ -134,7 +134,7 @@ class ResearchSync {
this.print_own_contribution()
needle.post(this.config.masterIP + ':' + this.config.masterPort + '/api/editSlaveMeta', {
needle.post(this.config.masterURL + '/api/editSlaveMeta', {
instanceID: this.config.unique,
password: this.config.clientPassword,
meta: {research: this.research}
+1 -2
View File
@@ -12,8 +12,7 @@ describe("researchSync/index.js", function() {
const config = {
instanceName: "test",
masterIP: "localhost",
masterPort: 8080,
masterURL: "http://localhost:8080",
unique: 99,
clientPassword: "password",
masterAuthToken: "masterToken",
+1 -1
View File
@@ -27,7 +27,7 @@ module.exports = class remoteCommands {
force: force.forceName,
forceData: JSON.stringify(force.data),
};
needle.post(`${mergedConfig.masterIP}:${mergedConfig.masterPort}/api/logStats`,
needle.post(`${mergedConfig.masterURL}/api/logStats`,
request,
needleOptionsWithTokenAuthHeader, (err, resp) => {
if(!err && resp.body && resp.body.ok == false) console.log("Error calling /api/logStats: " + resp.body);