Compare commits

..

1 Commits

Author SHA1 Message Date
R4SAS
3ab5ac66b6 modulize client protocols
Signed-off-by: R4SAS <r4sas@i2pmail.org>
2022-05-03 17:20:19 +03:00
18 changed files with 260 additions and 227 deletions

View File

@@ -40,6 +40,12 @@ USE_GIT_VERSION := $(or $(USE_GIT_VERSION),no)
# for MacOS only, waiting for "1", not "yes"
HOMEBREW := $(or $(HOMEBREW),0)
# Client protocols
USE_I2PC := $(or $(USE_I2PC),yes)
USE_I2CP := $(or $(USE_I2CP),yes)
USE_SAM := $(or $(USE_SAM),yes)
USE_BOB := $(or $(USE_BOB),yes)
ifeq ($(DEBUG),yes)
CXX_DEBUG = -g
else
@@ -47,6 +53,19 @@ else
LD_DEBUG = -s
endif
ifeq ($(USE_I2PC),yes)
NEEDED_CXXFLAGS += -DWITH_I2PC
endif
ifeq ($(USE_I2CP),yes)
NEEDED_CXXFLAGS += -DWITH_I2CP
endif
ifeq ($(USE_SAM),yes)
NEEDED_CXXFLAGS += -DWITH_SAM
endif
ifeq ($(USE_BOB),yes)
NEEDED_CXXFLAGS += -DWITH_BOB
endif
ifneq (, $(findstring darwin, $(SYS)))
DAEMON_SRC += $(DAEMON_SRC_DIR)/UnixDaemon.cpp
ifeq ($(HOMEBREW),1)

View File

@@ -26,7 +26,9 @@
#include "Streaming.h"
#include "Destination.h"
#include "HTTPServer.h"
#ifdef WITH_I2PC
#include "I2PControl.h"
#endif
#include "ClientContext.h"
#include "Crypto.h"
#include "UPnP.h"
@@ -45,7 +47,9 @@ namespace util
~Daemon_Singleton_Private() {};
std::unique_ptr<i2p::http::HTTPServer> httpServer;
#ifdef WITH_I2PC
std::unique_ptr<i2p::client::I2PControlService> m_I2PControlService;
#endif
std::unique_ptr<i2p::transport::UPnP> UPnP;
std::unique_ptr<i2p::util::NTPTimeSync> m_NTPSync;
};
@@ -441,6 +445,7 @@ namespace util
LogPrint(eLogInfo, "Daemon: Starting Client");
i2p::client::context.Start ();
#ifdef WITH_I2PC
// I2P Control Protocol
bool i2pcontrol; i2p::config::GetOption("i2pcontrol.enabled", i2pcontrol);
if (i2pcontrol) {
@@ -458,6 +463,7 @@ namespace util
ThrowFatal ("Unable to start I2PControl service at ", i2pcpAddr, ":", i2pcpPort, ": ", ex.what ());
}
}
#endif
return true;
}
@@ -490,12 +496,14 @@ namespace util
d.httpServer->Stop();
d.httpServer = nullptr;
}
#ifdef WITH_I2PC
if (d.m_I2PControlService)
{
LogPrint(eLogInfo, "Daemon: Stopping I2PControl");
d.m_I2PControlService->Stop ();
d.m_I2PControlService = nullptr;
}
#endif
i2p::crypto::TerminateCrypto ();
i2p::log::Logger().Stop();

View File

