client.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2024 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. std::vector<std::string> Client::GetOpt()
  151. {
  152. return records_opt_;
  153. }
  154. int Client::GetAnswerNum()
  155. {
  156. return answer_num_;
  157. }
  158. int Client::GetAuthorityNum()
  159. {
  160. return authority_num_;
  161. }
  162. std::string Client::GetStatus()
  163. {
  164. return status_;
  165. }
  166. std::string Client::GetServer()
  167. {
  168. return server_;
  169. }
  170. int Client::GetQueryTime()
  171. {
  172. return query_time_;
  173. }
  174. int Client::GetMsgSize()
  175. {
  176. return msg_size_;
  177. }
  178. std::string Client::GetFlags()
  179. {
  180. return flags_;
  181. }
  182. std::string Client::GetResult()
  183. {
  184. return result_;
  185. }
  186. void Client::Clear()
  187. {
  188. result_.clear();
  189. answer_num_ = 0;
  190. status_.clear();
  191. server_.clear();
  192. query_time_ = 0;
  193. msg_size_ = 0;
  194. flags_.clear();
  195. records_query_.clear();
  196. records_answer_.clear();
  197. records_authority_.clear();
  198. records_additional_.clear();
  199. }
  200. void Client::PrintResult()
  201. {
  202. std::cout << result_ << std::endl;
  203. }
  204. bool Client::ParserRecord(const std::string &record_str, std::vector<DNSRecord> &record)
  205. {
  206. DNSRecord r;
  207. std::vector<std::string> lines = StringSplit(record_str, '\n');
  208. for (auto &line : lines) {
  209. if (r.Parser(line) == false) {
  210. return false;
  211. }
  212. record.push_back(r);
  213. }
  214. return true;
  215. }
  216. bool Client::ParserResult()
  217. {
  218. std::smatch match;
  219. std::regex reg_goanswer(";; Got answer:");
  220. if (std::regex_search(result_, match, reg_goanswer) == false) {
  221. std::cout << "DIG FAILED:\n" << result_ << std::endl;
  222. return false;
  223. }
  224. std::regex reg_opt(";; OPT PSEUDOSECTION:\\n((?:.|\\n|\\r\\n)+?)\\n;;",
  225. std::regex::ECMAScript | std::regex::optimize);
  226. if (std::regex_search(result_, match, reg_opt)) {
  227. std::string opt_str = match[1];
  228. std::vector<std::string> lines = StringSplit(opt_str, '\n');
  229. for (auto &line : lines) {
  230. if (line.length() <= 0) {
  231. continue;
  232. }
  233. line = line.substr(2);
  234. records_opt_.push_back(line);
  235. }
  236. }
  237. std::regex reg_answer_num(", ANSWER: ([0-9]+),");
  238. if (std::regex_search(result_, match, reg_answer_num)) {
  239. answer_num_ = std::stoi(match[1]);
  240. }
  241. std::regex reg_authority_num(", AUTHORITY: ([0-9]+),");
  242. if (std::regex_search(result_, match, reg_authority_num)) {
  243. authority_num_ = std::stoi(match[1]);
  244. }
  245. std::regex reg_status(", status: ([A-Z]+),");
  246. if (std::regex_search(result_, match, reg_status)) {
  247. status_ = match[1];
  248. }
  249. std::regex reg_server(";; SERVER: ([0-9.]+)#");
  250. if (std::regex_search(result_, match, reg_server)) {
  251. server_ = match[1];
  252. }
  253. std::regex reg_querytime(";; Query time: ([0-9]+) msec");
  254. if (std::regex_search(result_, match, reg_querytime)) {
  255. query_time_ = std::stoi(match[1]);
  256. }
  257. std::regex reg_msg_size(";; MSG SIZE rcvd: ([0-9]+)");
  258. if (std::regex_search(result_, match, reg_msg_size)) {
  259. msg_size_ = std::stoi(match[1]);
  260. }
  261. std::regex reg_flags(";; flags: ([a-z A-Z]+);");
  262. if (std::regex_search(result_, match, reg_flags)) {
  263. flags_ = match[1];
  264. }
  265. std::regex reg_question(";; QUESTION SECTION:\\n((?:.|\\n|\\r\\n)+?)\\n{2,}",
  266. std::regex::ECMAScript | std::regex::optimize);
  267. if (std::regex_search(result_, match, reg_question)) {
  268. if (ParserRecord(match[1], records_query_) == false) {
  269. return false;
  270. }
  271. }
  272. std::regex reg_answer(";; ANSWER SECTION:\\n((?:.|\\n|\\r\\n)+?)\\n{2,}",
  273. std::regex::ECMAScript | std::regex::optimize);
  274. if (std::regex_search(result_, match, reg_answer)) {
  275. if (ParserRecord(match[1], records_answer_) == false) {
  276. return false;
  277. }
  278. if (answer_num_ != records_answer_.size()) {
  279. std::cout << "DIG FAILED: Num Not Match\n" << result_ << std::endl;
  280. return false;
  281. }
  282. }
  283. std::regex reg_authority(";; AUTHORITY SECTION:\\n((?:.|\\n|\\r\\n)+?)\\n{2,}",
  284. std::regex::ECMAScript | std::regex::optimize);
  285. if (std::regex_search(result_, match, reg_authority)) {
  286. if (ParserRecord(match[1], records_authority_) == false) {
  287. return false;
  288. }
  289. if (authority_num_ != records_authority_.size()) {
  290. std::cout << "DIG FAILED: Num Not Match\n" << result_ << std::endl;
  291. return false;
  292. }
  293. }
  294. std::regex reg_addition(";; ADDITIONAL SECTION:\\n((?:.|\\n|\\r\\n)+?)\\n{2,}",
  295. std::regex::ECMAScript | std::regex::optimize);
  296. if (std::regex_search(result_, match, reg_answer)) {
  297. if (ParserRecord(match[1], records_additional_) == false) {
  298. return false;
  299. }
  300. }
  301. return true;
  302. }
  303. Client::~Client() {}
  304. } // namespace smartdns