SshClient.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 <QProcess>
  18. #include <QRegExp>
  19. #include <QDateTime>
  20. #include <QTimerEvent>
  21. #include <QStringList>
  22. #include <QDebug>
  23. SshClient::SshClient(App* app, QObject *parent) :
  24. QObject(parent),
  25. myApp(app),
  26. myProcess(new QProcess(this)),
  27. 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(.+)$")),
  28. myConnectedFlag(false),
  29. myConnectionTimerID(0),
  30. myPingTimerID(0),
  31. myPongTimerID(0),
  32. myClients(new QStringList())
  33. {
  34. connect(myProcess, SIGNAL(readyRead()), SLOT(read()));
  35. connect(myProcess, SIGNAL(finished(int)), SLOT(exited(int)));
  36. }
  37. SshClient::~SshClient()
  38. {
  39. delete myCommandRegex;
  40. delete myClients;
  41. }
  42. bool SshClient::isConnected() const
  43. {
  44. return myConnectedFlag;
  45. }
  46. void SshClient::read()
  47. {
  48. QString data(myProcess->readAll().trimmed());
  49. if(data.isEmpty())
  50. return;
  51. QStringList lines = data.split("\n", QString::SkipEmptyParts);
  52. QString line;
  53. foreach(line, lines)
  54. {
  55. line = line.trimmed();
  56. if(myCommandRegex->indexIn(line) != -1)
  57. {
  58. QDateTime time = QDateTime::fromString(myCommandRegex->cap(1), "yyyy-MM-dd HH:mm:ss");
  59. time = time.addSecs(-myCommandRegex->cap(2).toInt()/100*60*60);
  60. parse(time,
  61. myCommandRegex->cap(3).section(' ', 0, 0),
  62. myCommandRegex->cap(3).section(' ', 1)
  63. );
  64. }
  65. }
  66. }
  67. void SshClient::exited(int exitCode)
  68. {
  69. if(exitCode)
  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. int serverCount, userCount;
  136. myApp->broadcast(a.cap(3) + " " + a.cap(5) + " - " + a.cap(4) + " : " + a.cap(6), &serverCount, &userCount);
  137. if(userCount)
  138. write("BC_RE " + a.cap(1) + " Players=" + QString::number(userCount) + ",Servers=" + QString::number(serverCount) + "\n");
  139. return;
  140. }
  141. /* BC_ID - Broadcast reply with the ID of the broadcast you just generated */
  142. if(command == "BC_ID")
  143. {
  144. //BC_ID 4e5fca581569e168c7a86a0a9a91949f for: QDEV,-dev-,qw://123.123
  145. QRegExp a("^([A-Za-z0-9]+) for: .+,-(.+)-,qw://(.+)$");
  146. if(a.indexIn(commandData) == -1)
  147. return;
  148. QString hash = a.cap(1);
  149. //QString frequency = a.cap(2);
  150. QString server = a.cap(3);
  151. myApp->setReplyHash(server, hash);
  152. return;
  153. }
  154. /* BC_RE - Broadcast reply to increment the counters */
  155. if(command == "BC_RE")
  156. {
  157. QRegExp a("^([A-Za-z0-9]+) (Users|Players)=([0-9]+),(Channels|Servers)=([0-9]+)$");
  158. if(a.indexIn(commandData) == -1)
  159. return;
  160. QString hash = a.cap(1);
  161. QString userType = a.cap(2);
  162. QString channelType = a.cap(4);
  163. int userCount = 0;
  164. int channelCount = 0;
  165. int playerCount = 0;
  166. int serverCount = 0;
  167. if(userType == "Users")
  168. {
  169. userCount = a.cap(3).toInt();
  170. playerCount = 0;
  171. }
  172. else if(userType == "Players")
  173. {
  174. playerCount = a.cap(3).toInt();
  175. userCount = 0;
  176. }
  177. if(channelType == "Channels")
  178. {
  179. channelCount = a.cap(5).toInt();
  180. serverCount = 0;
  181. }
  182. else if(channelType == "Servers")
  183. {
  184. serverCount = a.cap(5).toInt();
  185. channelCount = 0;
  186. }
  187. myApp->incrementReplyCounters(hash, userCount, channelCount, playerCount, serverCount);
  188. return;
  189. }
  190. myApp->print("Unparsed cmd from central: " + time.toString() + " " + command + " " + commandData + "\n");
  191. }
  192. bool SshClient::connectToHost(const QString &user, const QString &host)
  193. {
  194. myProcess->start("ssh", QStringList() << user + "@" + host);
  195. myUsername = user;
  196. myHostname = host;
  197. bool r = myProcess->waitForStarted();
  198. if(!r)
  199. return false;
  200. else
  201. {
  202. myConnectionTimerID = startTimer(30000);
  203. return true;
  204. }
  205. }
  206. void SshClient::reconnect()
  207. {
  208. if(!myUsername.isEmpty() && !myHostname.isEmpty())
  209. connectToHost(myUsername, myHostname);
  210. }
  211. void SshClient::disconnectFromHost()
  212. {
  213. killTimer(myConnectionTimerID);
  214. killTimer(myPingTimerID);
  215. killTimer(myPongTimerID);
  216. myProcess->terminate();
  217. myProcess->waitForFinished();
  218. myConnectedFlag = false;
  219. }
  220. void SshClient::timerEvent(QTimerEvent *e)
  221. {
  222. if(e->timerId() == myConnectionTimerID)
  223. {
  224. disconnectFromHost();
  225. emit error(ConnectionTimedOutError);
  226. return;
  227. }
  228. if(e->timerId() == myPingTimerID)
  229. {
  230. disconnectFromHost();
  231. emit error(ConnectionTimedOutError);
  232. return;
  233. }
  234. if(e->timerId() == myPongTimerID)
  235. {
  236. killTimer(myPongTimerID);
  237. ping();
  238. return;
  239. }
  240. }