public void Start(Func <IPEndPoint> localFunc, Func <IPEndPoint> remoteFunc) { var remote = remoteFunc.Invoke(); var local = localFunc.Invoke(); try { MainSocket.Bind(local); MainSocket.Listen(10); while (true) { Console.WriteLine("{0} : Starting forwarding with new tunnel", remote.Address.ToString()); var source = MainSocket.Accept(); var destination = new TcpForwarderSlim(); var state = new State(source, destination.MainSocket); destination.Connect(remote, source); source.BeginReceive(state.Buffer, 0, state.Buffer.Length, 0, OnDataReceive, state); Console.WriteLine("{0} : Request Forwarded", remote.Address.ToString()); } } catch (SocketException) { Console.WriteLine("{0} : Closing tunnel", remote.Address.ToString()); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
public PortForwardingWrapper(Func <IPEndPoint> localFunc, Func <IPEndPoint> remoteFunc) { cancellationToken = new CancellationTokenSource(); forwarder = new TcpForwarderSlim(); var portForwardingTask = Task.Factory.StartNew((x) => { forwarder.Start(localFunc, remoteFunc); }, cancellationToken); }