basefiles.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. struct option
  2. {
  3. int proc = 0;
  4. int mode = 0;
  5. int log = 0;
  6. int high = 0;
  7. int mesh = 0;
  8. std::string str_search;
  9. std::string rgx_search;
  10. std::string outputfile;
  11. };
  12. option conf;
  13. int config()
  14. {
  15. std::ifstream conffile ("sygcpp.conf");
  16. if(!conffile) // проверка наличия конфига
  17. {
  18. std::cout << " Configuration file not found... ";
  19. conffile.close();
  20. std::ofstream newconf ("sygcpp.conf"); // создание конфига
  21. if(!newconf)
  22. {
  23. std::cerr << "CREATION FAILED" << std::endl;
  24. return -1;
  25. }
  26. newconf << "####################################################################\n"
  27. << "# SimpleYggGen C++ configuration file. #\n"
  28. << "# If you have some errors, try delete this file and run SYG again. #\n"
  29. << "####################################################################\n\n"
  30. << " Parameter limited by processors count on PC.\n"
  31. << "* Count of thread: 16\n\n"
  32. << " 0 - IPv6 pattern, 1 - high address, 2 - search by pattern & high,\n"
  33. << " 3 - IPv6 regexp, 4 - meshname pattern, 5 - meshname regexp\n"
  34. << "* Mining option: 1\n\n"
  35. << " 0 - console output only, 1 - log to file.\n"
  36. << "* Logging mode: 1\n\n"
  37. << " High address search. Parameter is set in hexadecimal (0-9, a-f).\n"
  38. << "* Start position (2xx): 14\n\n"
  39. << " Used when \"Mining option\" set as 0, 2 or 4.\n"
  40. << " Meshname domains use base32 (RFC4648) alphabet symbols.\n"
  41. << "* Pattern: ::\n\n"
  42. << " Used when \"Mining option\" set as 3 or 5. Extended grep type.\n"
  43. << " When searching meshname domain, don't need use \".meshname\".\n"
  44. << "* Regexp: ^2.*f{1,4}.*.ace:(6|9)$\n\n"
  45. << " 0 - disable, 1 - enable.\n"
  46. << "* Display meshname domains: 1";
  47. newconf.close();
  48. std::ifstream conffile ("sygcpp.conf");
  49. if(conffile)
  50. std::cout << "CREATED" << std::endl;
  51. config();
  52. return 0;
  53. } else { // чтение конфигурации
  54. std::string str_temp_read;
  55. std::string str_read;
  56. while(getline(conffile, str_temp_read))
  57. str_read += str_temp_read;
  58. conffile.close();
  59. struct check
  60. {
  61. bool proc = false;
  62. bool mode = false;
  63. bool log = false;
  64. bool high = false;
  65. bool mesh = false;
  66. bool str_search = false;
  67. bool rgx_search = false;
  68. bool ok()
  69. {
  70. return(proc & mode & log & high & mesh & str_search & rgx_search);
  71. }
  72. };
  73. check complete;
  74. std::istringstream ss_input(str_read); // чтение конфига
  75. while(!ss_input.eof())
  76. {
  77. ss_input >> str_temp_read;
  78. if(str_temp_read == "thread:") // поиск параметра по предыдущему слову
  79. {
  80. ss_input >> conf.proc; // запись в соответствующую переменную
  81. if(ss_input.fail())
  82. {
  83. std::cerr << " Count of thread value incorrect." << std::endl;
  84. return -2;
  85. }
  86. complete.proc = true;
  87. }
  88. if(str_temp_read == "option:")
  89. {
  90. ss_input >> conf.mode;
  91. if(ss_input.fail() || (conf.mode > 5 || conf.mode < 0))
  92. {
  93. std::cerr << " Mining option value incorrect." << std::endl;
  94. return -3;
  95. }
  96. complete.mode = true;
  97. }
  98. if(str_temp_read == "mode:")
  99. {
  100. ss_input >> conf.log;
  101. if(ss_input.fail() || (conf.log != 0 && conf.log != 1))
  102. {
  103. std::cerr << " Logging mode value incorrect." << std::endl;
  104. return -4;
  105. }
  106. complete.log = true;
  107. }
  108. if(str_temp_read == "(2xx):")
  109. {
  110. ss_input >> std::hex >> conf.high;
  111. if(ss_input.fail())
  112. {
  113. std::cerr << " Start position value incorrect." << std::endl;
  114. return -5;
  115. }
  116. complete.high = true;
  117. }
  118. if(str_temp_read == "Pattern:")
  119. {
  120. ss_input >> conf.str_search;
  121. if(ss_input.fail())
  122. {
  123. std::cerr << " Pattern value incorrect." << std::endl;
  124. return -6;
  125. }
  126. complete.str_search = true;
  127. }
  128. if(str_temp_read == "Regexp:")
  129. {
  130. ss_input >> conf.rgx_search;
  131. if(ss_input.fail())
  132. {
  133. std::cerr << " Regexp value incorrect." << std::endl;
  134. return -7;
  135. }
  136. complete.rgx_search = true;
  137. }
  138. if(str_temp_read == "domains:")
  139. {
  140. ss_input >> conf.mesh;
  141. if(ss_input.fail() || (conf.mesh != 0 && conf.mesh != 1))
  142. {
  143. std::cerr << " Meshname display value incorrect." << std::endl;
  144. return -8;
  145. }
  146. complete.mesh = true;
  147. }
  148. }
  149. if(!complete.ok())
  150. {
  151. std::cerr << " Corrupted configuration file. Some parameters not found." << std::endl;
  152. return -9;
  153. }
  154. unsigned int processor_count = std::thread::hardware_concurrency(); // кол-во процессоров
  155. if (conf.proc > (int)processor_count)
  156. conf.proc = (int)processor_count;
  157. countsize = 800 << __bsrq(conf.proc);
  158. }
  159. return 0;
  160. }
  161. void DisplayConfig()
  162. {
  163. std::cout << " Threads: " << conf.proc << ", ";
  164. if(conf.mode == 0)
  165. std::cout << "IPv6 pattern (" << conf.str_search << "), ";
  166. else if(conf.mode == 1)
  167. std::cout << "search high addresses (2" << std::setw(2) << std::setfill('0') <<
  168. std::hex << conf.high << std::dec << "+), ";
  169. else if(conf.mode == 2)
  170. std::cout << "search by pattern & high (" << conf.str_search << " & 2" <<
  171. std::setw(2) << std::setfill('0') << std::hex << conf.high << std::dec << "+), ";
  172. else if(conf.mode == 3)
  173. std::cout << "IPv6 regexp (" << conf.rgx_search << "), ";
  174. else if(conf.mode == 4)
  175. std::cout << "meshname pattern (" << conf.str_search << "), ";
  176. else if(conf.mode == 5)
  177. std::cout << "meshname regexp (" << conf.rgx_search << "), ";
  178. if(conf.log)
  179. std::cout << "logging to text file.";
  180. else
  181. std::cout << "console log only.";
  182. std::cout << std::endl << std::endl;
  183. }
  184. void testoutput()
  185. {
  186. if(conf.log) // проверка включено ли логирование
  187. {
  188. if(conf.mode == 0)
  189. conf.outputfile = "syg-ipv6-pattern.txt";
  190. else if(conf.mode == 1)
  191. conf.outputfile = "syg-ipv6-high.txt";
  192. else if(conf.mode == 2)
  193. conf.outputfile = "syg-ipv6-pattern-high.txt";
  194. else if(conf.mode == 3)
  195. conf.outputfile = "syg-ipv6-regexp.txt";
  196. else if(conf.mode == 4)
  197. conf.outputfile = "syg-meshname-pattern.txt";
  198. else if(conf.mode == 5)
  199. conf.outputfile = "syg-meshname-regexp.txt";
  200. std::ifstream test(conf.outputfile);
  201. if(!test) // проверка наличия выходного файла
  202. {
  203. test.close();
  204. std::ofstream output(conf.outputfile);
  205. output << "**************************************************************************\n"
  206. << "Change EncryptionPublicKey and EncryptionPrivateKey to your yggdrasil.conf\n"
  207. << "Windows: C:\\ProgramData\\Yggdrasil\\yggdrasil.conf\n"
  208. << "Debian: /etc/yggdrasil.conf\n"
  209. << "**************************************************************************\n";
  210. output.close();
  211. } else test.close();
  212. }
  213. }