From 5a31d2e817974e2a4da487a540effd3804763610 Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 13 Mar 2015 16:05:39 -0400 Subject: [PATCH] read tunnels.cfg using property tree --- ClientContext.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++ ClientContext.h | 8 +++++++ 2 files changed, 63 insertions(+) diff --git a/ClientContext.cpp b/ClientContext.cpp index fb6e4791..3e242ef6 100644 --- a/ClientContext.cpp +++ b/ClientContext.cpp @@ -2,6 +2,8 @@ #include #include #include +#include +#include #include "util.h" #include "Log.h" #include "Identity.h" @@ -327,5 +329,58 @@ namespace client } } } + + void ClientContext::ReadTunnels1 () + { + boost::property_tree::ptree pt; + try + { + boost::property_tree::read_ini (i2p::util::filesystem::GetFullPath (TUNNELS_CONFIG_FILENAME), pt); + } + catch (std::exception& ex) + { + LogPrint (eLogWarning, "Can't read ", TUNNELS_CONFIG_FILENAME, ": ", ex.what ()); + return; + } + + int numClientTunnels = 0, numServerTunnels = 0; + for (auto& section: pt) + { + if (section.first == I2P_TUNNELS_SECTION_CLIENT) + { + try + { + // mandatory params + std::string dest = section.second.get (I2P_CLIENT_TUNNEL_DESTINATION1); + int port = section.second.get (I2P_CLIENT_TUNNEL_PORT1); + std::string keys = section.second.get (I2P_CLIENT_TUNNEL_KEYS1); + // optional params + //int destinationPort = section.second.get (I2P_CLIENT_TUNNEL_DESTINATION_PORT1, 0); + + std::shared_ptr localDestination = nullptr; + if (keys.length () > 0) + localDestination = LoadLocalDestination (keys, false); + auto clientTunnel = new I2PClientTunnel (dest, port, localDestination); + if (m_ClientTunnels.insert (std::make_pair (port, std::unique_ptr(clientTunnel))).second) + clientTunnel->Start (); + else + LogPrint (eLogError, "I2P client tunnel with port ", port, " already exists"); + numClientTunnels++; + } + catch (std::exception& ex) + { + LogPrint (eLogError, "Can't read client tunnel params: ", ex.what ()); + } + } + else if (section.first == I2P_TUNNELS_SECTION_SERVER) + { + numServerTunnels++; + } + else + LogPrint (eLogWarning, "Unknown section ", section.first, " in ", TUNNELS_CONFIG_FILENAME); + } + LogPrint (eLogInfo, numClientTunnels, " I2P client tunnels created"); + LogPrint (eLogInfo, numServerTunnels, " I2P server tunnels created"); + } } } diff --git a/ClientContext.h b/ClientContext.h index 007aa359..d535c0c9 100644 --- a/ClientContext.h +++ b/ClientContext.h @@ -17,6 +17,13 @@ namespace i2p { namespace client { + const char I2P_TUNNELS_SECTION_CLIENT[] = "client"; + const char I2P_TUNNELS_SECTION_SERVER[] = "server"; + const char I2P_CLIENT_TUNNEL_PORT1[] = "port"; + const char I2P_CLIENT_TUNNEL_DESTINATION1[] = "destination"; + const char I2P_CLIENT_TUNNEL_KEYS1[] = "keys"; + const char I2P_CLIENT_TUNNEL_DESTINATION_PORT1[] = "destinationport"; + const char I2P_CLIENT_TUNNEL_NAME[] = "client.name"; const char I2P_CLIENT_TUNNEL_PORT[] = "client.port"; const char I2P_CLIENT_TUNNEL_DESTINATION[] = "client.destination"; @@ -52,6 +59,7 @@ namespace client private: void ReadTunnels (); + void ReadTunnels1 (); // using propery tree private: