How do I protect this gun server? I only want traffic from one domain with many sub-domains.
In my use-case, I made a game for my kids on iPad. When they played for the first time - I realized it would be better if some of the game controls could be moved to their phone. So they use the ipad and the phone simultaneously to control the game. It works great. Data is sync'd between devices via gun. However, there's this problem of security. Anyone could use my gun server to share state between devices in real-time. I'd like to restrict it to my app
const ARGS = process.argv.slice(2);
var fs = require('fs');
var protocol = ARGS[0];
var port = ARGS[1];
var express = require('express');
var cors = require('cors')
var Gun = require('gun');
require('gun/axe');
var app = express();
var allowedOrigins = [
'localhost:8080',
];
app.use(cors({
origin: function (origin, callback) {
// allow requests with no origin
// (like mobile apps or curl requests)
// if (!origin) return callback(null, true);
if (allowedOrigins.indexOf(origin) === -1) {
var msg = 'The CORS policy for this site does not ' +
'allow access from the specified Origin.';
return callback(new Error(msg), false);
}
return callback(null, true);
}
}));
app.use(Gun.serve);
app.use(express.static('/gun'));
server = require(protocol);
if (protocol == 'https') {
var privateKey = fs.readFileSync('../json-data.ssl/key.pem', 'utf8');
var certificate = fs.readFileSync('../json-data.ssl/cert.pem', 'utf8');
var credentials = {key: privateKey, cert: certificate};
var server = server.createServer(credentials, app);
} else {
var server = server.createServer(app);
}
server.listen(port, () => {
console.log('listening on *:'.concat(port));
});
var gunDev = Gun({web: server, file: 'testingDB'});