api.cpp
1 /* 2 * Copyright (c) 2013-2025, The PurpleI2P Project 3 * 4 * This file is part of Purple i2pd project and licensed under BSD3 5 * 6 * See full license text in LICENSE file at top of project tree 7 */ 8 9 #include <string> 10 #include <map> 11 #include "Config.h" 12 #include "Log.h" 13 #include "NetDb.hpp" 14 #include "Transports.h" 15 #include "Tunnel.h" 16 #include "RouterContext.h" 17 #include "Identity.h" 18 #include "Destination.h" 19 #include "Crypto.h" 20 #include "FS.h" 21 #include "api.h" 22 23 namespace i2p 24 { 25 namespace api 26 { 27 void InitI2P (int argc, char* argv[], const char * appName) 28 { 29 i2p::config::Init (); 30 i2p::config::ParseCmdline (argc, argv, true); // ignore unknown options and help 31 i2p::config::Finalize (); 32 33 std::string datadir; i2p::config::GetOption("datadir", datadir); 34 35 i2p::fs::SetAppName (appName); 36 i2p::fs::DetectDataDir(datadir, false); 37 i2p::fs::Init(); 38 39 bool precomputation; i2p::config::GetOption("precomputation.elgamal", precomputation); 40 i2p::crypto::InitCrypto (precomputation); 41 42 int netID; i2p::config::GetOption("netid", netID); 43 i2p::context.SetNetID (netID); 44 45 bool checkReserved; i2p::config::GetOption("reservedrange", checkReserved); 46 i2p::transport::transports.SetCheckReserved(checkReserved); 47 48 i2p::context.Init (); 49 } 50 51 void TerminateI2P () 52 { 53 i2p::crypto::TerminateCrypto (); 54 } 55 56 void StartI2P (std::shared_ptr<std::ostream> logStream) 57 { 58 if (logStream) 59 i2p::log::Logger().SendTo (logStream); 60 else 61 i2p::log::Logger().SendTo (i2p::fs::DataDirPath (i2p::fs::GetAppName () + ".log")); 62 i2p::log::Logger().Start (); 63 i2p::transport::InitTransports (); 64 LogPrint(eLogInfo, "API: Starting NetDB"); 65 i2p::data::netdb.Start(); 66 LogPrint(eLogInfo, "API: Starting Transports"); 67 i2p::transport::transports.Start(); 68 LogPrint(eLogInfo, "API: Starting Tunnels"); 69 i2p::tunnel::tunnels.Start(); 70 LogPrint(eLogInfo, "API: Starting Router context"); 71 i2p::context.Start(); 72 } 73 74 void StopI2P () 75 { 76 LogPrint(eLogInfo, "API: Shutting down"); 77 LogPrint(eLogInfo, "API: Stopping Router context"); 78 i2p::context.Stop(); 79 LogPrint(eLogInfo, "API: Stopping Tunnels"); 80 i2p::tunnel::tunnels.Stop(); 81 LogPrint(eLogInfo, "API: Stopping Transports"); 82 i2p::transport::transports.Stop(); 83 LogPrint(eLogInfo, "API: Stopping NetDB"); 84 i2p::data::netdb.Stop(); 85 i2p::log::Logger().Stop (); 86 } 87 88 void RunPeerTest () 89 { 90 i2p::transport::transports.PeerTest (); 91 } 92 93 std::shared_ptr<i2p::client::ClientDestination> CreateLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic, 94 const i2p::util::Mapping * params) 95 { 96 auto localDestination = std::make_shared<i2p::client::RunnableClientDestination> (keys, isPublic, params); 97 localDestination->Start (); 98 return localDestination; 99 } 100 101 std::shared_ptr<i2p::client::ClientDestination> CreateLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType, 102 const i2p::util::Mapping * params) 103 { 104 i2p::data::PrivateKeys keys = i2p::data::PrivateKeys::CreateRandomKeys (sigType); 105 auto localDestination = std::make_shared<i2p::client::RunnableClientDestination> (keys, isPublic, params); 106 localDestination->Start (); 107 return localDestination; 108 } 109 110 void DestroyLocalDestination (std::shared_ptr<i2p::client::ClientDestination> dest) 111 { 112 if (dest) 113 dest->Stop (); 114 } 115 116 void RequestLeaseSet (std::shared_ptr<i2p::client::ClientDestination> dest, const i2p::data::IdentHash& remote) 117 { 118 if (dest) 119 dest->RequestDestination (remote); 120 } 121 122 std::shared_ptr<i2p::stream::Stream> CreateStream (std::shared_ptr<i2p::client::ClientDestination> dest, const i2p::data::IdentHash& remote) 123 { 124 if (!dest) return nullptr; 125 auto leaseSet = dest->FindLeaseSet (remote); 126 if (leaseSet) 127 { 128 auto stream = dest->CreateStream (leaseSet); 129 stream->Send (nullptr, 0); // connect 130 return stream; 131 } 132 else 133 { 134 RequestLeaseSet (dest, remote); 135 return nullptr; 136 } 137 } 138 139 void AcceptStream (std::shared_ptr<i2p::client::ClientDestination> dest, const i2p::stream::StreamingDestination::Acceptor& acceptor) 140 { 141 if (dest) 142 dest->AcceptStreams (acceptor); 143 } 144 145 void DestroyStream (std::shared_ptr<i2p::stream::Stream> stream) 146 { 147 if (stream) 148 stream->Close (); 149 } 150 } 151 }