public async Task Run() { while (true) { DeviceConnectionStatus connectionDetails = await http.CheckRemoteConnectionRequest(ssh.PublicKey); if (connectionDetails != null) { // Reset the connection if the client is connected with the wrong port (old connections) if (ssh.ConnectionState == EnumSshConnectionState.Open && ssh.CurrentForwardingPort != connectionDetails.SshForwarding) { ssh.CloseSshConnection("wrong port and is open"); } if ((connectionDetails.State == EnumClientConnectionState.Ready) && ssh.ConnectionState != EnumSshConnectionState.Open) { // Attesa di 2 secondi per fare in modo che OpenSSH acquisisca i certificati che abbiamo appena caricato System.Threading.Thread.Sleep(5000); ssh.OpenSshConnection(connectionDetails); ssh.EnableRemoteForwarding(connectionDetails); } else if ((connectionDetails.State == EnumClientConnectionState.NotRequest) && (ssh.ConnectionState == EnumSshConnectionState.Open)) { ssh.CloseSshConnection("Not request and is open"); } } else { ssh.CloseSshConnection("no connection details"); } // Connection status is beign constantly setted beacuse the server constantly check the last timestamp // If the client goes offline, the server will mark the device as not connected automatically if (ssh.ConnectionState == EnumSshConnectionState.Open) { await http.SetActiveDeviceConnection(); } else if (connectionDetails != null && (connectionDetails.State != EnumClientConnectionState.NotRequest)) { await http.SetClosedSshConnectionState(); } System.Threading.Thread.Sleep(5000); } }
public async Task Run() { while (true) { // Insert connection request to the target device await http.InsertConnectionRequest(); // Check device connection state to the ssh server DeviceConnectionStatus deviceConnectionDetails = await http.CheckDeviceConnectionState(ssh.PublicKey); if (deviceConnectionDetails != null) { // Reset the connection if the client is connected with the wrong port (old connections) if (ssh.ConnectionState == EnumSshConnectionState.Open && ssh.CurrentForwardingPort != deviceConnectionDetails.SshForwarding) { ssh.CloseSshConnection("wrong port"); } if (deviceConnectionDetails.State != EnumClientConnectionState.Connected) { logger.Output("Waiting for device connection.."); waiting = true; } else { if (ssh.ConnectionState != EnumSshConnectionState.Open) { logger.Debug("Device has been connected, connecting developer to the ssh server.."); ssh.OpenSshConnection(deviceConnectionDetails); ssh.EnableLocalForwarding(deviceConnectionDetails); logger.Output($"Developer SSH connection created: Device ssh server exposed on port {deviceConnectionDetails.SshForwarding})"); waiting = false; } else if (waiting) { logger.Output($"Developer SSH connection re-established: Device ssh server exposed on port {deviceConnectionDetails.SshForwarding})"); waiting = false; } } } System.Threading.Thread.Sleep(5000); } }