2018-07-30 04:38:14 +00:00
|
|
|
#include <getopt.h>
|
2018-05-21 12:51:10 +00:00
|
|
|
#include <llarp.h>
|
|
|
|
#include <signal.h>
|
2018-06-19 09:48:29 +00:00
|
|
|
#include "logger.hpp"
|
2018-05-21 12:51:10 +00:00
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
#include <llarp/router_contact.hpp>
|
2018-07-08 13:29:29 +00:00
|
|
|
#include <llarp/time.h>
|
2018-07-24 03:40:34 +00:00
|
|
|
|
2018-07-08 13:29:29 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include "buffer.hpp"
|
|
|
|
#include "crypto.hpp"
|
|
|
|
#include "fs.hpp"
|
2018-07-27 00:21:57 +00:00
|
|
|
#include "llarp/net.hpp"
|
2018-07-08 13:29:29 +00:00
|
|
|
#include "router.hpp"
|
|
|
|
|
2018-08-17 10:40:40 +00:00
|
|
|
#include <llarp/messages/dht.hpp>
|
|
|
|
//#include <llarp/dht/messages/findintro.hpp>
|
|
|
|
//#include <llarp/routing_endpoint.hpp>
|
2018-09-24 13:10:36 +00:00
|
|
|
//#include <llarp/crypt.hpp> // for llarp::pubkey
|
2018-08-17 10:40:40 +00:00
|
|
|
|
2018-06-19 09:48:29 +00:00
|
|
|
struct llarp_main *ctx = 0;
|
2018-05-21 12:51:10 +00:00
|
|
|
|
2018-05-22 18:41:38 +00:00
|
|
|
void
|
|
|
|
handle_signal(int sig)
|
2018-05-21 12:51:10 +00:00
|
|
|
{
|
2018-06-19 09:48:29 +00:00
|
|
|
if(ctx)
|
|
|
|
llarp_main_signal(ctx, sig);
|
2018-05-21 12:51:10 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 09:48:29 +00:00
|
|
|
#ifndef TESTNET
|
|
|
|
#define TESTNET 0
|
|
|
|
#endif
|
|
|
|
|
2018-07-13 13:36:51 +00:00
|
|
|
void
|
2018-08-31 12:46:54 +00:00
|
|
|
displayRC(const llarp::RouterContact &rc)
|
2018-07-08 13:29:29 +00:00
|
|
|
{
|
2018-08-31 12:46:54 +00:00
|
|
|
std::cout << rc.pubkey << std::endl;
|
|
|
|
for(const auto &addr : rc.addrs)
|
|
|
|
{
|
|
|
|
std::cout << "AddressInfo: " << addr << std::endl;
|
|
|
|
}
|
2018-07-08 13:29:29 +00:00
|
|
|
}
|
|
|
|
|
2018-06-28 11:34:36 +00:00
|
|
|
// fwd declr
|
|
|
|
struct check_online_request;
|
|
|
|
|
2018-06-29 12:15:15 +00:00
|
|
|
void
|
|
|
|
HandleDHTLocate(llarp_router_lookup_job *job)
|
2018-06-28 11:34:36 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogInfo("DHT result: ", job->found ? "found" : "not found");
|
2018-07-13 13:36:51 +00:00
|
|
|
if(job->found)
|
2018-07-03 11:26:54 +00:00
|
|
|
{
|
|
|
|
// save to nodedb?
|
2018-09-19 12:22:34 +00:00
|
|
|
displayRC(job->result);
|
2018-07-03 11:26:54 +00:00
|
|
|
}
|
|
|
|
// shutdown router
|
2018-07-08 11:41:44 +00:00
|
|
|
|
2018-07-13 13:36:51 +00:00
|
|
|
// well because we're in the gotroutermessage, we can't sigint because we'll
|
|
|
|
// deadlock because we're session locked
|
|
|
|
// llarp_main_signal(ctx, SIGINT);
|
2018-07-03 11:26:54 +00:00
|
|
|
|
|
|
|
// llarp_timer_run(logic->timer, logic->thread);
|
|
|
|
// we'll we don't want logic thread
|
|
|
|
// but we want to switch back to the main thread
|
2018-07-13 13:36:51 +00:00
|
|
|
// llarp_logic_stop();
|
2018-07-03 11:26:54 +00:00
|
|
|
// still need to exit this logic thread...
|
|
|
|
llarp_main_abort(ctx);
|
2018-06-23 14:57:36 +00:00
|
|
|
}
|
2018-05-21 12:51:10 +00:00
|
|
|
|
2018-05-22 18:41:38 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2018-06-19 09:48:29 +00:00
|
|
|
// take -c to set location of daemon.ini
|
2018-07-08 13:29:29 +00:00
|
|
|
// take -o to set log level
|
2018-05-21 12:51:10 +00:00
|
|
|
// --generate-blank /path/to/file.signed
|
|
|
|
// --update-ifs /path/to/file.signed
|
|
|
|
// --key /path/to/long_term_identity.key
|
2018-06-19 09:48:29 +00:00
|
|
|
// --import
|
|
|
|
// --export
|
2018-05-21 12:51:10 +00:00
|
|
|
|
|
|
|
// --generate /path/to/file.signed
|
|
|
|
// --update /path/to/file.signed
|
2018-08-24 16:07:17 +00:00
|
|
|
// --verify /path/to/file.signed
|
2018-05-22 18:41:38 +00:00
|
|
|
// printf("has [%d]options\n", argc);
|
2018-06-19 09:48:29 +00:00
|
|
|
if(argc < 2)
|
2018-05-22 18:41:38 +00:00
|
|
|
{
|
|
|
|
printf(
|
2018-06-19 09:48:29 +00:00
|
|
|
"please specify: \n"
|
2018-07-11 11:04:45 +00:00
|
|
|
"--generate with a path to a router contact file\n"
|
|
|
|
"--update with a path to a router contact file\n"
|
2018-08-24 17:26:17 +00:00
|
|
|
"--list path to nodedb skiplist\n"
|
2018-07-11 11:04:45 +00:00
|
|
|
"--import with a path to a router contact file\n"
|
|
|
|
"--export a hex formatted public key\n"
|
2018-08-17 10:40:40 +00:00
|
|
|
"--locate a hex formatted public key\n"
|
|
|
|
"--find a base32 formatted service address\n"
|
|
|
|
"--b32 a hex formatted public key\n"
|
|
|
|
"--hex a base32 formatted public key\n"
|
2018-07-11 11:04:45 +00:00
|
|
|
"--localInfo \n"
|
|
|
|
"--read with a path to a router contact file\n"
|
2018-08-24 16:07:17 +00:00
|
|
|
"--verify with a path to a router contact file\n"
|
2018-06-19 09:48:29 +00:00
|
|
|
"\n");
|
2018-05-22 18:41:38 +00:00
|
|
|
return 0;
|
2018-05-21 12:51:10 +00:00
|
|
|
}
|
2018-09-06 00:32:06 +00:00
|
|
|
bool haveRequiredOptions = false;
|
2018-09-24 13:10:36 +00:00
|
|
|
bool genMode = false;
|
|
|
|
bool updMode = false;
|
|
|
|
bool listMode = false;
|
|
|
|
bool importMode = false;
|
|
|
|
bool exportMode = false;
|
|
|
|
bool locateMode = false;
|
|
|
|
bool findMode = false;
|
|
|
|
bool localMode = false;
|
|
|
|
bool verifyMode = false;
|
|
|
|
bool readMode = false;
|
|
|
|
bool toHexMode = false;
|
|
|
|
bool toB32Mode = false;
|
2018-05-21 12:51:10 +00:00
|
|
|
int c;
|
2018-06-19 09:48:29 +00:00
|
|
|
char *conffname;
|
|
|
|
char defaultConfName[] = "daemon.ini";
|
|
|
|
conffname = defaultConfName;
|
2018-08-24 17:26:17 +00:00
|
|
|
char *rcfname = nullptr;
|
|
|
|
char *nodesdir = nullptr;
|
2018-08-31 12:46:54 +00:00
|
|
|
|
|
|
|
llarp::RouterContact rc;
|
2018-05-21 12:51:10 +00:00
|
|
|
while(1)
|
|
|
|
{
|
2018-05-22 18:41:38 +00:00
|
|
|
static struct option long_options[] = {
|
2018-08-24 17:26:17 +00:00
|
|
|
{"file", required_argument, 0, 'f'},
|
2018-06-19 09:48:29 +00:00
|
|
|
{"config", required_argument, 0, 'c'},
|
2018-07-11 11:04:45 +00:00
|
|
|
{"logLevel", required_argument, 0, 'o'},
|
2018-05-22 18:41:38 +00:00
|
|
|
{"generate", required_argument, 0, 'g'},
|
|
|
|
{"update", required_argument, 0, 'u'},
|
2018-08-24 17:26:17 +00:00
|
|
|
{"list", required_argument, 0, 'l'},
|
2018-06-19 09:48:29 +00:00
|
|
|
{"import", required_argument, 0, 'i'},
|
|
|
|
{"export", required_argument, 0, 'e'},
|
2018-06-23 14:57:36 +00:00
|
|
|
{"locate", required_argument, 0, 'q'},
|
2018-09-06 00:32:06 +00:00
|
|
|
{"find", required_argument, 0, 'F'},
|
2018-06-28 11:34:36 +00:00
|
|
|
{"localInfo", no_argument, 0, 'n'},
|
2018-07-11 11:04:45 +00:00
|
|
|
{"read", required_argument, 0, 'r'},
|
2018-08-17 10:40:40 +00:00
|
|
|
{"b32", required_argument, 0, 'b'},
|
|
|
|
{"hex", required_argument, 0, 'h'},
|
2018-08-24 16:07:17 +00:00
|
|
|
{"verify", required_argument, 0, 'V'},
|
2018-05-22 18:41:38 +00:00
|
|
|
{0, 0, 0, 0}};
|
2018-05-21 12:51:10 +00:00
|
|
|
int option_index = 0;
|
2018-09-24 13:10:36 +00:00
|
|
|
c = getopt_long(argc, argv, "c:f:o:g:lu:i:e:q:F:nr:b:h:V:", long_options,
|
2018-07-13 13:36:51 +00:00
|
|
|
&option_index);
|
2018-07-24 03:40:34 +00:00
|
|
|
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
|
2018-05-22 18:41:38 +00:00
|
|
|
if(c == -1)
|
2018-05-21 12:51:10 +00:00
|
|
|
break;
|
2018-05-22 18:41:38 +00:00
|
|
|
switch(c)
|
|
|
|
{
|
2018-05-21 12:51:10 +00:00
|
|
|
case 0:
|
|
|
|
break;
|
2018-06-19 09:48:29 +00:00
|
|
|
case 'c':
|
|
|
|
conffname = optarg;
|
|
|
|
break;
|
2018-07-08 13:29:29 +00:00
|
|
|
case 'o':
|
2018-07-13 13:36:51 +00:00
|
|
|
if(strncmp(optarg, "debug",
|
2018-07-24 03:40:34 +00:00
|
|
|
MIN(strlen(optarg), static_cast< unsigned long >(5)))
|
2018-07-13 13:36:51 +00:00
|
|
|
== 0)
|
2018-07-08 13:29:29 +00:00
|
|
|
{
|
|
|
|
llarp::SetLogLevel(llarp::eLogDebug);
|
|
|
|
}
|
2018-07-24 03:40:34 +00:00
|
|
|
else if(strncmp(optarg, "info",
|
|
|
|
MIN(strlen(optarg), static_cast< unsigned long >(4)))
|
2018-07-13 13:36:51 +00:00
|
|
|
== 0)
|
2018-07-08 13:29:29 +00:00
|
|
|
{
|
2018-07-13 13:36:51 +00:00
|
|
|
llarp::SetLogLevel(llarp::eLogInfo);
|
2018-07-08 13:29:29 +00:00
|
|
|
}
|
2018-07-24 03:40:34 +00:00
|
|
|
else if(strncmp(optarg, "warn",
|
|
|
|
MIN(strlen(optarg), static_cast< unsigned long >(4)))
|
2018-07-13 13:36:51 +00:00
|
|
|
== 0)
|
2018-07-08 13:29:29 +00:00
|
|
|
{
|
|
|
|
llarp::SetLogLevel(llarp::eLogWarn);
|
|
|
|
}
|
2018-07-24 03:40:34 +00:00
|
|
|
else if(strncmp(optarg, "error",
|
|
|
|
MIN(strlen(optarg), static_cast< unsigned long >(5)))
|
2018-07-13 13:36:51 +00:00
|
|
|
== 0)
|
2018-07-08 13:29:29 +00:00
|
|
|
{
|
|
|
|
llarp::SetLogLevel(llarp::eLogError);
|
|
|
|
}
|
|
|
|
break;
|
2018-08-24 16:07:17 +00:00
|
|
|
case 'V':
|
2018-09-27 18:08:42 +00:00
|
|
|
rcfname = optarg;
|
|
|
|
haveRequiredOptions = true;
|
|
|
|
verifyMode = true;
|
2018-08-24 17:26:17 +00:00
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
rcfname = optarg;
|
2018-08-24 16:07:17 +00:00
|
|
|
break;
|
2018-06-19 09:48:29 +00:00
|
|
|
case 'l':
|
2018-08-24 17:26:17 +00:00
|
|
|
nodesdir = optarg;
|
|
|
|
listMode = true;
|
2018-06-19 09:48:29 +00:00
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
// printf ("option -g with value `%s'\n", optarg);
|
2018-08-24 17:26:17 +00:00
|
|
|
nodesdir = optarg;
|
|
|
|
importMode = true;
|
2018-06-19 09:48:29 +00:00
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
// printf ("option -g with value `%s'\n", optarg);
|
2018-08-24 17:26:17 +00:00
|
|
|
rcfname = optarg;
|
|
|
|
exportMode = true;
|
2018-06-19 09:48:29 +00:00
|
|
|
break;
|
2018-06-23 14:57:36 +00:00
|
|
|
case 'q':
|
|
|
|
// printf ("option -g with value `%s'\n", optarg);
|
2018-08-24 17:26:17 +00:00
|
|
|
rcfname = optarg;
|
|
|
|
locateMode = true;
|
2018-06-23 14:57:36 +00:00
|
|
|
break;
|
2018-09-06 00:32:06 +00:00
|
|
|
case 'F':
|
2018-08-17 10:40:40 +00:00
|
|
|
rcfname = optarg;
|
|
|
|
haveRequiredOptions = true;
|
|
|
|
findMode = true;
|
|
|
|
break;
|
2018-05-21 12:51:10 +00:00
|
|
|
case 'g':
|
2018-05-22 18:41:38 +00:00
|
|
|
// printf ("option -g with value `%s'\n", optarg);
|
2018-08-24 17:26:17 +00:00
|
|
|
rcfname = optarg;
|
|
|
|
genMode = true;
|
2018-05-21 12:51:10 +00:00
|
|
|
break;
|
|
|
|
case 'u':
|
2018-05-22 18:41:38 +00:00
|
|
|
// printf ("option -u with value `%s'\n", optarg);
|
2018-08-24 17:26:17 +00:00
|
|
|
rcfname = optarg;
|
|
|
|
updMode = true;
|
2018-05-21 12:51:10 +00:00
|
|
|
break;
|
2018-06-28 11:34:36 +00:00
|
|
|
case 'n':
|
2018-08-24 17:26:17 +00:00
|
|
|
localMode = true;
|
2018-06-28 11:34:36 +00:00
|
|
|
break;
|
2018-07-11 11:04:45 +00:00
|
|
|
case 'r':
|
2018-08-24 17:26:17 +00:00
|
|
|
rcfname = optarg;
|
|
|
|
readMode = true;
|
2018-07-11 11:04:45 +00:00
|
|
|
break;
|
2018-08-17 10:40:40 +00:00
|
|
|
case 'b':
|
|
|
|
rcfname = optarg;
|
|
|
|
haveRequiredOptions = true;
|
|
|
|
toB32Mode = true;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
rcfname = optarg;
|
|
|
|
haveRequiredOptions = true;
|
|
|
|
toHexMode = true;
|
|
|
|
break;
|
2018-05-21 12:51:10 +00:00
|
|
|
default:
|
2018-08-24 16:07:17 +00:00
|
|
|
printf("Bad option: %c\n", c);
|
|
|
|
return -1;
|
2018-05-21 12:51:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-24 03:40:34 +00:00
|
|
|
#undef MIN
|
2018-09-19 12:22:34 +00:00
|
|
|
if(!haveRequiredOptions)
|
|
|
|
{
|
2018-09-24 13:10:36 +00:00
|
|
|
llarp::LogError("Parameters dont all have their required parameters.\n");
|
|
|
|
return 0;
|
2018-09-19 12:22:34 +00:00
|
|
|
}
|
2018-09-24 13:10:36 +00:00
|
|
|
// printf("parsed options\n");
|
2018-09-19 12:22:34 +00:00
|
|
|
|
|
|
|
if(!genMode && !updMode && !listMode && !importMode && !exportMode
|
|
|
|
&& !locateMode && !localMode && !readMode && !findMode && !toB32Mode
|
2018-09-27 18:08:42 +00:00
|
|
|
&& !toHexMode && !verifyMode)
|
2018-09-19 12:22:34 +00:00
|
|
|
{
|
|
|
|
llarp::LogError(
|
2018-09-24 13:10:36 +00:00
|
|
|
"I don't know what to do, no generate or update parameter\n");
|
2018-09-19 12:22:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
llarp::RouterContact tmp;
|
2018-09-06 00:32:06 +00:00
|
|
|
|
2018-08-24 16:07:17 +00:00
|
|
|
if(verifyMode)
|
2018-05-22 18:41:38 +00:00
|
|
|
{
|
2018-08-24 16:07:17 +00:00
|
|
|
llarp_crypto crypto;
|
2018-09-02 18:25:42 +00:00
|
|
|
llarp_crypto_init(&crypto);
|
2018-08-31 12:46:54 +00:00
|
|
|
if(!rc.Read(rcfname))
|
2018-08-24 16:07:17 +00:00
|
|
|
{
|
|
|
|
std::cout << "failed to read " << rcfname << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2018-10-15 12:02:32 +00:00
|
|
|
if(!rc.Verify(&crypto))
|
2018-08-24 16:07:17 +00:00
|
|
|
{
|
2018-10-15 12:02:32 +00:00
|
|
|
std::cout << rcfname << " is invalid" << std::endl;
|
2018-08-24 16:07:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2018-08-31 12:46:54 +00:00
|
|
|
if(!rc.IsPublicRouter())
|
2018-08-24 16:07:17 +00:00
|
|
|
{
|
|
|
|
std::cout << rcfname << " is not a public router";
|
2018-08-31 12:46:54 +00:00
|
|
|
if(rc.addrs.size() == 0)
|
2018-08-24 16:07:17 +00:00
|
|
|
{
|
|
|
|
std::cout << " because it has no public addresses";
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:46:54 +00:00
|
|
|
std::cout << "router identity and dht routing key: " << rc.pubkey
|
|
|
|
<< std::endl;
|
|
|
|
|
|
|
|
std::cout << "router encryption key: " << rc.enckey << std::endl;
|
2018-08-24 16:07:17 +00:00
|
|
|
|
|
|
|
if(rc.HasNick())
|
|
|
|
std::cout << "router nickname: " << rc.Nick() << std::endl;
|
|
|
|
|
2018-08-31 12:46:54 +00:00
|
|
|
std::cout << "advertised addresses: " << std::endl;
|
|
|
|
for(const auto &addr : rc.addrs)
|
|
|
|
{
|
|
|
|
std::cout << addr << std::endl;
|
|
|
|
}
|
2018-08-24 16:07:17 +00:00
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
std::cout << "advertised exits: ";
|
2018-08-31 12:46:54 +00:00
|
|
|
if(rc.exits.size())
|
2018-08-24 16:07:17 +00:00
|
|
|
{
|
2018-08-31 12:46:54 +00:00
|
|
|
for(const auto &exit : rc.exits)
|
|
|
|
{
|
|
|
|
std::cout << exit << std::endl;
|
|
|
|
}
|
2018-08-24 16:07:17 +00:00
|
|
|
}
|
|
|
|
else
|
2018-08-31 12:46:54 +00:00
|
|
|
{
|
2018-08-24 16:07:17 +00:00
|
|
|
std::cout << "none";
|
2018-08-31 12:46:54 +00:00
|
|
|
}
|
2018-08-24 16:07:17 +00:00
|
|
|
std::cout << std::endl;
|
2018-06-19 09:48:29 +00:00
|
|
|
return 0;
|
2018-05-21 12:51:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-27 18:08:42 +00:00
|
|
|
ctx = llarp_main_init(conffname, !TESTNET);
|
|
|
|
if(!ctx)
|
|
|
|
{
|
|
|
|
llarp::LogError("Cant set up context");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
signal(SIGINT, handle_signal);
|
|
|
|
|
2018-09-19 12:22:34 +00:00
|
|
|
// is this Neuro or Jeff's?
|
|
|
|
// this is the only one...
|
2018-08-24 17:26:17 +00:00
|
|
|
if(listMode)
|
|
|
|
{
|
|
|
|
llarp_crypto crypto;
|
2018-09-02 18:25:42 +00:00
|
|
|
llarp_crypto_init(&crypto);
|
2018-08-24 17:26:17 +00:00
|
|
|
auto nodedb = llarp_nodedb_new(&crypto);
|
|
|
|
llarp_nodedb_iter itr;
|
|
|
|
itr.visit = [](llarp_nodedb_iter *i) -> bool {
|
2018-08-31 12:46:54 +00:00
|
|
|
std::cout << i->rc->pubkey << std::endl;
|
2018-08-24 17:26:17 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
if(llarp_nodedb_load_dir(nodedb, nodesdir) > 0)
|
|
|
|
llarp_nodedb_iterate_all(nodedb, itr);
|
|
|
|
llarp_nodedb_free(&nodedb);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(importMode)
|
2018-06-25 15:12:08 +00:00
|
|
|
{
|
2018-08-24 17:26:17 +00:00
|
|
|
if(rcfname == nullptr)
|
|
|
|
{
|
|
|
|
std::cout << "no file to import" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
llarp_crypto crypto;
|
2018-09-02 18:25:42 +00:00
|
|
|
llarp_crypto_init(&crypto);
|
2018-08-24 17:26:17 +00:00
|
|
|
auto nodedb = llarp_nodedb_new(&crypto);
|
|
|
|
if(!llarp_nodedb_ensure_dir(nodesdir))
|
|
|
|
{
|
|
|
|
std::cout << "failed to ensure " << nodesdir << strerror(errno)
|
|
|
|
<< std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
llarp_nodedb_set_dir(nodedb, nodesdir);
|
2018-08-31 12:46:54 +00:00
|
|
|
if(!rc.Read(rcfname))
|
2018-08-24 17:26:17 +00:00
|
|
|
{
|
|
|
|
std::cout << "failed to read " << rcfname << " " << strerror(errno)
|
|
|
|
<< std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-10-15 12:02:32 +00:00
|
|
|
if(!rc.Verify(&crypto))
|
2018-08-24 17:26:17 +00:00
|
|
|
{
|
2018-10-15 12:02:32 +00:00
|
|
|
std::cout << rcfname << " is invalid" << std::endl;
|
2018-08-24 17:26:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:46:54 +00:00
|
|
|
if(!llarp_nodedb_put_rc(nodedb, rc))
|
2018-08-24 17:26:17 +00:00
|
|
|
{
|
|
|
|
std::cout << "failed to store " << strerror(errno) << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:46:54 +00:00
|
|
|
std::cout << "imported " << rc.pubkey << std::endl;
|
2018-08-24 17:26:17 +00:00
|
|
|
|
2018-06-19 09:48:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-05-21 12:51:10 +00:00
|
|
|
|
2018-05-22 18:41:38 +00:00
|
|
|
if(genMode)
|
|
|
|
{
|
2018-05-21 12:51:10 +00:00
|
|
|
printf("Creating [%s]\n", rcfname);
|
|
|
|
// if we zero it out then
|
|
|
|
// set updated timestamp
|
2018-08-31 12:46:54 +00:00
|
|
|
rc.last_updated = llarp_time_now_ms();
|
2018-05-21 12:51:10 +00:00
|
|
|
// load longterm identity
|
|
|
|
llarp_crypto crypt;
|
2018-09-02 18:25:42 +00:00
|
|
|
llarp_crypto_init(&crypt);
|
2018-06-21 12:54:43 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
// which is in daemon.ini config: router.encryption-privkey (defaults
|
|
|
|
// "encryption.key")
|
2018-06-21 12:54:43 +00:00
|
|
|
fs::path encryption_keyfile = "encryption.key";
|
|
|
|
llarp::SecretKey encryption;
|
2018-08-02 23:30:34 +00:00
|
|
|
|
2018-07-30 04:38:14 +00:00
|
|
|
llarp_findOrCreateEncryption(&crypt, encryption_keyfile.string().c_str(),
|
2018-08-31 12:46:54 +00:00
|
|
|
encryption);
|
2018-08-02 23:30:34 +00:00
|
|
|
|
2018-08-31 12:46:54 +00:00
|
|
|
rc.enckey = llarp::seckey_topublic(encryption);
|
2018-06-21 12:54:43 +00:00
|
|
|
|
|
|
|
// get identity public sig key
|
2018-05-21 12:51:10 +00:00
|
|
|
fs::path ident_keyfile = "identity.key";
|
2018-08-31 12:46:54 +00:00
|
|
|
llarp::SecretKey identity;
|
2018-08-02 23:30:34 +00:00
|
|
|
llarp_findOrCreateIdentity(&crypt, ident_keyfile.string().c_str(),
|
|
|
|
identity);
|
|
|
|
|
2018-08-31 12:46:54 +00:00
|
|
|
rc.pubkey = llarp::seckey_topublic(identity);
|
2018-06-21 12:54:43 +00:00
|
|
|
|
2018-05-21 12:51:10 +00:00
|
|
|
// this causes a segfault
|
2018-08-31 12:46:54 +00:00
|
|
|
if(!rc.Sign(&crypt, identity))
|
|
|
|
{
|
|
|
|
std::cout << "failed to sign" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2018-05-21 12:51:10 +00:00
|
|
|
// set filename
|
|
|
|
fs::path our_rc_file = rcfname;
|
|
|
|
// write file
|
2018-08-31 12:46:54 +00:00
|
|
|
rc.Write(our_rc_file.string().c_str());
|
2018-09-19 12:22:34 +00:00
|
|
|
|
2018-09-24 13:10:36 +00:00
|
|
|
// llarp_rc_write(&tmp, our_rc_file.string().c_str());
|
2018-08-02 23:30:34 +00:00
|
|
|
|
2018-05-21 12:51:10 +00:00
|
|
|
// release memory for tmp lists
|
2018-09-24 13:10:36 +00:00
|
|
|
// llarp_rc_free(&tmp);
|
2018-05-21 12:51:10 +00:00
|
|
|
}
|
2018-05-22 18:41:38 +00:00
|
|
|
if(updMode)
|
|
|
|
{
|
2018-05-22 23:55:00 +00:00
|
|
|
printf("rcutil.cpp - Loading [%s]\n", rcfname);
|
2018-09-19 12:22:34 +00:00
|
|
|
llarp::RouterContact tmp;
|
2018-09-24 13:10:36 +00:00
|
|
|
// llarp_rc_clear(&rc);
|
2018-09-19 12:22:34 +00:00
|
|
|
rc.Clear();
|
|
|
|
// FIXME: new rc api
|
2018-09-24 13:10:36 +00:00
|
|
|
// llarp_rc_read(rcfname, &rc);
|
2018-06-19 09:48:29 +00:00
|
|
|
|
2018-05-21 12:51:10 +00:00
|
|
|
// set updated timestamp
|
2018-07-25 01:24:37 +00:00
|
|
|
rc.last_updated = llarp_time_now_ms();
|
2018-05-21 12:51:10 +00:00
|
|
|
// load longterm identity
|
|
|
|
llarp_crypto crypt;
|
2018-09-19 12:22:34 +00:00
|
|
|
|
|
|
|
// no longer used?
|
2018-09-24 13:10:36 +00:00
|
|
|
// llarp_crypto_libsodium_init(&crypt);
|
|
|
|
llarp::SecretKey identityKey; // FIXME: Jeff requests we use this
|
2018-05-21 12:51:10 +00:00
|
|
|
fs::path ident_keyfile = "identity.key";
|
2018-06-12 11:57:14 +00:00
|
|
|
byte_t identity[SECKEYSIZE];
|
2018-08-02 23:30:34 +00:00
|
|
|
llarp_findOrCreateIdentity(&crypt, ident_keyfile.string().c_str(),
|
|
|
|
identity);
|
2018-09-19 12:22:34 +00:00
|
|
|
|
|
|
|
// FIXME: update RC API
|
2018-05-21 12:51:10 +00:00
|
|
|
// get identity public key
|
2018-09-24 13:10:36 +00:00
|
|
|
// const uint8_t *pubkey = llarp::seckey_topublic(identity);
|
2018-09-19 12:22:34 +00:00
|
|
|
|
|
|
|
// FIXME: update RC API
|
2018-09-24 13:10:36 +00:00
|
|
|
// llarp_rc_set_pubsigkey(&rc, pubkey);
|
2018-09-19 12:22:34 +00:00
|
|
|
// // FIXME: update RC API
|
2018-09-24 13:10:36 +00:00
|
|
|
// llarp_rc_sign(&crypt, identity, &rc);
|
2018-06-19 09:48:29 +00:00
|
|
|
|
2018-05-21 12:51:10 +00:00
|
|
|
// set filename
|
|
|
|
fs::path our_rc_file_out = "update_debug.rc";
|
|
|
|
// write file
|
2018-09-19 12:22:34 +00:00
|
|
|
// FIXME: update RC API
|
2018-09-24 13:10:36 +00:00
|
|
|
// rc.Write(our_rc_file.string().c_str());
|
|
|
|
// llarp_rc_write(&tmp, our_rc_file_out.string().c_str());
|
2018-05-21 12:51:10 +00:00
|
|
|
}
|
2018-08-24 17:26:17 +00:00
|
|
|
|
2018-09-24 13:10:36 +00:00
|
|
|
if(listMode)
|
|
|
|
{
|
|
|
|
llarp_crypto crypto;
|
|
|
|
// no longer used?
|
|
|
|
// llarp_crypto_libsodium_init(&crypto);
|
|
|
|
llarp_crypto_init(&crypto);
|
|
|
|
auto nodedb = llarp_nodedb_new(&crypto);
|
|
|
|
llarp_nodedb_iter itr;
|
|
|
|
itr.visit = [](llarp_nodedb_iter *i) -> bool {
|
|
|
|
std::cout << llarp::PubKey(i->rc->pubkey) << std::endl;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
if(llarp_nodedb_load_dir(nodedb, nodesdir) > 0)
|
|
|
|
llarp_nodedb_iterate_all(nodedb, itr);
|
|
|
|
llarp_nodedb_free(&nodedb);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-06-25 15:12:08 +00:00
|
|
|
if(exportMode)
|
|
|
|
{
|
2018-06-19 09:48:29 +00:00
|
|
|
llarp_main_loadDatabase(ctx);
|
2018-07-05 15:44:06 +00:00
|
|
|
// llarp::LogInfo("Looking for string: ", rcfname);
|
2018-06-21 11:15:28 +00:00
|
|
|
|
|
|
|
llarp::PubKey binaryPK;
|
|
|
|
llarp::HexDecode(rcfname, binaryPK.data());
|
2018-06-25 15:12:08 +00:00
|
|
|
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogInfo("Looking for binary: ", binaryPK);
|
2018-09-19 12:22:34 +00:00
|
|
|
llarp::RouterContact *rc = llarp_main_getDatabase(ctx, binaryPK.data());
|
2018-06-25 15:12:08 +00:00
|
|
|
if(!rc)
|
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("Can't load RC from database");
|
2018-06-21 11:15:28 +00:00
|
|
|
}
|
|
|
|
std::string filename(rcfname);
|
|
|
|
filename.append(".signed");
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogInfo("Writing out: ", filename);
|
2018-09-19 12:22:34 +00:00
|
|
|
// FIXME: update RC API
|
2018-09-24 13:10:36 +00:00
|
|
|
// rc.Write(our_rc_file.string().c_str());
|
|
|
|
// llarp_rc_write(rc, filename.c_str());
|
2018-06-19 09:48:29 +00:00
|
|
|
}
|
2018-06-29 12:15:15 +00:00
|
|
|
if(locateMode)
|
2018-06-28 11:34:36 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogInfo("Going online");
|
2018-06-23 14:57:36 +00:00
|
|
|
llarp_main_setup(ctx);
|
|
|
|
|
|
|
|
llarp::PubKey binaryPK;
|
|
|
|
llarp::HexDecode(rcfname, binaryPK.data());
|
2018-06-29 12:15:15 +00:00
|
|
|
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogInfo("Queueing job");
|
2018-06-23 14:57:36 +00:00
|
|
|
llarp_router_lookup_job *job = new llarp_router_lookup_job;
|
2018-07-08 13:29:29 +00:00
|
|
|
job->iterative = true;
|
2018-06-29 12:15:15 +00:00
|
|
|
job->found = false;
|
|
|
|
job->hook = &HandleDHTLocate;
|
2018-09-24 13:10:36 +00:00
|
|
|
// llarp_rc_new(&job->result);
|
2018-06-29 12:15:15 +00:00
|
|
|
memcpy(job->target, binaryPK, PUBKEYSIZE); // set job's target
|
2018-07-08 11:41:44 +00:00
|
|
|
|
2018-06-28 11:34:36 +00:00
|
|
|
// create query DHT request
|
|
|
|
check_online_request *request = new check_online_request;
|
2018-06-29 12:15:15 +00:00
|
|
|
request->ptr = ctx;
|
|
|
|
request->job = job;
|
|
|
|
request->online = false;
|
|
|
|
request->nodes = 0;
|
|
|
|
request->first = false;
|
2018-06-28 11:34:36 +00:00
|
|
|
llarp_main_queryDHT(request);
|
2018-06-23 14:57:36 +00:00
|
|
|
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogInfo("Processing");
|
2018-06-23 14:57:36 +00:00
|
|
|
// run system and wait
|
|
|
|
llarp_main_run(ctx);
|
|
|
|
}
|
2018-08-17 10:40:40 +00:00
|
|
|
if(findMode)
|
|
|
|
{
|
|
|
|
llarp::LogInfo("Going online");
|
|
|
|
llarp_main_setup(ctx);
|
|
|
|
|
|
|
|
llarp::LogInfo("Please find ", rcfname);
|
|
|
|
std::string str(rcfname);
|
|
|
|
|
|
|
|
llarp::service::Tag tag(rcfname);
|
|
|
|
llarp::LogInfo("Tag ", tag);
|
|
|
|
|
|
|
|
llarp::service::Address addr;
|
|
|
|
str = str.append(".loki");
|
|
|
|
llarp::LogInfo("Prestring ", str);
|
|
|
|
bool res = addr.FromString(str.c_str());
|
|
|
|
llarp::LogInfo(res ? "Success" : "not a base32 string");
|
|
|
|
|
|
|
|
// Base32Decode(rcfname, addr);
|
|
|
|
llarp::LogInfo("Addr ", addr);
|
|
|
|
llarp::routing::DHTMessage *msg = new llarp::routing::DHTMessage();
|
|
|
|
// uint64_t txid, const llarp::service::Address& addr
|
2018-09-19 12:22:34 +00:00
|
|
|
// FIXME: new API?
|
2018-09-24 13:10:36 +00:00
|
|
|
// msg->M.push_back(new llarp::dht::FindIntroMessage(tag, 1));
|
2018-08-17 10:40:40 +00:00
|
|
|
|
|
|
|
// I guess we may need a router to get any replies
|
|
|
|
llarp::LogInfo("Processing");
|
|
|
|
// run system and wait
|
|
|
|
llarp_main_run(ctx);
|
|
|
|
}
|
2018-06-29 12:15:15 +00:00
|
|
|
if(localMode)
|
2018-06-28 11:34:36 +00:00
|
|
|
{
|
2018-09-19 12:22:34 +00:00
|
|
|
// FIXME: update llarp_main_getLocalRC
|
2018-09-24 13:10:36 +00:00
|
|
|
// llarp::RouterContact *rc = llarp_main_getLocalRC(ctx);
|
|
|
|
// displayRC(rc);
|
|
|
|
// delete it
|
2018-06-28 11:34:36 +00:00
|
|
|
}
|
2018-07-11 11:04:45 +00:00
|
|
|
{
|
2018-08-31 12:46:54 +00:00
|
|
|
if(rc.Read(rcfname))
|
|
|
|
displayRC(rc);
|
2018-06-28 11:34:36 +00:00
|
|
|
}
|
2018-09-19 12:22:34 +00:00
|
|
|
|
2018-08-17 10:40:40 +00:00
|
|
|
if(toB32Mode)
|
|
|
|
{
|
|
|
|
llarp::LogInfo("Converting hex string ", rcfname);
|
|
|
|
std::string str(rcfname);
|
|
|
|
|
|
|
|
llarp::PubKey binaryPK;
|
2018-09-24 13:10:36 +00:00
|
|
|
// llarp::service::Address::FromString
|
2018-08-17 10:40:40 +00:00
|
|
|
llarp::HexDecode(rcfname, binaryPK.data());
|
|
|
|
char tmp[(1 + 32) * 2] = {0};
|
|
|
|
std::string b32 = llarp::Base32Encode(binaryPK, tmp);
|
|
|
|
llarp::LogInfo("to base32 ", b32);
|
|
|
|
}
|
|
|
|
if(toHexMode)
|
|
|
|
{
|
|
|
|
llarp::service::Address addr;
|
|
|
|
llarp::Base32Decode(rcfname, addr);
|
|
|
|
llarp::LogInfo("Converting base32 string ", addr);
|
|
|
|
|
2018-09-24 13:10:36 +00:00
|
|
|
// llarp::service::Address::ToString
|
2018-08-17 10:40:40 +00:00
|
|
|
char ftmp[68] = {0};
|
|
|
|
const char *hexname =
|
|
|
|
llarp::HexEncode< llarp::service::Address, decltype(ftmp) >(addr, ftmp);
|
|
|
|
|
|
|
|
llarp::LogInfo("to hex ", hexname);
|
|
|
|
}
|
2018-07-03 11:26:54 +00:00
|
|
|
// it's a unique_ptr, should clean up itself
|
2018-07-13 13:36:51 +00:00
|
|
|
// llarp_main_free(ctx);
|
2018-08-31 12:46:54 +00:00
|
|
|
return 0; // success
|
2018-05-21 12:51:10 +00:00
|
|
|
}
|