client.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2023 Ruilin Peng (Nick) <[email protected]>.
  4. *
  5. * smartdns is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * smartdns is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "client.h"
  19. #include <iostream>
  20. #include <memory>
  21. #include <regex>
  22. #include <signal.h>
  23. #include <string>
  24. #include <sys/types.h>
  25. #include <sys/wait.h>
  26. #include <vector>
  27. namespace smartdns
  28. {
  29. std::vector<std::string> StringSplit(const std::string &s, const char delim)
  30. {
  31. std::vector<std::string> ret;
  32. std::string::size_type lastPos = s.find_first_not_of(delim, 0);
  33. std::string::size_type pos = s.find_first_of(delim, lastPos);
  34. while (std::string::npos != pos || std::string::npos != lastPos) {
  35. ret.push_back(s.substr(lastPos, pos - lastPos));
  36. lastPos = s.find_first_not_of(delim, pos);
  37. pos = s.find_first_of(delim, lastPos);
  38. }
  39. return ret;
  40. }
  41. DNSRecord::DNSRecord() {}
  42. DNSRecord::~DNSRecord() {}
  43. bool DNSRecord::Parser(const std::string &line)
  44. {
  45. std::vector<std::string> fields_first = StringSplit(line, '\t');
  46. std::vector<std::string> fields;
  47. for (const auto &f : fields_first) {
  48. std::vector<std::string> fields_second = StringSplit(f, ' ');
  49. for (const auto &s : fields_second) {
  50. if (s.length() > 0) {
  51. fields.push_back(s);
  52. }
  53. }
  54. }
  55. if (fields.size() < 3) {
  56. std::cerr << "Invalid DNS record: " << line << ", size: " << fields.size() << std::endl;
  57. return false;
  58. }
  59. if (fields.size() == 3) {
  60. name_ = fields[0];
  61. if (name_.size() > 1) {
  62. name_.resize(name_.size() - 1);
  63. }
  64. class_ = fields[1];
  65. type_ = fields[2];
  66. return true;
  67. }
  68. name_ = fields[0];
  69. if (name_.size() > 1) {
  70. name_.resize(name_.size() - 1);
  71. }
  72. ttl_ = std::stoi(fields[1]);
  73. class_ = fields[2];
  74. type_ = fields[3];
  75. data_ = fields[4];
  76. for (int i = 5; i < fields.size(); i++) {
  77. data_ += " " + fields[i];
  78. }
  79. return true;
  80. }
  81. std::string DNSRecord::GetName()
  82. {
  83. return name_;
  84. }
  85. std::string DNSRecord::GetType()
  86. {
  87. return type_;
  88. }
  89. std::string DNSRecord::GetClass()
  90. {
  91. return class_;
  92. }
  93. int DNSRecord::GetTTL()
  94. {
  95. return ttl_;
  96. }
  97. std::string DNSRecord::GetData()
  98. {
  99. return data_;
  100. }
  101. Client::Client() {}
  102. bool Client::Query(const std::string &dig_cmds, int port, const std::string &ip)
  103. {
  104. Clear();
  105. std::string cmd = "dig ";
  106. if (port > 0) {
  107. cmd += "-p " + std::to_string(port);
  108. }
  109. if (ip.length() > 0) {
  110. cmd += " @" + ip;
  111. } else {
  112. cmd += " @127.0.0.1";
  113. }
  114. cmd += " " + dig_cmds;
  115. cmd += " +tries=1";
  116. FILE *fp = nullptr;
  117. fp = popen(cmd.c_str(), "r");
  118. if (fp == nullptr) {
  119. return false;
  120. }
  121. std::shared_ptr<FILE> pipe(fp, pclose);
  122. result_.clear();
  123. char buffer[4096];
  124. usleep(10000);
  125. while (fgets(buffer, 4096, pipe.get())) {
  126. result_ += buffer;
  127. }
  128. if (ParserResult() == false) {
  129. Clear();
  130. return false;
  131. }
  132. return true;
  133. }
  134. std::vector<DNSRecord> Client::GetQuery()
  135. {
  136. return records_query_;
  137. }
  138. std::vector<DNSRecord> Client::GetAnswer()
  139. {
  140. return records_answer_;
  141. }
  142. std::vector<DNSRecord> Client::GetAuthority()
  143. {
  144. return records_authority_;
  145. }
  146. std::vector<DNSRecord> Client::GetAdditional()
  147. {
  148. return records_additional_;
  149. }
  150. int Client::GetAnswerNum()
  151. {
  152. return answer_num_;
  153. }
  154. int Client::GetAuthorityNum()
  155. {
  156. return authority_num_;
  157. }
  158. std::string Client::GetStatus()
  159. {
  160. return status_;
  161. }
  162. std::string Client::GetServer()
  163. {
  164. return server_;
  165. }
  166. int Client::GetQueryTime()
  167. {
  168. return query_time_;
  169. }
  170. int Client::GetMsgSize()
  171. {
  172. return msg_size_;
  173. }
  174. std::string Client::GetFlags()
  175. {
  176. return flags_;
  177. }
  178. std::string Client::GetResult()
  179. {
  180. return result_;
  181. }
  182. void Client::Clear()
  183. {
  184. result_.clear();
  185. answer_num_ = 0;
  186. status_.clear();
  187. server_.clear();
  188. query_time_ = 0;
  189. msg_size_ = 0;
  190. flags_.clear();
  191. records_query_.clear();
  192. records_answer_.clear();
  193. records_authority_.clear();
  194. records_additional_.clear();
  195. }
  196. void Client::PrintResult()
  197. {
  198. std::cout << result_ << std::endl;
  199. }
  200. bool Client::ParserRecord(const std::string &record_str, std::vector<DNSRecord> &record)
  201. {
  202. DNSRecord r;
  203. std::vector<std::string> lines = StringSplit(record_str, '\n');
  204. for (auto &line : lines) {
  205. if (r.Parser(line) == false) {
  206. return false;
  207. }
  208. record.push_back(r);
  209. }
  210. return true;
  211. }
  212. bool Client::ParserResult()
  213. {
  214. std::smatch match;
  215. std::regex reg_goanswer(";; Got answer:");
  216. if (std::regex_search(result_, match, reg_goanswer) == false) {
  217. std::cout << "DIG FAILED:\n" << result_ << std::endl;
  218. return false;
  219. }
  220. std::regex reg_answer_num(", ANSWER: ([0-9]+),");
  221. if (std::regex_search(result_, match, reg_answer_num)) {
  222. answer_num_ = std::stoi(match[1]);
  223. }
  224. std::regex reg_authority_num(", AUTHORITY: ([0-9]+),");
  225. if (std::regex_search(result_, match, reg_authority_num)) {
  226. authority_num_ = std::stoi(match[1]);
  227. }
  228. std::regex reg_status(", status: ([A-Z]+),");
  229. if (std::regex_search(result_, match, reg_status)) {
  230. status_ = match[1];
  231. }
  232. std::regex reg_server(";; SERVER: ([0-9.]+)#");
  233. if (std::regex_search(result_, match, reg_server)) {
  234. server_ = match[1];
  235. }
  236. std::regex reg_querytime(";; Query time: ([0-9]+) msec");
  237. if (std::regex_search(result_, match, reg_querytime)) {
  238. query_time_ = std::stoi(match[1]);
  239. }
  240. std::regex reg_msg_size(";; MSG SIZE rcvd: ([0-9]+)");
  241. if (std::regex_search(result_, match, reg_msg_size)) {
  242. msg_size_ = std::stoi(match[1]);
  243. }
  244. std::regex reg_flags(";; flags: ([a-z A-Z]+);");
  245. if (std::regex_search(result_, match, reg_flags)) {
  246. flags_ = match[1];
  247. }
  248. std::regex reg_question(";; QUESTION SECTION:\\n((?:.|\\n|\\r\\n)+?)\\n{2,}",
  249. std::regex::ECMAScript | std::regex::optimize);
  250. if (std::regex_search(result_, match, reg_question)) {
  251. if (ParserRecord(match[1], records_query_) == false) {
  252. return false;
  253. }
  254. }
  255. std::regex reg_answer(";; ANSWER SECTION:\\n((?:.|\\n|\\r\\n)+?)\\n{2,}",
  256. std::regex::ECMAScript | std::regex::optimize);
  257. if (std::regex_search(result_, match, reg_answer)) {
  258. if (ParserRecord(match[1], records_answer_) == false) {
  259. return false;
  260. }
  261. if (answer_num_ != records_answer_.size()) {
  262. std::cout << "DIG FAILED: Num Not Match\n" << result_ << std::endl;
  263. return false;
  264. }
  265. }
  266. std::regex reg_authority(";; AUTHORITY SECTION:\\n((?:.|\\n|\\r\\n)+?)\\n{2,}",
  267. std::regex::ECMAScript | std::regex::optimize);
  268. if (std::regex_search(result_, match, reg_authority)) {
  269. if (ParserRecord(match[1], records_authority_) == false) {
  270. return false;
  271. }
  272. if (authority_num_ != records_authority_.size()) {
  273. std::cout << "DIG FAILED: Num Not Match\n" << result_ << std::endl;
  274. return false;
  275. }
  276. }
  277. std::regex reg_addition(";; ADDITIONAL SECTION:\\n((?:.|\\n|\\r\\n)+?)\\n{2,}",
  278. std::regex::ECMAScript | std::regex::optimize);
  279. if (std::regex_search(result_, match, reg_answer)) {
  280. if (ParserRecord(match[1], records_additional_) == false) {
  281. return false;
  282. }
  283. }
  284. return true;
  285. }
  286. Client::~Client() {}
  287. } // namespace smartdns