lokinet/daemon/rcutil.cpp

595 lines
16 KiB
C++
Raw Normal View History

2018-12-12 01:55:30 +00:00
#include <buffer.hpp>
2019-01-13 14:00:50 +00:00
#include <crypto/crypto.hpp>
2018-12-12 01:55:30 +00:00
#include <fs.hpp>
2018-05-21 12:51:10 +00:00
#include <llarp.h>
2018-12-12 01:55:30 +00:00
#include <logger.hpp>
#include <messages/dht.hpp>
#include <net.hpp>
#include <nodedb.hpp>
#include <router.hpp>
2018-12-12 01:55:30 +00:00
#include <router_contact.hpp>
#include <time.hpp>
2018-07-24 03:40:34 +00:00
#include <fstream>
2018-12-12 01:55:30 +00:00
#include <getopt.h>
#include <signal.h>
struct llarp_main *ctx = 0;
2018-05-21 12:51:10 +00:00
void
handle_signal(int sig)
2018-05-21 12:51:10 +00:00
{
if(ctx)
llarp_main_signal(ctx, sig);
2018-05-21 12:51:10 +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-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;
}
}
// fwd declr
struct check_online_request;
2018-06-29 12:15:15 +00:00
void
HandleDHTLocate(llarp_router_lookup_job *job)
{
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?
displayRC(job->result);
2018-07-03 11:26:54 +00:00
}
// shutdown router
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-05-21 12:51:10 +00:00
int
main(int argc, char *argv[])
{
// take -c to set location of daemon.ini
// 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
// --import
// --export
2018-05-21 12:51:10 +00:00
// --generate /path/to/file.signed
// --update /path/to/file.signed
// --verify /path/to/file.signed
// printf("has [%d]options\n", argc);
if(argc < 2)
{
printf(
"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"
"--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"
"--verify with a path to a router contact file\n"
"\n");
return 0;
2018-05-21 12:51:10 +00:00
}
bool haveRequiredOptions = false;
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;
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)
{
static struct option long_options[] = {
2018-08-24 17:26:17 +00:00
{"file", required_argument, 0, 'f'},
{"config", required_argument, 0, 'c'},
2018-07-11 11:04:45 +00:00
{"logLevel", required_argument, 0, 'o'},
{"generate", required_argument, 0, 'g'},
{"update", required_argument, 0, 'u'},
2018-08-24 17:26:17 +00:00
{"list", required_argument, 0, 'l'},
{"import", required_argument, 0, 'i'},
{"export", required_argument, 0, 'e'},
{"locate", required_argument, 0, 'q'},
{"find", required_argument, 0, 'F'},
{"localInfo", no_argument, 0, 'n'},
2018-07-11 11:04:45 +00:00
{"read", required_argument, 0, 'r'},
{"b32", required_argument, 0, 'b'},
{"hex", required_argument, 0, 'h'},
{"verify", required_argument, 0, 'V'},
{0, 0, 0, 0}};
2018-05-21 12:51:10 +00:00
int option_index = 0;
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))
if(c == -1)
2018-05-21 12:51:10 +00:00
break;
switch(c)
{
2018-05-21 12:51:10 +00:00
case 0:
break;
case 'c':
conffname = optarg;
break;
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)
{
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-13 13:36:51 +00:00
llarp::SetLogLevel(llarp::eLogInfo);
}
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)
{
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)
{
llarp::SetLogLevel(llarp::eLogError);
}
break;
case 'V':
rcfname = optarg;
haveRequiredOptions = true;
verifyMode = true;
2018-08-24 17:26:17 +00:00
break;
case 'f':
rcfname = optarg;
break;
case 'l':
2018-08-24 17:26:17 +00:00
nodesdir = optarg;
listMode = true;
break;
case 'i':
// printf ("option -g with value `%s'\n", optarg);
2018-08-24 17:26:17 +00:00
nodesdir = optarg;
importMode = true;
break;
case 'e':
// printf ("option -g with value `%s'\n", optarg);
2018-08-24 17:26:17 +00:00
rcfname = optarg;
exportMode = true;
break;
case 'q':
// printf ("option -g with value `%s'\n", optarg);
2018-08-24 17:26:17 +00:00
rcfname = optarg;
locateMode = true;
break;
case 'F':
rcfname = optarg;
haveRequiredOptions = true;
findMode = true;
break;
2018-05-21 12:51:10 +00:00
case 'g':
// 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':
// 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;
case 'n':
2018-08-24 17:26:17 +00:00
localMode = true;
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;
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:
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
if(!haveRequiredOptions)
{
llarp::LogError("Parameters dont all have their required parameters.\n");
return 0;
}
// printf("parsed options\n");
if(!genMode && !updMode && !listMode && !importMode && !exportMode
&& !locateMode && !localMode && !readMode && !findMode && !toB32Mode
&& !toHexMode && !verifyMode)
{
llarp::LogError(
"I don't know what to do, no generate or update parameter\n");
return 1;
}
llarp::RouterContact tmp;
if(verifyMode)
{
llarp::Crypto crypto(llarp::Crypto::sodium{});
2018-08-31 12:46:54 +00:00
if(!rc.Read(rcfname))
{
std::cout << "failed to read " << rcfname << std::endl;
return 1;
}
2018-10-15 12:02:32 +00:00
if(!rc.Verify(&crypto))
{
2018-10-15 12:02:32 +00:00
std::cout << rcfname << " is invalid" << std::endl;
return 1;
}
2018-08-31 12:46:54 +00:00
if(!rc.IsPublicRouter())
{
std::cout << rcfname << " is not a public router";
2018-08-31 12:46:54 +00:00
if(rc.addrs.size() == 0)
{
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;
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;
}
std::cout << std::endl;
std::cout << "advertised exits: ";
2018-08-31 12:46:54 +00:00
if(rc.exits.size())
{
2018-08-31 12:46:54 +00:00
for(const auto &exit : rc.exits)
{
std::cout << exit << std::endl;
}
}
else
2018-08-31 12:46:54 +00:00
{
std::cout << "none";
2018-08-31 12:46:54 +00:00
}
std::cout << std::endl;
return 0;
2018-05-21 12:51:10 +00:00
}
ctx = llarp_main_init(conffname, !TESTNET);
if(!ctx)
{
llarp::LogError("Cant set up context");
return 1;
}
signal(SIGINT, handle_signal);
// is this Neuro or Jeff's?
// this is the only one...
2018-08-24 17:26:17 +00:00
if(listMode)
{
llarp::Crypto crypto(llarp::Crypto::sodium{});
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-08-24 17:26:17 +00:00
if(rcfname == nullptr)
{
std::cout << "no file to import" << std::endl;
return 1;
}
llarp::Crypto crypto(llarp::Crypto::sodium{});
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
return 0;
}
2018-05-21 12:51:10 +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-11-19 22:45:37 +00:00
rc.last_updated = llarp::time_now_ms();
2018-05-21 12:51:10 +00:00
// load longterm identity
llarp::Crypto crypt(llarp::Crypto::sodium{});
// which is in daemon.ini config: router.encryption-privkey (defaults
// "encryption.key")
fs::path encryption_keyfile = "encryption.key";
llarp::SecretKey encryption;
2018-08-02 23:30:34 +00:00
llarp_findOrCreateEncryption(&crypt, encryption_keyfile, encryption);
2018-08-02 23:30:34 +00:00
2018-08-31 12:46:54 +00:00
rc.enckey = llarp::seckey_topublic(encryption);
// 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;
llarp_findOrCreateIdentity(&crypt, ident_keyfile, identity);
2018-08-02 23:30:34 +00:00
2018-08-31 12:46:54 +00:00
rc.pubkey = llarp::seckey_topublic(identity);
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());
// 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
// llarp_rc_free(&tmp);
2018-05-21 12:51:10 +00:00
}
if(updMode)
{
printf("rcutil.cpp - Loading [%s]\n", rcfname);
llarp::RouterContact tmp;
// llarp_rc_clear(&rc);
rc.Clear();
// FIXME: new rc api
// llarp_rc_read(rcfname, &rc);
2018-05-21 12:51:10 +00:00
// set updated timestamp
2018-11-19 22:45:37 +00:00
rc.last_updated = llarp::time_now_ms();
2018-05-21 12:51:10 +00:00
// load longterm identity
llarp::Crypto crypt(llarp::Crypto::sodium{});
// no longer used?
// 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";
llarp::SecretKey identity;
llarp_findOrCreateIdentity(&crypt, ident_keyfile, identity);
// FIXME: update RC API
2018-05-21 12:51:10 +00:00
// get identity public key
// const uint8_t *pubkey = llarp::seckey_topublic(identity);
// FIXME: update RC API
// llarp_rc_set_pubsigkey(&rc, pubkey);
// // FIXME: update RC API
// llarp_rc_sign(&crypt, identity, &rc);
2018-05-21 12:51:10 +00:00
// set filename
fs::path our_rc_file_out = "update_debug.rc";
// write file
// FIXME: update RC API
// 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
if(listMode)
{
llarp::Crypto crypto(llarp::Crypto::sodium{});
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;
}
if(exportMode)
{
llarp_main_loadDatabase(ctx);
// llarp::LogInfo("Looking for string: ", rcfname);
2018-06-21 11:15:28 +00:00
llarp::PubKey binaryPK;
2018-11-14 19:34:17 +00:00
llarp::HexDecode(rcfname, binaryPK.data(), binaryPK.size());
llarp::LogInfo("Looking for binary: ", binaryPK);
llarp::RouterContact *rc = llarp_main_getDatabase(ctx, binaryPK.data());
if(!rc)
{
llarp::LogError("Can't load RC from database");
2018-06-21 11:15:28 +00:00
}
std::string filename(rcfname);
filename.append(".signed");
llarp::LogInfo("Writing out: ", filename);
// FIXME: update RC API
// rc.Write(our_rc_file.string().c_str());
// llarp_rc_write(rc, filename.c_str());
}
2018-06-29 12:15:15 +00:00
if(locateMode)
{
llarp::LogInfo("Going online");
llarp_main_setup(ctx);
llarp::PubKey binaryPK;
2018-11-14 19:34:17 +00:00
llarp::HexDecode(rcfname, binaryPK.data(), binaryPK.size());
2018-06-29 12:15:15 +00:00
llarp::LogInfo("Queueing job");
llarp_router_lookup_job *job = new llarp_router_lookup_job;
job->iterative = true;
2018-06-29 12:15:15 +00:00
job->found = false;
job->hook = &HandleDHTLocate;
// llarp_rc_new(&job->result);
job->target = binaryPK; // set job's target
// 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;
llarp_main_queryDHT(request);
llarp::LogInfo("Processing");
// run system and wait
llarp_main_run(ctx);
}
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);
// uint64_t txid, const llarp::service::Address& addr
// FIXME: new API?
// msg->M.push_back(new llarp::dht::FindIntroMessage(tag, 1));
// 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)
{
// FIXME: update llarp_main_getLocalRC
// llarp::RouterContact *rc = llarp_main_getLocalRC(ctx);
// displayRC(rc);
// delete it
}
2018-07-11 11:04:45 +00:00
{
2018-08-31 12:46:54 +00:00
if(rc.Read(rcfname))
displayRC(rc);
}
if(toB32Mode)
{
llarp::LogInfo("Converting hex string ", rcfname);
std::string str(rcfname);
llarp::PubKey binaryPK;
// llarp::service::Address::FromString
2018-11-14 19:34:17 +00:00
llarp::HexDecode(rcfname, binaryPK.data(), binaryPK.size());
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);
// llarp::service::Address::ToString
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
}