Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

main.cpp 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <cstdlib>
  5. #include "../CodeDweller/faults.hpp"
  6. #include "../SNFMulti/GBUdb.hpp"
  7. namespace cd = codedweller;
  8. const std::string VersionInfo = "V0.1";
  9. const int ErrorResultCode = 1;
  10. const int SuccessResultCode = 0;
  11. struct Configuration {
  12. std::string GBXFile;
  13. double Probability;
  14. double Confidence;
  15. bool findBlack;
  16. bool showDetails;
  17. Configuration():
  18. GBXFile(""),
  19. Probability(0.0),
  20. Confidence(0.0),
  21. findBlack(true),
  22. showDetails(false) {}
  23. } myConfig;
  24. class BadArguments : public cd::RuntimeFault {public: BadArguments(std::string s):RuntimeFault(s){}};
  25. const BadArguments BadArgumentCount("Wrong number of arguments");
  26. const BadArguments BadSwitch("Bad command line switch");
  27. void setConfiguration(Configuration &conf, int argc, char* argv[]) {
  28. BadArgumentCount(argc > 6 || argc < 1);
  29. for(int i = 1; i < argc; i++) {
  30. std::string thisArgument(argv[i]);
  31. if(0 == thisArgument.find("-black")) {
  32. conf.findBlack = true;
  33. }
  34. else if(0 == thisArgument.find("-white")) {
  35. conf.findBlack = false;
  36. }
  37. else if(0 == thisArgument.find("-details")) {
  38. conf.showDetails = true;
  39. }
  40. else if(0 == thisArgument.find("-p=")) {
  41. conf.Probability = strtod(thisArgument.substr(3).c_str(),0);
  42. }
  43. else if(0 == thisArgument.find("-c=")) {
  44. conf.Confidence = strtod(thisArgument.substr(3).c_str(),0);
  45. }
  46. else if(0 == thisArgument.find("-")) {
  47. BadSwitch(true);
  48. }
  49. else {
  50. conf.GBXFile = thisArgument;
  51. }
  52. }
  53. }
  54. void displayHelp() {
  55. std::cerr << "GBUDBTool " << VersionInfo << std::endl;
  56. std::cerr << "Use: GBUDBTool [-black | -white] [-p=<value>] [-c=<value>] <gbx file>" << std::endl;
  57. std::cerr << " -black prints out blacklist IPs based on p & c" << std::endl;
  58. std::cerr << " -white prints out whitelist IPs based on p & c" << std::endl;
  59. std::cerr << " -details prints out counts, confidence, and probability" << std::endl;
  60. std::cerr << " -c=<value> sets the confidence figure for the list" << std::endl;
  61. std::cerr << " -p=<value> sets the probability figure for the list" << std::endl;
  62. std::cerr << " <gbx file> is the path to a GBUdb snapshot" << std::endl;
  63. }
  64. class Reporter : public GBUdbOperator {
  65. private:
  66. Configuration myConfig;
  67. std::string toIP4String(unsigned int IP) {
  68. int a, b, c, d;
  69. d = IP & 0x000000ff; IP >>= 8;
  70. c = IP & 0x000000ff; IP >>= 8;
  71. b = IP & 0x000000ff; IP >>= 8;
  72. a = IP;
  73. std::ostringstream S;
  74. S << a << "." << b << "." << c << "." << d;
  75. return S.str();
  76. }
  77. public:
  78. Reporter(Configuration Config) : myConfig(Config) {}
  79. GBUdbRecord& operator()(unsigned int IP, GBUdbRecord& R) {
  80. bool goodConfidence = (myConfig.Confidence <= R.Confidence());
  81. bool goodProbability = false;
  82. if(myConfig.findBlack) goodProbability = (myConfig.Probability <= R.Probability());
  83. else goodProbability = (myConfig.Probability <= R.Probability());
  84. if(goodProbability && goodConfidence) {
  85. std::cout << toIP4String(IP);
  86. if(myConfig.showDetails) {
  87. std::cout << "\t"
  88. << "g=" << R.Good()
  89. << ", b=" << R.Bad()
  90. << ", c=" << R.Confidence()
  91. << ", p=" << R.Probability();
  92. }
  93. std::cout << std::endl;
  94. }
  95. return R;
  96. }
  97. };
  98. int main(int argc, char* argv[]) {
  99. try {
  100. setConfiguration(myConfig, argc, argv);
  101. GBUdbDataset DB(myConfig.GBXFile.c_str());
  102. DB.load();
  103. Reporter R(myConfig);
  104. DB.doForAllRecords(R);
  105. }
  106. catch(const std::exception &e) {
  107. displayHelp();
  108. std::cerr << std::endl << "Exception: " << e.what() << std::endl;
  109. return ErrorResultCode;
  110. }
  111. return SuccessResultCode;
  112. }