mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
3.6 KiB
140 lines
3.6 KiB
10 years ago
|
#include <string>
|
||
|
#include <map>
|
||
9 years ago
|
#include "Config.h"
|
||
10 years ago
|
#include "Log.h"
|
||
|
#include "NetDb.h"
|
||
|
#include "Transports.h"
|
||
|
#include "Tunnel.h"
|
||
|
#include "RouterContext.h"
|
||
|
#include "Identity.h"
|
||
|
#include "Destination.h"
|
||
9 years ago
|
#include "Crypto.h"
|
||
9 years ago
|
#include "FS.h"
|
||
10 years ago
|
#include "api.h"
|
||
|
|
||
|
namespace i2p
|
||
|
{
|
||
|
namespace api
|
||
|
{
|
||
10 years ago
|
void InitI2P (int argc, char* argv[], const char * appName)
|
||
10 years ago
|
{
|
||
9 years ago
|
i2p::config::Init ();
|
||
8 years ago
|
i2p::config::ParseCmdline (argc, argv, true); // ignore unknown options and help
|
||
9 years ago
|
i2p::config::Finalize ();
|
||
9 years ago
|
|
||
|
std::string datadir; i2p::config::GetOption("datadir", datadir);
|
||
|
|
||
|
i2p::fs::SetAppName (appName);
|
||
|
i2p::fs::DetectDataDir(datadir, false);
|
||
|
i2p::fs::Init();
|
||
|
|
||
9 years ago
|
#if defined(__x86_64__)
|
||
|
i2p::crypto::InitCrypto (false);
|
||
|
#else
|
||
|
i2p::crypto::InitCrypto (true);
|
||
|
#endif
|
||
8 years ago
|
|
||
|
int netID; i2p::config::GetOption("netid", netID);
|
||
|
i2p::context.SetNetID (netID);
|
||
|
|
||
10 years ago
|
i2p::context.Init ();
|
||
|
}
|
||
|
|
||
9 years ago
|
void TerminateI2P ()
|
||
|
{
|
||
|
i2p::crypto::TerminateCrypto ();
|
||
|
}
|
||
|
|
||
9 years ago
|
void StartI2P (std::shared_ptr<std::ostream> logStream)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (logStream)
|
||
9 years ago
|
i2p::log::Logger().SendTo (logStream);
|
||
10 years ago
|
else
|
||
9 years ago
|
i2p::log::Logger().SendTo (i2p::fs::DataDirPath (i2p::fs::GetAppName () + ".log"));
|
||
8 years ago
|
i2p::log::Logger().Start ();
|
||
9 years ago
|
LogPrint(eLogInfo, "API: starting NetDB");
|
||
10 years ago
|
i2p::data::netdb.Start();
|
||
9 years ago
|
LogPrint(eLogInfo, "API: starting Transports");
|
||
10 years ago
|
i2p::transport::transports.Start();
|
||
9 years ago
|
LogPrint(eLogInfo, "API: starting Tunnels");
|
||
10 years ago
|
i2p::tunnel::tunnels.Start();
|
||
|
}
|
||
|
|
||
|
void StopI2P ()
|
||
|
{
|
||
9 years ago
|
LogPrint(eLogInfo, "API: shutting down");
|
||
|
LogPrint(eLogInfo, "API: stopping Tunnels");
|
||
10 years ago
|
i2p::tunnel::tunnels.Stop();
|
||
9 years ago
|
LogPrint(eLogInfo, "API: stopping Transports");
|
||
10 years ago
|
i2p::transport::transports.Stop();
|
||
9 years ago
|
LogPrint(eLogInfo, "API: stopping NetDB");
|
||
10 years ago
|
i2p::data::netdb.Stop();
|
||
8 years ago
|
i2p::log::Logger().Stop ();
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
9 years ago
|
void RunPeerTest ()
|
||
|
{
|
||
|
i2p::transport::transports.PeerTest ();
|
||
|
}
|
||
|
|
||
|
std::shared_ptr<i2p::client::ClientDestination> CreateLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic,
|
||
10 years ago
|
const std::map<std::string, std::string> * params)
|
||
10 years ago
|
{
|
||
9 years ago
|
auto localDestination = std::make_shared<i2p::client::ClientDestination> (keys, isPublic, params);
|
||
10 years ago
|
localDestination->Start ();
|
||
|
return localDestination;
|
||
|
}
|
||
|
|
||
9 years ago
|
std::shared_ptr<i2p::client::ClientDestination> CreateLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType,
|
||
10 years ago
|
const std::map<std::string, std::string> * params)
|
||
10 years ago
|
{
|
||
10 years ago
|
i2p::data::PrivateKeys keys = i2p::data::PrivateKeys::CreateRandomKeys (sigType);
|
||
9 years ago
|
auto localDestination = std::make_shared<i2p::client::ClientDestination> (keys, isPublic, params);
|
||
10 years ago
|
localDestination->Start ();
|
||
|
return localDestination;
|
||
|
}
|
||
|
|
||
9 years ago
|
void DestroyLocalDestination (std::shared_ptr<i2p::client::ClientDestination> dest)
|
||
10 years ago
|
{
|
||
|
if (dest)
|
||
|
dest->Stop ();
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
void RequestLeaseSet (std::shared_ptr<i2p::client::ClientDestination> dest, const i2p::data::IdentHash& remote)
|
||
10 years ago
|
{
|
||
|
if (dest)
|
||
10 years ago
|
dest->RequestDestination (remote);
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
std::shared_ptr<i2p::stream::Stream> CreateStream (std::shared_ptr<i2p::client::ClientDestination> dest, const i2p::data::IdentHash& remote)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (!dest) return nullptr;
|
||
|
auto leaseSet = dest->FindLeaseSet (remote);
|
||
10 years ago
|
if (leaseSet)
|
||
|
{
|
||
10 years ago
|
auto stream = dest->CreateStream (leaseSet);
|
||
10 years ago
|
stream->Send (nullptr, 0); // connect
|
||
|
return stream;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
RequestLeaseSet (dest, remote);
|
||
|
return nullptr;
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
void AcceptStream (std::shared_ptr<i2p::client::ClientDestination> dest, const i2p::stream::StreamingDestination::Acceptor& acceptor)
|
||
10 years ago
|
{
|
||
|
if (dest)
|
||
|
dest->AcceptStreams (acceptor);
|
||
|
}
|
||
|
|
||
10 years ago
|
void DestroyStream (std::shared_ptr<i2p::stream::Stream> stream)
|
||
10 years ago
|
{
|
||
|
if (stream)
|
||
|
stream->Close ();
|
||
|
}
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|