2021-04-30 14:22:58 +00:00
|
|
|
import type { ProsodyFilePaths } from './paths'
|
|
|
|
|
|
|
|
type ConfigEntryValue = boolean | number | string | ConfigEntryValue[]
|
|
|
|
|
2021-05-04 14:43:38 +00:00
|
|
|
type ConfigEntries = Map<string, ConfigEntryValue>
|
2021-04-30 14:22:58 +00:00
|
|
|
|
|
|
|
function writeValue (value: ConfigEntryValue): string {
|
|
|
|
if (typeof value === 'boolean') {
|
|
|
|
return value.toString() + ';\n'
|
|
|
|
}
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
return '"' + value.replace(/"/g, '\\"') + '"' + ';\n'
|
|
|
|
}
|
|
|
|
if (typeof value === 'number') {
|
|
|
|
return value.toString() + ';\n'
|
|
|
|
}
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
let s = '{\n'
|
|
|
|
for (let i = 0; i < value.length; i++) {
|
|
|
|
s += ' ' + writeValue(value[i])
|
|
|
|
}
|
|
|
|
s += '};\n'
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
throw new Error(`Don't know how to handle this value: ${value as string}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class ProsodyConfigBlock {
|
|
|
|
entries: ConfigEntries
|
|
|
|
prefix: string
|
|
|
|
|
|
|
|
constructor (prefix: string) {
|
|
|
|
this.prefix = prefix
|
2021-05-04 14:43:38 +00:00
|
|
|
this.entries = new Map()
|
2021-04-30 14:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set (name: string, value: ConfigEntryValue): void {
|
2021-05-04 14:43:38 +00:00
|
|
|
this.entries.set(name, value)
|
2021-04-30 14:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
add (name: string, value: ConfigEntryValue): void {
|
2021-05-04 14:43:38 +00:00
|
|
|
if (!this.entries.has(name)) {
|
|
|
|
this.entries.set(name, [])
|
2021-04-30 14:22:58 +00:00
|
|
|
}
|
2021-05-04 14:43:38 +00:00
|
|
|
let entry = this.entries.get(name) as ConfigEntryValue
|
2021-04-30 14:22:58 +00:00
|
|
|
if (!Array.isArray(entry)) {
|
|
|
|
entry = [entry]
|
|
|
|
}
|
|
|
|
entry.push(value)
|
2021-05-04 14:43:38 +00:00
|
|
|
this.entries.set(name, entry)
|
2021-04-30 14:22:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
write (): string {
|
|
|
|
let content = ''
|
2021-05-04 14:43:38 +00:00
|
|
|
// Map keeps order :)
|
|
|
|
this.entries.forEach((value, key) => {
|
|
|
|
content += this.prefix + key + ' = ' + writeValue(value)
|
2021-04-30 14:22:58 +00:00
|
|
|
})
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ProsodyConfigGlobal extends ProsodyConfigBlock {
|
|
|
|
constructor () {
|
|
|
|
super('')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ProsodyConfigVirtualHost extends ProsodyConfigBlock {
|
|
|
|
name: string
|
|
|
|
|
|
|
|
constructor (name: string) {
|
|
|
|
super(' ')
|
|
|
|
this.name = name
|
|
|
|
}
|
|
|
|
|
|
|
|
write (): string {
|
|
|
|
return `VirtualHost "${this.name}"\n` + super.write()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ProsodyConfigComponent extends ProsodyConfigBlock {
|
|
|
|
name: string
|
|
|
|
type: string
|
|
|
|
|
|
|
|
constructor (type: string, name: string) {
|
|
|
|
super(' ')
|
|
|
|
this.type = type
|
|
|
|
this.name = name
|
|
|
|
}
|
|
|
|
|
|
|
|
write (): string {
|
|
|
|
return `Component "${this.name}" "${this.type}"\n` + super.write()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 09:48:38 +00:00
|
|
|
type ProsodyLogLevel = 'debug' | 'info' | 'warn' | 'error'
|
2021-04-30 14:22:58 +00:00
|
|
|
|
|
|
|
class ProsodyConfigContent {
|
|
|
|
paths: ProsodyFilePaths
|
|
|
|
global: ProsodyConfigGlobal
|
2021-05-03 18:37:23 +00:00
|
|
|
authenticated?: ProsodyConfigVirtualHost
|
2021-04-30 14:22:58 +00:00
|
|
|
anon: ProsodyConfigVirtualHost
|
|
|
|
muc: ProsodyConfigComponent
|
|
|
|
log: string
|
2021-05-06 11:31:55 +00:00
|
|
|
prosodyDomain: string
|
2021-04-30 14:22:58 +00:00
|
|
|
|
2021-05-06 11:31:55 +00:00
|
|
|
constructor (paths: ProsodyFilePaths, prosodyDomain: string) {
|
2021-04-30 14:22:58 +00:00
|
|
|
this.paths = paths
|
|
|
|
this.global = new ProsodyConfigGlobal()
|
|
|
|
this.log = ''
|
2021-05-06 11:31:55 +00:00
|
|
|
this.prosodyDomain = prosodyDomain
|
|
|
|
this.anon = new ProsodyConfigVirtualHost('anon.' + prosodyDomain)
|
|
|
|
this.muc = new ProsodyConfigComponent('muc', 'room.' + prosodyDomain)
|
2021-04-30 14:22:58 +00:00
|
|
|
|
|
|
|
this.global.set('daemonize', false)
|
|
|
|
this.global.set('allow_registration', false)
|
|
|
|
this.global.set('admins', [])
|
|
|
|
|
|
|
|
this.global.set('pidfile', this.paths.pid)
|
|
|
|
this.global.set('plugin_paths', [this.paths.modules])
|
|
|
|
this.global.set('data_path', this.paths.data)
|
2021-05-11 14:49:49 +00:00
|
|
|
this.global.set('default_storage', 'internal')
|
2021-04-30 16:00:21 +00:00
|
|
|
this.global.set('storage', 'internal')
|
2021-04-30 14:22:58 +00:00
|
|
|
|
|
|
|
this.global.set('modules_enabled', [
|
|
|
|
'roster', // Allow users to have a roster. Recommended ;)
|
|
|
|
'saslauth', // Authentication for clients and servers. Recommended if you want to log in.
|
2021-05-04 11:16:59 +00:00
|
|
|
'carbons', // Keep multiple clients in sync
|
2021-04-30 14:22:58 +00:00
|
|
|
'version', // Replies to server version requests
|
|
|
|
'uptime', // Report how long server has been running
|
|
|
|
'ping', // Replies to XMPP pings with pongs
|
|
|
|
'bosh', // Enable BOSH clients, aka "Jabber over HTTP"
|
|
|
|
'posix' // POSIX functionality, sends server to background, enables syslog, etc.
|
|
|
|
])
|
|
|
|
this.global.set('modules_disabled', [
|
|
|
|
// 'offline' // Store offline messages
|
|
|
|
// 'c2s' // Handle client connections
|
|
|
|
's2s' // Handle server-to-server connections
|
|
|
|
])
|
|
|
|
|
|
|
|
this.global.set('cross_domain_bosh', false)
|
|
|
|
this.global.set('consider_bosh_secure', false)
|
|
|
|
this.global.set('cross_domain_websocket', false)
|
|
|
|
this.global.set('consider_websocket_secure', false)
|
|
|
|
|
|
|
|
this.anon.set('authentication', 'anonymous')
|
|
|
|
this.anon.set('modules_enabled', ['ping'])
|
|
|
|
|
|
|
|
this.muc.set('muc_room_locking', false)
|
|
|
|
this.muc.set('muc_tombstones', false)
|
|
|
|
this.muc.set('muc_room_default_language', 'en')
|
2021-05-02 12:35:58 +00:00
|
|
|
this.muc.set('muc_room_default_public', false)
|
2021-04-30 14:22:58 +00:00
|
|
|
this.muc.set('muc_room_default_persistent', false)
|
|
|
|
this.muc.set('muc_room_default_members_only', false)
|
|
|
|
this.muc.set('muc_room_default_moderated', false)
|
|
|
|
this.muc.set('muc_room_default_public_jids', false)
|
|
|
|
this.muc.set('muc_room_default_change_subject', false)
|
|
|
|
this.muc.set('muc_room_default_history_length', 20)
|
|
|
|
}
|
|
|
|
|
2021-05-03 18:37:23 +00:00
|
|
|
useHttpAuthentication (url: string): void {
|
2021-05-06 11:31:55 +00:00
|
|
|
this.authenticated = new ProsodyConfigVirtualHost(this.prosodyDomain)
|
2021-05-03 18:37:23 +00:00
|
|
|
|
|
|
|
this.authenticated.set('authentication', 'http')
|
|
|
|
this.authenticated.set('modules_enabled', ['ping', 'auth_http'])
|
|
|
|
|
|
|
|
this.authenticated.set('http_auth_url', url)
|
|
|
|
}
|
|
|
|
|
2021-05-06 11:31:55 +00:00
|
|
|
usePeertubeBosh (prosodyDomain: string, port: string): void {
|
2021-04-30 14:22:58 +00:00
|
|
|
this.global.set('c2s_require_encryption', false)
|
|
|
|
this.global.set('interfaces', ['127.0.0.1', '::1'])
|
|
|
|
this.global.set('c2s_ports', [])
|
|
|
|
this.global.set('c2s_interfaces', ['127.0.0.1', '::1'])
|
|
|
|
this.global.set('s2s_ports', [])
|
|
|
|
this.global.set('s2s_interfaces', ['127.0.0.1', '::1'])
|
|
|
|
this.global.set('http_ports', [port])
|
|
|
|
this.global.set('http_interfaces', ['127.0.0.1', '::1'])
|
|
|
|
this.global.set('https_ports', [])
|
|
|
|
this.global.set('https_interfaces', ['127.0.0.1', '::1'])
|
|
|
|
|
|
|
|
this.global.set('consider_bosh_secure', true)
|
|
|
|
|
|
|
|
this.anon.set('trusted_proxies', ['127.0.0.1', '::1'])
|
|
|
|
this.anon.set('allow_anonymous_s2s', false)
|
|
|
|
this.anon.add('modules_enabled', 'http')
|
|
|
|
this.anon.add('modules_enabled', 'bosh')
|
2021-05-06 11:31:55 +00:00
|
|
|
this.anon.set('http_host', prosodyDomain)
|
|
|
|
this.anon.set('http_external_url', 'http://' + prosodyDomain)
|
2021-04-30 14:22:58 +00:00
|
|
|
|
|
|
|
this.muc.set('restrict_room_creation', 'local')
|
2021-05-03 18:37:23 +00:00
|
|
|
|
|
|
|
if (this.authenticated) {
|
|
|
|
this.authenticated.set('trusted_proxies', ['127.0.0.1', '::1'])
|
|
|
|
this.authenticated.set('allow_anonymous_s2s', false)
|
|
|
|
this.authenticated.add('modules_enabled', 'http')
|
|
|
|
this.authenticated.add('modules_enabled', 'bosh')
|
2021-05-06 11:31:55 +00:00
|
|
|
this.authenticated.set('http_host', prosodyDomain)
|
|
|
|
this.authenticated.set('http_external_url', 'http://' + prosodyDomain)
|
2021-05-03 18:37:23 +00:00
|
|
|
}
|
2021-04-30 14:22:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 13:22:08 +00:00
|
|
|
useMucHttpDefault (url: string): void {
|
2021-04-30 14:22:58 +00:00
|
|
|
this.muc.add('modules_enabled', 'muc_http_defaults')
|
|
|
|
this.muc.add('muc_create_api_url', url)
|
2021-05-02 14:55:01 +00:00
|
|
|
|
|
|
|
// restrict_room_creation: we can override the 'local' value.
|
|
|
|
// Indeed, when muc_http_default is used, room creation will be managed by api.
|
|
|
|
this.muc.set('restrict_room_creation', false)
|
2021-04-30 14:22:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-11 14:10:57 +00:00
|
|
|
/**
|
|
|
|
* Calling this method makes Prosody use mod_muc_mam to store rooms history.
|
|
|
|
* @param duration: how long the server must store messages. See https://prosody.im/doc/modules/mod_muc_mam
|
|
|
|
*/
|
|
|
|
useMam (duration: string): void {
|
|
|
|
this.muc.add('modules_enabled', 'muc_mam')
|
|
|
|
|
|
|
|
this.muc.set('muc_log_by_default', true)
|
|
|
|
this.muc.set('muc_log_presences', true)
|
|
|
|
this.muc.set('log_all_rooms', true)
|
|
|
|
this.muc.set('muc_log_expires_after', duration)
|
|
|
|
this.muc.set('muc_log_cleanup_interval', 4 * 60 * 60)
|
2021-05-11 14:49:49 +00:00
|
|
|
|
|
|
|
// We can also use mod_muc_moderation
|
2021-05-11 15:31:20 +00:00
|
|
|
// NB: for now Prosody has a partial support of this feature with «internal» storage.
|
|
|
|
// It will be available in next version (0.11.8?).
|
2021-05-11 14:49:49 +00:00
|
|
|
this.muc.add('modules_enabled', 'muc_moderation')
|
2021-04-30 14:22:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 16:29:17 +00:00
|
|
|
/**
|
|
|
|
* Rooms will be persistent by default (they will not be deleted if no participant).
|
|
|
|
*/
|
|
|
|
useDefaultPersistent (): void {
|
|
|
|
this.muc.set('muc_room_default_persistent', true)
|
|
|
|
}
|
|
|
|
|
2021-06-22 10:57:24 +00:00
|
|
|
useTestModule (prosodyApikey: string, apiurl: string): void {
|
2021-06-22 08:26:45 +00:00
|
|
|
this.global.add('modules_enabled', 'http_peertubelivechat_test')
|
2021-06-22 10:57:24 +00:00
|
|
|
this.global.set('peertubelivechat_test_apikey', prosodyApikey)
|
|
|
|
this.global.set('peertubelivechat_test_peertube_api_url', apiurl)
|
2021-06-22 08:26:45 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 14:22:58 +00:00
|
|
|
setLog (level: ProsodyLogLevel, syslog?: ProsodyLogLevel[]): void {
|
|
|
|
let log = ''
|
|
|
|
log += 'log = {\n'
|
2021-05-12 09:48:38 +00:00
|
|
|
if (level !== 'error') {
|
|
|
|
log += ' ' + level + ' = ' + writeValue(this.paths.log)
|
|
|
|
}
|
|
|
|
// always log error level in a separate file.
|
2021-04-30 14:22:58 +00:00
|
|
|
log += ' error = ' + writeValue(this.paths.error)
|
|
|
|
if (syslog) {
|
|
|
|
log += ' { to = "syslog"; levels = ' + writeValue(syslog) + ' };\n'
|
|
|
|
}
|
|
|
|
log += '\n};\n'
|
|
|
|
this.log = log
|
|
|
|
}
|
|
|
|
|
|
|
|
public write (): string {
|
|
|
|
let content = ''
|
|
|
|
content += this.global.write()
|
|
|
|
content += this.log + '\n'
|
|
|
|
content += '\n\n'
|
2021-05-03 18:37:23 +00:00
|
|
|
if (this.authenticated) {
|
|
|
|
content += this.authenticated.write()
|
|
|
|
content += '\n\n'
|
|
|
|
}
|
2021-04-30 14:22:58 +00:00
|
|
|
content += this.anon.write()
|
|
|
|
content += '\n\n'
|
|
|
|
content += this.muc.write()
|
|
|
|
content += '\n\n'
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
2021-05-12 09:48:38 +00:00
|
|
|
ProsodyLogLevel,
|
2021-04-30 14:22:58 +00:00
|
|
|
ProsodyConfigContent
|
|
|
|
}
|