switch to https, place all object metadata in _meta for simpler projections

This commit is contained in:
Will Murphy 2019-09-19 20:55:32 -05:00
parent e4c150b840
commit 06d6f85960
8 changed files with 42 additions and 50 deletions

View file

@ -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({

View file

@ -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'));
});
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)

View file

@ -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"

View file

@ -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 => {

View file

@ -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 => {

View file

@ -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()

View file

@ -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}`,

View file

@ -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(),