mirror of https://github.com/calzoneman/sync.git
Initial work for proxy connections
This commit is contained in:
parent
e88971a011
commit
10d4ec8e60
|
@ -0,0 +1,20 @@
|
|||
export default class FrontendManager {
|
||||
constructor() {
|
||||
this.frontendConnections = {};
|
||||
}
|
||||
|
||||
onConnection(socket) {
|
||||
if (this.frontendConnections.hasOwnProperty(socket.remoteAddress)) {
|
||||
// TODO: do some validation, maybe check if the socket is still connected?
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
this.frontendConnections[socket.remoteAddressAndPort] = socket;
|
||||
console.log(socket.remoteAddressAndPort);
|
||||
socket.on('data', this.onData.bind(this, socket));
|
||||
}
|
||||
|
||||
onData(socket, data) {
|
||||
console.log(data);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
import Server from 'cytube-common/lib/tcpjson/server';
|
||||
import FrontendManager from './frontendmanager';
|
||||
|
||||
export default class IOBackend {
|
||||
constructor(proxyListenerConfig) {
|
||||
this.proxyListenerConfig = proxyListenerConfig;
|
||||
this.initFrontendManager();
|
||||
this.initProxyListener();
|
||||
}
|
||||
|
||||
initFrontendManager() {
|
||||
this.frontendManager = new FrontendManager();
|
||||
}
|
||||
|
||||
initProxyListener() {
|
||||
this.proxyListener = new Server(this.proxyListenerConfig);
|
||||
this.proxyListener.on('connection',
|
||||
this.frontendManager.onConnection.bind(this.frontendManager));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
import { EventEmitter } from 'events';
|
||||
|
||||
export default class ProxiedSocket extends EventEmitter {
|
||||
constructor(socketID, socketData, socketEmitter, frontendConnection) {
|
||||
super();
|
||||
this.id = socketID;
|
||||
this.ip = socketData.ip;
|
||||
this._realip = socketData.ip;
|
||||
this.socketEmitter = socketEmitter;
|
||||
this.frontendConnection = frontendConnection;
|
||||
}
|
||||
|
||||
emit() {
|
||||
const target = socketEmitter.to(this.id);
|
||||
target.emit.apply(target, arguments);
|
||||
}
|
||||
|
||||
onProxiedEventReceived() {
|
||||
EventEmitter.prototype.emit.apply(this, arguments);
|
||||
}
|
||||
|
||||
join(channel) {
|
||||
this.frontendConnection.write(
|
||||
this.frontendConnection.protocol.socketJoinSocketChannels(
|
||||
this.id, [channel]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
this.frontendConnection.write(
|
||||
this.frontendConnection.protocol.socketKick(this.id)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue