From 06d6f85960c77a68706d7e02110166312f3c22ac Mon Sep 17 00:00:00 2001 From: Will Murphy Date: Thu, 19 Sep 2019 20:55:32 -0500 Subject: [PATCH] switch to https, place all object metadata in _meta for simpler projections --- db/setup.js | 11 ++++++++- index.js | 59 +++++++++++++++++---------------------------- package.json | 4 ++- routes/inbox.js | 4 +-- routes/outbox.js | 4 +-- routes/user.js | 3 +-- utils/index.js | 5 +--- utils/validators.js | 2 +- 8 files changed, 42 insertions(+), 50 deletions(-) diff --git a/db/setup.js b/db/setup.js index eda76ac..459cf72 100644 --- a/db/setup.js +++ b/db/setup.js @@ -4,8 +4,17 @@ const crypto = require('crypto') module.exports = async function dbSetup (db, domain) { // inbox await db.collection('streams').createIndex({ - _target: 1, + '_meta._target': 1, _id: -1, + }, { + name: 'inbox' + }) + // followers + await db.collection('streams').createIndex({ + '_meta._target': 1, + }, { + partialFilterExpression: {type: 'Follow'}, + name: 'followers' }) // outbox await db.collection('streams').createIndex({ diff --git a/index.js b/index.js index ed51eca..13d68dd 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,19 @@ const { promisify } = require('util') -const config = require('./config.json'); -const { USER, PASS, DOMAIN, PRIVKEY_PATH, CERT_PATH, PORT } = config; +const path = require('path') const express = require('express'); -const app = express(); const MongoClient = require('mongodb').MongoClient; +const fs = require('fs'); +const routes = require('./routes') +const bodyParser = require('body-parser') +const cors = require('cors') +const http = require('http') +const https = require('https') +const basicAuth = require('express-basic-auth'); + +const config = require('./config.json'); +const { USER, PASS, DOMAIN, KEY_PATH, CERT_PATH, PORT, PORT_HTTPS } = config; + +const app = express(); // Connection URL const url = 'mongodb://localhost:27017'; @@ -16,32 +26,18 @@ const client = new MongoClient(url, {useUnifiedTopology: true}); let db; -const fs = require('fs'); -const routes = require('./routes'), - bodyParser = require('body-parser'), - cors = require('cors'), - http = require('http'), - https = require('https'), - basicAuth = require('express-basic-auth'); + let sslOptions; -try { sslOptions = { - key: fs.readFileSync(PRIVKEY_PATH), - cert: fs.readFileSync(CERT_PATH) + key: fs.readFileSync(path.join(__dirname, KEY_PATH)), + cert: fs.readFileSync(path.join(__dirname, CERT_PATH)) }; -} catch(err) { - if (err.errno === -2) { - console.log('No SSL key and/or cert found, not enabling https server'); - } - else { - console.log(err); - } -} + app.set('domain', DOMAIN); -app.set('port', process.env.PORT || PORT || 3000); -app.set('port-https', process.env.PORT_HTTPS || 8443); +app.set('port', process.env.PORT || PORT); +app.set('port-https', process.env.PORT_HTTPS || PORT_HTTPS); app.use(bodyParser.json({type: [ 'application/activity+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' @@ -82,35 +78,24 @@ app.use('/api/admin', cors({ credentials: true, origin: true }), basicUserAuth, app.use('/.well-known/webfinger', cors(), routes.webfinger); app.use('/u', cors(), routes.user); app.use('/m', cors(), routes.message); -// app.use('/api/inbox', cors(), routes.inbox); app.use('/u/:name/inbox', routes.inbox) app.use('/u/:name/outbox', routes.outbox) app.use('/admin', express.static('public/admin')); app.use('/f', express.static('public/files')); -app.use('/hubs', express.static('../hubs/dist')); +// app.use('/hubs', express.static('../hubs/dist')); // Use connect method to connect to the Server -let objs client.connect({useNewUrlParser: true}) .then(() => { console.log("Connected successfully to server"); db = client.db(dbName); app.set('db', db); - objs = db.collection('objects'); - app.set('objs', db.collection('objects')); - return dbSetup(db, DOMAIN) }) - .then(() => { - http.createServer(app).listen(app.get('port'), function(){ - console.log('Express server listening on port ' + app.get('port')); + https.createServer(sslOptions, app).listen(app.get('port-https'), function () { + console.log('Express server listening on port ' + app.get('port-https')); }); - if (sslOptions) { - https.createServer(sslOptions, app).listen(app.get('port-https'), function () { - console.log('Express server listening on port ' + app.get('port-https')); - }); - } }) .catch(err => { throw new Error(err) diff --git a/package.json b/package.json index 568cedd..a81e212 100644 --- a/package.json +++ b/package.json @@ -8,8 +8,10 @@ "cors": "^2.8.4", "express": "^4.16.3", "express-basic-auth": "^1.1.5", + "http-signature": "^1.2.0", "mongodb": "^3.3.2", - "request": "^2.87.0" + "request": "^2.88.0", + "request-promise-native": "^1.0.7" }, "engines": { "node": ">=10.10.0" diff --git a/routes/inbox.js b/routes/inbox.js index 9dc4984..b8c8d6c 100644 --- a/routes/inbox.js +++ b/routes/inbox.js @@ -18,9 +18,9 @@ router.post('/', utils.validators.activity, function (req, res) { router.get('/', function (req, res) { const db = req.app.get('db'); db.collection('streams') - .find({_target: req.user}) + .find({'_meta._target': utils.usernameToIRI(req.user)}) .sort({_id: -1}) - .project({_id: 0, _target: 0, _meta: 0, '@context': 0, 'object._id': 0, 'object.@context': 0, 'objecct._meta': 0}) + .project({_id: 0, _meta: 0, '@context': 0, 'object._id': 0, 'object.@context': 0, 'object._meta': 0}) .toArray() .then(stream => res.json(utils.arrayToCollection(stream, true))) .catch(err => { diff --git a/routes/outbox.js b/routes/outbox.js index f7ceb6d..6eb81c3 100644 --- a/routes/outbox.js +++ b/routes/outbox.js @@ -17,9 +17,9 @@ router.post('/', utils.validators.outboxActivity, function (req, res) { router.get('/', function (req, res) { const db = req.app.get('db'); db.collection('streams') - .find({actor: utils.userNameToIRI(req.user)}) + .find({actor: utils.usernameToIRI(req.user)}) .sort({_id: -1}) - .project({_id: 0, _target: 0, _meta: 0, 'object._id': 0, 'object.@context': 0, 'object._meta': 0}) + .project({_id: 0, _meta: 0, 'object._id': 0, 'object.@context': 0, 'object._meta': 0}) .toArray() .then(stream => res.json(utils.arrayToCollection(stream, true))) .catch(err => { diff --git a/routes/user.js b/routes/user.js index 11e9ba2..6f76119 100644 --- a/routes/user.js +++ b/routes/user.js @@ -28,8 +28,7 @@ router.get('/:name/followers', function (req, res) { db.collection('streams') .find({ type: 'Follow', - _target: name, - 'object.id': utils.usernameToIRI(name) + '_meta._target': utils.usernameToIRI(name), }) .project({_id: 0, actor: 1}) .toArray() diff --git a/utils/index.js b/utils/index.js index 78041b5..3d85cd4 100644 --- a/utils/index.js +++ b/utils/index.js @@ -70,10 +70,7 @@ function createLocalActor (name, type) { "preferredUsername": name, "name": "Dummy Person", "summary": "Gotta have someone in the db", - "icon": `http://${config.DOMAIN}/f/${name}.png`, - attachment: [ - `http://${config.DOMAIN}/f/${name}.glb` - ], + "icon": `https://${config.DOMAIN}/f/${name}.png`, publicKey: { 'id': `${actorBase}#main-key`, 'owner': `${actorBase}`, diff --git a/utils/validators.js b/utils/validators.js index 30fb976..3ce8014 100644 --- a/utils/validators.js +++ b/utils/validators.js @@ -33,7 +33,7 @@ module.exports.outboxActivity = function outboxActivity (req, res, next) { _id: newID, '@context': ASContext, type: 'Create', - id: `http://${req.app.get('domain')}/o/${newID.toHexString()}`, + id: `https://${req.app.get('domain')}/o/${newID.toHexString()}`, actor: req.body.attributedTo, object: req.body, published: new Date().toISOString(),