protected void OnForwardedPortRemoved(ForwardedPort forwardedPort) { if (ForwardedPortRemoved != null) { ForwardedPortRemoved(this, new ForwardedPortEventArgs(forwardedPort)); } }
public void CreateClientPort(string listenAddress, int listenPort, string connectAddress, int connectPort) { var listenIndex = ++ConnectionCount; ForwardedPort forwardedPort = null; var listener = new ClientListener { ListenResponse = response => { forwardedPort = new ForwardedPort { Direction = ForwardDirection.Local, ListenEndPoint = listenAddress + ":" + listenPort, ConnectEndPoint = connectAddress + ":" + connectPort, Remove = () => { Log("Cancelling local port " + listenAddress + ":" + listenPort + " forwarding to " + connectAddress + ":" + connectPort + " requested"); WriteMessage(new StreamError(listenIndex, new EndOfStreamException())); }, }; Log("Local port forwarding from " + listenAddress + ":" + listenPort + " enabled"); OnForwardedPortAdded(forwardedPort); }, StreamError = error => { if (forwardedPort == null) { Log("Local port forwarding from " + listenAddress + ":" + listenPort + " failed: " + error.Message); lock (clientListeners) { if (clientListeners.ContainsKey(listenIndex)) { clientListeners.Remove(listenIndex); } } } else { Log("Cancelled local port " + listenAddress + ":" + listenPort + " forwarding to " + connectAddress + ":" + connectPort); OnForwardedPortRemoved(forwardedPort); } }, AcceptRequest = request => { Log("Opening forwarded connection " + request.RemoteEndPoint + " to " + connectAddress + ":" + connectPort); new ConnectRequest(request.StreamIndex, request.RemoteEndPoint, connectAddress, connectPort).Process(this); }, }; lock (clientListeners) { clientListeners[listenIndex] = listener; } Log("Requesting local port " + listenAddress + ":" + listenPort + " forward to " + connectAddress + ":" + connectPort); WriteMessage(new ListenRequest(listenIndex, listenAddress, listenPort)); }
public void CreateServerPort(string listenAddress, int listenPort, string connectAddress, int connectPort) { try { var listenIndex = ++ConnectionCount; ForwardedPort forwardedPort = null; Listeners[listenIndex] = TcpListenerHelper.Start(listenAddress, listenPort, client => { var streamIndex = ++ConnectionCount; Log("Attempting to forward remote port " + client.Client.RemoteEndPoint + " to " + connectAddress + ":" + connectPort); Streams[streamIndex] = client.GetStream(); WriteMessage(new ConnectRequest(streamIndex, client.Client.RemoteEndPoint.ToString(), connectAddress, connectPort)); }, exception => { if (forwardedPort == null) { Log("Remote port " + listenAddress + ":" + listenPort + " forwarding to " + connectAddress + ":" + connectPort + " exception: " + exception); } else { Log("Cancelled remote port " + listenAddress + ":" + listenPort + " forwarding to " + connectAddress + ":" + connectPort); OnForwardedPortRemoved(forwardedPort); } }); Log("Remote port " + listenAddress + ":" + listenPort + " forwarding to " + connectAddress + ":" + connectPort); forwardedPort = new ForwardedPort { Direction = ForwardDirection.Remote, ListenEndPoint = listenAddress + ":" + listenPort, ConnectEndPoint = connectAddress + ":" + connectPort, Remove = () => { Log("Cancelling remote port " + listenAddress + ":" + listenPort + " forwarding to " + connectAddress + ":" + connectPort); new StreamError(listenIndex, new EndOfStreamException()).Process(this); }, }; OnForwardedPortAdded(forwardedPort); } catch (Exception ex) { Log("Remote port " + listenAddress + ":" + listenPort + " forwarding to " + connectAddress + ":" + connectPort + " failed: " + ex); } }
public ForwardedPortEventArgs(ForwardedPort forwardedPort) { ForwardedPort = forwardedPort; }