|
1 | 1 | /* |
2 | 2 | * ssh.cxx - part of SpiritVNC - FLTK |
3 | | - * 2016-2021 Will Brokenbourgh https://www.pismotek.com/brainout/ |
| 3 | + * 2016-2022 Will Brokenbourgh https://www.pismotek.com/brainout/ |
4 | 4 | */ |
5 | 5 |
|
6 | 6 | /* |
|
34 | 34 |
|
35 | 35 | #include "app.h" |
36 | 36 | #include "hostitem.h" |
37 | | -#include "ssh.h" |
38 | 37 |
|
39 | | -#include <pthread.h> |
40 | 38 |
|
| 39 | +/* close and clean up ssh connection */ |
| 40 | +void svCloseSSHConnection (void * itmData) |
| 41 | +{ |
| 42 | + HostItem * itm = static_cast<HostItem *>(itmData); |
41 | 43 |
|
42 | | -//#include <array> |
43 | | - |
44 | | - |
45 | | -//void sshExec(const char * cmd, HostItem * itm) |
46 | | -//{ |
47 | | - //char buffer[128]; |
48 | | - |
49 | | - //FILE * pipe = popen(cmd, "r"); |
50 | | - |
51 | | - //if (!pipe) //throw std::runtime_error("popen() failed!"); |
52 | | - //return; |
53 | | - |
54 | | - //while (fgets(buffer, sizeof buffer, pipe) != NULL) |
55 | | - //{ |
56 | | - //itm->sshCommandOutput.append(buffer); |
57 | | - //} |
| 44 | + if (itm == NULL || itm->sshCmdStream == NULL) |
| 45 | + return; |
58 | 46 |
|
59 | | - //pclose(pipe); |
| 47 | + int sshKillResult = -1; |
| 48 | + |
| 49 | + fprintf(itm->sshCmdStream, "%s\n", "exit"); |
| 50 | + fflush(itm->sshCmdStream); |
| 51 | + |
| 52 | + sshKillResult = pclose(itm->sshCmdStream); |
60 | 53 |
|
61 | | - //return; |
62 | | -//} |
| 54 | + (void)sshKillResult; |
| 55 | +} |
63 | 56 |
|
64 | 57 |
|
65 | 58 | /* create ssh session and ssh forwarding */ |
66 | | -/* (this is called as a thread because it blocks) */ |
67 | | -void * svCreateSSHConnection (void * data) |
| 59 | +void svCreateSSHConnection (void * data) |
68 | 60 | { |
69 | | - pthread_detach(pthread_self()); |
70 | | - |
| 61 | + std::string sshCommandLine; |
71 | 62 | std::string strError; |
72 | 63 |
|
73 | 64 | HostItem * itm = static_cast<HostItem *>(data); |
74 | 65 |
|
75 | 66 | if (itm == NULL) |
76 | | - return SV_RET_VOID; |
| 67 | + return; |
77 | 68 |
|
78 | | - std::string sshCheck = "which " + app->sshCommand + " > /dev/null"; |
| 69 | + std::string sshCheck = "which " + app->sshCommand + "5 > /dev/null"; |
79 | 70 |
|
80 | 71 | // first check to see if the ssh command is working |
81 | 72 | if (system(sshCheck.c_str()) != 0) |
82 | 73 | { |
83 | 74 | fl_beep(FL_BEEP_DEFAULT); |
84 | | - svMessageWindow("Error: The SSH command doesn't work.\n\nCheck that the SSH client " |
85 | | - "is installed", "SpiritVNC - FLTK"); |
| 75 | + svMessageWindow("Error: This connection requires SSH and \nthe SSH command isn't working." |
| 76 | + "\n\nCheck that the SSH client program is installed", "SpiritVNC - FLTK"); |
86 | 77 |
|
87 | 78 | svLogToFile("SSH command not working for connection '" |
88 | 79 | + itm->name + "' - " + itm->hostAddress); |
| 80 | + |
| 81 | + itm->lastErrorMessage = "SSH command not working"; |
89 | 82 |
|
90 | 83 | itm->sshReady = false; |
91 | 84 | itm->hasError = true; |
92 | 85 |
|
93 | | - return SV_RET_VOID; |
| 86 | + return; |
94 | 87 | } |
95 | 88 |
|
96 | 89 | // build the command string for our system() call |
97 | | - itm->sshCommandLine = app->sshCommand + " " + itm->sshUser + "@" + itm->hostAddress + " -N" + |
| 90 | + sshCommandLine = app->sshCommand + " " + itm->sshUser + "@" + itm->hostAddress + " -t" + " -t" + //" -N" + |
98 | 91 | " -p " + itm->sshPort + |
99 | 92 | " -L " + std::to_string(itm->sshLocalPort) + ":127.0.0.1:" + itm->vncPort + |
100 | 93 | " -i " + itm->sshKeyPrivate; |
101 | 94 |
|
102 | | - // (over-)optimistically declare that ssh is ready |
103 | | - itm->sshReady = true; |
104 | | - |
105 | 95 | // call the system's ssh client, if available |
106 | | - if (system(itm->sshCommandLine.c_str()) != -1) |
107 | | - { |
108 | | - svLogToFile("SSH connection disconnected normally from '" |
109 | | - + itm->name + "' - " + itm->hostAddress); |
110 | | - |
111 | | - itm->sshReady = false; |
112 | | - } |
| 96 | + itm->sshCmdStream = popen(sshCommandLine.c_str(), "w"); |
| 97 | + |
| 98 | + if (itm->sshCmdStream != NULL) |
| 99 | + // ssh started okay |
| 100 | + itm->sshReady = true; |
113 | 101 | else |
114 | 102 | { |
| 103 | + // something -- happened |
115 | 104 | svLogToFile("SSH connection disconnected abnormally from '" |
116 | 105 | + itm->name + "' - " + itm->hostAddress); |
117 | 106 |
|
118 | 107 | itm->sshReady = false; |
119 | 108 | itm->hasError = true; |
120 | 109 | } |
121 | 110 |
|
122 | | - return SV_RET_VOID; |
| 111 | + return; |
123 | 112 | } |
0 commit comments