Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <cstdlib>
  5. #include "../CodeDweller/faults.hpp"
  6. #include "../SNFMulti/GBUdb.hpp"
  7. using namespace std;
  8. const string VersionInfo = "V0.1";
  9. const int ErrorResultCode = 1;
  10. const int SuccessResultCode = 0;
  11. struct Configuration {
  12. 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 RuntimeFault {public: BadArguments(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. 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. cerr << "GBUDBTool " << VersionInfo << endl;
  56. cerr << "Use: GBUDBTool [-black | -white] [-p=<value>] [-c=<value>] <gbx file>" << endl;
  57. cerr << " -black prints out blacklist IPs based on p & c" << endl;
  58. cerr << " -white prints out whitelist IPs based on p & c" << endl;
  59. cerr << " -details prints out counts, confidence, and probability" << endl;
  60. cerr << " -c=<value> sets the confidence figure for the list" << endl;
  61. cerr << " -p=<value> sets the probability figure for the list" << endl;
  62. cerr << " <gbx file> is the path to a GBUdb snapshot" << endl;
  63. }
  64. class Reporter : public GBUdbOperator {
  65. private:
  66. Configuration myConfig;
  67. 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. 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. cout << toIP4String(IP);
  86. if(myConfig.showDetails) {
  87. cout << "\t"
  88. << "g=" << R.Good()
  89. << ", b=" << R.Bad()
  90. << ", c=" << R.Confidence()
  91. << ", p=" << R.Probability();
  92. }
  93. cout << 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(exception &e) {
  107. displayHelp();
  108. cerr << endl << "Exception: " << e.what() << endl;
  109. return ErrorResultCode;
  110. }
  111. return SuccessResultCode;
  112. }