@@ -68,9 +68,13 @@ namespace http {
const char HTTP_PAGE_TRANSPORTS[] = "transports";
const char HTTP_PAGE_LOCAL_DESTINATIONS[] = "local_destinations";
const char HTTP_PAGE_LOCAL_DESTINATION[] = "local_destination";
#ifdef WITH_I2CP
const char HTTP_PAGE_I2CP_LOCAL_DESTINATION[] = "i2cp_local_destination";
#endif
#ifdef WITH_SAM
const char HTTP_PAGE_SAM_SESSIONS[] = "sam_sessions";
const char HTTP_PAGE_SAM_SESSION[] = "sam_session";
#endif
const char HTTP_PAGE_I2P_TUNNELS[] = "i2p_tunnels";
const char HTTP_PAGE_COMMANDS[] = "commands";
const char HTTP_PAGE_LEASESETS[] = "leasesets";
@@ -87,7 +91,9 @@ namespace http {
const char HTTP_COMMAND_GET_REG_STRING[] = "get_reg_string";
const char HTTP_COMMAND_SETLANGUAGE[] = "setlanguage";
const char HTTP_COMMAND_RELOAD_CSS[] = "reload_css";
#ifdef WITH_SAM
const char HTTP_PARAM_SAM_SESSION_ID[] = "id";
#endif
const char HTTP_PARAM_ADDRESS[] = "address";
static std::string ConvertTime (uint64_t time)
@@ -202,8 +208,10 @@ namespace http {
s <<
" <a href=\"" << webroot << "?page=" << HTTP_PAGE_TRANSPORTS << "\">" << tr ("Transports") << "</a><br>\r\n"
" <a href=\"" << webroot << "?page=" << HTTP_PAGE_I2P_TUNNELS << "\">" << tr("I2P tunnels") << "</a><br>\r\n";
#ifdef WITH_SAM
if (i2p::client::context.GetSAMBridge ())
s << " <a href=\"" << webroot << "?page=" << HTTP_PAGE_SAM_SESSIONS << "\">" << tr("SAM sessions") << "</a><br>\r\n";
#endif
s <<
"</div>\r\n"
"<div class=\"content\">";
@@ -361,17 +369,25 @@ namespace http {
if (outputFormat==OutputFormatEnum::forWebConsole) {
bool httpproxy = i2p::client::context.GetHttpProxy () ? true : false;
bool socksproxy = i2p::client::context.GetSocksProxy () ? true : false;
bool bob = i2p::client::context.GetBOBCommandChannel () ? true : false;
bool sam = i2p::client::context.GetSAMBridge () ? true : false;
bool i2cp = i2p::client::context.GetI2CPServer () ? true : false;
bool i2pcontrol; i2p::config::GetOption("i2pcontrol.enabled", i2pcontrol);
s << "<table class=\"services\"><caption>" << tr("Services") << "</caption><tbody>\r\n";
s << "<tr><td>" << "HTTP " << tr("Proxy") << "</td><td class='" << (httpproxy ? "enabled" : "disabled") << "'>" << (httpproxy ? tr("Enabled") : tr("Disabled")) << "</td></tr>\r\n";
s << "<tr><td>" << "SOCKS " << tr("Proxy") << "</td><td class='" << (socksproxy ? "enabled" : "disabled") << "'>" << (socksproxy ? tr("Enabled") : tr("Disabled")) << "</td></tr>\r\n";
#ifdef WITH_BOB
bool bob = i2p::client::context.GetBOBCommandChannel () ? true : false;
s << "<tr><td>" << "BOB" << "</td><td class='" << (bob ? "enabled" : "disabled") << "'>" << (bob ? tr("Enabled") : tr("Disabled")) << "</td></tr>\r\n";
#endif
#ifdef WITH_SAM
bool sam = i2p::client::context.GetSAMBridge () ? true : false;
s << "<tr><td>" << "SAM" << "</td><td class='" << (sam ? "enabled" : "disabled") << "'>" << (sam ? tr("Enabled") : tr("Disabled")) << "</td></tr>\r\n";
#endif
#ifdef WITH_I2CP
bool i2cp = i2p::client::context.GetI2CPServer () ? true : false;
s << "<tr><td>" << "I2CP" << "</td><td class='" << (i2cp ? "enabled" : "disabled") << "'>" << (i2cp ? tr("Enabled") : tr("Disabled")) << "</td></tr>\r\n";
#endif
#ifdef WITH_I2PC
bool i2pcontrol; i2p::config::GetOption("i2pcontrol.enabled", i2pcontrol);
s << "<tr><td>" << "I2PControl" << "</td><td class='" << (i2pcontrol ? "enabled" : "disabled") << "'>" << (i2pcontrol ? tr("Enabled") : tr("Disabled")) << "</td></tr>\r\n";
#endif
s << "</tbody></table>\r\n";
}
}
@@ -388,6 +404,7 @@ namespace http {
}
s << "</div>\r\n";
#ifdef WITH_I2CP
auto i2cpServer = i2p::client::context.GetI2CPServer ();
if (i2cpServer && !(i2cpServer->GetSessions ().empty ()))
{
@@ -405,6 +422,7 @@ namespace http {
}
s << "</div>\r\n";
}
#endif
}
static void ShowLeaseSetDestination (std::stringstream& s, std::shared_ptr<const i2p::client::LeaseSetDestination> dest, uint32_t token)
@@ -572,6 +590,7 @@ namespace http {
}
}
#ifdef WITH_I2CP
void ShowI2CPLocalDestination (std::stringstream& s, const std::string& id)
{
auto i2cpServer = i2p::client::context.GetI2CPServer ();
@@ -587,6 +606,7 @@ namespace http {
else
ShowError(s, tr("I2CP is not enabled"));
}
#endif
void ShowLeasesSets(std::stringstream& s)
{
@@ -879,6 +899,7 @@ namespace http {
}
}
#ifdef WITH_SAM
void ShowSAMSessions (std::stringstream& s)
{
std::string webroot; i2p::config::GetOption("http.webroot", webroot);
@@ -941,6 +962,7 @@ namespace http {
}
s << "</div>\r\n";
}
#endif
void ShowI2PTunnels (std::stringstream& s)
{
@@ -1194,12 +1216,16 @@ namespace http {
uint32_t token = CreateToken ();
ShowLocalDestination (s, params["b32"], token);
}
#ifdef WITH_I2CP
else if (page == HTTP_PAGE_I2CP_LOCAL_DESTINATION)
ShowI2CPLocalDestination (s, params["i2cp_id"]);
#endif
#ifdef WITH_SAM
else if (page == HTTP_PAGE_SAM_SESSIONS)
ShowSAMSessions (s);
else if (page == HTTP_PAGE_SAM_SESSION)
ShowSAMSession (s, params["sam_id"]);
#endif
else if (page == HTTP_PAGE_I2P_TUNNELS)
ShowI2PTunnels (s);
else if (page == HTTP_PAGE_LEASESETS)

View File

@@ -95,11 +95,15 @@ namespace http
void ShowTunnels (std::stringstream& s);
void ShowTransitTunnels (std::stringstream& s);
void ShowTransports (std::stringstream& s);
void ShowSAMSessions (std::stringstream& s);
void ShowI2PTunnels (std::stringstream& s);
void ShowLocalDestination (std::stringstream& s, const std::string& b32, uint32_t token);
#ifdef WITH_SAM
void ShowSAMSessions (std::stringstream& s);
void ShowSAMSession (std::stringstream& s, const std::string& id);
#endif
#ifdef WITH_I2CP
void ShowI2CPLocalDestination (std::stringstream& s, const std::string& id);
#endif
} // http
} // i2p

View File

@@ -6,6 +6,8 @@
* See full license text in LICENSE file at top of project tree
*/
#ifdef WITH_I2PC
#include <stdio.h>
#include <sstream>
#include <openssl/x509.h>
@@ -87,8 +89,7 @@ namespace client
m_RouterInfoHandlers["i2p.router.net.bw.outbound.1s"] = &I2PControlService::OutboundBandwidth1S;
m_RouterInfoHandlers["i2p.router.net.status"] = &I2PControlService::NetStatusHandler;
m_RouterInfoHandlers["i2p.router.net.tunnels.participating"] = &I2PControlService::TunnelsParticipatingHandler;
m_RouterInfoHandlers["i2p.router.net.tunnels.successrate"] =
&I2PControlService::TunnelsSuccessRateHandler;
m_RouterInfoHandlers["i2p.router.net.tunnels.successrate"] = &I2PControlService::TunnelsSuccessRateHandler;
m_RouterInfoHandlers["i2p.router.net.total.received.bytes"] = &I2PControlService::NetTotalReceivedBytes;
m_RouterInfoHandlers["i2p.router.net.total.sent.bytes"] = &I2PControlService::NetTotalSentBytes;
@@ -105,9 +106,15 @@ namespace client
m_ClientServicesInfoHandlers["I2PTunnel"] = &I2PControlService::I2PTunnelInfoHandler;
m_ClientServicesInfoHandlers["HTTPProxy"] = &I2PControlService::HTTPProxyInfoHandler;
m_ClientServicesInfoHandlers["SOCKS"] = &I2PControlService::SOCKSInfoHandler;
#ifdef WITH_SAM
m_ClientServicesInfoHandlers["SAM"] = &I2PControlService::SAMInfoHandler;
#endif
#ifdef WITH_BOB
m_ClientServicesInfoHandlers["BOB"] = &I2PControlService::BOBInfoHandler;
#endif
#ifdef WITH_I2CP
m_ClientServicesInfoHandlers["I2CP"] = &I2PControlService::I2CPInfoHandler;
#endif
}
I2PControlService::~I2PControlService ()
@@ -346,8 +353,7 @@ namespace client
}
}
// handlers
// handlers
void I2PControlService::AuthenticateHandler (const boost::property_tree::ptree& params, std::ostringstream& results)
{
int api = params.get<int> ("API");
@@ -372,8 +378,7 @@ namespace client
}
// I2PControl
// I2PControl
void I2PControlService::I2PControlHandler (const boost::property_tree::ptree& params, std::ostringstream& results)
{
for (auto& it: params)
@@ -533,7 +538,7 @@ namespace client
i2p::data::netdb.Reseed ();
}
// network setting
// network setting
void I2PControlService::NetworkSettingHandler (const boost::property_tree::ptree& params, std::ostringstream& results)
{
for (auto it = params.begin (); it != params.end (); it++)
@@ -613,8 +618,7 @@ namespace client
EVP_PKEY_free (pkey);
}
// ClientServicesInfo
// ClientServicesInfo
void I2PControlService::ClientServicesInfoHandler (const boost::property_tree::ptree& params, std::ostringstream& results)
{
for (auto it = params.begin (); it != params.end (); it++)
@@ -720,6 +724,7 @@ namespace client
InsertParam (results, "SOCKS", pt);
}
#ifdef WITH_SAM
void I2PControlService::SAMInfoHandler (std::ostringstream& results)
{
boost::property_tree::ptree pt;
@@ -755,7 +760,9 @@ namespace client
InsertParam (results, "SAM", pt);
}
#endif // WITH_SAM
#ifdef WITH_BOB
void I2PControlService::BOBInfoHandler (std::ostringstream& results)
{
boost::property_tree::ptree pt;
@@ -770,7 +777,9 @@ namespace client
InsertParam (results, "BOB", pt);
}
#endif // WITH_BOB
#ifdef WITH_I2CP
void I2PControlService::I2CPInfoHandler (std::ostringstream& results)
{
boost::property_tree::ptree pt;
@@ -785,5 +794,7 @@ namespace client
InsertParam (results, "I2CP", pt);
}
#endif // WITH_I2CP
}
}
#endif // WITH_I2PC

View File

@@ -6,6 +6,8 @@
* See full license text in LICENSE file at top of project tree
*/
#ifdef WITH_I2PC
#ifndef I2P_CONTROL_H__
#define I2P_CONTROL_H__
@@ -114,9 +116,15 @@ namespace client
void I2PTunnelInfoHandler (std::ostringstream& results);
void HTTPProxyInfoHandler (std::ostringstream& results);
void SOCKSInfoHandler (std::ostringstream& results);
#ifdef WITH_SAM
void SAMInfoHandler (std::ostringstream& results);
#endif
#ifdef WITH_BOB
void BOBInfoHandler (std::ostringstream& results);
#endif
#ifdef WITH_I2CP
void I2CPInfoHandler (std::ostringstream& results);
#endif
private:
@@ -141,3 +149,4 @@ namespace client
}
#endif
#endif // WITH_I2PC

View File

@@ -319,5 +319,4 @@ namespace client
}
}
#endif

View File

@@ -6,6 +6,8 @@
* See full license text in LICENSE file at top of project tree
*/
#ifdef WITH_BOB
#include <string.h>
#include "Log.h"
#include "ClientContext.h"
@@ -884,3 +886,4 @@ namespace client
}
}
}
#endif // WITH_BOB

View File

@@ -6,6 +6,8 @@
* See full license text in LICENSE file at top of project tree
*/
#ifdef WITH_BOB
#ifndef BOB_H__
#define BOB_H__
@@ -277,5 +279,5 @@ namespace client
};
}
}
#endif
#endif // WITH_BOB

