Skip to content

Commit

Permalink
Add proxy URL (thanks to @lsafelix75 for the original PR) (#452)
Browse files Browse the repository at this point in the history
* Add proxy URL (thanks to @lsafelix75 for the original PR)

see #440

Signed-off-by: Dan Cunningham <[email protected]>

* Quick change of middleware order

Signed-off-by: Dan Cunningham <[email protected]>

* Use single ticks

Signed-off-by: Dan Cunningham <[email protected]>

---------

Signed-off-by: Dan Cunningham <[email protected]>
  • Loading branch information
digitaldan authored May 25, 2024
1 parent d3bcfd2 commit 4e56622
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ app.use(function (req, res, next) {
next(); // No host in header, just go ahead
}
// If host matches names for full /* proxying, go ahead and just proxy it.
if (host.indexOf('remote.') === 0 || host.indexOf('home.') === 0) {
if (host.indexOf('remote.') === 0 || host === system.getProxyHost()) {
//make sure this was not set by another server
if (req.url.indexOf('/remote') != 0) {
req.url = '/remote' + req.url;
Expand Down
38 changes: 22 additions & 16 deletions routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ var logger = require('../logger');
var system = require('../system');
var firebase = require('../notificationsender/firebase');

exports.notificationsget = function(req, res) {
exports.notificationsget = function (req, res) {
var limit = req.query.limit > 0 ? parseInt(req.query.limit) : 10,
skip = req.query.skip > 0 ? parseInt(req.query.skip) : 0;
Notification.find({user: req.user.id}, '-user')
skip = req.query.skip > 0 ? parseInt(req.query.skip) : 0;
Notification.find({ user: req.user.id }, '-user')
.limit(limit)
.skip(skip)
.sort({created: 'desc'})
.exec(function(error, notifications) {
if (!error) {
res.status(200).json(notifications);
} else {
return res.status(500).json({
errors: [{
message: "Error getting notifications"
}]
});
}
});
.sort({ created: 'desc' })
.exec(function (error, notifications) {
if (!error) {
res.status(200).json(notifications);
} else {
return res.status(500).json({
errors: [{
message: "Error getting notifications"
}]
});
}
});
};

exports.notificationssettingsget = function(req, res) {
exports.notificationssettingsget = function (req, res) {
var config = {};
if (system.isGcmConfigured()) {
config.gcm = {
Expand Down Expand Up @@ -59,3 +59,9 @@ exports.hidenotification = function (req, res) {
return res.status(200).json({});
});
}

exports.proxyurlget = function (req, res) {
res.status(200).json({
'url': system.getProxyURL()
});
};
4 changes: 3 additions & 1 deletion routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,15 @@ Routes.prototype.setupAppRoutes = function (app) {
app.all('/api/v1/notifications', this.ensureRestAuthenticated, this.setOpenhab, this.preassembleBody, api_routes.notificationsget);
app.all('/api/v1/hidenotification/:id', this.ensureRestAuthenticated, this.setOpenhab, this.preassembleBody, api_routes.hidenotification);
app.all('/api/v1/settings/notifications', this.ensureRestAuthenticated, this.setOpenhab, this.preassembleBody, api_routes.notificationssettingsget);
app.all('/api/v1/proxyurl', this.ensureRestAuthenticated, this.setOpenhab, this.preassembleBody, api_routes.proxyurlget);

// Android app registration (FCM)
app.all('/addAndroidRegistration*', this.ensureRestAuthenticated, this.setOpenhab, this.preassembleBody, fcmRegistrationService.registerAndroid);
// Apple app registration (FCM)
app.all('/addIosRegistration*', this.ensureRestAuthenticated, this.setOpenhab, this.preassembleBody, fcmRegistrationService.registerIos);
// Apple app registration (legacy)
app.all('/addAppleRegistration*', this.ensureRestAuthenticated, this.setOpenhab, this.preassembleBody, appleRegistrationService);

};

// Ensure user is authenticated for web requests
Expand Down Expand Up @@ -391,7 +393,7 @@ Routes.prototype.proxyRouteOpenhab = function (req, res) {
requestPath = requestPath.replace('/remote', '');
// TODO: this is too dirty :-(
delete requestHeaders['host'];
requestHeaders['host'] = 'home.' + system.getHost() + ':' + system.getPort();
requestHeaders['host'] = system.getProxyURL();
}

// Send a message with request to openhab agent module
Expand Down

0 comments on commit 4e56622

Please sign in to comment.