ServerQuery.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 "ServerQuery.h"
  16. #include <QUdpSocket>
  17. #include <QHostInfo>
  18. #include <QStringList>
  19. #include <QRegExp>
  20. #include <QTime>
  21. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  22. ServerQuery::ServerQuery(QObject *parent) :
  23. QObject(parent),
  24. mySocket(new QUdpSocket(this)),
  25. myPort(27500),
  26. myActiveFlag(false)
  27. {
  28. connect(mySocket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(socketError(QAbstractSocket::SocketError)));
  29. }
  30. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  31. ServerQuery::~ServerQuery()
  32. {
  33. delete mySocket;
  34. }
  35. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  36. void ServerQuery::run()
  37. {
  38. if(mySocket->hasPendingDatagrams())
  39. parseServerInfo();
  40. }
  41. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  42. bool ServerQuery::query(bool pingServer)
  43. {
  44. myActiveFlag = true;
  45. if(myAddress.isNull() || !myPort)
  46. {
  47. emit error(HostLookupError);
  48. myActiveFlag = false;
  49. return false;
  50. }
  51. if(mySocket->isOpen())
  52. mySocket->close();
  53. mySocket->connectToHost(myAddress, myPort);
  54. if(!mySocket->waitForConnected(100))
  55. {
  56. emit error(ConnectionError);
  57. myActiveFlag = false;
  58. return false;
  59. }
  60. if(mySocket->write("\xff\xff\xff\xffstatus 23\n", 14) == -1)
  61. {
  62. emit error(SendError);
  63. myActiveFlag = false;
  64. return false;
  65. }
  66. if(!mySocket->waitForBytesWritten(300))
  67. {
  68. emit error(SendError);
  69. myActiveFlag = false;
  70. return false;
  71. }
  72. if(pingServer)
  73. myPing = ping(10); //avg ping
  74. else
  75. myPing = 0;
  76. return true;
  77. }
  78. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  79. const QHostAddress &ServerQuery::address() const
  80. {
  81. return myAddress;
  82. }
  83. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  84. quint16 ServerQuery::port() const
  85. {
  86. return myPort;
  87. }
  88. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  89. bool ServerQuery::setAddress(const QHostAddress &address, quint16 port)
  90. {
  91. myAddress = address;
  92. myPort = port;
  93. return true;
  94. }
  95. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  96. bool ServerQuery::setAddress(const QString &address, quint16 port)
  97. {
  98. QHostInfo hi = QHostInfo::fromName(address);
  99. if(hi.error() != QHostInfo::NoError)
  100. {
  101. emit error(HostLookupError);
  102. return false;
  103. }
  104. myAddress = hi.addresses().at(0);
  105. myPort = port;
  106. return true;
  107. }
  108. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  109. void ServerQuery::parseServerInfo()
  110. {
  111. QByteArray serverData = mySocket->readAll();
  112. if(serverData.isEmpty())
  113. {
  114. emit error(EmptyResponseError);
  115. myActiveFlag = false;
  116. return;
  117. }
  118. if(!serverData.startsWith("\xff\xff\xff\xffn"))
  119. {
  120. emit error(InvalidResponseError);
  121. myActiveFlag = false;
  122. return;
  123. }
  124. /* Parse server rules */
  125. myRules.clear();
  126. QString infoString(serverData.data()+5);
  127. QStringList splitInfoString = infoString.split("\n", QString::SkipEmptyParts);
  128. int i;
  129. QStringList rules = splitInfoString.at(0).split("\\", QString::SkipEmptyParts);
  130. if((rules.size() % 2) != 0)
  131. {
  132. emit error(InvalidInfoStringError);
  133. myActiveFlag = false;
  134. return;
  135. }
  136. ServerRule rule;
  137. for(i = 0; i < rules.size(); i += 2)
  138. {
  139. rule.rule = rules.at(i);
  140. rule.value = rules.at(i+1);
  141. /* Proxy detection */
  142. if(rule.rule == "*QTV")
  143. {
  144. myIsProxyFlag = true;
  145. }
  146. else if(rule.rule == "*version")
  147. {
  148. QString value = rule.value;
  149. if(value.startsWith("qwfwd", Qt::CaseInsensitive) || value.startsWith("QTV", Qt::CaseInsensitive) || value.startsWith("2.91"))
  150. myIsProxyFlag = true;
  151. }
  152. /* Adjust FPS */
  153. if(rule.rule == "maxfps" && myPing > 0)
  154. myPing += rule.value.toUInt() * 12 / 77;
  155. myRules.append(rule);
  156. }
  157. /* Parse player info */
  158. myPlayers.clear();
  159. Player player;
  160. QRegExp regex("^([-0-9]+)\\s+([-0-9]+)\\s+([-0-9]+)\\s+([-0-9]+)\\s+\"([^\"]*)\"\\s+\"([^\"]*)\"\\s+([0-9]+)\\s+([0-9]+)(?:\\s+\"([^\"]*)\")?$");
  161. QStringList playerInfo;
  162. for(i = 1; i < splitInfoString.size(); i++)
  163. {
  164. if(regex.indexIn(splitInfoString.at(i)) == -1)
  165. {
  166. emit error(InvalidPlayerInfoError);
  167. continue;
  168. }
  169. playerInfo = regex.capturedTexts();
  170. player.id = playerInfo.at(1).toInt();
  171. player.frags = playerInfo.at(2).toInt();
  172. player.time = playerInfo.at(3).toInt();
  173. player.ping = playerInfo.at(4).toInt();
  174. player.name = convertNameFun(playerInfo.at(5));
  175. player.skin = playerInfo.at(6);
  176. player.topColor = playerInfo.at(7).toInt();
  177. player.bottomColor = playerInfo.at(8).toInt();
  178. player.team = convertNameFun(playerInfo.at(9));
  179. if(player.name.startsWith("\\s\\"))
  180. {
  181. player.spectator = true;
  182. player.team = "(spec)";
  183. player.name = player.name.replace(QRegExp("^\\\\s\\\\"), "");
  184. if(player.ping < 0)
  185. player.ping = -player.ping;
  186. }
  187. else
  188. {
  189. player.spectator = false;
  190. }
  191. myPlayers.append(player);
  192. }
  193. myActiveFlag = false;
  194. emit finished();
  195. return;
  196. }
  197. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  198. bool ServerQuery::isActive() const
  199. {
  200. return myActiveFlag;
  201. }
  202. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  203. void ServerQuery::socketError(QAbstractSocket::SocketError)
  204. {
  205. emit error(UnknownError);
  206. myActiveFlag = false;
  207. }
  208. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  209. int ServerQuery::ping(int count, int timeout)
  210. {
  211. int pong = 0;
  212. QTime timer;
  213. timer.start();
  214. for(int i = 0; i < count; i++)
  215. {
  216. mySocket->write("\xff\xff\xff\xffk\n");
  217. timer.restart();
  218. if(!mySocket->waitForReadyRead(timeout))
  219. {
  220. pong += 999;
  221. }
  222. else
  223. {
  224. if(mySocket->readAll() == "l")
  225. pong += timer.elapsed();
  226. else
  227. pong += 999;
  228. }
  229. }
  230. return pong/count;
  231. }
  232. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  233. PlayerList ServerQuery::playerList() const
  234. {
  235. return myPlayers;
  236. }
  237. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  238. ServerRules ServerQuery::serverRules() const
  239. {
  240. return myRules;
  241. }
  242. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  243. QString ServerQuery::serverRuleValue(const QString &ruleName) const
  244. {
  245. ServerRule r;
  246. foreach(r, myRules)
  247. {
  248. if(r.rule == ruleName)
  249. return r.value;
  250. }
  251. return QString();
  252. }
  253. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  254. char ServerQuery::ourReadableCharsTable[256] = { '.', '_' , '_' , '_' , '_' , '.' , '_' , '_' , '_' , '_' , '\n' , '_' , '\n' , '>' , '.' , '.',
  255. '[', ']', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '_', '_', '_'
  256. };
  257. bool ServerQuery::ourReadableCharsTableInitialized = false;
  258. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  259. void ServerQuery::fillReadableCharsTable()
  260. {
  261. int i;
  262. for(i = 32; i < 127; i++)
  263. ourReadableCharsTable[i] = ourReadableCharsTable[128 + i] = i;
  264. ourReadableCharsTable[127] = ourReadableCharsTable[128 + 127] = '_';
  265. for(i = 0; i < 32; i++)
  266. ourReadableCharsTable[128 + i] = ourReadableCharsTable[i];
  267. ourReadableCharsTable[128] = '_';
  268. ourReadableCharsTable[10 + 128] = '_';
  269. ourReadableCharsTable[12 + 128] = '_';
  270. ourReadableCharsTableInitialized = true;
  271. }
  272. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  273. QString ServerQuery::convertNameFun(const QString &name)
  274. {
  275. if(!ourReadableCharsTableInitialized)
  276. fillReadableCharsTable();
  277. QString stripped;
  278. for(int i = 0; i < name.length(); i++)
  279. stripped.append(QChar(ourReadableCharsTable[(unsigned char)name.at(i).toAscii()] & 127));
  280. return stripped;
  281. }