-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tcp server. Use UUID module insteal of int.
- Loading branch information
1 parent
3ce84dd
commit b34c6a8
Showing
5 changed files
with
72 additions
and
54 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,71 @@ | ||
const Net = require('net'); | ||
const TextEncoding = require('text-encoding'); | ||
import {handleSocketError} from './common/socket'; | ||
import {EventType, SocketType} from './common/config'; | ||
|
||
const decoder = new TextEncoding.TextDecoder('utf-8'); | ||
|
||
const createRemoteServer = () => { | ||
// const createRemoteServer = () => { | ||
// const remoteServer = Net.createServer(function (socket) { | ||
// handleSocketError(socket); | ||
// | ||
// const socketId = UUID++; | ||
// remoteSockets[socketId] = socket; | ||
// socket.on('close', () => { | ||
// delete remoteSockets[socketId]; | ||
// }); | ||
// socket.pause(); | ||
// const info = {type: 'createTunnel', uuid: socketId}; | ||
// mainClientSocket.write(JSON.stringify(info)); | ||
// }); | ||
// | ||
// remoteServer.listen(RemoteServerPort, '127.0.0.1'); | ||
// }; | ||
// | ||
// export const initTcpProxyServer = (proxies, bindPort) => { | ||
// let UUID = 0; | ||
// let mainClientSocket = null; | ||
// | ||
// const remoteSockets = {}; | ||
// const tunnelSockets = {}; | ||
// | ||
// const tunnelServer = Net.createServer((socket) => { | ||
// let isPiping = false; | ||
// handleSocketError(socket); | ||
// // data: {type: 'main'|'tunnel', uuid: int, remotePort: int} | ||
// socket.on('data', (data) => { | ||
// if (isPiping) { return; } | ||
// console.log('Tunnel receive data: ' + data); | ||
// const info = JSON.parse(decoder.decode(data)); | ||
// const uuid = info.uuid; | ||
// if (info.type === 'main') { | ||
// mainClientSocket = socket; | ||
// } else if (info.type === 'tunnel') { | ||
// tunnelSockets[uuid] = socket; | ||
// socket.on('close', () => { | ||
// delete tunnelSockets[uuid]; | ||
// }); | ||
// remoteSockets[uuid].pipe(socket).pipe(remoteSockets[uuid]); | ||
// remoteSockets[uuid].resume(); | ||
// isPiping = true; | ||
// } | ||
// }); | ||
// }); | ||
// | ||
// tunnelServer.listen(TunnelServerPort, '127.0.0.1'); | ||
// }; | ||
|
||
const createRemoteServer = (eventEmitter, listenPort) => { | ||
const remoteServer = Net.createServer(function (socket) { | ||
handleSocketError(socket); | ||
|
||
const socketId = UUID++; | ||
remoteSockets[socketId] = socket; | ||
socket.on('close', () => { | ||
delete remoteSockets[socketId]; | ||
}); | ||
socket.pause(); | ||
const info = {type: 'createTunnel', uuid: socketId}; | ||
mainClientSocket.write(JSON.stringify(info)); | ||
eventEmitter.emit(EventType.RECEIVE_REMOTE_CONNECTION, | ||
SocketType.TCP, socket, listenPort); | ||
}); | ||
|
||
remoteServer.listen(RemoteServerPort, '127.0.0.1'); | ||
remoteServer.listen(listenPort, '127.0.0.1'); | ||
}; | ||
|
||
export const initTcpProxyServer = (proxies, bindPort) => { | ||
let UUID = 0; | ||
let mainClientSocket = null; | ||
|
||
const remoteSockets = {}; | ||
const tunnelSockets = {}; | ||
|
||
const tunnelServer = Net.createServer((socket) => { | ||
let isPiping = false; | ||
handleSocketError(socket); | ||
// data: {type: 'main'|'tunnel', uuid: int, remotePort: int} | ||
socket.on('data', (data) => { | ||
if (isPiping) { return; } | ||
console.log('Tunnel receive data: ' + data); | ||
const info = JSON.parse(decoder.decode(data)); | ||
const uuid = info.uuid; | ||
if (info.type === 'main') { | ||
mainClientSocket = socket; | ||
} else if (info.type === 'tunnel') { | ||
tunnelSockets[uuid] = socket; | ||
socket.on('close', () => { | ||
delete tunnelSockets[uuid]; | ||
}); | ||
remoteSockets[uuid].pipe(socket).pipe(remoteSockets[uuid]); | ||
remoteSockets[uuid].resume(); | ||
isPiping = true; | ||
} | ||
}); | ||
}); | ||
|
||
tunnelServer.listen(TunnelServerPort, '127.0.0.1'); | ||
export const createTcpProxies = (eventEmitter, proxies) => { | ||
for (let proxy of proxies) { | ||
createRemoteServer(eventEmitter, proxy.listenPort); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters