RouterContext.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. #include <fstream>
  2. #include <openssl/rand.h>
  3. #include "Config.h"
  4. #include "Crypto.h"
  5. #include "Ed25519.h"
  6. #include "Timestamp.h"
  7. #include "I2NPProtocol.h"
  8. #include "NetDb.hpp"
  9. #include "FS.h"
  10. #include "util.h"
  11. #include "version.h"
  12. #include "Log.h"
  13. #include "Family.h"
  14. #include "RouterContext.h"
  15. namespace i2p
  16. {
  17. RouterContext context;
  18. RouterContext::RouterContext ():
  19. m_LastUpdateTime (0), m_AcceptsTunnels (true), m_IsFloodfill (false),
  20. m_ShareRatio (100), m_Status (eRouterStatusOK),
  21. m_Error (eRouterErrorNone), m_NetID (I2PD_NET_ID)
  22. {
  23. }
  24. void RouterContext::Init ()
  25. {
  26. srand (i2p::util::GetMillisecondsSinceEpoch () % 1000);
  27. m_StartupTime = std::chrono::steady_clock::now();
  28. if (!Load ())
  29. CreateNewRouter ();
  30. m_Decryptor = m_Keys.CreateDecryptor (nullptr);
  31. UpdateRouterInfo ();
  32. }
  33. void RouterContext::CreateNewRouter ()
  34. {
  35. m_Keys = i2p::data::PrivateKeys::CreateRandomKeys (i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519);
  36. SaveKeys ();
  37. NewRouterInfo ();
  38. }
  39. void RouterContext::NewRouterInfo ()
  40. {
  41. i2p::data::RouterInfo routerInfo;
  42. routerInfo.SetRouterIdentity (GetIdentity ());
  43. uint16_t port; i2p::config::GetOption("port", port);
  44. if (!port)
  45. {
  46. port = rand () % (30777 - 9111) + 9111; // I2P network ports range
  47. if (port == 9150) port = 9151; // Tor browser
  48. }
  49. bool ipv4; i2p::config::GetOption("ipv4", ipv4);
  50. bool ipv6; i2p::config::GetOption("ipv6", ipv6);
  51. bool ssu; i2p::config::GetOption("ssu", ssu);
  52. bool ntcp; i2p::config::GetOption("ntcp", ntcp);
  53. bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
  54. bool nat; i2p::config::GetOption("nat", nat);
  55. std::string ifname; i2p::config::GetOption("ifname", ifname);
  56. std::string ifname4; i2p::config::GetOption("ifname4", ifname4);
  57. std::string ifname6; i2p::config::GetOption("ifname6", ifname6);
  58. if (ipv4)
  59. {
  60. std::string host = "127.0.0.1";
  61. if (!i2p::config::IsDefault("host"))
  62. i2p::config::GetOption("host", host);
  63. else if (!nat && !ifname.empty())
  64. /* bind to interface, we have no NAT so set external address too */
  65. host = i2p::util::net::GetInterfaceAddress(ifname, false).to_string(); // v4
  66. if(ifname4.size())
  67. host = i2p::util::net::GetInterfaceAddress(ifname4, false).to_string();
  68. if (ssu)
  69. routerInfo.AddSSUAddress (host.c_str(), port, routerInfo.GetIdentHash ());
  70. if (ntcp)
  71. routerInfo.AddNTCPAddress (host.c_str(), port);
  72. }
  73. if (ipv6)
  74. {
  75. std::string host = "::1";
  76. if (!i2p::config::IsDefault("host") && !ipv4) // override if v6 only
  77. i2p::config::GetOption("host", host);
  78. else if (!ifname.empty())
  79. host = i2p::util::net::GetInterfaceAddress(ifname, true).to_string(); // v6
  80. if(ifname6.size())
  81. host = i2p::util::net::GetInterfaceAddress(ifname6, true).to_string();
  82. if (ssu)
  83. routerInfo.AddSSUAddress (host.c_str(), port, routerInfo.GetIdentHash ());
  84. if (ntcp)
  85. routerInfo.AddNTCPAddress (host.c_str(), port);
  86. }
  87. routerInfo.SetCaps (i2p::data::RouterInfo::eReachable |
  88. i2p::data::RouterInfo::eSSUTesting | i2p::data::RouterInfo::eSSUIntroducer); // LR, BC
  89. routerInfo.SetProperty ("netId", std::to_string (m_NetID));
  90. routerInfo.SetProperty ("router.version", I2P_VERSION);
  91. routerInfo.CreateBuffer (m_Keys);
  92. m_RouterInfo.SetRouterIdentity (GetIdentity ());
  93. m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ());
  94. if (ntcp2) // we don't store iv in the address if non published so we must update it from keys
  95. {
  96. if (!m_NTCP2Keys) NewNTCP2Keys ();
  97. UpdateNTCP2Address (true);
  98. if (!ntcp) // NTCP2 should replace NTCP
  99. {
  100. bool published; i2p::config::GetOption("ntcp2.published", published);
  101. if (published)
  102. {
  103. PublishNTCP2Address (port, true);
  104. if (ipv6)
  105. {
  106. // add NTCP2 ipv6 address
  107. std::string host = "::1";
  108. if (!i2p::config::IsDefault ("ntcp2.addressv6"))
  109. i2p::config::GetOption ("ntcp2.addressv6", host);
  110. m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv, boost::asio::ip::address_v6::from_string (host), port);
  111. }
  112. }
  113. }
  114. }
  115. }
  116. void RouterContext::UpdateRouterInfo ()
  117. {
  118. m_RouterInfo.CreateBuffer (m_Keys);
  119. m_RouterInfo.SaveToFile (i2p::fs::DataDirPath (ROUTER_INFO));
  120. m_LastUpdateTime = i2p::util::GetSecondsSinceEpoch ();
  121. }
  122. void RouterContext::NewNTCP2Keys ()
  123. {
  124. m_StaticKeys.reset (new i2p::crypto::X25519Keys ());
  125. m_StaticKeys->GenerateKeys ();
  126. m_NTCP2Keys.reset (new NTCP2PrivateKeys ());
  127. m_StaticKeys->GetPrivateKey (m_NTCP2Keys->staticPrivateKey);
  128. memcpy (m_NTCP2Keys->staticPublicKey, m_StaticKeys->GetPublicKey (), 32);
  129. RAND_bytes (m_NTCP2Keys->iv, 16);
  130. // save
  131. std::ofstream fk (i2p::fs::DataDirPath (NTCP2_KEYS), std::ofstream::binary | std::ofstream::out);
  132. fk.write ((char *)m_NTCP2Keys.get (), sizeof (NTCP2PrivateKeys));
  133. }
  134. void RouterContext::SetStatus (RouterStatus status)
  135. {
  136. if (status != m_Status)
  137. {
  138. m_Status = status;
  139. m_Error = eRouterErrorNone;
  140. switch (m_Status)
  141. {
  142. case eRouterStatusOK:
  143. SetReachable ();
  144. break;
  145. case eRouterStatusFirewalled:
  146. SetUnreachable ();
  147. break;
  148. default:
  149. ;
  150. }
  151. }
  152. }
  153. void RouterContext::UpdatePort (int port)
  154. {
  155. bool updated = false;
  156. for (auto& address : m_RouterInfo.GetAddresses ())
  157. {
  158. if (!address->IsNTCP2 () && address->port != port)
  159. {
  160. address->port = port;
  161. updated = true;
  162. }
  163. }
  164. if (updated)
  165. UpdateRouterInfo ();
  166. }
  167. void RouterContext::PublishNTCP2Address (int port, bool publish, bool v4only)
  168. {
  169. if (!m_NTCP2Keys) return;
  170. bool updated = false;
  171. for (auto& address : m_RouterInfo.GetAddresses ())
  172. {
  173. if (address->IsNTCP2 () && (address->port != port || address->ntcp2->isPublished != publish) && (!v4only || address->host.is_v4 ()))
  174. {
  175. if (!port && !address->port)
  176. {
  177. // select random port only if address's port is not set
  178. port = rand () % (30777 - 9111) + 9111; // I2P network ports range
  179. if (port == 9150) port = 9151; // Tor browser
  180. }
  181. if (port) address->port = port;
  182. address->cost = publish ? 3 : 14;
  183. address->ntcp2->isPublished = publish;
  184. address->ntcp2->iv = m_NTCP2Keys->iv;
  185. updated = true;
  186. }
  187. }
  188. if (updated)
  189. UpdateRouterInfo ();
  190. }
  191. void RouterContext::UpdateNTCP2Address (bool enable)
  192. {
  193. auto& addresses = m_RouterInfo.GetAddresses ();
  194. bool found = false, updated = false;
  195. for (auto it = addresses.begin (); it != addresses.end (); ++it)
  196. {
  197. if ((*it)->IsNTCP2 ())
  198. {
  199. found = true;
  200. if (!enable)
  201. {
  202. addresses.erase (it);
  203. updated= true;
  204. }
  205. break;
  206. }
  207. }
  208. if (enable && !found)
  209. {
  210. m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv);
  211. updated = true;
  212. }
  213. if (updated)
  214. UpdateRouterInfo ();
  215. }
  216. void RouterContext::UpdateAddress (const boost::asio::ip::address& host)
  217. {
  218. bool updated = false;
  219. for (auto& address : m_RouterInfo.GetAddresses ())
  220. {
  221. if (address->host != host && address->IsCompatible (host))
  222. {
  223. address->host = host;
  224. if (host.is_v6 () && address->transportStyle == i2p::data::RouterInfo::eTransportSSU)
  225. {
  226. // update MTU
  227. auto mtu = i2p::util::net::GetMTU (host);
  228. if (mtu)
  229. {
  230. LogPrint (eLogDebug, "Router: Our v6 MTU=", mtu);
  231. if (mtu > 1472) { // TODO: magic constant
  232. mtu = 1472;
  233. LogPrint(eLogWarning, "Router: MTU dropped to upper limit of 1472 bytes");
  234. }
  235. if (address->ssu) address->ssu->mtu = mtu;
  236. }
  237. }
  238. updated = true;
  239. }
  240. }
  241. auto ts = i2p::util::GetSecondsSinceEpoch ();
  242. if (updated || ts > m_LastUpdateTime + ROUTER_INFO_UPDATE_INTERVAL)
  243. UpdateRouterInfo ();
  244. }
  245. bool RouterContext::AddIntroducer (const i2p::data::RouterInfo::Introducer& introducer)
  246. {
  247. bool ret = m_RouterInfo.AddIntroducer (introducer);
  248. if (ret)
  249. UpdateRouterInfo ();
  250. return ret;
  251. }
  252. void RouterContext::RemoveIntroducer (const boost::asio::ip::udp::endpoint& e)
  253. {
  254. if (m_RouterInfo.RemoveIntroducer (e))
  255. UpdateRouterInfo ();
  256. }
  257. void RouterContext::SetFloodfill (bool floodfill)
  258. {
  259. m_IsFloodfill = floodfill;
  260. if (floodfill)
  261. m_RouterInfo.SetCaps (m_RouterInfo.GetCaps () | i2p::data::RouterInfo::eFloodfill);
  262. else
  263. {
  264. m_RouterInfo.SetCaps (m_RouterInfo.GetCaps () & ~i2p::data::RouterInfo::eFloodfill);
  265. // we don't publish number of routers and leaseset for non-floodfill
  266. m_RouterInfo.DeleteProperty (i2p::data::ROUTER_INFO_PROPERTY_LEASESETS);
  267. m_RouterInfo.DeleteProperty (i2p::data::ROUTER_INFO_PROPERTY_ROUTERS);
  268. }
  269. UpdateRouterInfo ();
  270. }
  271. std::string RouterContext::GetFamily () const
  272. {
  273. return m_RouterInfo.GetProperty (i2p::data::ROUTER_INFO_PROPERTY_FAMILY);
  274. }
  275. void RouterContext::SetFamily (const std::string& family)
  276. {
  277. std::string signature;
  278. if (family.length () > 0)
  279. signature = i2p::data::CreateFamilySignature (family, GetIdentHash ());
  280. if (signature.length () > 0)
  281. {
  282. m_RouterInfo.SetProperty (i2p::data::ROUTER_INFO_PROPERTY_FAMILY, family);
  283. m_RouterInfo.SetProperty (i2p::data::ROUTER_INFO_PROPERTY_FAMILY_SIG, signature);
  284. }
  285. else
  286. {
  287. m_RouterInfo.DeleteProperty (i2p::data::ROUTER_INFO_PROPERTY_FAMILY);
  288. m_RouterInfo.DeleteProperty (i2p::data::ROUTER_INFO_PROPERTY_FAMILY_SIG);
  289. }
  290. }
  291. void RouterContext::SetBandwidth (char L)
  292. {
  293. uint32_t limit = 0;
  294. enum { low, high, extra, unlim } type = high;
  295. /* detect parameters */
  296. switch (L)
  297. {
  298. case i2p::data::CAPS_FLAG_LOW_BANDWIDTH1 : limit = 12; type = low; break;
  299. case i2p::data::CAPS_FLAG_LOW_BANDWIDTH2 : limit = 48; type = low; break;
  300. case i2p::data::CAPS_FLAG_HIGH_BANDWIDTH1 : limit = 64; type = high; break;
  301. case i2p::data::CAPS_FLAG_HIGH_BANDWIDTH2 : limit = 128; type = high; break;
  302. case i2p::data::CAPS_FLAG_HIGH_BANDWIDTH3 : limit = 256; type = high; break;
  303. case i2p::data::CAPS_FLAG_EXTRA_BANDWIDTH1 : limit = 2048; type = extra; break;
  304. case i2p::data::CAPS_FLAG_EXTRA_BANDWIDTH2 : limit = 1000000; type = unlim; break; // 1Gbyte/s
  305. default:
  306. limit = 48; type = low;
  307. }
  308. /* update caps & flags in RI */
  309. auto caps = m_RouterInfo.GetCaps ();
  310. caps &= ~i2p::data::RouterInfo::eHighBandwidth;
  311. caps &= ~i2p::data::RouterInfo::eExtraBandwidth;
  312. switch (type)
  313. {
  314. case low : /* not set */; break;
  315. case extra : caps |= i2p::data::RouterInfo::eExtraBandwidth; break; // 'P'
  316. case unlim : caps |= i2p::data::RouterInfo::eExtraBandwidth; // no break here, extra + high means 'X'
  317. case high : caps |= i2p::data::RouterInfo::eHighBandwidth; break;
  318. }
  319. m_RouterInfo.SetCaps (caps);
  320. UpdateRouterInfo ();
  321. m_BandwidthLimit = limit;
  322. }
  323. void RouterContext::SetBandwidth (int limit)
  324. {
  325. if (limit > 2000) { SetBandwidth('X'); }
  326. else if (limit > 256) { SetBandwidth('P'); }
  327. else if (limit > 128) { SetBandwidth('O'); }
  328. else if (limit > 64) { SetBandwidth('N'); }
  329. else if (limit > 48) { SetBandwidth('M'); }
  330. else if (limit > 12) { SetBandwidth('L'); }
  331. else { SetBandwidth('K'); }
  332. }
  333. void RouterContext::SetShareRatio (int percents)
  334. {
  335. if (percents < 0) percents = 0;
  336. if (percents > 100) percents = 100;
  337. m_ShareRatio = percents;
  338. }
  339. bool RouterContext::IsUnreachable () const
  340. {
  341. return m_RouterInfo.GetCaps () & i2p::data::RouterInfo::eUnreachable;
  342. }
  343. void RouterContext::PublishNTCPAddress (bool publish, bool v4only)
  344. {
  345. auto& addresses = m_RouterInfo.GetAddresses ();
  346. if (publish)
  347. {
  348. for (const auto& addr : addresses) // v4
  349. {
  350. if (addr->transportStyle == i2p::data::RouterInfo::eTransportSSU &&
  351. addr->host.is_v4 ())
  352. {
  353. // insert NTCP address with host/port from SSU
  354. m_RouterInfo.AddNTCPAddress (addr->host.to_string ().c_str (), addr->port);
  355. break;
  356. }
  357. }
  358. if (!v4only)
  359. {
  360. for (const auto& addr : addresses) // v6
  361. {
  362. if (addr->transportStyle == i2p::data::RouterInfo::eTransportSSU &&
  363. addr->host.is_v6 ())
  364. {
  365. // insert NTCP address with host/port from SSU
  366. m_RouterInfo.AddNTCPAddress (addr->host.to_string ().c_str (), addr->port);
  367. break;
  368. }
  369. }
  370. }
  371. }
  372. else
  373. {
  374. for (auto it = addresses.begin (); it != addresses.end ();)
  375. {
  376. if ((*it)->transportStyle == i2p::data::RouterInfo::eTransportNTCP && !(*it)->IsNTCP2 () &&
  377. (!v4only || (*it)->host.is_v4 ()))
  378. {
  379. it = addresses.erase (it);
  380. if (v4only) break; // otherwise might be more than one address
  381. }
  382. else
  383. ++it;
  384. }
  385. }
  386. }
  387. void RouterContext::SetUnreachable ()
  388. {
  389. // set caps
  390. uint8_t caps = m_RouterInfo.GetCaps ();
  391. caps &= ~i2p::data::RouterInfo::eReachable;
  392. caps |= i2p::data::RouterInfo::eUnreachable;
  393. caps &= ~i2p::data::RouterInfo::eFloodfill; // can't be floodfill
  394. caps &= ~i2p::data::RouterInfo::eSSUIntroducer; // can't be introducer
  395. m_RouterInfo.SetCaps (caps);
  396. uint16_t port = 0;
  397. // delete previous introducers
  398. auto& addresses = m_RouterInfo.GetAddresses ();
  399. for (auto& addr : addresses)
  400. if (addr->ssu)
  401. {
  402. addr->ssu->introducers.clear ();
  403. port = addr->port;
  404. }
  405. // remove NTCP or NTCP2 v4 address
  406. bool ntcp; i2p::config::GetOption("ntcp", ntcp);
  407. if (ntcp)
  408. PublishNTCPAddress (false);
  409. else
  410. {
  411. bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
  412. if (ntcp2)
  413. PublishNTCP2Address (port, false, true);
  414. }
  415. // update
  416. UpdateRouterInfo ();
  417. }
  418. void RouterContext::SetReachable ()
  419. {
  420. // update caps
  421. uint8_t caps = m_RouterInfo.GetCaps ();
  422. caps &= ~i2p::data::RouterInfo::eUnreachable;
  423. caps |= i2p::data::RouterInfo::eReachable;
  424. caps |= i2p::data::RouterInfo::eSSUIntroducer;
  425. if (m_IsFloodfill)
  426. caps |= i2p::data::RouterInfo::eFloodfill;
  427. m_RouterInfo.SetCaps (caps);
  428. uint16_t port = 0;
  429. // delete previous introducers
  430. auto& addresses = m_RouterInfo.GetAddresses ();
  431. for (auto& addr : addresses)
  432. if (addr->ssu)
  433. {
  434. addr->ssu->introducers.clear ();
  435. port = addr->port;
  436. }
  437. // insert NTCP or NTCP2 back
  438. bool ntcp; i2p::config::GetOption("ntcp", ntcp);
  439. if (ntcp)
  440. PublishNTCPAddress (true);
  441. else
  442. {
  443. // ntcp2
  444. bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
  445. if (ntcp2)
  446. {
  447. bool published; i2p::config::GetOption ("ntcp2.published", published);
  448. if (published)
  449. {
  450. uint16_t ntcp2Port; i2p::config::GetOption ("ntcp2.port", ntcp2Port);
  451. if (!ntcp2Port) ntcp2Port = port;
  452. PublishNTCP2Address (ntcp2Port, true, true);
  453. }
  454. }
  455. }
  456. // update
  457. UpdateRouterInfo ();
  458. }
  459. void RouterContext::SetSupportsV6 (bool supportsV6)
  460. {
  461. if (supportsV6)
  462. {
  463. m_RouterInfo.EnableV6 ();
  464. // insert v6 addresses if necessary
  465. bool foundSSU = false, foundNTCP = false, foundNTCP2 = false;
  466. uint16_t port = 0;
  467. auto& addresses = m_RouterInfo.GetAddresses ();
  468. for (auto& addr: addresses)
  469. {
  470. if (addr->host.is_v6 ())
  471. {
  472. if (addr->transportStyle == i2p::data::RouterInfo::eTransportSSU)
  473. foundSSU = true;
  474. else if (addr->IsNTCP2 ())
  475. {
  476. if (addr->IsPublishedNTCP2 ()) foundNTCP2 = true;
  477. }
  478. else
  479. foundNTCP = true;
  480. }
  481. port = addr->port;
  482. }
  483. if (!port) i2p::config::GetOption("port", port);
  484. // SSU
  485. if (!foundSSU)
  486. {
  487. bool ssu; i2p::config::GetOption("ssu", ssu);
  488. if (ssu)
  489. {
  490. std::string host = "::1"; // TODO: read host
  491. m_RouterInfo.AddSSUAddress (host.c_str (), port, GetIdentHash ());
  492. }
  493. }
  494. // NTCP2
  495. if (!foundNTCP2)
  496. {
  497. bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
  498. bool ntcp2Published; i2p::config::GetOption("ntcp2.published", ntcp2Published);
  499. if (ntcp2 && ntcp2Published)
  500. {
  501. std::string ntcp2Host;
  502. if (!i2p::config::IsDefault ("ntcp2.addressv6"))
  503. i2p::config::GetOption ("ntcp2.addressv6", ntcp2Host);
  504. else
  505. ntcp2Host = "::1";
  506. uint16_t ntcp2Port; i2p::config::GetOption ("ntcp2.port", ntcp2Port);
  507. if (!ntcp2Port) ntcp2Port = port;
  508. m_RouterInfo.AddNTCP2Address (m_NTCP2Keys->staticPublicKey, m_NTCP2Keys->iv, boost::asio::ip::address::from_string (ntcp2Host), ntcp2Port);
  509. }
  510. }
  511. // NTCP
  512. if (!foundNTCP)
  513. {
  514. bool ntcp; i2p::config::GetOption("ntcp", ntcp);
  515. if (ntcp)
  516. {
  517. std::string host = "::1";
  518. m_RouterInfo.AddNTCPAddress (host.c_str (), port);
  519. }
  520. }
  521. }
  522. else
  523. m_RouterInfo.DisableV6 ();
  524. UpdateRouterInfo ();
  525. }
  526. void RouterContext::SetSupportsV4 (bool supportsV4)
  527. {
  528. if (supportsV4)
  529. m_RouterInfo.EnableV4 ();
  530. else
  531. m_RouterInfo.DisableV4 ();
  532. UpdateRouterInfo ();
  533. }
  534. void RouterContext::UpdateNTCP2V6Address (const boost::asio::ip::address& host)
  535. {
  536. bool updated = false;
  537. auto& addresses = m_RouterInfo.GetAddresses ();
  538. for (auto& addr: addresses)
  539. {
  540. if (addr->IsPublishedNTCP2 ())
  541. {
  542. if (addr->host.is_v6 ())
  543. {
  544. if (addr->host != host)
  545. {
  546. addr->host = host;
  547. updated = true;
  548. }
  549. break;
  550. }
  551. }
  552. }
  553. if (updated)
  554. UpdateRouterInfo ();
  555. }
  556. void RouterContext::UpdateStats ()
  557. {
  558. if (m_IsFloodfill)
  559. {
  560. // update routers and leasesets
  561. m_RouterInfo.SetProperty (i2p::data::ROUTER_INFO_PROPERTY_LEASESETS, std::to_string(i2p::data::netdb.GetNumLeaseSets ()));
  562. m_RouterInfo.SetProperty (i2p::data::ROUTER_INFO_PROPERTY_ROUTERS, std::to_string(i2p::data::netdb.GetNumRouters ()));
  563. UpdateRouterInfo ();
  564. }
  565. }
  566. void RouterContext::UpdateTimestamp (uint64_t ts)
  567. {
  568. if (ts > m_LastUpdateTime + ROUTER_INFO_UPDATE_INTERVAL)
  569. UpdateRouterInfo ();
  570. }
  571. bool RouterContext::Load ()
  572. {
  573. std::ifstream fk (i2p::fs::DataDirPath (ROUTER_KEYS), std::ifstream::in | std::ifstream::binary);
  574. if (!fk.is_open ()) return false;
  575. fk.seekg (0, std::ios::end);
  576. size_t len = fk.tellg();
  577. fk.seekg (0, std::ios::beg);
  578. if (len == sizeof (i2p::data::Keys)) // old keys file format
  579. {
  580. i2p::data::Keys keys;
  581. fk.read ((char *)&keys, sizeof (keys));
  582. m_Keys = keys;
  583. }
  584. else // new keys file format
  585. {
  586. uint8_t * buf = new uint8_t[len];
  587. fk.read ((char *)buf, len);
  588. m_Keys.FromBuffer (buf, len);
  589. delete[] buf;
  590. }
  591. // read NTCP2 keys if available
  592. std::ifstream n2k (i2p::fs::DataDirPath (NTCP2_KEYS), std::ifstream::in | std::ifstream::binary);
  593. if (n2k)
  594. {
  595. n2k.seekg (0, std::ios::end);
  596. len = n2k.tellg();
  597. n2k.seekg (0, std::ios::beg);
  598. if (len == sizeof (NTCP2PrivateKeys))
  599. {
  600. m_NTCP2Keys.reset (new NTCP2PrivateKeys ());
  601. n2k.read ((char *)m_NTCP2Keys.get (), sizeof (NTCP2PrivateKeys));
  602. }
  603. n2k.close ();
  604. }
  605. // read RouterInfo
  606. m_RouterInfo.SetRouterIdentity (GetIdentity ());
  607. i2p::data::RouterInfo routerInfo(i2p::fs::DataDirPath (ROUTER_INFO));
  608. if (!routerInfo.IsUnreachable ()) // router.info looks good
  609. {
  610. m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ());
  611. m_RouterInfo.SetProperty ("coreVersion", I2P_VERSION);
  612. m_RouterInfo.SetProperty ("router.version", I2P_VERSION);
  613. // Migration to 0.9.24. TODO: remove later
  614. m_RouterInfo.DeleteProperty ("coreVersion");
  615. m_RouterInfo.DeleteProperty ("stat_uptime");
  616. }
  617. else
  618. {
  619. LogPrint (eLogError, ROUTER_INFO, " is malformed. Creating new");
  620. NewRouterInfo ();
  621. }
  622. if (IsUnreachable ())
  623. SetReachable (); // we assume reachable until we discover firewall through peer tests
  624. // read NTCP2
  625. bool ntcp2; i2p::config::GetOption("ntcp2.enabled", ntcp2);
  626. if (ntcp2)
  627. {
  628. if (!m_NTCP2Keys) NewNTCP2Keys ();
  629. UpdateNTCP2Address (true); // enable NTCP2
  630. }
  631. else
  632. UpdateNTCP2Address (false); // disable NTCP2
  633. return true;
  634. }
  635. void RouterContext::SaveKeys ()
  636. {
  637. // save in the same format as .dat files
  638. std::ofstream fk (i2p::fs::DataDirPath (ROUTER_KEYS), std::ofstream::binary | std::ofstream::out);
  639. size_t len = m_Keys.GetFullLen ();
  640. uint8_t * buf = new uint8_t[len];
  641. m_Keys.ToBuffer (buf, len);
  642. fk.write ((char *)buf, len);
  643. delete[] buf;
  644. }
  645. std::shared_ptr<i2p::tunnel::TunnelPool> RouterContext::GetTunnelPool () const
  646. {
  647. return i2p::tunnel::tunnels.GetExploratoryPool ();
  648. }
  649. void RouterContext::HandleI2NPMessage (const uint8_t * buf, size_t len, std::shared_ptr<i2p::tunnel::InboundTunnel> from)
  650. {
  651. i2p::HandleI2NPMessage (CreateI2NPMessage (buf, GetI2NPMessageLength (buf, len), from));
  652. }
  653. void RouterContext::ProcessGarlicMessage (std::shared_ptr<I2NPMessage> msg)
  654. {
  655. std::unique_lock<std::mutex> l(m_GarlicMutex);
  656. i2p::garlic::GarlicDestination::ProcessGarlicMessage (msg);
  657. }
  658. void RouterContext::ProcessDeliveryStatusMessage (std::shared_ptr<I2NPMessage> msg)
  659. {
  660. std::unique_lock<std::mutex> l(m_GarlicMutex);
  661. i2p::garlic::GarlicDestination::ProcessDeliveryStatusMessage (msg);
  662. }
  663. void RouterContext::CleanupDestination ()
  664. {
  665. std::unique_lock<std::mutex> l(m_GarlicMutex);
  666. i2p::garlic::GarlicDestination::CleanupExpiredTags ();
  667. }
  668. uint32_t RouterContext::GetUptime () const
  669. {
  670. return std::chrono::duration_cast<std::chrono::seconds> (std::chrono::steady_clock::now() - m_StartupTime).count ();
  671. }
  672. bool RouterContext::Decrypt (const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx) const
  673. {
  674. return m_Decryptor ? m_Decryptor->Decrypt (encrypted, data, ctx, true) : false;
  675. }
  676. bool RouterContext::DecryptTunnelBuildRecord (const uint8_t * encrypted, uint8_t * data, BN_CTX * ctx) const
  677. {
  678. return m_Decryptor ? m_Decryptor->Decrypt (encrypted, data, ctx, false) : false;
  679. }
  680. i2p::crypto::X25519Keys& RouterContext::GetStaticKeys ()
  681. {
  682. if (!m_StaticKeys)
  683. {
  684. if (!m_NTCP2Keys) NewNTCP2Keys ();
  685. auto x = new i2p::crypto::X25519Keys (m_NTCP2Keys->staticPrivateKey, m_NTCP2Keys->staticPublicKey);
  686. if (!m_StaticKeys)
  687. m_StaticKeys.reset (x);
  688. else
  689. delete x;
  690. }
  691. return *m_StaticKeys;
  692. }
  693. }