Skip to content

Commit

Permalink
Add support for PW protected Shellies
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlyth committed Apr 20, 2021
1 parent 7f03e12 commit 5baa4ef
Show file tree
Hide file tree
Showing 7 changed files with 368 additions and 285 deletions.
28 changes: 20 additions & 8 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ const createError = require('http-errors');
const cookieParser = require('cookie-parser');
const compression = require('compression');
const cors = require('cors');
//const helmet = require('helmet');
const proxy = require('express-http-proxy');
const express = require('express');
const SSE = require('express-sse');
const morgan = require('morgan');
const shellycoap = require('./shelly-coap.js')
const authHeader = require('basic-auth-header');

const indexRouter = require('./routes/index');
const apiRouter = require('./routes/api');
const app = express();
const sse = new SSE();
[app.locals.shellylist, app.locals.shellycoaplist] = shellycoap(sse);
const sse = new SSE({}, { isSerialized: false, initialEvent: 'shellysLoad' });

// view engine setup
app.set('views', path.join(__dirname, 'views'));
Expand All @@ -31,7 +30,6 @@ app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(process.env.PREFIX, express.static(path.join(__dirname, 'public')));
//app.use(helmet());
// Add handler for client to be able to request no compression. This is required for express-sse
app.use(compression({
filter: function (req, res) {
Expand All @@ -40,17 +38,31 @@ app.use(compression({
}));
app.use(path.join(process.env.PREFIX, '/'), indexRouter);
app.use(path.join(process.env.PREFIX, '/api'), apiRouter);
app.use(path.join(process.env.PREFIX, '/proxy/:addr'), proxy(function (req, res) {
const addr = req.params.addr;
return 'http://' + addr;
app.use(path.join(process.env.PREFIX, '/proxy/:devicekey'), proxy(function (req, res) {
const devicekey = req.params.devicekey;
const shelly = app.locals.shellylist[devicekey];
return 'http://' + shelly.ip;
}, {
userResDecorator: function (proxyRes, proxyResData, userReq, userRes) {
let data = proxyResData.toString('utf8');
data = data.replace(',url:"/"+url,', ',url:""+url,');
return data;
},
proxyReqOptDecorator: function (proxyReqOpts, srcReq) {
const devicekey = proxyReqOpts.params.devicekey;
const shelly = app.locals.shellylist[devicekey];
if (shelly.auth) {
console.warn('Need to add auth headers for this device');
proxyReqOpts.headers['Authorization'] = authHeader(process.env.SHELLYUSER, process.env.SHELLYPW);
}
return proxyReqOpts;
}
}));
}
));
app.get(path.join(process.env.PREFIX, '/events'), sse.init);

[app.locals.shellylist, app.locals.shellycoaplist] = shellycoap(sse);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
Expand Down
4 changes: 2 additions & 2 deletions bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ var host = process.env.HOST = normalizeHost(process.env.HOST || 'localhost');
process.env.TRUSTPROXY = process.env.TRUSTPROXY || 'loopback';
process.env.UIMODE = process.env.UIMODE || 'light';
process.env.PREFIX = normalizePrefix(process.env.PREFIX || '');
process.env.SHELLYUSER = process.env.SHELLYUSER || '';
process.env.SHELLYPW = process.env.SHELLYPW || '';

/**
* Module dependencies.
Expand All @@ -21,8 +23,6 @@ var debug = require('debug')('shelly-admin:server');
var http = require('http');




/**
* Create HTTP server.
*/
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "shelly-admin",
"description": "Shelly Admin",
"version": "0.2.3",
"version": "0.3.0",
"author": "Max Lyth",
"private": true,
"main": "main.js",
Expand All @@ -15,6 +15,8 @@
},
"dependencies": {
"assert": "^2.0.0",
"basic-auth-header": "^1.0.1",
"coiot-coap": "^1.0.0",
"compression": "^1.7.4",
"cookie-parser": "~1.4.4",
"cors": "^2.8.5",
Expand Down Expand Up @@ -90,4 +92,4 @@
]
}
}
}
}
Loading

0 comments on commit 5baa4ef

Please sign in to comment.