View File

@@ -26,8 +26,16 @@ namespace client
ClientContext context;
ClientContext::ClientContext (): m_SharedLocalDestination (nullptr),
m_HttpProxy (nullptr), m_SocksProxy (nullptr), m_SamBridge (nullptr),
m_BOBCommandChannel (nullptr), m_I2CPServer (nullptr)
m_HttpProxy (nullptr), m_SocksProxy (nullptr)
#ifdef WITH_SAM
, m_SamBridge (nullptr)
#endif
#ifdef WITH_BOB
, m_BOBCommandChannel (nullptr)
#endif
#ifdef WITH_I2CP
, m_I2CPServer (nullptr)
#endif
{
}
@@ -35,9 +43,15 @@ namespace client
{
delete m_HttpProxy;
delete m_SocksProxy;
#ifdef WITH_SAM
delete m_SamBridge;
#endif
#ifdef WITH_BOB
delete m_BOBCommandChannel;
#endif
#ifdef WITH_I2CP
delete m_I2CPServer;
#endif
}
void ClientContext::Start ()
@@ -58,6 +72,7 @@ namespace client
// I2P tunnels
ReadTunnels ();
#ifdef WITH_SAM
// SAM
bool sam; i2p::config::GetOption("sam.enabled", sam);
if (sam)
@@ -77,7 +92,9 @@ namespace client
ThrowFatal ("Unable to start SAM bridge at ", samAddr, ":", samPort, ": ", e.what ());
}
}
#endif
#ifdef WITH_BOB
// BOB
bool bob; i2p::config::GetOption("bob.enabled", bob);
if (bob) {
@@ -95,7 +112,9 @@ namespace client
ThrowFatal ("Unable to start BOB bridge at ", bobAddr, ":", bobPort, ": ", e.what ());
}
}
#endif
#ifdef WITH_I2CP
// I2CP
bool i2cp; i2p::config::GetOption("i2cp.enabled", i2cp);
if (i2cp)
@@ -115,6 +134,7 @@ namespace client
ThrowFatal ("Unable to start I2CP at ", i2cpAddr, ":", i2cpPort, ": ", e.what ());
}
}
#endif
m_AddressBook.StartResolvers ();
@@ -158,6 +178,7 @@ namespace client
}
m_ServerTunnels.clear ();
#ifdef WITH_SAM
if (m_SamBridge)
{
LogPrint(eLogInfo, "Clients: Stopping SAM bridge");
@@ -165,7 +186,9 @@ namespace client
delete m_SamBridge;
m_SamBridge = nullptr;
}
#endif
#ifdef WITH_BOB
if (m_BOBCommandChannel)
{
LogPrint(eLogInfo, "Clients: Stopping BOB command channel");
@@ -173,7 +196,9 @@ namespace client
delete m_BOBCommandChannel;
m_BOBCommandChannel = nullptr;
}
#endif
#ifdef WITH_I2CP
if (m_I2CPServer)
{
LogPrint(eLogInfo, "Clients: Stopping I2CP");
@@ -181,6 +206,7 @@ namespace client
delete m_I2CPServer;
m_I2CPServer = nullptr;
}
#endif
LogPrint(eLogInfo, "Clients: Stopping AddressBook");
m_AddressBook.Stop ();
@@ -634,8 +660,7 @@ namespace client
{
// socks proxy
std::string outproxy = section.second.get("outproxy", "");
uint32_t outproxyport = section.second.get("outproxyport", 0);
auto tun = std::make_shared<i2p::proxy::SOCKSProxy>(name, address, port, !outproxy.empty(), outproxy, outproxyport, localDestination);
auto tun = std::make_shared<i2p::proxy::SOCKSProxy>(name, address, port, !outproxy.empty(), outproxy, destinationPort, localDestination);
clientTunnel = tun;
clientEndpoint = tun->GetLocalEndpoint ();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2021, The PurpleI2P Project
* Copyright (c) 2013-2022, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
@@ -18,9 +18,19 @@
#include "HTTPProxy.h"
#include "SOCKS.h"
#include "I2PTunnel.h"
#ifdef WITH_SAM
#include "SAM.h"
#endif
#ifdef WITH_BOB
#include "BOB.h"
#endif
#ifdef WITH_I2CP
#include "I2CP.h"
#endif
#include "AddressBook.h"
#include "I18N_langs.h"
@@ -76,31 +86,45 @@ namespace client
void ReloadConfig ();
std::shared_ptr<ClientDestination> GetSharedLocalDestination () const { return m_SharedLocalDestination; };
std::shared_ptr<ClientDestination> CreateNewLocalDestination (bool isPublic = false, // transient
std::shared_ptr<ClientDestination> CreateNewLocalDestination (
bool isPublic = false, // transient
i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519,
i2p::data::CryptoKeyType cryptoType = i2p::data::CRYPTO_KEY_TYPE_ELGAMAL,
const std::map<std::string, std::string> * params = nullptr); // used by SAM only
std::shared_ptr<ClientDestination> CreateNewLocalDestination (boost::asio::io_service& service,
bool isPublic = false, i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519,
i2p::data::CryptoKeyType cryptoType = i2p::data::CRYPTO_KEY_TYPE_ELGAMAL,
const std::map<std::string, std::string> * params = nullptr); // same as previous but on external io_service
std::shared_ptr<ClientDestination> CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic = true,
const std::map<std::string, std::string> * params = nullptr);
std::shared_ptr<ClientDestination> CreateNewLocalDestination (boost::asio::io_service& service,
const i2p::data::PrivateKeys& keys, bool isPublic = true,
const std::map<std::string, std::string> * params = nullptr); // same as previous but on external io_service
std::shared_ptr<ClientDestination> CreateNewMatchedTunnelDestination(const i2p::data::PrivateKeys &keys,
const std::string & name, const std::map<std::string, std::string> * params = nullptr);
void DeleteLocalDestination (std::shared_ptr<ClientDestination> destination);
std::shared_ptr<ClientDestination> FindLocalDestination (const i2p::data::IdentHash& destination) const;
bool LoadPrivateKeys (i2p::data::PrivateKeys& keys, const std::string& filename,
i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519,
i2p::data::CryptoKeyType cryptoType = i2p::data::CRYPTO_KEY_TYPE_ELGAMAL);
AddressBook& GetAddressBook () { return m_AddressBook; };
#ifdef WITH_BOB
const BOBCommandChannel * GetBOBCommandChannel () const { return m_BOBCommandChannel; };
#endif
#ifdef WITH_SAM
const SAMBridge * GetSAMBridge () const { return m_SamBridge; };
#endif
#ifdef WITH_I2CP
const I2CPServer * GetI2CPServer () const { return m_I2CPServer; };
#endif
std::vector<std::shared_ptr<DatagramSessionInfo> > GetForwardInfosFor(const i2p::data::IdentHash & destination);
@@ -149,9 +173,15 @@ namespace client
std::map<boost::asio::ip::udp::endpoint, std::shared_ptr<I2PUDPClientTunnel> > m_ClientForwards; // local endpoint -> udp tunnel
std::map<std::pair<i2p::data::IdentHash, int>, std::shared_ptr<I2PUDPServerTunnel> > m_ServerForwards; // <destination,port> -> udp tunnel
#ifdef WITH_SAM
SAMBridge * m_SamBridge;
#endif
#ifdef WITH_BOB
BOBCommandChannel * m_BOBCommandChannel;
#endif
#ifdef WITH_I2CP
I2CPServer * m_I2CPServer;
#endif
std::unique_ptr<boost::asio::deadline_timer> m_CleanupUDPTimer;

View File

@@ -483,10 +483,8 @@ namespace proxy {
void HTTPReqHandler::HandleUpstreamSocksProxyConnect(const boost::system::error_code & ec)
{
if(!ec)
{
if(m_RequestURL.host.size() > 255)
{
if(!ec) {
if(m_RequestURL.host.size() > 255) {
GenericProxyError(tr("hostname too long"), m_RequestURL.host);
return;
}
@@ -514,9 +512,7 @@ namespace proxy {
reqsize += host.size();
m_socks_buf[++reqsize] = 0;
boost::asio::async_write(*m_proxysock, boost::asio::buffer(m_socks_buf, reqsize), boost::asio::transfer_all(), std::bind(&HTTPReqHandler::HandleSocksProxySendHandshake, this, std::placeholders::_1, std::placeholders::_2));
}
else
GenericProxyError(tr("cannot connect to upstream socks proxy"), ec.message());
} else GenericProxyError(tr("cannot connect to upstream socks proxy"), ec.message());
}
void HTTPReqHandler::HandleSocksProxySendHandshake(const boost::system::error_code & ec, std::size_t bytes_transferred)

View File

@@ -6,6 +6,8 @@
* See full license text in LICENSE file at top of project tree
*/
#ifdef WITH_I2CP
#include <string.h>
#include <stdlib.h>
#include <openssl/rand.h>
@@ -1035,3 +1037,4 @@ namespace client
}
}
}
#endif // WITH_I2CP

View File

@@ -6,6 +6,8 @@
* See full license text in LICENSE file at top of project tree
*/
#ifdef WITH_I2CP
#ifndef I2CP_H__
#define I2CP_H__
@@ -249,3 +251,4 @@ namespace client
}
#endif
#endif // WITH_I2CP

