SshClient.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. GNU General Public License version 3 notice
  3. Copyright (C) 2012 Mihawk <luiz@netdome.biz>. All rights reserved.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see < http://www.gnu.org/licenses/ >.
  14. */
  15. #include "SshClient.h"
  16. #include "App.h"
  17. #include "Settings.h"
  18. #include <QProcess>
  19. #include <QRegExp>
  20. #include <QDateTime>
  21. #include <QTimerEvent>
  22. #include <QStringList>
  23. #include <QDebug>
  24. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  25. SshClient::SshClient(App* app, QObject *parent) :
  26. QObject(parent),
  27. myApp(app),
  28. myProcess(new QProcess(this)),
  29. myCommandRegex(new QRegExp("^([0-9]{4}-[0-9]{2}-[0-9]{2}\\s[0-9]{2}:[0-9]{2}:[0-9]{2})\\s(\\+[0-9]{4}):\\s(.+)$")),
  30. myConnectedFlag(false),
  31. myConnectionTimerID(0),
  32. myPingTimerID(0),
  33. myPongTimerID(0)
  34. {
  35. connect(myProcess, SIGNAL(readyRead()), SLOT(read()));
  36. connect(myProcess, SIGNAL(finished(int)), SLOT(exited(int)));
  37. }
  38. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  39. SshClient::~SshClient()
  40. {
  41. delete myCommandRegex;
  42. }
  43. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  44. bool SshClient::isConnected() const
  45. {
  46. return myConnectedFlag;
  47. }
  48. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  49. void SshClient::read()
  50. {
  51. QString data(myProcess->readAll().trimmed());
  52. if(data.isEmpty())
  53. return;
  54. QStringList lines = data.split("\n", QString::SkipEmptyParts);
  55. QString line;
  56. foreach(line, lines)
  57. {
  58. line = line.trimmed();
  59. if(myCommandRegex->indexIn(line) != -1)
  60. {
  61. QDateTime time = QDateTime::fromString(myCommandRegex->cap(1), "yyyy-MM-dd HH:mm:ss");
  62. time = time.addSecs(-myCommandRegex->cap(2).toInt()/100*60*60);
  63. parse(time,
  64. myCommandRegex->cap(3).section(' ', 0, 0),
  65. myCommandRegex->cap(3).section(' ', 1)
  66. );
  67. }
  68. }
  69. }
  70. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  71. void SshClient::exited(int)
  72. {
  73. reconnect();
  74. }
  75. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  76. void SshClient::ping()
  77. {
  78. write("PING\n");
  79. myPingTimerID = startTimer(30000);
  80. }
  81. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  82. void SshClient::pong()
  83. {
  84. killTimer(myPingTimerID);
  85. myPingTimerID = -1;
  86. myPongTimerID = startTimer(30000);
  87. }
  88. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  89. void SshClient::write(const QString &data)
  90. {
  91. myProcess->write(data.toAscii());
  92. myProcess->waitForBytesWritten();
  93. }
  94. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  95. void SshClient::parse(const QDateTime &time, const QString &command, const QString &commandData)
  96. {
  97. /* JOINED - Another user has just joined central */
  98. if(command == "JOINED")
  99. {
  100. QRegExp a("^User '([a-zA-Z]+)'.+$");
  101. if(a.indexIn(commandData) != -1)
  102. {
  103. if(!myClients.contains(a.capturedTexts().at(1)))
  104. myClients.push_back(a.capturedTexts().at(1));
  105. }
  106. return;
  107. }
  108. /* PARTED - Another user has left central */
  109. if(command == "PARTED")
  110. {
  111. QRegExp a("^User '([a-zA-Z]+)'.+$");
  112. if(a.indexIn(commandData) != -1)
  113. myClients.removeAll(a.capturedTexts().at(1));
  114. return;
  115. }
  116. /* HELLO - Server acknowledge */
  117. if(command == "HELLO")
  118. {
  119. ping();
  120. myConnectedFlag = true;
  121. killTimer(myConnectionTimerID);
  122. myConnectionTimerID = -1;
  123. emit connected();
  124. return;
  125. }
  126. /* PING PONG - The connection is still up */
  127. if(command == "PONG")
  128. {
  129. pong();
  130. return;
  131. }
  132. /* SYS - Central generic message */
  133. if(command == "SYS")
  134. {
  135. myApp->print("Central Message: " + commandData + "\n");
  136. return;
  137. }
  138. /* BC - Broadcast order from central */
  139. if(command == "BC")
  140. {
  141. QRegExp a("^([A-Za-z0-9]+) (.+),(.+),(.+),'(.+)','(.+)'$");
  142. if(a.indexIn(commandData) == -1)
  143. return;
  144. if(a.cap(2) == "QDEV" && !Settings::globalInstance()->developerMode()) //developer mode not enabled ignore this message from developers
  145. return;
  146. int serverCount, userCount;
  147. myApp->broadcast(a.cap(3) + " " + a.cap(5) + " - " + a.cap(4) + " : " + a.cap(6), &serverCount, &userCount);
  148. if(userCount)
  149. write("BC_RE " + a.cap(1) + " Players=" + QString::number(userCount) + ",Servers=" + QString::number(serverCount) + "\n");
  150. return;
  151. }
  152. /* BC_ID - Broadcast reply with the ID of the broadcast you just generated */
  153. if(command == "BC_ID")
  154. {
  155. //BC_ID 4e5fca581569e168c7a86a0a9a91949f for: QDEV,-dev-,qw://123.123
  156. // qDebug() << commandData;
  157. QRegExp a("^([A-Za-z0-9]+) for: .+,-(.+)-,qw://(.+) [0-9]+/[0-9]+$");
  158. if(a.indexIn(commandData) == -1)
  159. return;
  160. QString hash = a.cap(1);
  161. //QString frequency = a.cap(2);
  162. QString server = a.cap(3);
  163. myApp->setReplyHashAndWaitForReply(server, hash);
  164. return;
  165. }
  166. /* BC_RE - Broadcast reply to increment the counters */
  167. if(command == "BC_RE")
  168. {
  169. QRegExp a("^([A-Za-z0-9]+) (Users|Players)=([0-9]+),(Channels|Servers)=([0-9]+)$");
  170. if(a.indexIn(commandData) == -1)
  171. return;
  172. QString hash = a.cap(1);
  173. QString userType = a.cap(2);
  174. QString channelType = a.cap(4);
  175. int userCount = 0;
  176. int channelCount = 0;
  177. int playerCount = 0;
  178. int serverCount = 0;
  179. if(userType == "Users")
  180. {
  181. userCount = a.cap(3).toInt();
  182. playerCount = 0;
  183. }
  184. else if(userType == "Players")
  185. {
  186. playerCount = a.cap(3).toInt();
  187. userCount = 0;
  188. }
  189. if(channelType == "Channels")
  190. {
  191. channelCount = a.cap(5).toInt();
  192. serverCount = 0;
  193. }
  194. else if(channelType == "Servers")
  195. {
  196. serverCount = a.cap(5).toInt();
  197. channelCount = 0;
  198. }
  199. // qDebug() << commandData;
  200. myApp->incrementReplyCounters(hash, userCount, channelCount, playerCount, serverCount);
  201. return;
  202. }
  203. /* DNS_RE - Reply from REQ_DNS, returns the HostName for the requested ip:port pair */
  204. if(command == "DNS_RE")
  205. {
  206. QRegExp a("^([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+:[0-9]+) ([0-9A-Za-z][A-Za-z0-9\\-\\.]+)$");
  207. if(a.indexIn(commandData) == -1)
  208. return;
  209. myApp->setServerHostName(a.cap(1), a.cap(2));
  210. return;
  211. }
  212. /* COMMANDS - List of commands allowed for me */
  213. if(command == "COMMANDS")
  214. {
  215. myCommands = commandData.split(", ", QString::SkipEmptyParts);
  216. return;
  217. }
  218. /* ROLES - List of roles I exert */
  219. if(command == "ROLES")
  220. {
  221. myRoles = commandData.split(", ", QString::SkipEmptyParts);
  222. return;
  223. }
  224. myApp->print("Unparsed cmd from central: " + time.toString() + " " + command + " " + commandData + "\n");
  225. }
  226. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  227. bool SshClient::connectToHost(const QString &user, const QString &host)
  228. {
  229. myProcess->start("ssh", QStringList() << user + "@" + host);
  230. myUsername = user;
  231. myHostname = host;
  232. bool r = myProcess->waitForStarted();
  233. if(!r)
  234. return false;
  235. else
  236. {
  237. myConnectionTimerID = startTimer(60000*3); // up to 3 minutes of waiting
  238. return true;
  239. }
  240. }
  241. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  242. void SshClient::reconnect()
  243. {
  244. if(!myUsername.isEmpty() && !myHostname.isEmpty())
  245. connectToHost(myUsername, myHostname);
  246. }
  247. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  248. void SshClient::disconnectFromHost()
  249. {
  250. killTimer(myConnectionTimerID);
  251. killTimer(myPingTimerID);
  252. killTimer(myPongTimerID);
  253. myConnectionTimerID = myPingTimerID = myPongTimerID = -1;
  254. myProcess->terminate();
  255. myProcess->waitForFinished();
  256. myConnectedFlag = false;
  257. }
  258. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  259. void SshClient::timerEvent(QTimerEvent *e)
  260. {
  261. if(e->timerId() == myConnectionTimerID)
  262. {
  263. myApp->print("Timedout while trying to connect to central...\n");
  264. disconnectFromHost();
  265. emit error(ConnectionTimedOutError);
  266. return;
  267. }
  268. if(e->timerId() == myPingTimerID)
  269. {
  270. myApp->print("Ping with no pong! Disconnecting from central...\n");
  271. disconnectFromHost();
  272. emit error(ConnectionTimedOutError);
  273. return;
  274. }
  275. if(e->timerId() == myPongTimerID)
  276. {
  277. killTimer(myPongTimerID);
  278. myPongTimerID = -1;
  279. ping();
  280. return;
  281. }
  282. }