fediblock-instance/server.js

154 lines
6.0 KiB
JavaScript
Raw Normal View History

2024-09-26 00:23:49 +00:00
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0
2024-09-15 17:44:53 +00:00
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'),
2024-10-14 18:13:12 +00:00
taskdeletedup = require('./lib/taskdeletedup'),
2024-09-15 17:44:53 +00:00
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'
},
2024-10-20 20:48:17 +00:00
itemsPerPage: 150
2024-09-15 17:44:53 +00:00
}),
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
}
2024-10-14 18:13:12 +00:00
taskdeletedup(client)
2024-09-15 17:44:53 +00:00
await apex.startDelivery()
console.log('Server listening on ' + server.address().address + ':' + server.address().port)
2024-10-14 19:29:58 +00:00
await fediblock(client, apex, app)
2024-09-15 17:44:53 +00:00
} 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'))
2024-09-30 02:10:42 +00:00
.use('/u/' + constant.nick, (req, res) => {
res.redirect('/api/inbox')
}).use(Object.keys(routes).map(route => routes[route]), (req, res) => {
2024-09-30 01:27:40 +00:00
res.redirect('/')
}).use((err, req, res, next) => {
2024-09-15 17:44:53 +00:00
if (res.headersSent) {
console.error(err.stack)
return next(err)
}
res.status(500).end('Error')
})