Skip to content

Commit 33e81c1

Browse files
Enabling a bunch of features on the Node.js proxy
1 parent 6bafbd8 commit 33e81c1

4 files changed

Lines changed: 144 additions & 43 deletions

File tree

app/pods/components/application-area/template.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
{{!-- Nagios Server: <strong>{{nagios.settings.nagiosServerHost}}</strong><br /> --}}
2424

25+
Connection Type: {{nagios.settings.connectionStyle}}<br />
2526
Connection Status: <strong class={{connectionStatusClass}}>{{nagios.connectionStatus}}</strong><br />
2627

2728
{{#if nagios.connectionError}}

app/pods/nagios/service.js

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ export default Ember.Service.extend({
2525
title: 'NagiosTV for Nagios 4',
2626
iconUrl: '/images/tv-xxl.png',
2727
connectionStyle: 'direct', // direct, proxy
28+
nodeServerHost: 'http://localhost:3000',
29+
nodeServerPath: '/nagios',
2830
nagiosServerHost: 'http://example.com',
2931
nagiosServerCgiPath: '/nagios/cgi-bin',
30-
nodeServerHost: 'http://localhost:3000',
3132
auth: true,
3233
username: 'nagiosadmin',
3334
password: ''
@@ -103,27 +104,105 @@ export default Ember.Service.extend({
103104
// overlay the loaded settings over top of the default settings
104105
// this helps prevent issues when we add new settings
105106
const defaultSettings = this.get('settings');
106-
const mergedSettings = Object.assign(defaultSettings, loadedSettings);
107+
const mergedSettings = Object.assign({}, defaultSettings, loadedSettings);
107108
this.set('settings', mergedSettings);
109+
110+
// If connectionStyle is 'proxy', then let's try to load settings from the proxy location
111+
if (mergedSettings.connectionStyle === 'proxy') {
112+
this.fetchProxySettings();
113+
}
108114
}
109115
},
110116

111117
saveLocalSettings: function() {
112118
console.log('saveLocalSettings()');
113119
localStorage.setItem('nagiostv-settings', JSON.stringify(this.settings));
120+
121+
// If connectionStyle is 'proxy', then let's try to load settings from the proxy location
122+
if (this.get('settings').connectionStyle === 'proxy') {
123+
this.fetchProxySettings();
124+
}
114125
},
115126

116127
clearLocalSettings: function() {
117128
console.log('clearLocalSettings()');
118129
localStorage.removeItem('nagiostv-settings');
119130
},
120131

132+
fetchProxySettings: function() {
133+
this.fetchProxySettingsPromise().then((data) => {
134+
//console.log('fetchProxySettings success');
135+
//console.log(data);
136+
//console.log('Overwriting local settings with server settings');
137+
this.set('settings', data);
138+
}, (err) => {
139+
console.log('fetchProxySettings error');
140+
});
141+
},
142+
143+
fetchProxySettingsPromise: function() {
144+
var nodeServerHost = this.get('settings.nodeServerHost');
145+
return new Promise((resolve, reject) => {
146+
$.ajax({
147+
url: nodeServerHost + '/settings',
148+
method: 'GET',
149+
dataType: 'json',
150+
success: function(data) {
151+
resolve(data);
152+
},
153+
error: function(fail) {
154+
reject(fail);
155+
}
156+
});
157+
});
158+
},
159+
121160
saveProxySettings: function() {
122161
console.log('saveProxySettings()');
162+
163+
// save a subset of the settings locally
164+
const localSettings = {};
165+
localSettings.connectionStyle = this.settings.connectionStyle;
166+
localSettings.nodeServerHost = this.settings.nodeServerHost;
167+
localSettings.nodeServerPath = this.settings.nodeServerPath;
168+
localStorage.setItem('nagiostv-settings', JSON.stringify(localSettings));
169+
170+
// send the settings to the proxy
171+
var settings = this.get('settings');
172+
return new Promise((resolve, reject) => {
173+
$.ajax({
174+
url: settings.nodeServerHost + '/settings',
175+
method: 'POST',
176+
contentType: "application/json; charset=utf-8",
177+
data: JSON.stringify(settings),
178+
dataType: 'json',
179+
success: function(data) {
180+
resolve(data);
181+
},
182+
error: function(fail) {
183+
reject(fail);
184+
}
185+
});
186+
});
187+
123188
},
124189

125190
clearProxySettings: function() {
126191
console.log('clearProxySettings()');
192+
var nodeServerHost = this.get('settings.nodeServerHost');
193+
return new Promise((resolve, reject) => {
194+
$.ajax({
195+
url: nodeServerHost + '/settings-clear',
196+
method: 'GET',
197+
dataType: 'json',
198+
success: function(data) {
199+
resolve(data);
200+
},
201+
error: function(fail) {
202+
reject(fail);
203+
}
204+
});
205+
});
127206
},
128207

129208
/**************************************
@@ -205,8 +284,18 @@ export default Ember.Service.extend({
205284
fetchUpdateFromNagios4: function() {
206285

207286
var that = this;
208-
var baseUrl = this.get('settings.nagiosServerHost');
209-
var basePath = this.get('settings.nagiosServerCgiPath');
287+
var settings = this.get('settings');
288+
289+
let baseUrl;
290+
let basePath;
291+
// switch for direct vs proxy connection
292+
if (settings.connectionStyle === 'proxy') {
293+
baseUrl = settings.nodeServerHost;
294+
basePath = settings.nodeServerPath;
295+
} else {
296+
baseUrl = settings.nagiosServerHost;
297+
basePath = settings.nagiosServerCgiPath;
298+
}
210299

211300
this.getJSON(baseUrl + basePath + '/statusjson.cgi?query=hostlist&details=true').then((data) => {
212301

node/app.js

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
var express = require('express');
1313
var app = express();
1414
var cors = require('cors');
15-
//var http = require('http');
16-
//var url = require('url');
1715
var fs = require('fs');
1816
var bodyParser = require('body-parser');
1917
var requestProxy = require('express-request-proxy');
@@ -22,45 +20,52 @@ var requestProxy = require('express-request-proxy');
2220
// Settings
2321
//=============================================================================
2422

25-
var settings;
23+
let settings;
24+
let settingsJson;
2625

27-
// Load the settings file, if it exists
26+
// Load the settings.js file, if it exists
2827
try {
29-
stats = fs.lstatSync('settings.js');
30-
settings = require('./settings');
28+
const stats = fs.lstatSync('settings.js');
3129
if (stats.isFile()) { console.log('settings.js file found.'); }
30+
settings = require('./settings');
3231
}
3332
catch (e) {
3433
console.log('Copy the file settings.dist.js to settings.js and edit settings.js');
3534
process.exit();
3635
}
3736

38-
// Check if the user changed the username and password in settings.js
39-
if (settings.username === 'changeme') {
40-
console.log('Please set username and password for Nagios web interface in settings.js');
41-
process.exit();
42-
}
37+
loadSettings();
4338

4439
//=============================================================================
4540
// loadSettings and saveSettings
4641
//=============================================================================
47-
48-
function loadSettings(req, res) {
49-
console.log('loadSettings');
50-
const settingsNoPassword = {};
51-
for (var s in settings) {
52-
if (s !== 'password') {
53-
settingsNoPassword[s] = settings[s];
54-
}
42+
function loadSettings() {
43+
// Load the settings-json.js file, if it exists
44+
try {
45+
const stats = fs.lstatSync('settings-json.js');
46+
if (stats.isFile()) { console.log('settings-json.js file found.'); }
47+
settingsJson = require('./settings-json');
48+
console.log(settingsJson.nagiosServerHost);
49+
} catch (e) {
50+
console.log('No settings-json.js found');
5551
}
56-
res.send(settingsNoPassword);
52+
}
53+
54+
function sendSettings(req, res) {
55+
console.log('sendSettings()');
56+
res.json(settingsJson);
5757
}
5858

5959
function saveSettings(req, res) {
60-
console.log('saveSettings');
61-
//console.log(req.body);
60+
console.log('saveSettings()');
61+
62+
if (!req.body) {
63+
console.log('saveSettings() no data so no save');
64+
return;
65+
}
66+
6267
var text = 'module.exports = ' + JSON.stringify(req.body, null, "\t");
63-
fs.writeFile('settings-json.js', text, function(err) {
68+
fs.writeFile('settings-json.js', text, (err) => {
6469
if(err) {
6570
res.send({
6671
success: false,
@@ -73,8 +78,12 @@ function saveSettings(req, res) {
7378
success: true,
7479
successMessage: 'Thanks for the settings.'
7580
});
76-
});
7781

82+
// save the settings locally
83+
settingsJson = req.body;
84+
// update the proxy
85+
decorateProxyOptions();
86+
});
7887
}
7988

8089
//=============================================================================
@@ -95,23 +104,31 @@ app.use('/', express.static('../dist'));
95104

96105
// GET Load settings
97106
app.get('/settings', function(req, res) {
98-
loadSettings(req, res);
107+
sendSettings(req, res);
99108
});
100109

101110
// POST Save settings
102111
app.post('/settings', function(req, res) {
103112
saveSettings(req, res);
104113
});
105114

106-
// Proxy to Nagios server
107-
var proxyOptions = {
108-
url: settings.nagiosServerHost + settings.nagiosServerPath + ':resource'
109-
};
110-
if (settings.auth) {
111-
proxyOptions.headers = {
112-
Authorization: "Basic " + new Buffer(settings.username + ':' + settings.password).toString('base64')
113-
};
115+
function decorateProxyOptions() {
116+
if (!settingsJson) {
117+
return;
118+
}
119+
proxyOptions.url = settingsJson.nagiosServerHost + settingsJson.nagiosServerCgiPath + '/:resource';
120+
if (settingsJson.auth) {
121+
proxyOptions.headers = {
122+
Authorization: "Basic " + new Buffer(settingsJson.username + ':' + settingsJson.password).toString('base64')
123+
};
124+
}
125+
//console.log('proxyOptions is', proxyOptions);
114126
}
127+
128+
// Proxy to Nagios server
129+
var proxyOptions = {};
130+
decorateProxyOptions();
131+
115132
app.get('/nagios/:resource', requestProxy(proxyOptions));
116133

117134
// Server listen on port

node/settings.dist.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,4 @@ module.exports = {
66

77
serverPort: 3000,
88

9-
nagiosServerHost: 'http://192.168.1.2',
10-
nagiosServerPath: '/nagios/cgi-bin/',
11-
auth: true,
12-
username: 'nagiosadmin',
13-
password: 'changeme'
14-
159
};

0 commit comments

Comments
 (0)