-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
151 lines (125 loc) · 4.44 KB
/
client.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
'use strict'
const dgram = require('dgram')
const DEBUG = true
const SECRET = process.env['EDR_SECRET'] || 'unsecure'
const SERVER = process.env['EDR_SERVER'] || '127.0.0.1'
const PORT = process.env['EDR_PORT'] || 41234
const POLLING_PERIOD = 1000 * 1 // 10s
const DIAL_TIMEOUT = 1000 * 10 // 10s
const TIMEOUT = new Error('TIMEOUT')
function logError (msg, data) {
const name = data && data.name && data.stack ? data.name : undefined
const stack = data && data.name && data.stack ? data.stack : undefined
console.log(JSON.stringify({ type: 'error', msg, data, name, stack }))
}
function logInfo (msg, data) {
const name = data && data.name && data.stack ? data.name : undefined
const stack = data && data.name && data.stack ? data.stack : undefined
console.log(JSON.stringify({ type: 'info', msg, data, name, stack }))
}
function logDebug (msg, data) {
if (!DEBUG) return
const name = data && data.name && data.stack ? data.name : undefined
const stack = data && data.name && data.stack ? data.stack : undefined
console.log(JSON.stringify({ type: 'debug', msg, data, name, stack }))
}
async function info (args, server) {
return process.report.getReport()
}
async function stop (args, api) {
api.stop()
return 'ok'
}
class EDRTransport {
constructor (props) {
this._requestTimeout = props.timeout || DIAL_TIMEOUT
this._server = props.server || SERVER
this._port = props.port || PORT
this._secret = props.secret || SECRET
}
async _requestTasks () {
throw new Error('not implemented')
return [
[123, 'name', {}],
]
}
async _sendTaskResult (id, name, result) {
throw new Error('not implemented')
}
async _sendData (channel, data) {
throw new Error('not implemented')
}
}
class EDRClient {
constructor (protocol, transport, props) {
if (typeof protocol !== 'object' || !protocol || !Object.keys(protocol).length <= 0) throw new Error('EDRClient: first argument should be an protocol')
this._protocol = protocol
if (!transport) throw new Error('EDRClient: no transport argument')
this._transport = new EDRTransport(props)
if (!props) props = {}
this._props = props
this._pullInterval = props.pullInterval || POLLING_PERIOD
this._isStarted = false
this._startedHandler = null
this._protocolHandlerApi = {
stop: this.stop.bind(this),
start: this.start.bind(this),
send: (channel, data) => this._transport._sendData(channel, data),
// isStarted: () => this._isStarted,
}
}
start () {
logInfo('EDRClient: starting', this._props)
if (this._isStarted) return
this._isStarted = true
this._startedHandler = setTimeout(() => this._pull(), this._pullInterval)
}
stop () {
logInfo('EDRClient: stopping', this._props)
this._isStarted = false
if (this._startedHandler) clearTimeout(this._startedHandler)
this._startedHandler = null
}
async _pull () {
if (!this._isStarted) return
try {
const tasks = await this._transport._requestTasks()
for (const task of tasks) {
try {
const [id, name, args] = task
const cb = this._protocol[name]
if (!cb) throw new Error(`unknown protocol command: "${name}"`)
const result = await cb(args, this._protocolHandlerApi)
if (typeof result !== 'undefined') {
await this._transport._sendTaskResult(id, name, result)
}
} catch (err) {
logInfo('EDRClient: execute task error (ignored)', err)
}
}
} catch (err) {
logInfo('EDRClient: pull tasks error (ignored)', err)
}
this._startedHandler = setTimeout(() => this._pull(), this._pullInterval)
}
}
async function main () {
const protocol = {
'i1': info,
's1': stop,
}
const transport = new EDRTransport({
secret: SECRET,
server: SERVER,
port: PORT,
timeout: DIAL_TIMEOUT,
})
const client = new EDRClient(protocol, transport, {
pullInterval: POLLING_PERIOD,
})
client.start()
}
process.on('unhandledRejection', (err) => {
logInfo('unhandled error (ignored)', err)
})
main()