/// <summary> /// Requests that the remote computer accepts socket connections and forward them to the local /// computer. The <see cref="Maverick.SSH.ForwardingRequestListener"/> provides callback methods to create /// the connections and to initialize the tunnel. /// </summary> /// <param name="bindAddress"></param> /// <param name="bindPort"></param> /// <param name="hostToConnect"></param> /// <param name="portToConnect"></param> /// <param name="listener"></param> /// <returns></returns> public bool RequestRemoteForwarding(String bindAddress, int bindPort, String hostToConnect, int portToConnect, ForwardingRequestListener listener) { if (listener == null) { throw new SSHException( "You must specify a listener to receive connection requests", SSHException.BAD_API_USAGE); } ByteBuffer baw = new ByteBuffer(); baw.WriteString(bindAddress); baw.WriteInt(bindPort); GlobalRequest request = new GlobalRequest("tcpip-forward", baw.ToByteArray()); if (SendGlobalRequest(request, true)) { forwardingListeners.Add((bindAddress + ":" + bindPort), listener); forwardingDestinations.Add((bindAddress + ":" + bindPort), (hostToConnect + ":" + portToConnect)); // Setup the forwarding listener return(true); } else { return(false); } }
public void Initialize() { _request = ImageRequestBuilder.NewBuilderWithSource(new Uri("http://request")).Build(); _callerContext = new object(); _error = new Exception(); _immutableMap = new Dictionary <string, string>(); ProducerListenerImpl producerListener1 = new ProducerListenerImpl( (_, __) => { ++_onProducerStartFuncCalls1; }, (_, __, ___) => { ++_onProducerEventFuncCalls1; }, (_, __, ___) => { ++_onProducerFinishWithSuccessFuncCalls1; }, (_, __, ___, ____) => { ++_onProducerFinishWithFailureFuncCalls1; }, (_, __, ___) => { ++_onProducerFinishWithCancellationFuncCalls1; }, (_) => { ++_requiresExtraMapFuncCalls1; return(false); }); ProducerListenerImpl producerListener2 = new ProducerListenerImpl( (_, __) => { ++_onProducerStartFuncCalls2; }, (_, __, ___) => { ++_onProducerEventFuncCalls2; }, (_, __, ___) => { ++_onProducerFinishWithSuccessFuncCalls2; }, (_, __, ___, ____) => { ++_onProducerFinishWithFailureFuncCalls2; }, (_, __, ___) => { ++_onProducerFinishWithCancellationFuncCalls2; }, (_) => { ++_requiresExtraMapFuncCalls2; return(false); }); ProducerListenerImpl producerListener3 = new ProducerListenerImpl( (_, __) => { ++_onProducerStartFuncCalls3; }, (_, __, ___) => { ++_onProducerEventFuncCalls3; }, (_, __, ___) => { ++_onProducerFinishWithSuccessFuncCalls3; }, (_, __, ___, ____) => { ++_onProducerFinishWithFailureFuncCalls3; }, (_, __, ___) => { ++_onProducerFinishWithCancellationFuncCalls3; }, (_) => { ++_requiresExtraMapFuncCalls3; return(false); }); _requestListener1 = new RequestListenerImpl( producerListener1, (_, __, ___, ____) => { ++_onRequestStartFuncCalls1; }, (_, __, ___) => { ++_onRequestSuccessFuncCall1; }, (_, __, ___, ____) => { ++_onRequestFailureFuncCalls1; }, (_) => { ++_onRequestCancellationFuncCalls1; }); _requestListener2 = new RequestListenerImpl( producerListener2, (_, __, ___, ____) => { ++_onRequestStartFuncCalls2; }, (_, __, ___) => { ++_onRequestSuccessFuncCall2; }, (_, __, ___, ____) => { ++_onRequestFailureFuncCalls2; }, (_) => { ++_onRequestCancellationFuncCalls2; }); _requestListener3 = new RequestListenerImpl( producerListener3, (_, __, ___, ____) => { ++_onRequestStartFuncCalls3; }, (_, __, ___) => { ++_onRequestSuccessFuncCall3; }, (_, __, ___, ____) => { ++_onRequestFailureFuncCalls3; }, (_) => { ++_onRequestCancellationFuncCalls3; }); _listenerManager = new ForwardingRequestListener(new HashSet <IRequestListener> { _requestListener1, _requestListener2, _requestListener3 }); }
public SSH2Channel CreateChannel(String channeltype, byte[] requestdata) { if (channeltype.Equals(SSH2ForwardingChannel.REMOTE_FORWARDING_CHANNEL)) { try { ByteBuffer bar = new ByteBuffer(requestdata); System.String address = bar.ReadString(); int port = (int)bar.ReadUINT32(); System.String originatorIP = bar.ReadString(); int originatorPort = (int)bar.ReadUINT32(); System.String key = address + ":" + port.ToString(); if (enclosingInstance.forwardingListeners.ContainsKey(key)) { ForwardingRequestListener listener = (ForwardingRequestListener)enclosingInstance.forwardingListeners[key]; String destination = (System.String)enclosingInstance.forwardingDestinations[key]; String hostToConnect = destination.Substring(0, (destination.IndexOf((System.Char) ':')) - (0)); int portToConnect = System.Int32.Parse(destination.Substring(destination.IndexOf((System.Char) ':') + 1)); SSH2ForwardingChannel channel = new SSH2ForwardingChannel(SSH2ForwardingChannel.REMOTE_FORWARDING_CHANNEL, 32768, 32768, hostToConnect, portToConnect, address, port, originatorIP, originatorPort, listener.CreateConnection(hostToConnect, portToConnect, address, port), false); listener.InitializeTunnel(channel); return(channel); } else { throw new ChannelOpenException("Forwarding had not previously been requested", ChannelOpenException.ADMINISTRATIVIVELY_PROHIBITED); } } catch (System.IO.IOException ex) { throw new ChannelOpenException(ex.Message, ChannelOpenException.RESOURCE_SHORTAGE); } catch (SSHException ex) { throw new ChannelOpenException(ex.Message, ChannelOpenException.CONNECT_FAILED); } } else if (channeltype.Equals("x11")) { if (!enclosingInstance.isXForwarding) { throw new ChannelOpenException("X Forwarding had not previously been requested", ChannelOpenException.ADMINISTRATIVIVELY_PROHIBITED); } try { ByteBuffer bar = new ByteBuffer(requestdata); System.String originatorIP = bar.ReadString(); int originatorPort = (int)bar.ReadUINT32(); System.String display = enclosingInstance.connection.Context.X11Display; int i = display.IndexOf(":"); System.String targetAddr; int targetPort; int displ = 0; int screen = 0; String tmp; if (i != -1) { targetAddr = display.Substring(0, (i) - (0)); tmp = display.Substring(i + 1); } else { targetAddr = "localhost"; tmp = display; } i = tmp.IndexOf("."); if (i != -1) { displ = Int32.Parse(tmp.Substring(0, i)); screen = Int32.Parse(tmp.Substring(i + 1)); } else { displ = Int32.Parse(tmp); } targetPort = displ; if (targetPort < 64) { targetPort += 6000; } ForwardingRequestListener listener = enclosingInstance.connection.Context.X11RequestListener; SSH2ForwardingChannel channel = new SSH2ForwardingChannel(SSH2ForwardingChannel.X11_FORWARDING_CHANNEL, 32768, 32768, targetAddr, targetPort, targetAddr, 6000 + displ, originatorIP, originatorPort, listener.CreateConnection(targetAddr, targetPort, originatorIP, originatorPort), false); listener.InitializeTunnel(channel); return(channel); } catch (System.Exception ex) { throw new ChannelOpenException(ex.Message, ChannelOpenException.CONNECT_FAILED); } } throw new ChannelOpenException(channeltype + " is not supported", ChannelOpenException.UNKNOWN_CHANNEL_TYPE); }