View File

@@ -243,7 +243,7 @@ namespace client
LogPrint(eLogDebug, "TCPIPPipe: Downstream: ", (int) bytes_transfered, " bytes received");
if (ecode)
{
LogPrint(eLogError, "TCPIPPipe: Downstream read error: " , ecode.message());
LogPrint(eLogError, "TCPIPPipe: Downstream read error:" , ecode.message());
if (ecode != boost::asio::error::operation_aborted)
Terminate();
} else {
@@ -256,7 +256,7 @@ namespace client
void TCPIPPipe::HandleDownstreamWrite(const boost::system::error_code & ecode) {
if (ecode)
{
LogPrint(eLogError, "TCPIPPipe: Downstream write error: " , ecode.message());
LogPrint(eLogError, "TCPIPPipe: Downstream write error:" , ecode.message());
if (ecode != boost::asio::error::operation_aborted)
Terminate();
}
@@ -267,7 +267,7 @@ namespace client
void TCPIPPipe::HandleUpstreamWrite(const boost::system::error_code & ecode) {
if (ecode)
{
LogPrint(eLogError, "TCPIPPipe: Upstream write error: " , ecode.message());
LogPrint(eLogError, "TCPIPPipe: Upstream write error:" , ecode.message());
if (ecode != boost::asio::error::operation_aborted)
Terminate();
}
@@ -280,7 +280,7 @@ namespace client
LogPrint(eLogDebug, "TCPIPPipe: Upstream ", (int)bytes_transfered, " bytes received");
if (ecode)
{
LogPrint(eLogError, "TCPIPPipe: Upstream read error: " , ecode.message());
LogPrint(eLogError, "TCPIPPipe: Upstream read error:" , ecode.message());
if (ecode != boost::asio::error::operation_aborted)
Terminate();
} else {

View File

@@ -1,11 +1,13 @@
/*
* Copyright (c) 2013-2021, The PurpleI2P Project
* Copyright (c) 2013-2022, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*/
#ifdef WITH_SAM
#include <string.h>
#include <stdio.h>
#ifdef _MSC_VER
@@ -154,11 +156,7 @@ namespace client
if (SAMVersionAcceptable(version))
{
#ifdef _MSC_VER
size_t l = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_HANDSHAKE_REPLY, version.c_str ());
#else
size_t l = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_HANDSHAKE_REPLY, version.c_str ());
#endif
boost::asio::async_write (m_Socket, boost::asio::buffer (m_Buffer, l), boost::asio::transfer_all (),
std::bind(&SAMSocket::HandleHandshakeReplySent, shared_from_this (),
std::placeholders::_1, std::placeholders::_2));
@@ -465,11 +463,7 @@ namespace client
size_t l = session->GetLocalDestination ()->GetPrivateKeys ().ToBuffer (buf, 1024);
size_t l1 = i2p::data::ByteStreamToBase64 (buf, l, priv, 1024);
priv[l1] = 0;
#ifdef _MSC_VER
size_t l2 = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_SESSION_CREATE_REPLY_OK, priv);
#else
size_t l2 = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_SESSION_CREATE_REPLY_OK, priv);
#endif
SendMessageReply (m_Buffer, l2, false);
}
}
@@ -710,13 +704,8 @@ namespace client
}
}
auto keys = i2p::data::PrivateKeys::CreateRandomKeys (signatureType, cryptoType);
#ifdef _MSC_VER
size_t l = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_DEST_REPLY,
keys.GetPublic ()->ToBase64 ().c_str (), keys.ToBase64 ().c_str ());
#else
size_t l = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_DEST_REPLY,
keys.GetPublic ()->ToBase64 ().c_str (), keys.ToBase64 ().c_str ());
#endif
SendMessageReply (m_Buffer, l, false);
}
@@ -754,11 +743,7 @@ namespace client
else
{
LogPrint (eLogError, "SAM: Naming failed, unknown address ", name);
#ifdef _MSC_VER
size_t len = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY_INVALID_KEY, name.c_str());
#else
size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY_INVALID_KEY, name.c_str());
#endif
SendMessageReply (m_Buffer, len, false);
}
}
@@ -833,11 +818,7 @@ namespace client
void SAMSocket::SendI2PError(const std::string & msg)
{
LogPrint (eLogError, "SAM: I2P error: ", msg);
#ifdef _MSC_VER
size_t len = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_SESSION_STATUS_I2P_ERROR, msg.c_str());
#else
size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_SESSION_STATUS_I2P_ERROR, msg.c_str());
#endif
SendMessageReply (m_Buffer, len, true);
}
@@ -851,11 +832,7 @@ namespace client
else
{
LogPrint (eLogError, "SAM: Naming lookup failed. LeaseSet for ", name, " not found");
#ifdef _MSC_VER
size_t len = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY_INVALID_KEY, name.c_str());
#else
size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY_INVALID_KEY, name.c_str());
#endif
SendMessageReply (m_Buffer, len, false);
}
}
@@ -863,11 +840,7 @@ namespace client
void SAMSocket::SendNamingLookupReply (const std::string& name, std::shared_ptr<const i2p::data::IdentityEx> identity)
{
auto base64 = identity->ToBase64 ();
#ifdef _MSC_VER
size_t l = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY, name.c_str (), base64.c_str ());
#else
size_t l = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY, name.c_str (), base64.c_str ());
#endif
SendMessageReply (m_Buffer, l, false);
}
@@ -1121,11 +1094,7 @@ namespace client
}
else
{
#ifdef _MSC_VER
size_t l = sprintf_s ((char *)m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE, SAM_DATAGRAM_RECEIVED, base64.c_str (), (long unsigned int)len);
#else
size_t l = snprintf ((char *)m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE, SAM_DATAGRAM_RECEIVED, base64.c_str (), (long unsigned int)len);
#endif
if (len < SAM_SOCKET_BUFFER_SIZE - l)
{
memcpy (m_StreamBuffer + l, buf, len);
@@ -1149,11 +1118,7 @@ namespace client
m_Owner.SendTo({ {buf, len} }, *ep);
else
{
#ifdef _MSC_VER
size_t l = sprintf_s ((char *)m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE, SAM_RAW_RECEIVED, (long unsigned int)len);
#else
size_t l = snprintf ((char *)m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE, SAM_RAW_RECEIVED, (long unsigned int)len);
#endif
if (len < SAM_SOCKET_BUFFER_SIZE - l)
{
memcpy (m_StreamBuffer + l, buf, len);
@@ -1528,3 +1493,4 @@ namespace client
}
}
}
#endif // WITH_SAM

View File

@@ -6,6 +6,8 @@
* See full license text in LICENSE file at top of project tree
*/
#ifdef WITH_SAM
#ifndef SAM_H__
#define SAM_H__
@@ -286,3 +288,4 @@ namespace client
}
#endif
#endif // WITH_SAM

