-
Notifications
You must be signed in to change notification settings - Fork 234
Expand file tree
/
Copy pathapp.ts
More file actions
172 lines (146 loc) · 6.24 KB
/
Copy pathapp.ts
File metadata and controls
172 lines (146 loc) · 6.24 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { Cluster, Worker } from 'cluster'
import { cpus, hostname } from 'os'
import { path, pathEq } from 'ramda'
import { FSWatcher } from 'fs'
import { addOnion } from '../tor/client'
import { createLogger } from '../factories/logger-factory'
import { IRunnable } from '../@types/base'
import packageJson from '../../package.json'
import { Serializable } from 'child_process'
import { Settings } from '../@types/settings'
import { SettingsStatic } from '../utils/settings'
const logger = createLogger('app-primary')
export class App implements IRunnable {
private workers: WeakMap<Worker, Record<string, string>>
private watchers: FSWatcher[] | undefined
public constructor(
private readonly process: NodeJS.Process,
private readonly cluster: Cluster,
private readonly settings: () => Settings,
) {
logger('starting')
this.workers = new WeakMap()
this.cluster.on('message', this.onClusterMessage.bind(this)).on('exit', this.onClusterExit.bind(this))
this.process.on('SIGTERM', this.onExit.bind(this))
logger('started')
}
public run(): void {
const settings = this.settings()
this.watchers = SettingsStatic.watchSettings()
logger.info(`
███▄ █ ▒█████ ██████ ▄▄▄█████▓ ██▀███ ▓█████ ▄▄▄ ███▄ ▄███▓
██ ▀█ █ ▒██▒ ██▒▒██ ▒ ▓ ██▒ ▓▒▓██ ▒ ██▒▓█ ▀▒████▄ ▓██▒▀█▀ ██▒
▓██ ▀█ ██▒▒██░ ██▒░ ▓██▄ ▒ ▓██░ ▒░▓██ ░▄█ ▒▒███ ▒██ ▀█▄ ▓██ ▓██░
▓██▒ ▐▌██▒▒██ ██░ ▒ ██▒░ ▓██▓ ░ ▒██▀▀█▄ ▒▓█ ▄░██▄▄▄▄██ ▒██ ▒██
▒██░ ▓██░░ ████▓▒░▒██████▒▒ ▒██▒ ░ ░██▓ ▒██▒░▒████▒▓█ ▓██▒▒██▒ ░██▒
░ ▒░ ▒ ▒ ░ ▒░▒░▒░ ▒ ▒▓▒ ▒ ░ ▒ ░░ ░ ▒▓ ░▒▓░░░ ▒░ ░▒▒ ▓▒█░░ ▒░ ░ ░
░ ░░ ░ ▒░ ░ ▒ ▒░ ░ ░▒ ░ ░ ░ ░▒ ░ ▒░ ░ ░ ░ ▒ ▒▒ ░░ ░ ░
░ ░ ░ ░ ░ ░ ▒ ░ ░ ░ ░ ░░ ░ ░ ░ ▒ ░ ░
░ ░ ░ ░ ░ ░ ░ ░ ░ ░`)
const width = 74
const torHiddenServicePort = process.env.HIDDEN_SERVICE_PORT ? Number(process.env.HIDDEN_SERVICE_PORT) : 80
const port = process.env.RELAY_PORT ? Number(process.env.RELAY_PORT) : 8008
const logCentered = (input: string, width: number) => {
const start = (width - input.length) >> 1
logger.info(' '.repeat(start), input)
}
logCentered(`v${packageJson.version}`, width)
logCentered(`NIPs implemented: ${packageJson.supportedNips}`, width)
const paymentsEnabled = pathEq(['payments', 'enabled'], true, settings)
logCentered(`Pay-to-relay ${paymentsEnabled ? 'enabled' : 'disabled'}`, width)
if (paymentsEnabled) {
logCentered(`Payments provider: ${path(['payments', 'processor'], settings)}`, width)
}
if (
paymentsEnabled &&
(typeof this.process.env.SECRET !== 'string' ||
this.process.env.SECRET === '' ||
this.process.env.SECRET === 'changeme')
) {
logger.error('Please configure the secret using the SECRET environment variable.')
this.process.exit(1)
}
const workerCount = this.process.env.WORKER_COUNT
? Number(this.process.env.WORKER_COUNT)
: this.settings().workers?.count || cpus().length
const createWorker = (env: Record<string, string>) => {
const worker = this.cluster.fork(env)
this.workers.set(worker, env)
}
for (let i = 0; i < workerCount; i++) {
logger('starting worker')
createWorker({
WORKER_TYPE: 'worker',
WORKER_INDEX: i.toString(),
})
}
logCentered(`${workerCount} client workers started`, width)
createWorker({
WORKER_TYPE: 'maintenance',
})
logCentered('1 maintenance worker started', width)
const mirrors = settings?.mirroring?.static
if (Array.isArray(mirrors) && mirrors.length) {
for (let i = 0; i < mirrors.length; i++) {
createWorker({
WORKER_TYPE: 'static-mirroring',
MIRROR_INDEX: i.toString(),
})
}
logCentered(`${mirrors.length} static-mirroring worker started`, width)
}
logger('settings: %O', settings)
const host = `${hostname()}:${port}`
addOnion(torHiddenServicePort, host).then(
(value) => {
logCentered(`Tor hidden service: ${value}:${torHiddenServicePort}`, width)
},
() => {
logCentered('Tor hidden service: disabled', width)
},
)
}
private onClusterMessage(source: Worker, message: Serializable) {
logger('message received from worker %s: %o', source.process.pid, message)
for (const worker of Object.values(this.cluster.workers as any) as Worker[]) {
if (source.id === worker.id) {
continue
}
logger('sending message to worker %s: %o', worker.process.pid, message)
worker.send(message)
}
}
private onClusterExit(deadWorker: Worker, code: number, signal: string) {
logger('worker %s died', deadWorker.process.pid)
if (code === 0 || signal === 'SIGINT') {
return
}
setTimeout(() => {
logger('starting worker')
const workerEnv = this.workers.get(deadWorker)
if (!workerEnv) {
throw new Error('Mistakes were made')
}
const newWorker = this.cluster.fork(workerEnv)
this.workers.set(newWorker, workerEnv)
logger('started worker %s', newWorker.process.pid)
}, 10000)
}
private onExit() {
logger.info('exiting')
this.close(() => {
this.process.exit(0)
})
}
public close(callback?: (...args: any[]) => void): void {
logger.info('close')
if (Array.isArray(this.watchers)) {
for (const watcher of this.watchers) {
watcher.close()
}
}
if (typeof callback === 'function') {
callback()
}
}
}