-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.mjs
156 lines (147 loc) · 5.33 KB
/
server.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { createRequestHandler } from '@remix-run/express'
import AppServer from '@twake/matrix-application-server'
import TomServer from '@twake/server'
import express from 'express'
import path from 'node:path'
import { fileURLToPath } from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const appServerConf = {
base_url: process.env.BASE_URL,
sender_localpart: process.env.SENDER_LOCALPART,
registration_file_path: process.env.REGISTRATION_FILE_PATH,
namespaces: process.env.NAMESPACES,
push_ephemeral: process.env.PUSH_EPHEMERAL || true
}
let conf = {
...appServerConf,
additional_features: process.env.ADDITIONAL_FEATURES || false,
cron_service: process.env.CRON_SERVICE ?? true,
database_engine: process.env.DATABASE_ENGINE,
database_host: process.env.DATABASE_HOST,
database_name: process.env.DATABASE_NAME,
database_user: process.env.DATABASE_USER,
database_ssl: process.env.DATABASE_SSL
? JSON.parse(process.env.DATABASE_SSL)
: false,
database_password: process.env.DATABASE_PASSWORD,
federated_identity_services: process.env.FEDERATED_IDENTITY_SERVICES
? process.env.FEDERATED_IDENTITY_SERVICES.split(/[,\s]+/)
: [],
hashes_rate_limit: process.env.HASHES_RATE_LIMIT,
is_federated_identity_service: false,
jitsiBaseUrl: process.env.JITSI_BASE_URL,
jitsiJwtAlgorithm: process.env.JITSI_JWT_ALGORITHM,
jitsiJwtIssuer: process.env.JITSI_JWT_ISSUER,
jitsiJwtSecret: process.env.JITSI_SECRET,
jitsiPreferredDomain: process.env.JITSI_PREFERRED_DOMAIN,
jitsiUseJwt: Boolean(process.env.JITSI_USE_JWT),
ldap_base: process.env.LDAP_BASE,
ldap_filter: process.env.LDAP_FILTER,
ldap_user: process.env.LDAP_USER,
ldap_password: process.env.LDAP_PASSWORD,
ldap_uri: process.env.LDAP_URI,
matrix_server: process.env.MATRIX_SERVER,
matrix_database_engine: process.env.MATRIX_DATABASE_ENGINE,
matrix_database_host: process.env.MATRIX_DATABASE_HOST,
matrix_database_name: process.env.MATRIX_DATABASE_NAME,
matrix_database_password: process.env.MATRIX_DATABASE_PASSWORD,
matrix_database_user: process.env.MATRIX_DATABASE_USER,
matrix_database_ssl: process.env.MATRIX_DATABASE_SSL
? JSON.parse(process.env.MATRIX_DATABASE_SSL)
: false,
oidc_issuer: process.env.OIDC_ISSUER,
opensearch_ca_cert_path: process.env.OPENSEARCH_CA_CERT_PATH,
opensearch_host: process.env.OPENSEARCH_HOST,
opensearch_is_activated: process.env.OPENSEARCH_IS_ACTIVATED || false,
opensearch_max_retries: +process.env.OPENSEARCH_MAX_RETRIES || null,
opensearch_number_of_shards: +process.env.OPENSEARCH_NUMBER_OF_SHARDS || null,
opensearch_number_of_replicas:
+process.env.OPENSEARCH_NUMBER_OF_REPLICAS || null,
opensearch_password: process.env.OPENSEARCH_PASSWORD,
opensearch_ssl: process.env.OPENSEARCH_SSL || false,
opensearch_user: process.env.OPENSEARCH_USER,
opensearch_wait_for_active_shards:
process.env.OPENSEARCH_WAIT_FOR_ACTIVE_SHARDS,
pepperCron: process.env.PEPPER_CRON || '9 1 * * *',
rate_limiting_window: process.env.RATE_LIMITING_WINDOW || 600000,
rate_limiting_nb_requests: process.env.RATE_LIMITING_NB_REQUESTS || 100,
server_name: process.env.SERVER_NAME,
template_dir:
process.env.TEMPLATE_DIR ||
path.join(__dirname, 'node_modules', '@twake', 'server', 'templates'),
update_federated_identity_hashes_cron:
process.env.UPDATE_FEDERATED_IDENTITY_HASHES_CRON || '*/10 * * * *',
update_users_cron: process.env.UPDATE_USERS_CRON || '*/10 * * * *',
userdb_engine: 'ldap',
sms_api_key: process.env.SMS_API_KEY,
sms_api_login: process.env.SMS_API_LOGIN,
sms_api_url: process.env.SMS_API_URL,
qr_code_url: process.env.QRCODE_URL ?? 'twake.chat://login'
}
if (process.argv[2] === 'generate') {
// eslint-disable-next-line no-unused-vars
const appServer = new AppServer(appServerConf)
} else {
const app = express()
const trustProxy = process.env.TRUSTED_PROXIES
? process.env.TRUSTED_PROXIES.split(/\s+/)
: []
if (trustProxy.length > 0) {
conf.trust_x_forwarded_for = true
app.set('trust proxy', ...trustProxy)
}
const tomServer = new TomServer(conf)
const promises = [tomServer.ready]
if (process.env.CROWDSEC_URI) {
if (!process.env.CROWDSEC_KEY) {
throw new Error('Missing CROWDSEC_KEY')
}
promises.push(
new Promise((resolve, reject) => {
import('@crowdsec/express-bouncer')
.then((m) =>
m.default({
url: process.env.CROWDSEC_URI,
apiKey: process.env.CROWDSEC_KEY
})
)
.then((crowdsecMiddleware) => {
app.use(crowdsecMiddleware)
resolve()
})
.catch(reject)
})
)
}
app.use(
'/build',
express.static(path.join(process.cwd(), 'landing', 'public', 'build'), {
immutable: true,
maxAge: '1y'
})
)
app.use(
express.static(path.join(process.cwd(), 'landing', 'public'), {
maxAge: '1h'
})
)
app.get(
'/',
createRequestHandler({
build: await import(
path.join(process.cwd(), 'landing', 'build', 'index.js')
)
})
)
Promise.all(promises)
.then(() => {
app.use(tomServer.endpoints)
const port = process.argv[2] != null ? parseInt(process.argv[2]) : 3000
console.log(`Listening on port ${port}`)
app.listen(port)
})
.catch((e) => {
throw e
})
}