switch to https, place all object metadata in _meta for simpler projections
This commit is contained in:
parent
e4c150b840
commit
06d6f85960
8 changed files with 42 additions and 50 deletions
11
db/setup.js
11
db/setup.js
|
@ -4,8 +4,17 @@ const crypto = require('crypto')
|
||||||
module.exports = async function dbSetup (db, domain) {
|
module.exports = async function dbSetup (db, domain) {
|
||||||
// inbox
|
// inbox
|
||||||
await db.collection('streams').createIndex({
|
await db.collection('streams').createIndex({
|
||||||
_target: 1,
|
'_meta._target': 1,
|
||||||
_id: -1,
|
_id: -1,
|
||||||
|
}, {
|
||||||
|
name: 'inbox'
|
||||||
|
})
|
||||||
|
// followers
|
||||||
|
await db.collection('streams').createIndex({
|
||||||
|
'_meta._target': 1,
|
||||||
|
}, {
|
||||||
|
partialFilterExpression: {type: 'Follow'},
|
||||||
|
name: 'followers'
|
||||||
})
|
})
|
||||||
// outbox
|
// outbox
|
||||||
await db.collection('streams').createIndex({
|
await db.collection('streams').createIndex({
|
||||||
|
|
55
index.js
55
index.js
|
@ -1,9 +1,19 @@
|
||||||
const { promisify } = require('util')
|
const { promisify } = require('util')
|
||||||
const config = require('./config.json');
|
const path = require('path')
|
||||||
const { USER, PASS, DOMAIN, PRIVKEY_PATH, CERT_PATH, PORT } = config;
|
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const app = express();
|
|
||||||
const MongoClient = require('mongodb').MongoClient;
|
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
|
// Connection URL
|
||||||
const url = 'mongodb://localhost:27017';
|
const url = 'mongodb://localhost:27017';
|
||||||
|
|
||||||
|
@ -16,32 +26,18 @@ const client = new MongoClient(url, {useUnifiedTopology: true});
|
||||||
|
|
||||||
let db;
|
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;
|
let sslOptions;
|
||||||
|
|
||||||
try {
|
|
||||||
sslOptions = {
|
sslOptions = {
|
||||||
key: fs.readFileSync(PRIVKEY_PATH),
|
key: fs.readFileSync(path.join(__dirname, KEY_PATH)),
|
||||||
cert: fs.readFileSync(CERT_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('domain', DOMAIN);
|
||||||
app.set('port', process.env.PORT || PORT || 3000);
|
app.set('port', process.env.PORT || PORT);
|
||||||
app.set('port-https', process.env.PORT_HTTPS || 8443);
|
app.set('port-https', process.env.PORT_HTTPS || PORT_HTTPS);
|
||||||
app.use(bodyParser.json({type: [
|
app.use(bodyParser.json({type: [
|
||||||
'application/activity+json',
|
'application/activity+json',
|
||||||
'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
|
'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('/.well-known/webfinger', cors(), routes.webfinger);
|
||||||
app.use('/u', cors(), routes.user);
|
app.use('/u', cors(), routes.user);
|
||||||
app.use('/m', cors(), routes.message);
|
app.use('/m', cors(), routes.message);
|
||||||
// app.use('/api/inbox', cors(), routes.inbox);
|
|
||||||
app.use('/u/:name/inbox', routes.inbox)
|
app.use('/u/:name/inbox', routes.inbox)
|
||||||
app.use('/u/:name/outbox', routes.outbox)
|
app.use('/u/:name/outbox', routes.outbox)
|
||||||
app.use('/admin', express.static('public/admin'));
|
app.use('/admin', express.static('public/admin'));
|
||||||
app.use('/f', express.static('public/files'));
|
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
|
// Use connect method to connect to the Server
|
||||||
let objs
|
|
||||||
client.connect({useNewUrlParser: true})
|
client.connect({useNewUrlParser: true})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log("Connected successfully to server");
|
console.log("Connected successfully to server");
|
||||||
db = client.db(dbName);
|
db = client.db(dbName);
|
||||||
app.set('db', db);
|
app.set('db', db);
|
||||||
objs = db.collection('objects');
|
|
||||||
app.set('objs', db.collection('objects'));
|
|
||||||
|
|
||||||
return dbSetup(db, DOMAIN)
|
return dbSetup(db, DOMAIN)
|
||||||
})
|
})
|
||||||
|
|
||||||
.then(() => {
|
.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 () {
|
https.createServer(sslOptions, app).listen(app.get('port-https'), function () {
|
||||||
console.log('Express server listening on port ' + app.get('port-https'));
|
console.log('Express server listening on port ' + app.get('port-https'));
|
||||||
});
|
});
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
throw new Error(err)
|
throw new Error(err)
|
||||||
|
|
|
@ -8,8 +8,10 @@
|
||||||
"cors": "^2.8.4",
|
"cors": "^2.8.4",
|
||||||
"express": "^4.16.3",
|
"express": "^4.16.3",
|
||||||
"express-basic-auth": "^1.1.5",
|
"express-basic-auth": "^1.1.5",
|
||||||
|
"http-signature": "^1.2.0",
|
||||||
"mongodb": "^3.3.2",
|
"mongodb": "^3.3.2",
|
||||||
"request": "^2.87.0"
|
"request": "^2.88.0",
|
||||||
|
"request-promise-native": "^1.0.7"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=10.10.0"
|
"node": ">=10.10.0"
|
||||||
|
|
|
@ -18,9 +18,9 @@ router.post('/', utils.validators.activity, function (req, res) {
|
||||||
router.get('/', function (req, res) {
|
router.get('/', function (req, res) {
|
||||||
const db = req.app.get('db');
|
const db = req.app.get('db');
|
||||||
db.collection('streams')
|
db.collection('streams')
|
||||||
.find({_target: req.user})
|
.find({'_meta._target': utils.usernameToIRI(req.user)})
|
||||||
.sort({_id: -1})
|
.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()
|
.toArray()
|
||||||
.then(stream => res.json(utils.arrayToCollection(stream, true)))
|
.then(stream => res.json(utils.arrayToCollection(stream, true)))
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
|
|
|
@ -17,9 +17,9 @@ router.post('/', utils.validators.outboxActivity, function (req, res) {
|
||||||
router.get('/', function (req, res) {
|
router.get('/', function (req, res) {
|
||||||
const db = req.app.get('db');
|
const db = req.app.get('db');
|
||||||
db.collection('streams')
|
db.collection('streams')
|
||||||
.find({actor: utils.userNameToIRI(req.user)})
|
.find({actor: utils.usernameToIRI(req.user)})
|
||||||
.sort({_id: -1})
|
.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()
|
.toArray()
|
||||||
.then(stream => res.json(utils.arrayToCollection(stream, true)))
|
.then(stream => res.json(utils.arrayToCollection(stream, true)))
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
|
|
|
@ -28,8 +28,7 @@ router.get('/:name/followers', function (req, res) {
|
||||||
db.collection('streams')
|
db.collection('streams')
|
||||||
.find({
|
.find({
|
||||||
type: 'Follow',
|
type: 'Follow',
|
||||||
_target: name,
|
'_meta._target': utils.usernameToIRI(name),
|
||||||
'object.id': utils.usernameToIRI(name)
|
|
||||||
})
|
})
|
||||||
.project({_id: 0, actor: 1})
|
.project({_id: 0, actor: 1})
|
||||||
.toArray()
|
.toArray()
|
||||||
|
|
|
@ -70,10 +70,7 @@ function createLocalActor (name, type) {
|
||||||
"preferredUsername": name,
|
"preferredUsername": name,
|
||||||
"name": "Dummy Person",
|
"name": "Dummy Person",
|
||||||
"summary": "Gotta have someone in the db",
|
"summary": "Gotta have someone in the db",
|
||||||
"icon": `http://${config.DOMAIN}/f/${name}.png`,
|
"icon": `https://${config.DOMAIN}/f/${name}.png`,
|
||||||
attachment: [
|
|
||||||
`http://${config.DOMAIN}/f/${name}.glb`
|
|
||||||
],
|
|
||||||
publicKey: {
|
publicKey: {
|
||||||
'id': `${actorBase}#main-key`,
|
'id': `${actorBase}#main-key`,
|
||||||
'owner': `${actorBase}`,
|
'owner': `${actorBase}`,
|
||||||
|
|
|
@ -33,7 +33,7 @@ module.exports.outboxActivity = function outboxActivity (req, res, next) {
|
||||||
_id: newID,
|
_id: newID,
|
||||||
'@context': ASContext,
|
'@context': ASContext,
|
||||||
type: 'Create',
|
type: 'Create',
|
||||||
id: `http://${req.app.get('domain')}/o/${newID.toHexString()}`,
|
id: `https://${req.app.get('domain')}/o/${newID.toHexString()}`,
|
||||||
actor: req.body.attributedTo,
|
actor: req.body.attributedTo,
|
||||||
object: req.body,
|
object: req.body,
|
||||||
published: new Date().toISOString(),
|
published: new Date().toISOString(),
|
||||||
|
|
Loading…
Reference in a new issue