resolver.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. #include "cppcodec/base32_rfc4648.hpp"
  2. #include "resolver.h"
  3. #include "funcs.h"
  4. #include "HTTPheaders.h"
  5. #include <QTcpSocket>
  6. #include <QRegularExpression>
  7. #include <QJsonArray>
  8. #include <QDateTime>
  9. #include <QHostInfo>
  10. #include <QDebug>
  11. #include <QFile>
  12. #include <array>
  13. #ifdef _WIN32
  14. #include <ws2tcpip.h>
  15. #else
  16. #include <arpa/inet.h>
  17. #endif
  18. constexpr int LIMIT_TO_READ = 2048;
  19. Resolver::Resolver(const QString& addr, quint16 port, QObject* parent) :
  20. QObject(parent),
  21. m_tcpServer(new QTcpServer),
  22. m_address(addr),
  23. m_port(port),
  24. m_NoApi(false),
  25. m_NoHttp(false),
  26. m_NoResolve(false)
  27. {
  28. if (!m_tcpServer->listen(m_address, m_port)) {
  29. throw "Server not binded";
  30. }
  31. qInfo().noquote() << "<-" << m_tcpServer->serverAddress().toString() + " : " +
  32. QString::number(m_tcpServer->serverPort()) << "[" + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz") + "]";
  33. connect(m_tcpServer, SIGNAL(newConnection()), this, SLOT(slotNewUser()));
  34. }
  35. void Resolver::disableAPI()
  36. {
  37. m_NoApi = true;
  38. }
  39. void Resolver::disableWebPage()
  40. {
  41. m_NoHttp = true;
  42. }
  43. void Resolver::disableResolv()
  44. {
  45. m_NoResolve = true;
  46. http::HTML_PAGE.replace("IPv6 address or domain", "IPv6 address or meship domain");
  47. }
  48. void Resolver::slotNewUser()
  49. {
  50. if (!m_tcpServer->isListening()) return;
  51. QTcpSocket* clientSocket = m_tcpServer->nextPendingConnection();
  52. connect (clientSocket, SIGNAL(readyRead()), this, SLOT(slotReadyClient()));
  53. connect (clientSocket, SIGNAL(disconnected()), clientSocket, SLOT(deleteLater()));
  54. }
  55. void Resolver::slotReadyClient()
  56. {
  57. QTcpSocket* clientSocket = static_cast<QTcpSocket*>(sender());
  58. QString req = clientSocket->read(LIMIT_TO_READ);
  59. qInfo().noquote() << "\n->" << clientSocket->peerAddress().toString()
  60. << "[" + QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz") + "]";
  61. QSharedPointer<QTextStream> ts(new QTextStream(clientSocket));
  62. if (m_NoHttp and (req.startsWith("HEAD /") or req.startsWith("GET /toConverting") or req.startsWith("GET / ")))
  63. {
  64. qInfo().noquote() << " └ HTML (dropped)";
  65. *ts << http::HEADER_REJECT;
  66. }
  67. else if (m_NoApi and req.startsWith("GET /api/"))
  68. {
  69. qInfo().noquote() << " └ API (dropped)";
  70. JsonAnswer a;
  71. a.setValue("status", false);
  72. *ts << a.result();
  73. }
  74. else if (req.startsWith("GET /api/"))
  75. {
  76. qInfo().noquote() << " └ API";
  77. req.contains("toConverting=") ?
  78. processPage(req, ts, false) :
  79. startPage(ts, false);
  80. }
  81. else if (req.startsWith("GET /"))
  82. {
  83. qInfo().noquote() << " └ HTML";
  84. req.contains("toConverting=") ?
  85. processPage(req, ts, true) :
  86. startPage(ts, true);
  87. }
  88. else if (req.startsWith("HEAD /"))
  89. {
  90. qInfo().noquote() << " └ HEAD request";
  91. *ts << http::HEADER_OK;
  92. }
  93. clientSocket->close();
  94. }
  95. void Resolver::convertStrToRaw(const QString &str, Address &array)
  96. {
  97. inet_pton(AF_INET6, str.toUtf8(), (void*)array.data());
  98. }
  99. QString Resolver::getBase32(const Address &rawAddr)
  100. {
  101. return cppcodec::base32_rfc4648::encode(rawAddr.data(), ADDRIPV6_SIZE).c_str();
  102. }
  103. QString Resolver::decodeMeshToIP(const QString &meshname)
  104. {
  105. std::string mesh = pickupStringForMeshname(meshname.toStdString()) + "======"; // 6 паддингов - норма для IPv6 адреса
  106. std::vector<uint8_t> raw;
  107. try {
  108. raw = cppcodec::base32_rfc4648::decode(mesh);
  109. } catch (cppcodec::padding_error const&) {
  110. return QString();
  111. } catch (cppcodec::symbol_error const&) {
  112. return QString();
  113. }
  114. Address rawAddr;
  115. for(int i = 0; i < 16; ++i)
  116. rawAddr[i] = raw[i];
  117. return getAddress(rawAddr);
  118. }
  119. std::string Resolver::pickupStringForMeshname(std::string str)
  120. {
  121. bool dot = false;
  122. std::string::iterator delend;
  123. for (auto it = str.begin(); it != str.end(); it++)
  124. {
  125. *it = toupper(*it); // делаем все буквы заглавными для обработки
  126. if(*it == '.') {
  127. delend = it;
  128. dot = true;
  129. }
  130. }
  131. if (dot)
  132. for (auto it = str.end(); it != delend; it--)
  133. str.pop_back(); // удаляем доменную зону
  134. return str;
  135. }
  136. QString Resolver::getAddress(const Address& rawAddr)
  137. {
  138. char ipStrBuf[46];
  139. inet_ntop(AF_INET6, rawAddr.data(), ipStrBuf, 46);
  140. return QString(ipStrBuf);
  141. }
  142. void Resolver::startPage(QSharedPointer<QTextStream> ts, bool html)
  143. {
  144. if (html)
  145. {
  146. qInfo().noquote() << " └ Start page";
  147. QString page = http::HTML_PAGE;
  148. QString css = http::CSS_DEFAULT;
  149. QString text;
  150. if (m_NoApi and not m_NoResolve)
  151. {
  152. text = "<p>Input any domain to resolve or IPv6 to convert to <a href=\"https://github.com/zhoreeq/meshname\" style=\"text-decoration: none\">meship</a>.</p>";
  153. }
  154. else if (m_NoApi and m_NoResolve)
  155. {
  156. text = "<p>Input meship domain to resolve or IPv6 to convert to <a href=\"https://github.com/zhoreeq/meshname\" style=\"text-decoration: none\">meship</a>.</p>";
  157. }
  158. else if (not m_NoResolve)
  159. {
  160. text = "<p>Input any domain to resolve or IPv6 to convert to <a href=\"https://github.com/zhoreeq/meshname\" style=\"text-decoration: none\">meship</a>.</p>"
  161. "<p>Also you can use Mario DNS tool via API to get result in JSON (GET request like a /api/toConverting=yourvalue).";
  162. }
  163. else
  164. { // no resolve
  165. text = "<p>Input meship domain to resolve or IPv6 to convert to <a href=\"https://github.com/zhoreeq/meshname\" style=\"text-decoration: none\">meship</a>.</p>"
  166. "<p>Also you can use Mario DNS tool via API to get result in JSON (GET request like a /api/toConverting=yourvalue).";
  167. }
  168. css.replace("{{COLOR}}", "gray");
  169. page.replace("{{STYLES}}", css);
  170. page.replace("{{TEXT}}", text);
  171. *ts << page;
  172. }
  173. else
  174. {
  175. QString msg;
  176. if (m_NoResolve) {
  177. msg = "Push meship domain to resolve or IPv6 to convert to meship like a /api/toConverting=yourvalue";
  178. } else {
  179. msg = "Push any domain to resolve or IPv6 to convert to meship like a /api/toConverting=yourvalue";
  180. }
  181. qInfo().noquote() << " └ Incorrect request";
  182. JsonAnswer a;
  183. a.setValue("status", false);
  184. a.setValue("answer", msg);
  185. *ts << a.result();
  186. }
  187. }
  188. void Resolver::startPageWithMessage(const QString & value, QSharedPointer<QTextStream> ts, const QString & msg, bool html)
  189. {
  190. qInfo().noquote() << " └ " + msg;
  191. if (html)
  192. {
  193. QString page = http::HTML_PAGE;
  194. QString css = http::CSS_DEFAULT;
  195. css.replace("{{COLOR}}", "red");
  196. page.replace("{{STYLES}}", css);
  197. page.replace("{{TEXT}}", msg);
  198. if (!value.isEmpty()) {
  199. page.replace(QRegularExpression("placeholder=\".*domain\""), "value=\"" + value + "\"");
  200. }
  201. *ts << page;
  202. }
  203. else
  204. {
  205. JsonAnswer a;
  206. a.setValue("status", false);
  207. a.setValue("answer", msg);
  208. *ts << a.result();
  209. }
  210. }
  211. void Resolver::processPage(const QString& req, QSharedPointer<QTextStream> ts, bool html)
  212. {
  213. QString value = funcs::getValue(req, "toConverting");
  214. value.remove('+');
  215. value = QByteArray::fromPercentEncoding(value.toUtf8());
  216. qInfo().noquote() << " └ " + value;
  217. const uint8_t MAX_LEN = 60;
  218. if (value.size() > MAX_LEN)
  219. {
  220. startPageWithMessage("", ts, "Maximum input value length = " + QString::number(MAX_LEN), html);
  221. }
  222. else if (value == "version") {
  223. toVersion(value, ts, html);
  224. }
  225. else if (value.contains(":")) {
  226. toMeship(value, ts, html);
  227. }
  228. else if (value.endsWith(".meship")) {
  229. toIp(value, ts, html);
  230. }
  231. else if (value.contains(".") and not m_NoResolve) {
  232. toRealResolv(value, ts, html);
  233. }
  234. else
  235. {
  236. QString text;
  237. m_NoResolve
  238. ? text ="Input value must contains meship domain or IPv6"
  239. : text = "Input value must contains domain or IPv6";
  240. startPageWithMessage(value, ts, text, html);
  241. }
  242. }
  243. void Resolver::toMeship(const QString & value, QSharedPointer<QTextStream> ts, bool html)
  244. {
  245. Address rawAddr;
  246. convertStrToRaw(value, rawAddr);
  247. QString base32 = getBase32(rawAddr);
  248. base32 = base32.toLower();
  249. base32.remove('=');
  250. base32 += ".meship";
  251. if (html)
  252. {
  253. QString page = http::HTML_PAGE;
  254. QString css = http::CSS_DEFAULT;
  255. css.replace("{{COLOR}}", "green");
  256. page.replace("{{STYLES}}", css);
  257. page.replace("{{TEXT}}", base32);
  258. page.replace(QRegularExpression("placeholder=\".*domain\""), "value=\"" + value + "\"");
  259. *ts << page;
  260. }
  261. else
  262. {
  263. JsonAnswer a;
  264. a.setValue("status", true);
  265. a.setValue("answer", base32);
  266. *ts << a.result();
  267. }
  268. }
  269. void Resolver::toIp(const QString & value, QSharedPointer<QTextStream> ts, bool html)
  270. {
  271. QString result = decodeMeshToIP(value);
  272. if (result.isEmpty()) {
  273. startPageWithMessage(value, ts, "Failed: meship domain must have 26 base32 (RFC4648) characters only", html);
  274. return;
  275. }
  276. if (html)
  277. {
  278. QString page = http::HTML_PAGE;
  279. QString css = http::CSS_DEFAULT;
  280. css.replace("{{COLOR}}", "green");
  281. page.replace("{{STYLES}}", css);
  282. page.replace("{{TEXT}}", result);
  283. page.replace(QRegularExpression("placeholder=\".*domain\""), "value=\"" + value + "\"");
  284. *ts << page;
  285. }
  286. else
  287. {
  288. JsonAnswer a;
  289. a.setValue("status", true);
  290. a.setValue("answer", result);
  291. *ts << a.result();
  292. }
  293. }
  294. void Resolver::toRealResolv(const QString & value, QSharedPointer<QTextStream> ts, bool html)
  295. {
  296. QHostInfo host = QHostInfo::fromName(value);
  297. if (host.error() == QHostInfo::NoError)
  298. {
  299. auto addrs = host.addresses();
  300. if (html)
  301. {
  302. QString addresses;
  303. for (auto a: addrs) {
  304. addresses += a.toString() + "<br>";
  305. }
  306. QString page = http::HTML_PAGE;
  307. QString css = http::CSS_DEFAULT;
  308. css.replace("{{COLOR}}", "green");
  309. page.replace("{{STYLES}}", css);
  310. page.replace("{{TEXT}}", addresses);
  311. page.replace(QRegularExpression("placeholder=\".*domain\""), "value=\"" + value + "\"");
  312. *ts << page;
  313. }
  314. else
  315. {
  316. QJsonArray array;
  317. for (const auto& address: addrs)
  318. {
  319. array.push_back(address.toString());
  320. }
  321. JsonAnswer a;
  322. a.setValue("status", true);
  323. a.setValue("answer", array);
  324. *ts << a.result();
  325. }
  326. }
  327. else
  328. {
  329. QString msg;
  330. if (value.endsWith(".meshname")) {
  331. msg = "Domain not resolved. Try \".meship\" instead \".meshname\" for direct translation to address";
  332. } else {
  333. msg = "Failed: " + host.errorString();
  334. }
  335. startPageWithMessage(value, ts, msg, html);
  336. }
  337. }
  338. void Resolver::toVersion(const QString &value , QSharedPointer<QTextStream> ts, bool html)
  339. {
  340. if (html)
  341. {
  342. QString page = http::HTML_PAGE;
  343. QString css = http::CSS_DEFAULT;
  344. css.replace("{{COLOR}}", "gray");
  345. page.replace("{{STYLES}}", css);
  346. page.replace("{{TEXT}}", VERSION);
  347. page.replace(QRegularExpression("placeholder=\".*domain\""), "value=\"" + value + "\"");
  348. *ts << page;
  349. }
  350. else
  351. {
  352. JsonAnswer a;
  353. a.setValue("status", true);
  354. a.setValue("answer", VERSION);
  355. *ts << a.result();
  356. }
  357. }