-
Notifications
You must be signed in to change notification settings - Fork 4
/
Proxy.js
35 lines (30 loc) · 1.41 KB
/
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
import { connect as TCPConnect } from 'net'
import { connect as TLSConnect } from 'tls'
// TODO: no TLS/SSL support (HTTPS)
// TODO: no multi target server support (load balancing)
// Usage:
// server.on('connection', createTCPProxyListener({ ... }))
const createTCPProxyListener = ({
isSecure,
getTargetOption = (socket) => ({ hostname: '127.0.0.1', port: 80 }) // can also be used for connection check
}) => async (socket) => {
let preConnectDataList = [] // will be set to null after connect
const preConnectListener = (data) => preConnectDataList.push(data)
socket.on('data', preConnectListener)
const socketEndListener = () => targetSocket.destroy()
socket.on('error', socketEndListener) // prevent server error
socket.on('close', socketEndListener)
const option = await getTargetOption(socket)
if (option.host === undefined) option.host = option.hostname
const targetSocket = (isSecure ? TLSConnect : TCPConnect)(option, () => {
for (const data of preConnectDataList) targetSocket.write(data)
preConnectDataList = null
socket.off('data', preConnectListener)
socket.on('data', (data) => targetSocket.write(data))
})
const targetSocketEndListener = () => socket.destroy()
targetSocket.on('error', targetSocketEndListener) // prevent server error
targetSocket.on('close', targetSocketEndListener)
targetSocket.on('data', (data) => socket.write(data))
}
export { createTCPProxyListener }