private void HandleMessage(DirectTcpIpMessage message) { var channel = HandleChannelOpenMessage(message); var args = new TcpRequestArgs(channel, message.Host, (int)message.Port, message.OriginatorIPAddress, (int)message.OriginatorPort, _auth); TcpForwardRequest?.Invoke(this, args); }
private void HandlePortForward(ChannelOpenMessage message, string address, uint port, string originationIP, uint originatorPort) { var channel = new SessionChannel( this, message.SenderChannel, message.InitialWindowSize, message.MaximumPacketSize, (uint)Interlocked.Increment(ref _serverChannelCounter)); var readyToAcceptData = false; var args = new TcpRequestArgs( this._session, address, port, originationIP, originatorPort, () => { return(readyToAcceptData); }, (byte[] data) => { if (!channel.ClientClosed && !channel.ServerClosed) { channel.SendData(data); } else { throw new IOException("Connection closed"); } }, () => { if (!channel.ClientClosed && !channel.ServerClosed) { try { channel.SendClose(); } catch { } } } ); TcpForwardRequest(this, args); Action <object, byte[]> eventHandlerForDataReceived = (sender, data) => { try { args.OnClientData(data); } catch { channel.SendClose(); } }; channel.DataReceived += (o, ea) => eventHandlerForDataReceived(o, ea); lock (_locker) _channels.Add(channel); // send SSH_MSG_CHANNEL_OPEN_CONFIRMATION var msg = new ChannelOpenConfirmationMessage { RecipientChannel = channel.ClientChannelId, SenderChannel = channel.ServerChannelId, InitialWindowSize = channel.ServerInitialWindowSize, MaximumPacketSize = channel.ServerMaxPacketSize }; _session.SendMessage(msg); readyToAcceptData = true; }