From 4109ab1590791f3c1bf4e9eceec2d43be7b5ea47 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Edward=20Tj=C3=B6rnhammar?= <ed@cflags.cc>
Date: Fri, 20 Nov 2015 13:52:38 +0100
Subject: [PATCH] Make tunnels.cfg configurable

---
 ClientContext.cpp | 7 ++++---
 ClientContext.h   | 1 -
 README.md         | 1 +
 util.cpp          | 8 ++++++++
 util.h            | 1 +
 5 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/ClientContext.cpp b/ClientContext.cpp
index 0adeee9b..84ffb538 100644
--- a/ClientContext.cpp
+++ b/ClientContext.cpp
@@ -243,13 +243,14 @@ namespace client
 	void ClientContext::ReadTunnels ()
 	{
 		boost::property_tree::ptree pt;
+		std::string pathTunnelsConfigFile = i2p::util::filesystem::GetTunnelsConfigFile().string();
 		try
 		{
-			boost::property_tree::read_ini (i2p::util::filesystem::GetFullPath (TUNNELS_CONFIG_FILENAME), pt);
+			boost::property_tree::read_ini (pathTunnelsConfigFile, pt);
 		}
 		catch (std::exception& ex)
 		{
-			LogPrint (eLogWarning, "Can't read ", TUNNELS_CONFIG_FILENAME, ": ", ex.what ());
+			LogPrint (eLogWarning, "Can't read ", pathTunnelsConfigFile, ": ", ex.what ());
 			return;
 		}
 			
@@ -313,7 +314,7 @@ namespace client
 					numServerTunnels++;
 				}
 				else
-					LogPrint (eLogWarning, "Unknown section type=", type, " of ", name, " in ", TUNNELS_CONFIG_FILENAME);
+					LogPrint (eLogWarning, "Unknown section type=", type, " of ", name, " in ", pathTunnelsConfigFile);
 				
 			}
 			catch (std::exception& ex)
diff --git a/ClientContext.h b/ClientContext.h
index 762b2e75..842ee918 100644
--- a/ClientContext.h
+++ b/ClientContext.h
@@ -29,7 +29,6 @@ namespace client
 	const char I2P_SERVER_TUNNEL_KEYS[] = "keys";
 	const char I2P_SERVER_TUNNEL_INPORT[] = "inport";
 	const char I2P_SERVER_TUNNEL_ACCESS_LIST[] = "accesslist";		
-	const char TUNNELS_CONFIG_FILENAME[] = "tunnels.cfg";
 
 	class ClientContext
 	{
diff --git a/README.md b/README.md
index 393aab79..631bd55d 100644
--- a/README.md
+++ b/README.md
@@ -94,6 +94,7 @@ Cmdline options
 * --samport=            - Port of SAM bridge. Usually 7656. SAM is off if not specified
 * --bobport=            - Port of BOB command channel. Usually 2827. BOB is off if not specified
 * --i2pcontrolport=     - Port of I2P control service. Usually 7650. I2PControl is off if not specified
+* --tunnelscfg=         - Tunnels Config file (default: ~/.i2pd/tunnels.cfg or /var/lib/i2pd/tunnels.cfg)
 * --conf=               - Config file (default: ~/.i2pd/i2p.conf or /var/lib/i2pd/i2p.conf)
                           This parameter will be silently ignored if the specified config file does not exist.
                           Options specified on the command line take precedence over those in the config file.
diff --git a/util.cpp b/util.cpp
index fbaf2dbf..097dc90c 100644
--- a/util.cpp
+++ b/util.cpp
@@ -193,6 +193,14 @@ namespace filesystem
 		return pathConfigFile;
 	}
 
+	boost::filesystem::path GetTunnelsConfigFile()
+	{
+	  boost::filesystem::path pathTunnelsConfigFile(i2p::util::config::GetArg("-tunnelscfg", "tunnels.cfg"));
+		if (!pathTunnelsConfigFile.is_complete())
+		  pathTunnelsConfigFile = GetDataDir() / pathTunnelsConfigFile;
+		return pathTunnelsConfigFile;
+	}
+
 	void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet,
 						std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet)
 	{
diff --git a/util.h b/util.h
index 4b229380..1a579283 100644
--- a/util.h
+++ b/util.h
@@ -33,6 +33,7 @@ namespace util
 		std::string GetFullPath (const std::string& filename);	
 		boost::filesystem::path GetDefaultDataDir();
 		boost::filesystem::path GetConfigFile();
+		boost::filesystem::path GetTunnelsConfigFile();
 		void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet,
                 std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet);
 		boost::filesystem::path GetCertificatesDir();