SshClient.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. SshClient::SshClient(App* app, QObject *parent) :
  25. QObject(parent),
  26. myApp(app),
  27. myProcess(new QProcess(this)),
  28. 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(.+)$")),
  29. myConnectedFlag(false),
  30. myConnectionTimerID(0),
  31. myPingTimerID(0),
  32. myPongTimerID(0),
  33. myClients(new QStringList())
  34. {
  35. connect(myProcess, SIGNAL(readyRead()), SLOT(read()));
  36. connect(myProcess, SIGNAL(finished(int)), SLOT(exited(int)));
  37. }
  38. SshClient::~SshClient()
  39. {
  40. delete myCommandRegex;
  41. delete myClients;
  42. }
  43. bool SshClient::isConnected() const
  44. {
  45. return myConnectedFlag;
  46. }
  47. void SshClient::read()
  48. {
  49. QString data(myProcess->readAll().trimmed());
  50. if(data.isEmpty())
  51. return;
  52. QStringList lines = data.split("\n", QString::SkipEmptyParts);
  53. QString line;
  54. foreach(line, lines)
  55. {
  56. line = line.trimmed();
  57. if(myCommandRegex->indexIn(line) != -1)
  58. {
  59. QDateTime time = QDateTime::fromString(myCommandRegex->cap(1), "yyyy-MM-dd HH:mm:ss");
  60. time = time.addSecs(-myCommandRegex->cap(2).toInt()/100*60*60);
  61. parse(time,
  62. myCommandRegex->cap(3).section(' ', 0, 0),
  63. myCommandRegex->cap(3).section(' ', 1)
  64. );
  65. }
  66. }
  67. }
  68. void SshClient::exited(int)
  69. {
  70. reconnect();
  71. }
  72. void SshClient::ping()
  73. {
  74. write("PING\n");
  75. myPingTimerID = startTimer(30000);
  76. }
  77. void SshClient::pong()
  78. {
  79. killTimer(myPingTimerID);
  80. myPongTimerID = startTimer(30000);
  81. }
  82. void SshClient::write(const QString &data)
  83. {
  84. myProcess->write(data.toAscii());
  85. myProcess->waitForBytesWritten();
  86. }
  87. void SshClient::parse(const QDateTime &time, const QString &command, const QString &commandData)
  88. {
  89. /* JOINED - Another user has just joined central */
  90. if(command == "JOINED")
  91. {
  92. QRegExp a("^User '([a-zA-Z]+)'.+$");
  93. if(a.indexIn(commandData) != -1)
  94. {
  95. if(!myClients->contains(a.capturedTexts().at(1)))
  96. myClients->push_back(a.capturedTexts().at(1));
  97. }
  98. return;
  99. }
  100. /* PARTED - Another user has left central */
  101. if(command == "PARTED")
  102. {
  103. QRegExp a("^User '([a-zA-Z]+)'.+$");
  104. if(a.indexIn(commandData) != -1)
  105. myClients->removeAll(a.capturedTexts().at(1));
  106. return;
  107. }
  108. /* HELLO - Server acknowledge */
  109. if(command == "HELLO")
  110. {
  111. ping();
  112. myConnectedFlag = true;
  113. killTimer(myConnectionTimerID);
  114. emit connected();
  115. return;
  116. }
  117. /* PING PONG - The connection is still up */
  118. if(command == "PONG")
  119. {
  120. pong();
  121. return;
  122. }
  123. /* SYS - Central generic message */
  124. if(command == "SYS")
  125. {
  126. myApp->print("Central Message: " + commandData + "\n");
  127. return;
  128. }
  129. /* BC - Broadcast order from central */
  130. if(command == "BC")
  131. {
  132. QRegExp a("^([A-Za-z0-9]+) (.+),(.+),(.+),'(.+)','(.+)'$");
  133. if(a.indexIn(commandData) == -1)
  134. return;
  135. if(a.cap(2) == "QDEV" && !Settings::globalInstance()->developerMode()) //developer mode not enabled ignore this message from developers
  136. return;
  137. int serverCount, userCount;
  138. myApp->broadcast(a.cap(3) + " " + a.cap(5) + " - " + a.cap(4) + " : " + a.cap(6), &serverCount, &userCount);
  139. if(userCount)
  140. write("BC_RE " + a.cap(1) + " Players=" + QString::number(userCount) + ",Servers=" + QString::number(serverCount) + "\n");
  141. return;
  142. }
  143. /* BC_ID - Broadcast reply with the ID of the broadcast you just generated */
  144. if(command == "BC_ID")
  145. {
  146. //BC_ID 4e5fca581569e168c7a86a0a9a91949f for: QDEV,-dev-,qw://123.123
  147. // qDebug() << commandData;
  148. QRegExp a("^([A-Za-z0-9]+) for: .+,-(.+)-,qw://(.+) [0-9]+/[0-9]+$");
  149. if(a.indexIn(commandData) == -1)
  150. return;
  151. QString hash = a.cap(1);
  152. //QString frequency = a.cap(2);
  153. QString server = a.cap(3);
  154. myApp->setReplyHash(server, hash);
  155. return;
  156. }
  157. /* BC_RE - Broadcast reply to increment the counters */
  158. if(command == "BC_RE")
  159. {
  160. QRegExp a("^([A-Za-z0-9]+) (Users|Players)=([0-9]+),(Channels|Servers)=([0-9]+)$");
  161. if(a.indexIn(commandData) == -1)
  162. return;
  163. QString hash = a.cap(1);
  164. QString userType = a.cap(2);
  165. QString channelType = a.cap(4);
  166. int userCount = 0;
  167. int channelCount = 0;
  168. int playerCount = 0;
  169. int serverCount = 0;
  170. if(userType == "Users")
  171. {
  172. userCount = a.cap(3).toInt();
  173. playerCount = 0;
  174. }
  175. else if(userType == "Players")
  176. {
  177. playerCount = a.cap(3).toInt();
  178. userCount = 0;
  179. }
  180. if(channelType == "Channels")
  181. {
  182. channelCount = a.cap(5).toInt();
  183. serverCount = 0;
  184. }
  185. else if(channelType == "Servers")
  186. {
  187. serverCount = a.cap(5).toInt();
  188. channelCount = 0;
  189. }
  190. // qDebug() << commandData;
  191. myApp->incrementReplyCounters(hash, userCount, channelCount, playerCount, serverCount);
  192. return;
  193. }
  194. myApp->print("Unparsed cmd from central: " + time.toString() + " " + command + " " + commandData + "\n");
  195. }
  196. bool SshClient::connectToHost(const QString &user, const QString &host)
  197. {
  198. myProcess->start("ssh", QStringList() << user + "@" + host);
  199. myUsername = user;
  200. myHostname = host;
  201. bool r = myProcess->waitForStarted();
  202. if(!r)
  203. return false;
  204. else
  205. {
  206. myConnectionTimerID = startTimer(30000);
  207. return true;
  208. }
  209. }
  210. void SshClient::reconnect()
  211. {
  212. if(!myUsername.isEmpty() && !myHostname.isEmpty())
  213. connectToHost(myUsername, myHostname);
  214. }
  215. void SshClient::disconnectFromHost()
  216. {
  217. killTimer(myConnectionTimerID);
  218. killTimer(myPingTimerID);
  219. killTimer(myPongTimerID);
  220. myProcess->terminate();
  221. myProcess->waitForFinished();
  222. myConnectedFlag = false;
  223. }
  224. void SshClient::timerEvent(QTimerEvent *e)
  225. {
  226. if(e->timerId() == myConnectionTimerID)
  227. {
  228. disconnectFromHost();
  229. emit error(ConnectionTimedOutError);
  230. return;
  231. }
  232. if(e->timerId() == myPingTimerID)
  233. {
  234. disconnectFromHost();
  235. emit error(ConnectionTimedOutError);
  236. return;
  237. }
  238. if(e->timerId() == myPongTimerID)
  239. {
  240. killTimer(myPongTimerID);
  241. ping();
  242. return;
  243. }
  244. }