2020-05-22 13:18:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013-2020, The PurpleI2P Project
|
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
2014-11-17 19:42:45 +00:00
|
|
|
#include <string>
|
|
|
|
#include <map>
|
2016-01-20 00:00:00 +00:00
|
|
|
#include "Config.h"
|
2014-11-17 19:42:45 +00:00
|
|
|
#include "Log.h"
|
2017-04-22 00:04:16 +00:00
|
|
|
#include "NetDb.hpp"
|
2014-11-17 19:42:45 +00:00
|
|
|
#include "Transports.h"
|
|
|
|
#include "Tunnel.h"
|
|
|
|
#include "RouterContext.h"
|
|
|
|
#include "Identity.h"
|
|
|
|
#include "Destination.h"
|
2015-12-31 21:02:10 +00:00
|
|
|
#include "Crypto.h"
|
2016-02-11 00:00:00 +00:00
|
|
|
#include "FS.h"
|
2014-11-17 17:37:40 +00:00
|
|
|
#include "api.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace api
|
|
|
|
{
|
2014-11-18 14:33:58 +00:00
|
|
|
void InitI2P (int argc, char* argv[], const char * appName)
|
2014-11-17 19:42:45 +00:00
|
|
|
{
|
2016-01-20 00:00:00 +00:00
|
|
|
i2p::config::Init ();
|
2017-03-29 14:51:32 +00:00
|
|
|
i2p::config::ParseCmdline (argc, argv, true); // ignore unknown options and help
|
2016-01-20 00:00:00 +00:00
|
|
|
i2p::config::Finalize ();
|
2016-02-11 00:00:00 +00:00
|
|
|
|
|
|
|
std::string datadir; i2p::config::GetOption("datadir", datadir);
|
|
|
|
|
|
|
|
i2p::fs::SetAppName (appName);
|
|
|
|
i2p::fs::DetectDataDir(datadir, false);
|
|
|
|
i2p::fs::Init();
|
|
|
|
|
2019-01-08 14:23:14 +00:00
|
|
|
bool precomputation; i2p::config::GetOption("precomputation.elgamal", precomputation);
|
2020-11-14 22:31:20 +00:00
|
|
|
bool aesni; i2p::config::GetOption("cpuext.aesni", aesni);
|
|
|
|
bool avx; i2p::config::GetOption("cpuext.avx", avx);
|
|
|
|
bool forceCpuExt; i2p::config::GetOption("cpuext.force", forceCpuExt);
|
|
|
|
i2p::crypto::InitCrypto (precomputation, aesni, avx, forceCpuExt);
|
2017-01-13 19:24:53 +00:00
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
int netID; i2p::config::GetOption("netid", netID);
|
|
|
|
i2p::context.SetNetID (netID);
|
2017-01-13 19:24:53 +00:00
|
|
|
|
2018-01-06 03:48:51 +00:00
|
|
|
i2p::context.Init ();
|
2014-11-17 19:42:45 +00:00
|
|
|
}
|
|
|
|
|
2015-12-31 21:02:10 +00:00
|
|
|
void TerminateI2P ()
|
|
|
|
{
|
|
|
|
i2p::crypto::TerminateCrypto ();
|
2018-01-06 03:48:51 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 17:36:58 +00:00
|
|
|
void StartI2P (std::shared_ptr<std::ostream> logStream)
|
2014-11-17 19:42:45 +00:00
|
|
|
{
|
2014-12-08 21:27:10 +00:00
|
|
|
if (logStream)
|
2016-03-27 00:17:29 +00:00
|
|
|
i2p::log::Logger().SendTo (logStream);
|
2014-12-08 21:27:10 +00:00
|
|
|
else
|
2016-03-27 00:17:29 +00:00
|
|
|
i2p::log::Logger().SendTo (i2p::fs::DataDirPath (i2p::fs::GetAppName () + ".log"));
|
2016-11-17 20:46:28 +00:00
|
|
|
i2p::log::Logger().Start ();
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogInfo, "API: Starting NetDB");
|
2014-11-17 19:42:45 +00:00
|
|
|
i2p::data::netdb.Start();
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogInfo, "API: Starting Transports");
|
2014-11-17 19:42:45 +00:00
|
|
|
i2p::transport::transports.Start();
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogInfo, "API: Starting Tunnels");
|
2014-11-17 19:42:45 +00:00
|
|
|
i2p::tunnel::tunnels.Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StopI2P ()
|
|
|
|
{
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogInfo, "API: Shutting down");
|
|
|
|
LogPrint(eLogInfo, "API: Stopping Tunnels");
|
2014-11-17 19:42:45 +00:00
|
|
|
i2p::tunnel::tunnels.Stop();
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogInfo, "API: Stopping Transports");
|
2014-11-17 19:42:45 +00:00
|
|
|
i2p::transport::transports.Stop();
|
2021-11-27 19:53:53 +00:00
|
|
|
LogPrint(eLogInfo, "API: Stopping NetDB");
|
2014-11-17 19:42:45 +00:00
|
|
|
i2p::data::netdb.Stop();
|
2016-11-17 20:46:28 +00:00
|
|
|
i2p::log::Logger().Stop ();
|
2014-11-17 19:42:45 +00:00
|
|
|
}
|
2014-11-17 22:00:30 +00:00
|
|
|
|
2015-11-03 14:15:49 +00:00
|
|
|
void RunPeerTest ()
|
|
|
|
{
|
|
|
|
i2p::transport::transports.PeerTest ();
|
2018-01-06 03:48:51 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 14:15:49 +00:00
|
|
|
std::shared_ptr<i2p::client::ClientDestination> CreateLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic,
|
2014-12-11 18:28:37 +00:00
|
|
|
const std::map<std::string, std::string> * params)
|
2014-11-17 22:00:30 +00:00
|
|
|
{
|
2020-02-04 19:17:23 +00:00
|
|
|
auto localDestination = std::make_shared<i2p::client::RunnableClientDestination> (keys, isPublic, params);
|
2014-11-17 22:00:30 +00:00
|
|
|
localDestination->Start ();
|
|
|
|
return localDestination;
|
|
|
|
}
|
|
|
|
|
2015-11-03 14:15:49 +00:00
|
|
|
std::shared_ptr<i2p::client::ClientDestination> CreateLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType,
|
2014-12-11 18:28:37 +00:00
|
|
|
const std::map<std::string, std::string> * params)
|
2014-11-17 22:00:30 +00:00
|
|
|
{
|
2014-11-28 21:46:26 +00:00
|
|
|
i2p::data::PrivateKeys keys = i2p::data::PrivateKeys::CreateRandomKeys (sigType);
|
2020-02-04 19:17:23 +00:00
|
|
|
auto localDestination = std::make_shared<i2p::client::RunnableClientDestination> (keys, isPublic, params);
|
2014-11-17 22:00:30 +00:00
|
|
|
localDestination->Start ();
|
|
|
|
return localDestination;
|
|
|
|
}
|
|
|
|
|
2015-11-03 14:15:49 +00:00
|
|
|
void DestroyLocalDestination (std::shared_ptr<i2p::client::ClientDestination> dest)
|
2014-11-17 22:00:30 +00:00
|
|
|
{
|
|
|
|
if (dest)
|
|
|
|
dest->Stop ();
|
|
|
|
}
|
2014-11-18 14:33:58 +00:00
|
|
|
|
2015-11-03 14:15:49 +00:00
|
|
|
void RequestLeaseSet (std::shared_ptr<i2p::client::ClientDestination> dest, const i2p::data::IdentHash& remote)
|
2014-11-18 14:33:58 +00:00
|
|
|
{
|
|
|
|
if (dest)
|
2014-12-26 02:13:51 +00:00
|
|
|
dest->RequestDestination (remote);
|
2014-11-18 14:33:58 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 14:15:49 +00:00
|
|
|
std::shared_ptr<i2p::stream::Stream> CreateStream (std::shared_ptr<i2p::client::ClientDestination> dest, const i2p::data::IdentHash& remote)
|
2014-11-18 14:33:58 +00:00
|
|
|
{
|
2014-12-09 19:59:19 +00:00
|
|
|
if (!dest) return nullptr;
|
|
|
|
auto leaseSet = dest->FindLeaseSet (remote);
|
2014-11-18 14:33:58 +00:00
|
|
|
if (leaseSet)
|
|
|
|
{
|
2015-01-27 16:27:58 +00:00
|
|
|
auto stream = dest->CreateStream (leaseSet);
|
2014-11-18 14:33:58 +00:00
|
|
|
stream->Send (nullptr, 0); // connect
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RequestLeaseSet (dest, remote);
|
2018-01-06 03:48:51 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2014-11-18 14:33:58 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 14:15:49 +00:00
|
|
|
void AcceptStream (std::shared_ptr<i2p::client::ClientDestination> dest, const i2p::stream::StreamingDestination::Acceptor& acceptor)
|
2014-11-18 14:33:58 +00:00
|
|
|
{
|
|
|
|
if (dest)
|
|
|
|
dest->AcceptStreams (acceptor);
|
|
|
|
}
|
|
|
|
|
2014-11-23 16:33:58 +00:00
|
|
|
void DestroyStream (std::shared_ptr<i2p::stream::Stream> stream)
|
2014-11-18 14:33:58 +00:00
|
|
|
{
|
|
|
|
if (stream)
|
|
|
|
stream->Close ();
|
|
|
|
}
|
2014-11-17 17:37:40 +00:00
|
|
|
}
|
|
|
|
}
|