void CloseTunnel() { // if a tunnel is opened signal it to stop if (tunnelState == TunnelManagerState.Started) { tunnelState = TunnelManagerState.StopSignaled; } }
void OpenSelectedTunnel() { // if no tunnel is open start one if (tunnelState == TunnelManagerState.Stopped) { tunnelConfiguration = GetCurrentTunnelConfiguration(); // extract tunnel configuration while on the UI thread tunnelTask = Task.Factory.StartNew(TunnelWorkerMethod).ContinueWith((t) => { SetCaption(applicationText); SetGuiLockedState(false); tunnelState = TunnelManagerState.Stopped; }); } }
void TunnelWorkerMethod() { tunnelState = TunnelManagerState.Starting; SetGuiLockedState(true); if (tunnelConfiguration != null) { SetMessage("Initializing..."); // parse local endpoint string localIp; int localPort; var localEndpointParts = tunnelConfiguration.LocalEndpoint.Split(':'); if (localEndpointParts.Length != 2) { SetMessage("Invalid local endpoint format!"); return; } localIp = localEndpointParts[0]; if (string.Compare(localIp, "127.0.0.1") != 0) { SetMessage("Only local IP 127.0.0.1 is supported!"); return; } if (int.TryParse(localEndpointParts[1], out localPort) == false) { SetMessage("Invalid local endpoint format!"); return; } // parse remote endpoint string remoteIp; int remotePort; var remoteEndpointParts = tunnelConfiguration.RemoteEndpoint.Split(':'); if (remoteEndpointParts.Length != 2) { SetMessage("Invalid remote endpoint format!"); return; } remoteIp = remoteEndpointParts[0]; if (int.TryParse(remoteEndpointParts[1], out remotePort) == false) { SetMessage("Invalid remote endpoint format!"); return; } SetMessage("Creating session..."); using (var session = nabto.CreateSession(tunnelConfiguration.Email, tunnelConfiguration.Password)) { if (session == null) { SetMessage("Failed creating session!"); return; } SetMessage("Creating tunnel..."); using (var tunnel = session.CreateTunnel(tunnelConfiguration.Server, localPort, remoteIp, remotePort)) { if (tunnel == null) { SetMessage("Failed creating tunnel!"); return; } SetCaption(tunnelConfiguration.Name + " - " + applicationText); tunnelState = TunnelManagerState.Started; var version = tunnel.Version; while (tunnelState == TunnelManagerState.Started) { var state = tunnel.State; SetMessage(string.Format("Tunnel state: {0}", state.ToString())); if (state == TunnelState.Closed) { tunnelState = TunnelManagerState.StopSignaled; } Thread.Sleep(100); } } SetMessage("Tunnel closed."); } SetMessage("Session closed."); } }