fediblock-instance/server.js
ale db1f3de490
All checks were successful
continuous-integration/drone/push Build is passing
rate limit
2024-11-06 01:21:38 +01:00

164 lines
6.4 KiB
JavaScript

process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0
const apexinstance = require('./lib/apex'),
apexcustom = require('./lib/apexcustom'),
apiswagger = require('./lib/apiswagger'),
api = require('./lib/api'),
logger = require('./lib/logger'),
swagger = require('./lib/swagger'),
fediblock = require('./lib/fediblock'),
taskdeletedup = require('./lib/taskdeletedup'),
constant = require('./lib/constant'),
http = require('http'),
express = require('express'),
rateLimit = require("express-rate-limit"),
app = express(),
events = require('events'),
{ 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
}),
ActivitypubExpress = require('activitypub-express'),
routes = {
actor: '/u/:actor',
object: '/o/:id',
activity: '/s/:id',
inbox: '/u/:actor/inbox',
outbox: '/u/:actor/outbox',
followers: '/u/:actor/followers',
following: '/u/:actor/following',
liked: '/u/:actor/liked',
collections: '/u/:actor/c/:id',
blocked: '/u/:actor/blocked',
rejections: '/u/:actor/rejections',
rejected: '/u/:actor/rejected',
shares: '/s/:id/shares',
likes: '/s/:id/likes'
},
apex = ActivitypubExpress({
name: 'Fediblock Instance',
version: '1.0.0',
domain: constant.apexdomain,
actorParam: 'actor',
objectParam: 'id',
activityParam: 'id',
routes,
endpoints: {
proxyUrl: 'https://' + constant.apexdomain + '/proxy'
},
itemsPerPage: 200
}),
server = http.createServer(app).listen(4000, () => {
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 {
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
}
taskdeletedup(client)
await apex.startDelivery()
console.log('Server listening on ' + server.address().address + ':' + server.address().port)
await fediblock(client, apex, app)
} catch (e) {
console.error(e)
}
})
})
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', true)
logger(app)
app.use(rateLimit({
windowMs: 5 * 60 * 1000, // 5 minutes
limit: 10, // each IP can make up to 10 requests per `windowsMs` (5 minutes)
standardHeaders: true, // add the `RateLimit-*` headers to the response
legacyHeaders: false,
delayAfter: 10, // allow 10 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
)
apexinstance(app, apex, routes)
apexcustom(app, apex, client)
api(app, apex, client)
apiswagger(app, client)
swagger(app)
app.use(express.static('dist'))
.use('/u/' + constant.nick, (req, res) => {
res.redirect('/api/inbox')
}).use(Object.keys(routes).map(route => routes[route]), (req, res) => {
res.redirect('/')
}).use((err, req, res, next) => {
if (res.headersSent) {
console.error(err.stack)
return next(err)
}
res.status(500).end('Error')
})