-
Notifications
You must be signed in to change notification settings - Fork 106
/
bind-backend-proxy.js
198 lines (184 loc) · 5.58 KB
/
bind-backend-proxy.js
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
let nanoid = require('nanoid')
let https = require('https')
let http = require('http')
const VERSION = 1
const UNKNOWN_CHANNEL = /^\[\s*\[\s*"unknownChannel"/
const UNKNOWN_ACTION = /^\[\s*\[\s*"unknownAction"/
const AUTHENTICATED = /^\[\s*\[\s*"authenticated"/
const FORBIDDEN = /^\[\s*\[\s*"forbidden"/
const APPROVED = /^\[\s*\[\s*"approved"/
const DENIED = /^\[\s*\[\s*"denied"/
const ERROR = /^\[\s*\[\s*"error"/
function parseAnswer (str) {
let json
try {
json = JSON.parse(str)
} catch (e) {
return false
}
let answered = false
for (let command of json) {
if (!Array.isArray(command)) return false
if (typeof command[0] !== 'string') return false
if (command[0] === 'processed' || command[0] === 'error') answered = true
}
if (!answered) return false
return json
}
function send (backend, command, chulkCallback, endCallback) {
let body = JSON.stringify({
version: VERSION,
password: backend.password,
commands: [command]
})
let protocol = backend.protocol === 'https:' ? https : http
let resolved = false
let errored = false
return new Promise((resolve, reject) => {
let req = protocol.request({
method: 'POST',
host: backend.hostname,
port: backend.port,
path: backend.pathname + backend.search,
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(body)
}
}, res => {
let received = ''
if (res.statusCode < 200 || res.statusCode > 299) {
errored = true
reject(new Error(`Backend responsed with ${ res.statusCode } code`))
} else {
res.on('data', part => {
received += part
if (!resolved) {
if (ERROR.test(received)) {
errored = true
let error = new Error('Backend error during access check')
try {
let json = JSON.parse(received)
error.stack = json[0][1]
} catch (e) { }
reject(error)
} else {
let result = chulkCallback(received)
if (typeof result !== 'undefined') {
resolved = true
resolve(result)
}
}
}
})
res.on('end', () => {
if (!errored && resolved) {
if (endCallback) endCallback(received)
} else if (!errored) {
reject(new Error('Backend wrong answer'))
}
})
}
})
req.on('error', reject)
req.end(body)
})
}
function bindBackendProxy (app) {
if (!app.options.controlPassword) {
let e = new Error('`backend` requires `controlPassword` option')
e.code = 'LOGUX_NO_CONTROL_PASSWORD'
throw e
}
let backend = new URL(app.options.backend)
backend.password = app.options.controlPassword
let processing = { }
async function access (ctx, action, meta) {
let processResolve, processReject
processing[meta.id] = new Promise((resolve, reject) => {
processResolve = resolve
processReject = reject
})
let start = Date.now()
app.emitter.emit('backendSent', action, meta)
try {
let result = await send(backend, ['action', action, meta], received => {
if (APPROVED.test(received)) {
app.emitter.emit('backendGranted', action, meta, Date.now() - start)
return true
} else if (FORBIDDEN.test(received)) {
delete processing[meta.id]
return false
} else if (UNKNOWN_ACTION.test(received)) {
delete processing[meta.id]
app.unknownType(action, meta)
return false
} else if (UNKNOWN_CHANNEL.test(received)) {
delete processing[meta.id]
app.wrongChannel(action, meta)
return false
} else {
return undefined
}
}, response => {
if (processing[meta.id]) {
app.emitter.emit('backendProcessed', action, meta, Date.now() - start)
let json = parseAnswer(response)
if (!json) {
processReject(new Error('Backend wrong answer'))
} else if (json.some(i => i[0] === 'processed')) {
processResolve()
} else {
let error = new Error('Backend error during processing')
let report = json.find(i => i[0] === 'error')
if (report) error.stack = report[1]
processReject(error)
}
}
})
return result
} catch (e) {
delete processing[meta.id]
throw e
}
}
async function process (ctx, action, meta) {
try {
let res = await processing[meta.id]
delete processing[meta.id]
return res
} catch (e) {
delete processing[meta.id]
throw e
}
}
app.auth((userId, credentials) => {
return send(backend, ['auth', userId, credentials, nanoid()], received => {
if (AUTHENTICATED.test(received)) {
return true
} else if (DENIED.test(received)) {
return false
} else {
return undefined
}
})
})
app.otherType({ access, process })
app.otherChannel({ access, init: process })
app.controls['/'] = {
isValid (command) {
return command.length === 3 &&
command[0] === 'action' &&
typeof command[1] === 'object' &&
typeof command[2] === 'object' &&
typeof command[1].type === 'string'
},
command (command, req) {
if (!app.types[command[1].type]) {
command[2].status = 'processed'
}
command[2].backend = req.connection.remoteAddress
return app.log.add(command[1], command[2])
}
}
}
module.exports = bindBackendProxy