-
Notifications
You must be signed in to change notification settings - Fork 28
/
client.js
152 lines (129 loc) · 4.33 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
152
'use strict'
const net = require('net')
const { tryParseJSON, log, removeElement, crypt } = require('./utils')
const { v4: uuid } = require('uuid')
const clientName = process.env.N_T_CLIENT_NAME || 'dbg'
if (clientName.length > 128) {
log.info('Name should not be more than 128 symbols length.')
process.exit(1)
}
const serverHost = process.env.N_T_SERVER_HOST || 'localhost'
const serverPort = parseInt(process.env.N_T_SERVER_PORT) || 1337
const localPort = parseInt(process.env.N_T_CLIENT_PORT) || 8000
let connectionToServerLost = false
let localConnections = []
let dataConnections = []
let serviceClient = new net.Socket()
let isDataClient = false
let dataJson
// local
let localServer = net.createServer({ pauseOnConnect: true }, localSocket => {
let isDataClientConnected = false
if (!isDataClient || !dataJson) {
return localSocket.destroy()
}
localConnections.push(localSocket)
let dataClient = new net.Socket()
dataClient.uuid = 'client-' + uuid()
dataConnections.push(dataClient)
dataClient.on('connect', () => {
dataClient.write(crypt.encrypt(`{ "type": "client", "uuid": "${dataClient.uuid}" }`))
})
dataClient.once('data', data => {
dataClient
.pipe(localSocket)
.pipe(dataClient)
isDataClientConnected = true
localSocket.resume()
})
dataClient.connect(dataJson.port, serverHost)
dataClient.on('close', err => {
removeElement(dataConnections, dataClient)
if (err) log.err(`closed dataClient (${dataClient.uuid})`)
if (localSocket && !localSocket.destroyed) localSocket.destroy()
})
dataClient.on('error', err => log.err('DATA_CLIENT', err.name || err.code, err.message))
localSocket.on('error', err => log.err('LOCAL_SOCKET', err.name || err.code, err.message))
localSocket.on('close', hadError => {
removeElement(localConnections, localSocket)
if (isDataClientConnected) {
dataClient
.unpipe(localSocket)
.unpipe(dataClient)
if (!dataClient.destroyed) dataClient.destroy()
}
})
})
localServer.listen(localPort)
localServer.on('listening', listener => log.info(`Client listening on port ${localPort}. Connecting to server...`))
localServer.on('error', err => {
log.info('Something went wrong with client server. Stopping...\n', err.name || err.code, err.message)
localServer.close()
process.exit(1)
})
serviceClient.on('data', dataEnc => {
// try decrypt otherwise - kill
let data = crypt.decrypt(dataEnc.toString('utf8'))
if (data === null) return
let tmpJson = tryParseJSON(data.toString('utf8'))
if (tmpJson.pong) return
if (tmpJson.agentDied || !tmpJson.port) {
dataJson = null
return
}
dataJson = tmpJson
log.debug(dataJson)
if (dataJson.port === null) return
log.info('Agent found, ready!')
isDataClient = true
})
let pinger
serviceClient.on('connect', () => {
log.info('Connection to server established, waiting for agent.')
let msg = { type: 'client', name: clientName }
if (dataJson && dataJson.uuid) msg.uuid = dataJson.uuid
serviceClient.write(crypt.encrypt(JSON.stringify(msg)))
pinger = setInterval(() => {
serviceClient.write(crypt.encrypt('' + Math.random()))
}, 15000)
if (dataJson) isDataClient = true
})
serviceClient.on('error', err => log.err('SERVICE_SOCKET', err.name || err.code, err.message))
serviceClient.on('close', hadError => {
if (!connectionToServerLost) {
connectionToServerLost = true
log.info('Connection to server lost')
}
if (pinger) clearInterval(pinger)
if (!serviceClient.destroyed) serviceClient.destroy()
isDataClient = false
connectWithDelay(5000)
})
function connect () {
serviceClient.connect(serverPort, serverHost)
}
function connectWithDelay (delay) {
if (!delay) return connect()
setTimeout(connect, delay)
}
connectWithDelay(500)
process.on('exit', (code) => {
log.info(`Stopping client, trying to close connections - Local: ${localConnections.length}, Data: ${dataConnections.length}`)
localConnections.forEach(localConnection => {
if (localConnection && !localConnection.destroyed) {
localConnection.unpipe()
localConnection.destroy()
}
})
dataConnections.forEach(dataConnection => {
if (dataConnection && !dataConnection.destroyed) {
dataConnection.unpipe()
dataConnection.destroy()
}
})
serviceClient.end()
serviceClient.destroy()
})
process.on('SIGINT', () => {
process.exit()
})