1
0
mirror of https://github.com/PurpleI2P/i2pd-tools synced 2025-01-15 17:20:08 +00:00
i2pd-tools/routerinfo.cpp

126 lines
2.4 KiB
C++
Raw Normal View History

2016-11-25 09:55:22 -05:00
#include <iostream>
#include <unistd.h>
#include "Crypto.h"
#include "RouterInfo.h"
static void usage(const char * argv)
{
2024-09-25 12:18:25 +02:00
std::cout << "usage: " << argv << " [-6|-f|-p|-y] routerinfo.dat" << std::endl;
2016-11-25 09:55:22 -05:00
}
template<typename Addr>
static std::string address_style_string(Addr addr)
{
if(addr->transportStyle == i2p::data::RouterInfo::eTransportNTCP2) {
return "NTCP2";
} else if (addr->transportStyle == i2p::data::RouterInfo::eTransportSSU2) {
return "SSU2";
}
return "???";
}
2016-11-25 09:55:22 -05:00
template<typename Addr>
static void write_firewall_entry(std::ostream & o, Addr addr)
{
std::string proto;
if(addr->transportStyle == i2p::data::RouterInfo::eTransportNTCP2) {
2016-11-25 09:55:22 -05:00
proto = "tcp";
} else if (addr->transportStyle == i2p::data::RouterInfo::eTransportSSU2) {
2016-11-25 09:55:22 -05:00
proto = "udp";
} else {
// bail
return;
}
2016-11-25 09:55:22 -05:00
o << " -A OUTPUT -p " << proto;
o << " -d " << addr->host << " --dport " << addr->port;
2016-11-25 09:55:22 -05:00
o << " -j ACCEPT";
}
int main(int argc, char * argv[])
{
if (argc < 2) {
usage(argv[0]);
return 1;
}
2024-05-07 18:13:03 -04:00
i2p::crypto::InitCrypto(false, true, false);
2016-11-25 09:55:22 -05:00
int opt;
bool ipv6 = false;
bool firewall = false;
bool port = false;
2024-09-25 12:18:25 +02:00
bool yggdrasil = false;
while((opt = getopt(argc, argv, "6fpy")) != -1) {
2016-11-25 09:55:22 -05:00
switch(opt) {
case '6':
ipv6 = true;
break;
case 'f':
firewall = true;
break;
case 'p':
port = true;
break;
2024-09-25 12:18:25 +02:00
case 'y':
yggdrasil = true;
break;
2016-11-25 09:55:22 -05:00
default:
usage(argv[0]);
return 1;
}
}
while(optind < argc) {
int idx = optind;
optind ++;
std::string fname(argv[idx]);
i2p::data::RouterInfo ri(fname);
std::vector<std::shared_ptr<const i2p::data::RouterInfo::Address> > addrs;
2021-03-20 10:09:37 -04:00
auto a = ri.GetPublishedNTCP2V4Address();
if(a)
addrs.push_back(a);
a = ri.GetSSU2V4Address();
2016-11-25 09:55:22 -05:00
if(a)
addrs.push_back(a);
2021-03-20 10:09:37 -04:00
if (ipv6)
{
a = ri.GetPublishedNTCP2V6Address();
if(a)
addrs.push_back(a);
a = ri.GetSSU2V6Address();
if(a)
addrs.push_back(a);
2024-09-25 12:18:25 +02:00
}
if(yggdrasil){
a = ri.GetYggdrasilAddress();
if(a)
addrs.push_back(a);
}
2016-11-25 09:55:22 -05:00
if(firewall)
std::cout << "# ";
else
std::cout << "Router Hash: ";
2016-11-25 09:55:22 -05:00
std::cout << ri.GetIdentHashBase64() << std::endl;
2016-11-25 09:55:22 -05:00
for (const auto & a : addrs) {
2016-11-25 09:55:22 -05:00
if(firewall) {
write_firewall_entry(std::cout, a);
} else {
std::cout << address_style_string(a) << ": " << a->host;
2016-11-25 09:55:22 -05:00
if (port)
std::cout << ":" << a->port;
2016-11-25 09:55:22 -05:00
}
std::cout << std::endl;
}
}
2016-11-25 09:55:22 -05:00
return 0;
}