routerinfo.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <iostream>
  2. #include <unistd.h>
  3. #include "Crypto.h"
  4. #include "RouterInfo.h"
  5. static void usage(const char * argv)
  6. {
  7. std::cout << "usage: " << argv << " [-6|-f|-p] routerinfo.dat" << std::endl;
  8. }
  9. template<typename Addr>
  10. static std::string address_style_string(Addr addr)
  11. {
  12. if(addr->transportStyle == i2p::data::RouterInfo::eTransportNTCP2) {
  13. return "NTCP2";
  14. } else if (addr->transportStyle == i2p::data::RouterInfo::eTransportSSU2) {
  15. return "SSU2";
  16. }
  17. return "???";
  18. }
  19. template<typename Addr>
  20. static void write_firewall_entry(std::ostream & o, Addr addr)
  21. {
  22. std::string proto;
  23. if(addr->transportStyle == i2p::data::RouterInfo::eTransportNTCP2) {
  24. proto = "tcp";
  25. } else if (addr->transportStyle == i2p::data::RouterInfo::eTransportSSU2) {
  26. proto = "udp";
  27. } else {
  28. // bail
  29. return;
  30. }
  31. o << " -A OUTPUT -p " << proto;
  32. o << " -d " << addr->host << " --dport " << addr->port;
  33. o << " -j ACCEPT";
  34. }
  35. int main(int argc, char * argv[])
  36. {
  37. if (argc < 2) {
  38. usage(argv[0]);
  39. return 1;
  40. }
  41. i2p::crypto::InitCrypto(false, true, true, false);
  42. int opt;
  43. bool ipv6 = false;
  44. bool firewall = false;
  45. bool port = false;
  46. while((opt = getopt(argc, argv, "6fp")) != -1) {
  47. switch(opt) {
  48. case '6':
  49. ipv6 = true;
  50. break;
  51. case 'f':
  52. firewall = true;
  53. break;
  54. case 'p':
  55. port = true;
  56. break;
  57. default:
  58. usage(argv[0]);
  59. return 1;
  60. }
  61. }
  62. while(optind < argc) {
  63. int idx = optind;
  64. optind ++;
  65. std::string fname(argv[idx]);
  66. i2p::data::RouterInfo ri(fname);
  67. std::vector<std::shared_ptr<const i2p::data::RouterInfo::Address> > addrs;
  68. auto a = ri.GetPublishedNTCP2V4Address();
  69. if(a)
  70. addrs.push_back(a);
  71. a = ri.GetSSU2V4Address();
  72. if(a)
  73. addrs.push_back(a);
  74. if (ipv6)
  75. {
  76. a = ri.GetPublishedNTCP2V6Address();
  77. if(a)
  78. addrs.push_back(a);
  79. a = ri.GetSSU2V6Address();
  80. if(a)
  81. addrs.push_back(a);
  82. }
  83. if(firewall)
  84. std::cout << "# ";
  85. else
  86. std::cout << "Router Hash: ";
  87. std::cout << ri.GetIdentHashBase64() << std::endl;
  88. for (const auto & a : addrs) {
  89. if(firewall) {
  90. write_firewall_entry(std::cout, a);
  91. } else {
  92. std::cout << address_style_string(a) << ": " << a->host;
  93. if (port)
  94. std::cout << ":" << a->port;
  95. }
  96. std::cout << std::endl;
  97. }
  98. }
  99. return 0;
  100. }