|
1 | 1 | #include <iostream> |
2 | | -#include <vector> |
3 | | -#include <string> |
4 | | -#include <memory> |
5 | | -#include <cctype> |
| 2 | +#include <unistd.h> |
6 | 3 | #include "Crypto.h" |
7 | 4 | #include "RouterInfo.h" |
8 | 5 |
|
| 6 | + |
| 7 | +static void usage(const char * argv) |
| 8 | +{ |
| 9 | + std::cout << "usage: " << argv << " [-6|-f|-p|-y] routerinfo.dat" << std::endl; |
| 10 | +} |
| 11 | + |
| 12 | +template<typename Addr> |
| 13 | +static std::string address_style_string(Addr addr) |
| 14 | +{ |
| 15 | + if(addr->transportStyle == i2p::data::RouterInfo::eTransportNTCP2) { |
| 16 | + return "NTCP2"; |
| 17 | + } else if (addr->transportStyle == i2p::data::RouterInfo::eTransportSSU2) { |
| 18 | + return "SSU2"; |
| 19 | + } |
| 20 | + return "???"; |
| 21 | + |
| 22 | +} |
| 23 | + |
| 24 | +template<typename Addr> |
| 25 | +static void write_firewall_entry(std::ostream & o, Addr addr) |
| 26 | +{ |
| 27 | + |
| 28 | + std::string proto; |
| 29 | + if(addr->transportStyle == i2p::data::RouterInfo::eTransportNTCP2) { |
| 30 | + proto = "tcp"; |
| 31 | + } else if (addr->transportStyle == i2p::data::RouterInfo::eTransportSSU2) { |
| 32 | + proto = "udp"; |
| 33 | + } else { |
| 34 | + // bail |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + o << " -A OUTPUT -p " << proto; |
| 39 | + o << " -d " << addr->host << " --dport " << addr->port; |
| 40 | + o << " -j ACCEPT"; |
| 41 | +} |
| 42 | + |
9 | 43 | int main(int argc, char * argv[]) |
10 | 44 | { |
11 | | - if (argc < 2) return 1; |
12 | | - i2p::crypto::InitCrypto(false); |
13 | | - |
14 | | - for (int i = 1; i < argc; ++i) { |
15 | | - std::string fname(argv[i]); |
16 | | - i2p::data::RouterInfo ri(fname); |
17 | | - |
18 | | - std::cout << "Router Hash: " << ri.GetIdentHashBase64() << std::endl; |
19 | | - |
20 | | - const uint8_t* buf = ri.GetBuffer(); |
21 | | - size_t len = ri.GetBufferLen(); |
22 | | - |
23 | | - if (buf && len > 0) { |
24 | | - std::string raw(reinterpret_cast<const char*>(buf), len); |
25 | | - |
26 | | - // --- PARSE FROM THE END --- |
27 | | - // List of specific tags to find by searching backwards |
28 | | - // Example for my friends from ilita chat: |
29 | | - // Use any tags you need |
30 | | - // Insert them in tags below |
31 | | - // "router.version=", |
32 | | - // "caps=", |
33 | | - // "netId=", |
34 | | - // "netdb.knownRouters=", |
35 | | - // "netdb.knownLeaseSets=" |
36 | | - std::vector<std::string> tags = { |
37 | | - "router.version=", |
38 | | - "caps=" |
39 | | - }; |
40 | | - |
41 | | - for (const auto& tag : tags) { |
42 | | - // Search from the end of the file |
43 | | - size_t tag_pos = raw.rfind(tag); |
44 | | - |
45 | | - if (tag_pos != std::string::npos) { |
46 | | - // Value starts after the '=' |
47 | | - size_t v_start = tag_pos + tag.length(); |
48 | | - // Value ends at the next ';' |
49 | | - size_t v_end = raw.find(';', v_start); |
50 | | - |
51 | | - if (v_end != std::string::npos) { |
52 | | - std::string val = raw.substr(v_start, v_end - v_start); |
53 | | - std::string key = tag.substr(0, tag.length() - 1); |
54 | | - std::cout << "Property: " << key << " = " << val << std::endl; |
55 | | - } |
56 | | - } |
57 | | - } |
58 | | - } |
59 | | - |
60 | | - // --- NETWORK TRANSPORTS --- |
61 | | - auto ntcp2v4 = ri.GetPublishedNTCP2V4Address(); |
62 | | - if (ntcp2v4) std::cout << "NTCP2: " << ntcp2v4->host.to_string() << std::endl; |
63 | | - |
64 | | - auto ssu2v4 = ri.GetSSU2V4Address(); |
65 | | - if (ssu2v4) std::cout << "SSU2: " << ssu2v4->host.to_string() << std::endl; |
66 | | - |
67 | | - auto ntcp2v6 = ri.GetPublishedNTCP2V6Address(); |
68 | | - if (ntcp2v6) std::cout << "NTCP2_V6: " << ntcp2v6->host.to_string() << std::endl; |
69 | | - |
70 | | - auto ssu2v6 = ri.GetSSU2V6Address(); |
71 | | - if (ssu2v6) std::cout << "SSU2_V6: [" << ssu2v6->host.to_string() << std::endl; |
72 | | - |
73 | | - } |
74 | | - |
75 | | - i2p::crypto::TerminateCrypto(); |
76 | | - return 0; |
| 45 | + if (argc < 2) { |
| 46 | + usage(argv[0]); |
| 47 | + return 1; |
| 48 | + } |
| 49 | + i2p::crypto::InitCrypto(false); |
| 50 | + int opt; |
| 51 | + bool ipv6 = false; |
| 52 | + bool firewall = false; |
| 53 | + bool port = false; |
| 54 | + bool yggdrasil = false; |
| 55 | + while((opt = getopt(argc, argv, "6fpy")) != -1) { |
| 56 | + switch(opt) { |
| 57 | + case '6': |
| 58 | + ipv6 = true; |
| 59 | + break; |
| 60 | + case 'f': |
| 61 | + firewall = true; |
| 62 | + break; |
| 63 | + case 'p': |
| 64 | + port = true; |
| 65 | + break; |
| 66 | + case 'y': |
| 67 | + yggdrasil = true; |
| 68 | + break; |
| 69 | + default: |
| 70 | + usage(argv[0]); |
| 71 | + return 1; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + while(optind < argc) { |
| 76 | + int idx = optind; |
| 77 | + optind ++; |
| 78 | + std::string fname(argv[idx]); |
| 79 | + i2p::data::RouterInfo ri(fname); |
| 80 | + |
| 81 | + std::vector<std::shared_ptr<const i2p::data::RouterInfo::Address> > addrs; |
| 82 | + auto a = ri.GetPublishedNTCP2V4Address(); |
| 83 | + if(a) |
| 84 | + addrs.push_back(a); |
| 85 | + a = ri.GetSSU2V4Address(); |
| 86 | + if(a) |
| 87 | + addrs.push_back(a); |
| 88 | + if (ipv6) |
| 89 | + { |
| 90 | + a = ri.GetPublishedNTCP2V6Address(); |
| 91 | + if(a) |
| 92 | + addrs.push_back(a); |
| 93 | + a = ri.GetSSU2V6Address(); |
| 94 | + if(a) |
| 95 | + addrs.push_back(a); |
| 96 | + } |
| 97 | + |
| 98 | + if(yggdrasil){ |
| 99 | + a = ri.GetYggdrasilAddress(); |
| 100 | + if(a) |
| 101 | + addrs.push_back(a); |
| 102 | + } |
| 103 | + |
| 104 | + if(firewall) |
| 105 | + std::cout << "# "; |
| 106 | + else |
| 107 | + std::cout << "Router Hash: "; |
| 108 | + std::cout << ri.GetIdentHashBase64() << std::endl; |
| 109 | + |
| 110 | + for (const auto & a : addrs) { |
| 111 | + |
| 112 | + if(firewall) { |
| 113 | + write_firewall_entry(std::cout, a); |
| 114 | + } else { |
| 115 | + std::cout << address_style_string(a) << ": " << a->host; |
| 116 | + |
| 117 | + if (port) |
| 118 | + std::cout << ":" << a->port; |
| 119 | + } |
| 120 | + std::cout << std::endl; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return 0; |
77 | 125 | } |
0 commit comments