View File

@@ -30,7 +30,6 @@ namespace proxy
static const size_t SOCKS_FORWARDER_BUFFER_SIZE = 8192;
static const size_t SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE = 8;
static const size_t SOCKS_UPSTREAM_SOCKS5_INIT_REPLY_SIZE = 2;
struct SOCKSDnsAddress
{
@@ -74,16 +73,16 @@ namespace proxy
};
enum authMethods
{
AUTH_NONE = 0, // No authentication, skip to next step
AUTH_GSSAPI = 1, // GSSAPI authentication
AUTH_USERPASSWD = 2, // Username and password
AUTH_UNACCEPTABLE = 0xff // No acceptable method found
AUTH_NONE = 0, //No authentication, skip to next step
AUTH_GSSAPI = 1, //GSSAPI authentication
AUTH_USERPASSWD = 2, //Username and password
AUTH_UNACCEPTABLE = 0xff //No acceptable method found
};
enum addrTypes
{
ADDR_IPV4 = 1, // IPv4 address (4 octets)
ADDR_IPV4 = 1, //IPv4 address (4 octets)
ADDR_DNS = 3, // DNS name (up to 255 octets)
ADDR_IPV6 = 4 // IPV6 address (16 octets)
ADDR_IPV6 = 4 //IPV6 address (16 octets)
};
enum errTypes
{
@@ -128,7 +127,7 @@ namespace proxy
boost::asio::const_buffers_1 GenerateSOCKS5SelectAuth(authMethods method);
boost::asio::const_buffers_1 GenerateSOCKS4Response(errTypes error, uint32_t ip, uint16_t port);
boost::asio::const_buffers_1 GenerateSOCKS5Response(errTypes error, addrTypes type, const address &addr, uint16_t port);
boost::asio::const_buffers_1 GenerateUpstreamRequest(int version, bool initial);
boost::asio::const_buffers_1 GenerateUpstreamRequest();
bool Socks5ChooseAuth();
void SocksRequestFailed(errTypes error);
void SocksRequestSuccess();
@@ -140,7 +139,7 @@ namespace proxy
void SocksUpstreamSuccess();
void AsyncUpstreamSockRead();
void SendUpstreamRequest(int version, bool initial);
void SendUpstreamRequest();
void HandleUpstreamData(uint8_t * buff, std::size_t len);
void HandleUpstreamSockSend(const boost::system::error_code & ecode, std::size_t bytes_transfered);
void HandleUpstreamSockRecv(const boost::system::error_code & ecode, std::size_t bytes_transfered);
@@ -155,9 +154,9 @@ namespace proxy
std::shared_ptr<i2p::stream::Stream> m_stream;
uint8_t *m_remaining_data; //Data left to be sent
uint8_t *m_remaining_upstream_data; //upstream data left to be forwarded
uint8_t m_response[7 + max_socks_hostname_size];
uint8_t m_response[7+max_socks_hostname_size];
uint8_t m_upstream_response[SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE];
uint8_t m_upstream_request[14 + max_socks_hostname_size];
uint8_t m_upstream_request[14+max_socks_hostname_size];
std::size_t m_upstream_response_len;
address m_address; //Address
std::size_t m_remaining_data_len; //Size of the data left to be sent
@@ -257,68 +256,30 @@ namespace proxy
break;
case ADDR_DNS:
std::string address(addr.dns.value, addr.dns.size);
//if(address.substr(addr.dns.size - 4, 4) == ".i2p") // overwrite if requested address inside I2P
if(address.substr(addr.dns.size - 4, 4) == ".i2p") // overwrite if requested address inside I2P
{
m_response[3] = ADDR_IPV4;
size += 4;
memset(m_response + 4, 0, 6); // six HEX zeros
}
/*else
else
{
size += (1 + addr.dns.size); // name length + resolved address
size += (1 + addr.dns.size); /* name length + resolved address */
m_response[4] = addr.dns.size;
memcpy(m_response + 5, addr.dns.value, addr.dns.size); // 4 - header + 1 - record size
memcpy(m_response + 5, addr.dns.value, addr.dns.size);
htobe16buf(m_response + size - 2, port);
}*/
}
break;
}
return boost::asio::const_buffers_1(m_response, size);
}
boost::asio::const_buffers_1 SOCKSHandler::GenerateUpstreamRequest(int version, bool initial)
boost::asio::const_buffers_1 SOCKSHandler::GenerateUpstreamRequest()
{
size_t upstreamRequestSize = 0;
// TODO: negotiate with upstream
if (version == 5) // SOCKS5
{
if (initial)
{
m_upstream_request[0] = '\x05'; // VER
m_upstream_request[1] = 1; // NAUTH
m_upstream_request[2] = 0; // AUTH
upstreamRequestSize += 3;
}
else
{
upstreamRequestSize = 6; // header + port
m_upstream_request[0] = '\x05'; // version
m_upstream_request[1] = '\x01'; // request tcp socket opening
m_upstream_request[2] = '\x00'; // reserved
m_upstream_request[3] = m_addrtype; // address type
switch (m_addrtype)
{
case ADDR_IPV4:
upstreamRequestSize += 4;
htobe32buf(m_upstream_request + 4, m_address.ip);
htobe16buf(m_upstream_request + upstreamRequestSize - 2, m_port);
break;
case ADDR_IPV6:
upstreamRequestSize += 16;
memcpy(m_upstream_request + 4, m_address.ipv6, 16);
htobe16buf(m_upstream_request + upstreamRequestSize - 2, m_port);
break;
case ADDR_DNS:
upstreamRequestSize += (1 + m_address.dns.size); // name length + resolved address
m_upstream_request[4] = m_address.dns.size;
memcpy(m_upstream_request + 5, m_address.dns.value, m_address.dns.size); // 4 - header + 1 - record size
htobe16buf(m_upstream_request + upstreamRequestSize - 2, m_port);
break;
}
}
}
else if (version == 4) // SOCKS4a
{
m_upstream_request[0] = '\x04'; // VER
// SOCKS 4a
m_upstream_request[0] = '\x04'; //version
m_upstream_request[1] = m_cmd;
htobe16buf(m_upstream_request + 2, m_port);
m_upstream_request[4] = 0;
@@ -339,8 +300,7 @@ namespace proxy
// null terminate
m_upstream_request[++upstreamRequestSize] = 0;
} else {
LogPrint(eLogError, "SOCKS: BUG!!! m_addr.dns.size > max_socks_hostname - ( upstreamRequestSize + 1 ) )");
}
LogPrint(eLogError, "SOCKS: BUG!!! m_addr.dns.sizs > max_socks_hostname - ( upstreamRequestSize + 1 ) )");
}
return boost::asio::const_buffers_1(m_upstream_request, upstreamRequestSize);
}
@@ -457,7 +417,6 @@ namespace proxy
{
case GET_SOCKSV:
m_socksv = (SOCKSHandler::socksVersions) *sock_buff;
LogPrint(eLogInfo, "SOCKS: received version: ", ((int)*sock_buff));
switch (*sock_buff)
{
case SOCKS4:
@@ -745,7 +704,7 @@ namespace proxy
break;
case SOCKS5:
LogPrint(eLogInfo, "SOCKS: v5 connection success");
// HACK only 16 bits passed in port as SOCKS5 doesn't allow for more
//HACK only 16 bits passed in port as SOCKS5 doesn't allow for more
response = GenerateSOCKS5Response(SOCKS5_OK, ADDR_DNS, m_address, m_port);
break;
}
@@ -756,51 +715,22 @@ namespace proxy
GetOwner()->AddHandler(forwarder);
forwarder->Start();
Terminate();
}
void SOCKSHandler::HandleUpstreamData(uint8_t * dataptr, std::size_t len)
{
if (m_state == UPSTREAM_HANDSHAKE)
{
if (m_state == UPSTREAM_HANDSHAKE) {
m_upstream_response_len += len;
// handle handshake data
if (m_upstream_response_len == SOCKS_UPSTREAM_SOCKS5_INIT_REPLY_SIZE)
{
if (m_upstream_response[0] == '\x05' && m_upstream_response[1] != AUTH_UNACCEPTABLE)
{
LogPrint(eLogInfo, "SOCKS: upstream SOCKS5 proxy: success greeting, sending connection request");
SendUpstreamRequest(m_socksv, false);
}
else
{
LogPrint(eLogError, "SOCKS: upstream SOCKS5 proxy failure: no acceptable methods were offered");
SocksRequestFailed(SOCKS5_GEN_FAIL);
}
}
else if (m_upstream_response[0] == '\x05')
{
if (m_upstream_response[1] == SOCKS5_OK)
{
LogPrint(eLogInfo, "SOCKS: upstream SOCKS5 proxy: successully established");
SocksUpstreamSuccess();
}
else
{
LogPrint(eLogError, "SOCKS: upstream proxy failure: ", (int) m_upstream_response[1]);
SocksRequestFailed(SOCKS5_GEN_FAIL);
}
}
else if (m_upstream_response_len < SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE)
{
if (m_upstream_response_len < SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE) {
// too small, continue reading
AsyncUpstreamSockRead();
}
else if (len == SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE)
{
} else if (len == SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE) {
// just right
uint8_t resp = m_upstream_response[1];
if (resp == SOCKS4_OK) {
// we have connected!
// we have connected !
SocksUpstreamSuccess();
} else {
// upstream failure
@@ -808,26 +738,22 @@ namespace proxy
// TODO: runtime error?
SocksRequestFailed(SOCKS5_GEN_FAIL);
}
}
else
{
} else {
// too big
SocksRequestFailed(SOCKS5_GEN_FAIL);
}
}
else
{
} else {
// invalid state
LogPrint(eLogError, "SOCKS: Invalid state reading from upstream: ", (int) m_state);
}
}
void SOCKSHandler::SendUpstreamRequest(int version, bool initial)
void SOCKSHandler::SendUpstreamRequest()
{
LogPrint(eLogInfo, "SOCKS: Negotiating with upstream proxy");
EnterState(UPSTREAM_HANDSHAKE);
if (m_upstreamSock) {
boost::asio::write(*m_upstreamSock, GenerateUpstreamRequest(version, initial));
boost::asio::write(*m_upstreamSock, GenerateUpstreamRequest());
AsyncUpstreamSockRead();
} else {
LogPrint(eLogError, "SOCKS: No upstream socket to send handshake to");
@@ -842,7 +768,7 @@ namespace proxy
return;
}
LogPrint(eLogInfo, "SOCKS: Connected to upstream proxy");
SendUpstreamRequest(m_socksv, true); // try SOCKS5 first
SendUpstreamRequest();
}
void SOCKSHandler::HandleUpstreamResolved(const boost::system::error_code & ecode, boost::asio::ip::tcp::resolver::iterator itr)