Todas las comprobaciones han sido exitosas
continuous-integration/drone Build is passing
Signed-off-by: ale <ale@manalejandro.com>
158 líneas
5.7 KiB
JavaScript
158 líneas
5.7 KiB
JavaScript
const http = require('http'),
|
|
express = require('express'),
|
|
rateLimit = require('express-rate-limit'),
|
|
app = express(),
|
|
events = require('events'),
|
|
constant = require('../constant'),
|
|
logger = require('./logger'),
|
|
{ routes, apex } = require('./apex'),
|
|
apexcustom = require('./apexcustom'),
|
|
apiswagger = require('./apiswagger'),
|
|
api = require('./api'),
|
|
swagger = require('./swagger'),
|
|
fediblock = require('./fediblock'),
|
|
taskdeletedup = require('./taskdeletedup'),
|
|
{ generateKeyPairSync } = require('crypto'),
|
|
{ MongoClient } = require('mongodb'),
|
|
mongoclient = new MongoClient(constant.dburl),
|
|
{ Client } = require('@elastic/elasticsearch'),
|
|
client = new Client({
|
|
node: constant.elasticnode,
|
|
pingTimeout: 10000,
|
|
requestTimeout: 60000,
|
|
retryOnTimeout: true,
|
|
maxRetries: 3
|
|
}),
|
|
register = async () => {
|
|
const admin = await apex.store.getObject(apex.utils.usernameToIRI('admin'), true)
|
|
if (!admin) {
|
|
const adminaccount = await apex.createActor('admin', 'Fediblock Admin', 'Fediblock Admin - https://' + constant.apexdomain,
|
|
{ type: 'Image', mediaType: 'image/png', url: constant.icon }, 'Service'),
|
|
keys = generateKeyPairSync('rsa', {
|
|
modulusLength: 2048,
|
|
publicKeyEncoding: {
|
|
type: 'spki',
|
|
format: 'pem'
|
|
},
|
|
privateKeyEncoding: {
|
|
type: 'pkcs8',
|
|
format: 'pem'
|
|
}
|
|
})
|
|
adminaccount.publicKey[0].publicKeyPem[0] = keys.publicKey
|
|
adminaccount._meta.privateKey = keys.privateKey
|
|
await apex.store.saveObject(adminaccount)
|
|
const bot = await apex.createActor(constant.nick, 'Fediblock Bot', 'Fediblock #Bot - https://' + constant.apexdomain,
|
|
{ type: 'Image', mediaType: 'image/png', url: constant.icon }, 'Person'),
|
|
keysbot = generateKeyPairSync('rsa', {
|
|
modulusLength: 2048,
|
|
publicKeyEncoding: {
|
|
type: 'spki',
|
|
format: 'pem'
|
|
},
|
|
privateKeyEncoding: {
|
|
type: 'pkcs8',
|
|
format: 'pem'
|
|
}
|
|
})
|
|
bot.publicKey[0].publicKeyPem[0] = keysbot.publicKey
|
|
bot._meta.privateKey = keysbot.privateKey
|
|
await apex.store.saveObject(bot)
|
|
apex.systemUser = adminaccount
|
|
apex.store.setup(adminaccount)
|
|
} else {
|
|
apex.systemUser = admin
|
|
}
|
|
},
|
|
connect = () => {
|
|
mongoclient.connect()
|
|
.then(async () => {
|
|
const exists = await client.indices.exists({ index: constant.index })
|
|
if (!exists) {
|
|
await client.indices.create({
|
|
index: constant.index,
|
|
body: require('../fediblock-mapping.json')
|
|
})
|
|
}
|
|
apex.store.db = mongoclient.db(constant.dbname)
|
|
return apex.store.db
|
|
})
|
|
.then(async () => {
|
|
try {
|
|
await register()
|
|
taskdeletedup(client)
|
|
await apex.startDelivery()
|
|
await fediblock(client, app)
|
|
} catch (e) {
|
|
console.error(e)
|
|
}
|
|
})
|
|
},
|
|
server = http.createServer(app).listen(4000, () => {
|
|
connect()
|
|
})
|
|
|
|
app.locals.scan = new events.EventEmitter()
|
|
app.locals.scannum = 0
|
|
app.locals.scantotal = 0
|
|
app.locals.server = ''
|
|
app.locals.peers = 0
|
|
app.locals.instances = 0
|
|
app.locals.created = 0
|
|
app.locals.updated = 0
|
|
app.disable('x-powered-by')
|
|
app.set('json spaces', 2)
|
|
app.set('trust proxy', 1)
|
|
app.use(logger)
|
|
app.use(rateLimit({
|
|
windowMs: 1 * 60 * 1000, // 1 minutes
|
|
limit: 120, // each IP can make up to 120 requests per `windowsMs` (5 minutes)
|
|
standardHeaders: true, // add the `RateLimit-*` headers to the response
|
|
legacyHeaders: false,
|
|
delayAfter: 30, // allow 30 requests per `windowMs` (5 minutes) without slowing them down
|
|
delayMs: (hits) => hits * 200, // add 200 ms of delay to every request after the 10th
|
|
maxDelayMs: 5000
|
|
}))
|
|
app.use(
|
|
express.json({ type: apex.consts.jsonldTypes }),
|
|
express.urlencoded({ extended: true }),
|
|
apex
|
|
)
|
|
app.route(routes.inbox)
|
|
.get(apex.net.inbox.get)
|
|
.post(apex.net.inbox.post)
|
|
app.route(routes.outbox)
|
|
.get(apex.net.outbox.get)
|
|
.post(apex.net.outbox.post)
|
|
app.get(routes.actor, apex.net.actor.get)
|
|
app.get(routes.followers, apex.net.followers.get)
|
|
app.get(routes.following, apex.net.following.get)
|
|
app.get(routes.liked, apex.net.liked.get)
|
|
app.get(routes.object, apex.net.object.get)
|
|
app.get(routes.activity, apex.net.activityStream.get)
|
|
app.get(routes.shares, apex.net.shares.get)
|
|
app.get(routes.likes, apex.net.likes.get)
|
|
app.get('/.well-known/webfinger', apex.net.webfinger.get)
|
|
app.get('/.well-known/nodeinfo', apex.net.nodeInfoLocation.get)
|
|
app.get('/nodeinfo/:version', apex.net.nodeInfo.get)
|
|
app.post('/proxy', apex.net.proxy.post)
|
|
apexcustom(app, apex, client)
|
|
api(app, apex, client)
|
|
apiswagger(app, client)
|
|
swagger(app)
|
|
app.get('/u/' + constant.nick, (req, res) => {
|
|
res.redirect('/api/inbox')
|
|
})
|
|
app.get(Object.keys(routes).map(route => routes[route]), (req, res) => {
|
|
res.redirect('/')
|
|
})
|
|
app.use(express.static(__dirname + '/../../front/build'))
|
|
.use((err, req, res, next) => {
|
|
if (res.headersSent) {
|
|
console.error(err.stack)
|
|
return next(err)
|
|
}
|
|
res.status(500).end('Error')
|
|
})
|
|
|
|
module.exports = server |