fediblock-instance/server.js
ale e0fae7a6b4
All checks were successful
continuous-integration/drone/tag Build is passing
initial commit
2024-09-15 19:44:53 +02:00

147 lines
5.6 KiB
JavaScript

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'),
constant = require('./lib/constant'),
http = require('http'),
express = require('express'),
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: 50
}),
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
}
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(
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((err, req, res, next) => {
if (res.headersSent) {
console.error(err.stack)
return next(err)
}
res.status(500).end('Error')
})