Merge pull request #2132 from sehraf/pr_add_i2p_sam3

Replace I2P BOB with SAMv3
This commit is contained in:
csoler 2021-10-24 20:39:22 +02:00 committed by GitHub
commit 3ddf3d0853
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
31 changed files with 1797 additions and 1863 deletions

View file

@ -156,7 +156,7 @@ rs_webui {
HEADERS += plugins/pluginmanager.h \
plugins/dlfcn_win32.h \
rsitems/rspluginitems.h \
rsitems/rspluginitems.h \
util/i2pcommon.h \
util/rsinitedptr.h
@ -393,7 +393,6 @@ HEADERS += pqi/authssl.h \
pqi/pqissl.h \
pqi/pqissllistener.h \
pqi/pqisslpersongrp.h \
pqi/pqissli2pbob.h \
pqi/pqisslproxy.h \
pqi/pqistore.h \
pqi/pqistreamer.h \
@ -454,7 +453,7 @@ HEADERS += rsitems/rsitem.h \
rsitems/rsgxsupdateitems.h \
rsitems/rsserviceinfoitems.h \
HEADERS += services/autoproxy/p3i2pbob.h \
HEADERS += \
services/rseventsservice.h \
services/autoproxy/rsautoproxymonitor.h \
services/p3msgservice.h \
@ -558,7 +557,6 @@ SOURCES += pqi/authgpg.cc \
pqi/pqissl.cc \
pqi/pqissllistener.cc \
pqi/pqisslpersongrp.cc \
pqi/pqissli2pbob.cpp \
pqi/pqisslproxy.cc \
pqi/pqistore.cc \
pqi/pqistreamer.cc \
@ -616,7 +614,6 @@ SOURCES += serialiser/rsbaseserial.cc \
SOURCES += services/autoproxy/rsautoproxymonitor.cc \
services/rseventsservice.cc \
services/autoproxy/p3i2pbob.cc \
services/p3msgservice.cc \
services/p3service.cc \
services/p3statusservice.cc \
@ -1012,6 +1009,34 @@ rs_broadcast_discovery {
}
}
rs_sam3 {
SOURCES += \
services/autoproxy/p3i2psam3.cpp \
pqi/pqissli2psam3.cpp \
HEADERS += \
services/autoproxy/p3i2psam3.h \
pqi/pqissli2psam3.h \
}
rs_sam3_libsam3 {
DUMMYQMAKECOMPILERINPUT = FORCE
libsam3.name = Generating libsam3.
libsam3.input = DUMMYQMAKECOMPILERINPUT
libsam3.output = $$clean_path($${LIBSAM3_BUILD_PATH}/libsam3.a)
libsam3.CONFIG += target_predeps combine
libsam3.variable_out = PRE_TARGETDEPS
libsam3.commands = \
cd $${RS_SRC_PATH} && ( \
git submodule update --init supportlibs/libsam3 || \
true ) && \
mkdir -p $${UDP_DISCOVERY_BUILD_PATH} && \
cp -r $${LIBSAM3_SRC_PATH}/* $${LIBSAM3_BUILD_PATH} && \
cd $${LIBSAM3_BUILD_PATH} && \
$(MAKE) build
QMAKE_EXTRA_COMPILERS += libsam3
}
###########################################################################################################
# OLD CONFIG OPTIONS.
# Not used much - but might be useful one day.

View file

@ -1742,6 +1742,13 @@ bool pqissl::moretoread(uint32_t usec)
{
rslog(RSL_ALERT, pqisslzone,
"pqissl::moretoread() Select ERROR!");
RS_WARN(strerror(errno));
if (errno == EBADF) {
// happens when SAM is shut down
rslog(RSL_ALERT, pqisslzone, "pqissl::moretoread() -> calling reset()");
reset_locked();
}
return 0;
}

View file

@ -1,52 +0,0 @@
/*******************************************************************************
* libretroshare/src/pqi: pqissli2pbob.cc *
* *
* libretroshare: retroshare core library *
* *
* Copyright 2016 by Sehraf *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
#include "pqissli2pbob.h"
bool pqissli2pbob::connect_parameter(uint32_t type, const std::string &value)
{
if (type == NET_PARAM_CONNECT_DOMAIN_ADDRESS)
{
RS_STACK_MUTEX(mSslMtx);
// a new line must be appended!
mI2pAddr = value + '\n';
return true;
}
return pqissl::connect_parameter(type, value);
}
int pqissli2pbob::Basic_Connection_Complete()
{
int ret;
if ((ret = pqissl::Basic_Connection_Complete()) != 1)
{
// basic connection not complete.
return ret;
}
// send addr. (new line is already appended)
ret = send(sockfd, mI2pAddr.c_str(), mI2pAddr.length(), 0);
if (ret != (int)mI2pAddr.length())
return -1;
return 1;
}

View file

@ -1,52 +0,0 @@
/*******************************************************************************
* libretroshare/src/pqi: pqissli2pbob.h *
* *
* libretroshare: retroshare core library *
* *
* Copyright 2016 by Sehraf *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
#ifndef PQISSLI2PBOB_H
#define PQISSLI2PBOB_H
#include "pqi/pqissl.h"
/*
* This class is a minimal varied version of pqissl to work with I2P BOB tunnels.
* The only difference is that the [.b32].i2p addresses must be sent first.
*
* Everything else is untouched.
*/
class pqissli2pbob : public pqissl
{
public:
pqissli2pbob(pqissllistener *l, PQInterface *parent, p3LinkMgr *lm)
: pqissl(l, parent, lm) {}
// NetInterface interface
public:
bool connect_parameter(uint32_t type, const std::string &value);
// pqissl interface
protected:
int Basic_Connection_Complete();
private:
std::string mI2pAddr;
};
#endif // PQISSLI2PBOB_H

View file

@ -0,0 +1,261 @@
#include "pqissli2psam3.h"
#include <libsam3.h>
RS_SET_CONTEXT_DEBUG_LEVEL(2)
static constexpr int pqiDone = 1;
static constexpr int pqiWait = 0;
static constexpr int pqiError = -1;
pqissli2psam3::pqissli2psam3(pqissllistener *l, PQInterface *parent, p3LinkMgr *lm)
: pqissl(l, parent, lm), mState(pqisslSam3State::NONE), mI2pAddrB32(), mI2pAddrLong()
{
RS_DBG4();
mConn = nullptr;
}
bool pqissli2psam3::connect_parameter(uint32_t type, const std::string &value)
{
RS_DBG4();
if (type == NET_PARAM_CONNECT_DOMAIN_ADDRESS)
{
RS_DBG1("got addr:", value);
RS_STACK_MUTEX(mSslMtx);
mI2pAddrB32 = value;
return true;
}
return pqissl::connect_parameter(type, value);
}
int pqissli2psam3::Initiate_Connection()
{
RS_DBG4();
if(waiting != WAITING_DELAY)
{
RS_ERR("Already Attempt in Progress!");
return pqiError;
}
switch (mState) {
case(pqisslSam3State::NONE):
RS_DBG2("NONE");
{
if(mConn) {
// how did we end up here?
RS_ERR("state is NONE but a connection is existing?!");
}
mConn = 0;
// get SAM session
mConn = 0;
samSettings ss;
ss.session = nullptr;
rsAutoProxyMonitor::taskSync(autoProxyType::I2PSAM3, autoProxyTask::getSettings, static_cast<void*>(&ss));
if (!!ss.session) {
RS_DBG3("NONE->DO_LOOKUP");
mState = pqisslSam3State::DO_LOOKUP;
} else {
RS_DBG3("NONE->DO_LOOKUP NOPE", ss.session);
}
}
break;
case(pqisslSam3State::DO_LOOKUP):
RS_DBG1("DO_LOOKUP");
if (!mI2pAddrLong.empty()) {
// skip lookup, it is highly unlikely/impossible for a public key to change (isn't it?)
mState = pqisslSam3State::WAIT_LOOKUP;
break;
}
{
i2p::address *addr = new i2p::address;
addr->clear();
addr->base32 = mI2pAddrB32;
rsAutoProxyMonitor::taskAsync(autoProxyType::I2PSAM3, autoProxyTask::lookupKey, this, static_cast<void*>(addr));
}
mState = pqisslSam3State::WAIT_LOOKUP;
break;
case(pqisslSam3State::DO_CONNECT):
RS_DBG2("DO_CONNECT");
{
auto wrapper = new samEstablishConnectionWrapper();
wrapper->address.clear();
wrapper->address.publicKey = mI2pAddrLong;
wrapper->connection = nullptr;
rsAutoProxyMonitor::taskAsync(autoProxyType::I2PSAM3, autoProxyTask::establishConnection, this, static_cast<void*>(wrapper));
}
mState = pqisslSam3State::WAIT_CONNECT;
break;
case(pqisslSam3State::DONE):
RS_DBG2("DONE");
if (setupSocket())
return pqiDone;
return pqiError;
/* waiting */
case(pqisslSam3State::WAIT_LOOKUP):
RS_DBG3("WAIT_LOOKUP");
break;
case(pqisslSam3State::WAIT_CONNECT):
RS_DBG3("WAIT_CONNECT");
break;
}
return pqiWait;
}
int pqissli2psam3::net_internal_close(int fd)
{
RS_DBG4();
// sanity check
if (mConn && fd != mConn->fd) {
// this should never happen!
RS_ERR("fd != mConn");
// sam3CloseConnection(mConn);
}
// now to the actuall closing
int ret = pqissl::net_internal_close(fd);
rsAutoProxyMonitor::taskAsync(autoProxyType::I2PSAM3, autoProxyTask::closeConnection, this, mConn);
// finally cleanup
mConn = 0;
mState = pqisslSam3State::NONE;
return ret;
}
void pqissli2psam3::taskFinished(taskTicket *&ticket)
{
RS_DBG4();
switch (ticket->task) {
case autoProxyTask::lookupKey:
{
auto addr = static_cast<i2p::address*>(ticket->data);
RS_STACK_MUTEX(mSslMtx);
if (ticket->result == autoProxyStatus::ok) {
mI2pAddrLong = addr->publicKey;
mState = pqisslSam3State::DO_CONNECT;
} else {
waiting = WAITING_FAIL_INTERFACE;
}
delete addr;
ticket->data = nullptr;
addr = nullptr;
}
break;
case autoProxyTask::establishConnection:
{
auto wrapper = static_cast<struct samEstablishConnectionWrapper*>(ticket->data);
RS_STACK_MUTEX(mSslMtx);
if (ticket->result == autoProxyStatus::ok) {
mConn = wrapper->connection;
mState = pqisslSam3State::DONE;
} else {
waiting = WAITING_FAIL_INTERFACE;
}
delete wrapper;
ticket->data = nullptr;
wrapper = nullptr;
}
break;
case autoProxyTask::closeConnection:
// nothing to do here
break;
default:
RS_WARN("unkown task", ticket->task);
}
// clean up!
delete ticket;
ticket = nullptr;
}
bool pqissli2psam3::setupSocket()
{
/*
* This function contains the generis part from pqissl::Initiate_Connection()
*/
int err;
int osock = mConn->fd;
err = unix_fcntl_nonblock(osock);
if (err < 0)
{
RS_ERR("Cannot make socket NON-Blocking:", err);
waiting = WAITING_FAIL_INTERFACE;
net_internal_close(osock);
return false;
}
#ifdef WINDOWS_SYS
/* Set TCP buffer size for Windows systems */
int sockbufsize = 0;
int size = sizeof(int);
err = getsockopt(osock, SOL_SOCKET, SO_RCVBUF, (char *)&sockbufsize, &size);
#ifdef PQISSL_DEBUG
if (err == 0) {
std::cerr << "pqissl::Initiate_Connection: Current TCP receive buffer size " << sockbufsize << std::endl;
} else {
std::cerr << "pqissl::Initiate_Connection: Error getting TCP receive buffer size. Error " << err << std::endl;
}
#endif
sockbufsize = 0;
err = getsockopt(osock, SOL_SOCKET, SO_SNDBUF, (char *)&sockbufsize, &size);
#ifdef PQISSL_DEBUG
if (err == 0) {
std::cerr << "pqissl::Initiate_Connection: Current TCP send buffer size " << sockbufsize << std::endl;
} else {
std::cerr << "pqissl::Initiate_Connection: Error getting TCP send buffer size. Error " << err << std::endl;
}
#endif
sockbufsize = WINDOWS_TCP_BUFFER_SIZE;
err = setsockopt(osock, SOL_SOCKET, SO_RCVBUF, (char *)&sockbufsize, sizeof(sockbufsize));
#ifdef PQISSL_DEBUG
if (err == 0) {
std::cerr << "pqissl::Initiate_Connection: TCP receive buffer size set to " << sockbufsize << std::endl;
} else {
std::cerr << "pqissl::Initiate_Connection: Error setting TCP receive buffer size. Error " << err << std::endl;
}
#endif
err = setsockopt(osock, SOL_SOCKET, SO_SNDBUF, (char *)&sockbufsize, sizeof(sockbufsize));
#ifdef PQISSL_DEBUG
if (err == 0) {
std::cerr << "pqissl::Initiate_Connection: TCP send buffer size set to " << sockbufsize << std::endl;
} else {
std::cerr << "pqissl::Initiate_Connection: Error setting TCP send buffer size. Error " << err << std::endl;
}
#endif
#endif // WINDOWS_SYS
mTimeoutTS = time(NULL) + mConnectTimeout;
//std::cerr << "Setting Connect Timeout " << mConnectTimeout << " Seconds into Future " << std::endl;
waiting = WAITING_SOCK_CONNECT;
sockfd = osock;
return true;
}

View file

@ -0,0 +1,47 @@
#ifndef PQISSLI2PSAM3_H
#define PQISSLI2PSAM3_H
#include "pqi/pqissl.h"
#include "services/autoproxy/rsautoproxymonitor.h"
#include "services/autoproxy/p3i2psam3.h"
// Use a state machine as the whole pqi code is designed around them and some operation (like lookup) might be blocking
enum class pqisslSam3State : uint8_t {
NONE = 0,
DO_LOOKUP,
WAIT_LOOKUP,
DO_CONNECT,
WAIT_CONNECT,
DONE
};
class pqissli2psam3 : public pqissl, public autoProxyCallback
{
public:
pqissli2psam3(pqissllistener *l, PQInterface *parent, p3LinkMgr *lm);
// NetInterface interface
public:
bool connect_parameter(uint32_t type, const std::string &value);
// pqissl interface
protected:
int Initiate_Connection();
int net_internal_close(int fd);
// autoProxyCallback interface
public:
void taskFinished(taskTicket *&ticket);
private:
bool setupSocket();
private:
pqisslSam3State mState;
std::string mI2pAddrB32;
std::string mI2pAddrLong;
Sam3Connection *mConn;
};
#endif // PQISSLI2PSAM3_H

View file

@ -186,8 +186,8 @@ public:
virtual int finaliseConnection(int fd, SSL *ssl, const RsPeerId& peerId,
const sockaddr_storage &raddr);
RS_SET_CONTEXT_DEBUG_LEVEL(2)
private:
std::map<RsPeerId, pqissl*> listenaddr;
RS_SET_CONTEXT_DEBUG_LEVEL(2)
};

View file

@ -46,7 +46,7 @@ static struct RsLog::logInfo pqipersongrpzoneInfo = {RsLog::Default, "pqipersong
#endif
#include "pqi/pqisslproxy.h"
#include "pqi/pqissli2pbob.h"
#include "pqi/pqissli2psam3.h"
pqilistener * pqisslpersongrp::locked_createListener(const struct sockaddr_storage &laddr)
{
@ -74,26 +74,26 @@ pqiperson * pqisslpersongrp::locked_createPerson(const RsPeerId& id, pqilistener
std::cerr << std::endl;
#endif
// Use pqicI2PBOB for I2P
pqiconnect *pqicSOCKSProxy, *pqicI2PBOB;
// Use pqicI2P for I2P
pqiconnect *pqicSOCKSProxy, *pqicI2P;
{
pqisslproxy *pqis = new pqisslproxy((pqissllistener *) listener, pqip, mLinkMgr);
RsSerialiser *rss = new RsSerialiser();
rss->addSerialType(new RsRawSerialiser());
pqicSOCKSProxy = new pqiconnect(pqip, rss, pqis);
}
if (rsAutoProxyMonitor::instance()->isEnabled(autoProxyType::I2PBOB))
if (rsAutoProxyMonitor::instance()->isEnabled(autoProxyType::I2PSAM3))
{
pqissli2pbob *pqis = new pqissli2pbob((pqissllistener *) listener, pqip, mLinkMgr);
pqissli2psam3 *pqis = new pqissli2psam3((pqissllistener *) listener, pqip, mLinkMgr);
RsSerialiser *rss = new RsSerialiser();
rss->addSerialType(new RsRawSerialiser());
pqicI2PBOB = new pqiconnect(pqip, rss, pqis);
pqicI2P = new pqiconnect(pqip, rss, pqis);
} else {
pqicI2PBOB = pqicSOCKSProxy;
pqicI2P = pqicSOCKSProxy;
}
/* first select type based on peer */
uint32_t typePeer = mPeerMgr->getHiddenType(id);
switch (typePeer) {
@ -101,7 +101,7 @@ pqiperson * pqisslpersongrp::locked_createPerson(const RsPeerId& id, pqilistener
pqip -> addChildInterface(PQI_CONNECT_HIDDEN_TOR_TCP, pqicSOCKSProxy);
break;
case RS_HIDDEN_TYPE_I2P:
pqip -> addChildInterface(PQI_CONNECT_HIDDEN_I2P_TCP, pqicI2PBOB);
pqip -> addChildInterface(PQI_CONNECT_HIDDEN_I2P_TCP, pqicI2P);
break;
default:
/* peer is not a hidden one but we are */
@ -109,7 +109,7 @@ pqiperson * pqisslpersongrp::locked_createPerson(const RsPeerId& id, pqilistener
uint32_t typeOwn = mPeerMgr->getHiddenType(AuthSSL::getAuthSSL()->OwnId());
switch (typeOwn) {
case RS_HIDDEN_TYPE_I2P:
pqip -> addChildInterface(PQI_CONNECT_HIDDEN_I2P_TCP, pqicI2PBOB);
pqip -> addChildInterface(PQI_CONNECT_HIDDEN_I2P_TCP, pqicI2P);
break;
default:
/* this case shouldn't happen! */

View file

@ -195,7 +195,7 @@ public:
/*
* Setup Hidden Location;
*/
static void SetHiddenLocation(const std::string& hiddenaddress, uint16_t port, bool useBob);
static void SetHiddenLocation(const std::string& hiddenaddress, uint16_t port, bool useI2p);
static bool LoadPassword(const std::string& passwd) ;

View file

@ -43,7 +43,7 @@
class p3heartbeat;
class p3discovery2;
class p3I2pBob;
class p3I2pSam3;
/* GXS Classes - just declare the classes.
so we don't have to totally recompile to switch */
@ -161,8 +161,8 @@ public:
p3ChatService *chatSrv;
p3StatusService *mStatusSrv;
p3GxsTunnelService *mGxsTunnels;
#ifdef RS_USE_I2P_BOB
p3I2pBob *mI2pBob;
#ifdef RS_USE_I2P_SAM3
p3I2pSam3 *mI2pSam3;
#endif
// This list contains all threaded services. It will be used to shut them down properly.

View file

@ -170,7 +170,7 @@ struct RsInitConfig
std::string hiddenNodeAddress;
uint16_t hiddenNodePort;
bool hiddenNodeI2PBOB;
bool hiddenNodeI2P;
/* Logging */
bool haveLogFile;
@ -664,13 +664,13 @@ void RsInit::setAutoLogin(bool autoLogin){
}
/* Setup Hidden Location; */
void RsInit::SetHiddenLocation(const std::string& hiddenaddress, uint16_t port, bool useBob)
void RsInit::SetHiddenLocation(const std::string& hiddenaddress, uint16_t port, bool useI2p)
{
/* parse the bugger (todo) */
rsInitConfig->hiddenNodeSet = true;
rsInitConfig->hiddenNodeAddress = hiddenaddress;
rsInitConfig->hiddenNodePort = port;
rsInitConfig->hiddenNodeI2PBOB = useBob;
rsInitConfig->hiddenNodeI2P = useI2p;
}
@ -717,7 +717,7 @@ RsGRouter *rsGRouter = NULL ;
# include "rs_upnp/upnphandler_miniupnp.h"
#endif // def RS_USE_LIBUPNP
#include "services/autoproxy/p3i2pbob.h"
#include "services/autoproxy/p3i2psam3.h"
#include "services/autoproxy/rsautoproxymonitor.h"
#include "services/p3gxsreputation.h"
@ -923,9 +923,9 @@ int RsServer::StartupRetroShare()
mNetMgr->setManagers(mPeerMgr, mLinkMgr);
rsAutoProxyMonitor *autoProxy = rsAutoProxyMonitor::instance();
#ifdef RS_USE_I2P_BOB
mI2pBob = new p3I2pBob(mPeerMgr);
autoProxy->addProxy(autoProxyType::I2PBOB, mI2pBob);
#ifdef RS_USE_I2P_SAM3
mI2pSam3 = new p3I2pSam3(mPeerMgr);
autoProxy->addProxy(autoProxyType::I2PSAM3, mI2pSam3);
#endif
//load all the SSL certs as friends
@ -1655,8 +1655,9 @@ int RsServer::StartupRetroShare()
mConfigMgr->addConfiguration("wire.cfg", wire_ns);
#endif
#endif //RS_ENABLE_GXS
#ifdef RS_USE_I2P_BOB
mConfigMgr->addConfiguration("I2PBOB.cfg", mI2pBob);
#ifdef RS_USE_I2P_SAM3
// to make migration easiert, SAM will use BOBs configuration, as they are compatible / the same.
mConfigMgr->addConfiguration("I2PBOB.cfg", mI2pSam3);
#endif
mPluginsManager->addConfigurations(mConfigMgr) ;
@ -1709,34 +1710,33 @@ int RsServer::StartupRetroShare()
{
std::cout << "RsServer::StartupRetroShare setting up hidden locations" << std::endl;
if (rsInitConfig->hiddenNodeI2PBOB) {
std::cout << "RsServer::StartupRetroShare setting up BOB" << std::endl;
if (rsInitConfig->hiddenNodeI2P) {
std::cout << "RsServer::StartupRetroShare setting up SAMv3" << std::endl;
// we need a local port!
mNetMgr->checkNetAddress();
// add i2p proxy
// bob will use this address
sockaddr_storage i2pInstance;
sockaddr_storage_ipv4_aton(i2pInstance, rsInitConfig->hiddenNodeAddress.c_str());
mPeerMgr->setProxyServerAddress(RS_HIDDEN_TYPE_I2P, i2pInstance);
std::string addr; // will be set by auto proxy service
uint16_t port = rsInitConfig->hiddenNodePort; // unused by bob
uint16_t port; // unused by SAM
bool r = autoProxy->initialSetup(autoProxyType::I2PBOB, addr, port);
bool r = autoProxy->initialSetup(autoProxyType::I2PSAM3, addr, port);
if (r && !addr.empty()) {
mPeerMgr->setupHiddenNode(addr, port);
// now enable bob
bobSettings bs;
autoProxy->taskSync(autoProxyType::I2PBOB, autoProxyTask::getSettings, &bs);
bs.enable = true;
autoProxy->taskSync(autoProxyType::I2PBOB, autoProxyTask::setSettings, &bs);
// now enable SAM
samSettings ss;
autoProxy->taskSync(autoProxyType::I2PSAM3, autoProxyTask::getSettings, &ss);
ss.enable = true;
autoProxy->taskSync(autoProxyType::I2PSAM3, autoProxyTask::setSettings, &ss);
} else {
std::cerr << "RsServer::StartupRetroShare failed to receive keys" << std::endl;
/// TODO add notify for failed bob setup
/// TODO add notify for failed i2p setup
}
} else {
mPeerMgr->setupHiddenNode(rsInitConfig->hiddenNodeAddress, rsInitConfig->hiddenNodePort);
@ -1758,19 +1758,17 @@ int RsServer::StartupRetroShare()
if (rsInitConfig->hiddenNodeSet) {
// newly created location
// mNetMgr->checkNetAddress() will setup ports for us
#if 0 // this was used for BOB but is not requires for SAMv3
// trigger updates for auto proxy services
std::vector<autoProxyType::autoProxyType_enum> types;
// i2p bob need to rebuild its command map
types.push_back(autoProxyType::I2PBOB);
rsAutoProxyMonitor::taskSync(types, autoProxyTask::reloadConfig);
#endif
}
/**************************************************************************/
/* startup (stuff dependent on Ids/peers is after this point) */
/**************************************************************************/
autoProxy->startAll();
pqih->init_listener();
@ -1803,8 +1801,8 @@ int RsServer::StartupRetroShare()
/**************************************************************************/
// auto proxy threads
#ifdef RS_USE_I2P_BOB
startServiceThread(mI2pBob, "I2P-BOB");
#ifdef RS_USE_I2P_SAM3
startServiceThread(mI2pSam3, "I2P-SAM3");
#endif
#ifdef RS_ENABLE_GXS

File diff suppressed because it is too large Load diff

View file

@ -1,256 +0,0 @@
/*******************************************************************************
* libretroshare/src/services/autoproxy: p3i2pbob.h *
* *
* libretroshare: retroshare core library *
* *
* Copyright 2016 by Sehraf *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
#ifndef P3I2PBOB_H
#define P3I2PBOB_H
#include <map>
#include <queue>
#include <sys/types.h>
#include "util/rstime.h"
#ifndef WINDOWS_SYS
#include <sys/socket.h>
#endif
#include "pqi/p3cfgmgr.h"
#include "services/autoproxy/rsautoproxymonitor.h"
#include "util/rsthreads.h"
#include "util/i2pcommon.h"
/*
* This class implements I2P BOB (BASIC OPEN BRIDGE) communication to allow RS
* to automatically remote control I2P to setup the needed tunnel.
* BOB is a simple text-based interface: https://geti2p.net/en/docs/api/bob
*
* Note 1:
* One tunnel is enough even for hidden locations since it can be used
* bidirectional. (In contrast to what RS I2P users had to set up manually.)
*
* Note 2:
* BOB tunnels are no SOCKS tunnel. Therefore pqissli2pbob implements a simplified
* proxy specially for BOB tunnels.
*
* Note 3:
* BOB needs a unique name as an ID for each tunnel.
* We use 'RetroShare-' + 8 random base32 characters.
*
* Design:
* The service uses three state machines to manage its task:
* int stateMachineBOB();
* mBOBState
* int stateMachineController();
* mState
* mTask
*
* stateMachineBOB:
* This state machine manages the low level communication with BOB. It basically has a linked
* list (currently a implemented as a std::map) that contains a command and the next
* state.
* Each high level operation (start up / shut down / get keys) is represented by a
* chain of states. E.g. the chain to retrieve new keys:
* mCommands[bobState::setnickN] = {setnick, bobState::newkeysN};
* mCommands[bobState::newkeysN] = {newkeys, bobState::getkeys};
* mCommands[bobState::getkeys] = {getkeys, bobState::clear};
* mCommands[bobState::clear] = {clear, bobState::quit};
* mCommands[bobState::quit] = {quit, bobState::cleared};
*
* stateMachineController:
* This state machine manages the high level tasks.
* It is controlled by mState and mTask.
*
* mTast:
* Tracks the high level operation (like start up).
* It will keep its value even when a task is done to track
* the requested BOB state.
* When other operations are performed like a conection check
* the last task gets backed up and is later restored again
*
* mState:
* This state lives only for one operation an manages the communication
* with the BOB instance. This is basically connecting, starting BOB
* protocol and disconnecting
*
* How a task looks like:
* 1) RS sets task using the ticket system
* 2) stateMachineController connects to BOBs control port, sets mBobState to a lists head
* 3) stateMachineBOB processes command chain
* 4) stateMachineBOB is done and sets mBobState to cleared signaling that the connection
* is cleared and can be closed
* 5) stateMachineController disconnects from BOBs control port and updates mState
*/
///
/// \brief The controllerState enum
/// States for the controller to keep track of what he is currently doing
enum controllerState {
csIdel,
csDoConnect,
csConnected,
csWaitForBob,
csDoDisconnect,
csDisconnected,
csError
};
///
/// \brief The controllerTask enum
/// This state tracks the controllers tast (e.g. setup a BOB tunnel or shut down
/// an existing one).
enum controllerTask {
ctIdle,
ctRunSetUp,
ctRunShutDown,
ctRunGetKeys,
ctRunCheck
};
///
/// \brief The bobState enum
/// One state for each message
///
enum bobState {
bsCleared,
bsSetnickC, // chain head for only client tunnel
bsSetnickN, // chain head for getting new (server) keys
bsSetnickS, // chain head for client and server tunnel
bsGetnick,
bsNewkeysC, // part of chain for only client tunnel
bsNewkeysN, // part of chain for getting new (server) keys
bsGetkeys,
bsSetkeys,
bsInhost,
bsOuthost,
bsInport,
bsOutport,
bsInlength,
bsOutlength,
bsInvariance,
bsOutvariance,
bsInquantity,
bsOutquantity,
bsQuiet,
bsStart,
bsStop,
bsClear,
bsList, // chain head for 'list' command
bsQuit
};
///
/// \brief The bobStateInfo struct
/// State machine with commands
/// \todo This could be replaced by a linked list instead of a map
struct bobStateInfo {
std::string command;
bobState nextState;
};
struct bobSettings : i2p::settings {};
///
/// \brief The bobStates struct
/// This container struct is used to pass all states.
/// Additionally, the tunnel name is included to to show it in the GUI.
/// The advantage of a struct is that it can be forward declared.
struct bobStates {
bobState bs;
controllerState cs;
controllerTask ct;
std::string tunnelName;
};
class p3PeerMgr;
class p3I2pBob : public RsTickingThread, public p3Config, public autoProxyService
{
public:
explicit p3I2pBob(p3PeerMgr *peerMgr);
// autoProxyService interface
public:
bool isEnabled();
bool initialSetup(std::string &addr, uint16_t &);
void processTaskAsync(taskTicket *ticket);
void processTaskSync(taskTicket *ticket);
void threadTick() override; /// @see RsTickingThread
private:
int stateMachineBOB();
int stateMachineBOB_locked_failure(const std::string &answer, const bobStateInfo &currentState);
int stateMachineController();
int stateMachineController_locked_idle();
int stateMachineController_locked_connected();
int stateMachineController_locked_disconnected();
int stateMachineController_locked_error();
// p3Config interface
protected:
RsSerialiser *setupSerialiser();
bool saveList(bool &cleanup, std::list<RsItem *> &lst);
bool loadList(std::list<RsItem *> &load);
private:
// helpers
void getBOBSettings(bobSettings *settings);
void setBOBSettings(const bobSettings *settings);
void getStates(bobStates *bs);
std::string executeCommand(const std::string &command);
bool connectI2P();
bool disconnectI2P();
void finalizeSettings_locked();
void updateSettings_locked();
std::string recv();
// states for state machines
controllerState mState;
controllerTask mTask;
// used to store old state when in error state
// mStateOld is also used as a flag when an error occured in BOB protocol
controllerState mStateOld;
// mTaskOld is used to keep the previous task (start up / shut down) when requesting keys or checking the connection
controllerTask mTaskOld;
bobSettings mSetting;
bobState mBOBState;
// used variables
p3PeerMgr *mPeerMgr;
bool mConfigLoaded;
int mSocket;
rstime_t mLastProxyCheck;
sockaddr_storage mI2PProxyAddr;
std::map<bobState, bobStateInfo> mCommands;
std::string mErrorMsg;
std::string mTunnelName;
std::queue<taskTicket *> mPending;
taskTicket *mProcessing;
// mutex
RsMutex mLock;
};
#endif // P3I2PBOB_H

View file

@ -0,0 +1,798 @@
#include "p3i2psam3.h"
#include <libsam3.h>
#include "pqi/p3peermgr.h"
#include "rsitems/rsconfigitems.h"
static const std::string kConfigKeySAM3Enable = "SAM3_ENABLE";
static const std::string kConfigKeyDestPriv = "DEST_PRIV";
static const std::string kConfigKeyInLength = "IN_LENGTH";
static const std::string kConfigKeyInQuantity = "IN_QUANTITY";
static const std::string kConfigKeyInVariance = "IN_VARIANCE";
static const std::string kConfigKeyInBackupQuantity = "IN_BACKUPQUANTITY";
static const std::string kConfigKeyOutLength = "OUT_LENGTH";
static const std::string kConfigKeyOutQuantity = "OUT_QUANTITY";
static const std::string kConfigKeyOutVariance = "OUT_VARIANCE";
static const std::string kConfigKeyOutBackupQuantity = "OUT_BACKUPQUANTITY";
#ifdef RS_I2P_SAM3_BOB_COMPAT
// used for migration from BOB to SAM
static const std::string kConfigKeyBOBEnable = "BOB_ENABLE";
static const std::string kConfigKeyBOBKey = "BOB_KEY";
static const std::string kConfigKeyBOBAddr = "BOB_ADDR";
#endif
static constexpr bool kDefaultSAM3Enable = false;
RS_SET_CONTEXT_DEBUG_LEVEL(2)
static void inline doSleep(std::chrono::duration<long, std::ratio<1,1000>> timeToSleepMS) {
std::this_thread::sleep_for(timeToSleepMS);
}
p3I2pSam3::p3I2pSam3(p3PeerMgr *peerMgr) :
mConfigLoaded(false), mPeerMgr(peerMgr), mPending(), mLock("p3i2p-sam3")
#ifdef RS_USE_I2P_SAM3_LIBSAM3
, mLockSam3Access("p3i2p-sam3-access")
#endif
{
RS_DBG4();
// set defaults
mSetting.initDefault();
mSetting.enable = kDefaultSAM3Enable;
mSetting.session = nullptr;
libsam3_debug = 0;
}
bool p3I2pSam3::isEnabled()
{
RS_STACK_MUTEX(mLock);
return mSetting.enable;
}
bool p3I2pSam3::initialSetup(std::string &addr, uint16_t &/*port*/)
{
RS_DBG4();
RS_STACK_MUTEX(mLock);
if (!mSetting.address.publicKey.empty() || !mSetting.address.privateKey.empty())
RS_WARN("overwriting keys!");
bool success = generateKey(mSetting.address.publicKey, mSetting.address.privateKey);
if (!success) {
RS_WARN("failed to retrieve keys");
return false;
} else {
std::string s, c;
i2p::getKeyTypes(mSetting.address.publicKey, s, c);
RS_INFO("received key ", s, " ", c);
RS_INFO("public key: ", mSetting.address.publicKey);
RS_INFO("private key: ", mSetting.address.privateKey);
RS_INFO("address: ", i2p::keyToBase32Addr(mSetting.address.publicKey));
// sanity check
auto pub = i2p::publicKeyFromPrivate(mSetting.address.privateKey);
RS_INFO("pub key derived: ", pub);
RS_INFO("address: ", i2p::keyToBase32Addr(pub));
if (pub != mSetting.address.publicKey) {
RS_WARN("public key does not match private key! fixing ...");
mSetting.address.publicKey = pub;
}
mSetting.address.base32 = i2p::keyToBase32Addr(mSetting.address.publicKey);
IndicateConfigChanged();
}
addr = mSetting.address.base32;
return true;
}
void p3I2pSam3::processTaskAsync(taskTicket *ticket)
{
RS_DBG4();
switch (ticket->task) {
case autoProxyTask::stop: [[fallthrough]];
case autoProxyTask::start: [[fallthrough]];
case autoProxyTask::receiveKey: [[fallthrough]];
case autoProxyTask::lookupKey: [[fallthrough]];
case autoProxyTask::proxyStatusCheck: [[fallthrough]];
case autoProxyTask::establishConnection: [[fallthrough]];
case autoProxyTask::closeConnection:
{
RS_STACK_MUTEX(mLock);
mPending.push(ticket);
}
break;
case autoProxyTask::status: [[fallthrough]];
case autoProxyTask::getSettings: [[fallthrough]];
case autoProxyTask::setSettings: [[fallthrough]];
case autoProxyTask::getErrorInfo: [[fallthrough]];
case autoProxyTask::reloadConfig:
// These are supposed to be sync!
RS_DBG("unknown task or sync one!");
rsAutoProxyMonitor::taskError(ticket);
break;
}
}
void p3I2pSam3::processTaskSync(taskTicket *ticket)
{
// RS_DBG4();
const bool data = !!ticket->data;
switch (ticket->task) {
case autoProxyTask::status:
{
samStatus *ss = static_cast<struct samStatus *>(ticket->data);
RS_STACK_MUTEX(mLock);
ss->state = mState;
if (mSetting.session)
ss->sessionName = mSetting.session->channel;
else
ss->sessionName = "none";
}
rsAutoProxyMonitor::taskDone(ticket, autoProxyStatus::ok);
break;
case autoProxyTask::getSettings:
// check if everything needed is set
if (!data) {
RS_DBG("autoProxyTask::getSettings data is missing");
rsAutoProxyMonitor::taskError(ticket);
break;
}
// get settings
{
RS_STACK_MUTEX(mLock);
*static_cast<struct samSettings *>(ticket->data) = mSetting;
}
// finish task
rsAutoProxyMonitor::taskDone(ticket, autoProxyStatus::ok);
break;
case autoProxyTask::setSettings:
// check if everything needed is set
if (!data) {
RS_DBG("autoProxyTask::setSettings data is missing");
rsAutoProxyMonitor::taskError(ticket);
break;
}
// set settings
{
RS_STACK_MUTEX(mLock);
mSetting = *static_cast<struct samSettings *>(ticket->data);
updateSettings_locked();
}
// finish task
rsAutoProxyMonitor::taskDone(ticket, autoProxyStatus::ok);
break;
case autoProxyTask::getErrorInfo:
*static_cast<std::string *>(ticket->data) = mSetting.session->error;
rsAutoProxyMonitor::taskDone(ticket, autoProxyStatus::ok);
break;
case autoProxyTask::reloadConfig:
{
RS_STACK_MUTEX(mLock);
updateSettings_locked();
}
rsAutoProxyMonitor::taskDone(ticket, autoProxyStatus::ok);
break;
case autoProxyTask::stop:
#if 0 // doesn't seem to work, socket stays "CLOSE_WAIT"
// there can be a case where libsam3 will block forever because for some reason it does not detect that the socket it has is dead
// as a workaroung kill it from here
if (mState == samStatus::samState::connectSession || mState == samStatus::samState::connectForward) {
// lock should be held by the main thread
if (!mTmpSession) {
// now it's getting weird
RS_WARN("session is nullptr but mState says it is connecting.");
// no break! just ignore for now ...
} else {
// just close it from here, libsam3 is not thread safe.
// a bit of a hack but should do the trick
// sam3CloseSession(mSetting.session);
sam3tcpDisconnect(mTmpSession->fd);
// no break! continue as usual to keep everything in line
}
}
#endif
[[fallthrough]];
case autoProxyTask::start: [[fallthrough]];
case autoProxyTask::receiveKey: [[fallthrough]];
case autoProxyTask::lookupKey: [[fallthrough]];
case autoProxyTask::proxyStatusCheck: [[fallthrough]];
case autoProxyTask::establishConnection: [[fallthrough]];
case autoProxyTask::closeConnection:
// These are supposed to be async!
RS_WARN("unknown task or async one!");
rsAutoProxyMonitor::taskError(ticket);
break;
}
}
void p3I2pSam3::threadTick()
{
// {
// RS_STACK_MUTEX(mLock);
// Dbg4() << __PRETTY_FUNCTION__ << " mPending: " << mPending.size() << std::endl;
// }
if(mPending.empty()) {
// sleep outisde of lock!
doSleep(std::chrono::milliseconds(250));
return;
}
// get task
taskTicket *tt = nullptr;
{
RS_STACK_MUTEX(mLock);
tt = mPending.front();
mPending.pop();
}
switch (tt->task) {
case autoProxyTask::stop:
mState = samStatus::samState::offline;
stopForwarding();
stopSession();
rsAutoProxyMonitor::taskDone(tt, autoProxyStatus::offline);
break;
case autoProxyTask::start:
{
if (!mSetting.enable) {
rsAutoProxyMonitor::taskDone(tt, autoProxyStatus::disabled);
break;
}
// create main session
mState = samStatus::samState::connectSession;
bool ret = startSession();
if (!ret) {
mState = samStatus::samState::offline;
rsAutoProxyMonitor::taskError(tt);
break;
}
// start forwarding
mState = samStatus::samState::connectForward;
ret = startForwarding();
// finish ticket
if (ret) {
mState = samStatus::samState::online;
rsAutoProxyMonitor::taskDone(tt, autoProxyStatus::online);
} else {
mState = samStatus::samState::offline;
rsAutoProxyMonitor::taskError(tt);
}
}
break;
case autoProxyTask::receiveKey:
{
i2p::address *addr = static_cast<i2p::address *>(tt->data);
if (generateKey(addr->publicKey, addr->privateKey)) {
addr->base32 = i2p::keyToBase32Addr(addr->publicKey);
rsAutoProxyMonitor::taskDone(tt, autoProxyStatus::ok);
} else {
rsAutoProxyMonitor::taskError(tt);
}
}
break;
case autoProxyTask::lookupKey:
lookupKey(tt);
break;
case autoProxyTask::proxyStatusCheck:
{
// TODO better detection of status
bool ok;
ok = !!mSetting.session->fd;
ok &= !!mSetting.session->fwd_fd;
*static_cast<bool*>(tt->data) = ok;
rsAutoProxyMonitor::taskDone(tt, ok ? autoProxyStatus::ok : autoProxyStatus::error);
}
break;
case autoProxyTask::establishConnection:
establishConnection(tt);
break;
case autoProxyTask::closeConnection:
closeConnection(tt);
break;
case autoProxyTask::status: [[fallthrough]];
case autoProxyTask::getSettings: [[fallthrough]];
case autoProxyTask::setSettings: [[fallthrough]];
case autoProxyTask::getErrorInfo: [[fallthrough]];
case autoProxyTask::reloadConfig:
RS_ERR("unable to handle! This is a bug! task:", tt->task);
rsAutoProxyMonitor::taskError(tt);
break;
}
tt = nullptr;
// give i2p backend some time
doSleep(std::chrono::milliseconds(100));
}
RsSerialiser *p3I2pSam3::setupSerialiser()
{
RsSerialiser* rsSerialiser = new RsSerialiser();
rsSerialiser->addSerialType(new RsGeneralConfigSerialiser());
return rsSerialiser;
}
#define addKVS(_key, _value) \
kv.key = _key;\
kv.value = _value;\
vitem->tlvkvs.pairs.push_back(kv);
#define addKVSInt(_key, _value) \
kv.key = _key;\
rs_sprintf(kv.value, "%d", _value);\
vitem->tlvkvs.pairs.push_back(kv);
bool p3I2pSam3::saveList(bool &cleanup, std::list<RsItem *> &lst)
{
RS_DBG4();
cleanup = true;
RsConfigKeyValueSet *vitem = new RsConfigKeyValueSet;
RsTlvKeyValue kv;
RS_STACK_MUTEX(mLock);
addKVS(kConfigKeySAM3Enable, mSetting.enable ? "TRUE" : "FALSE")
addKVS(kConfigKeyDestPriv, mSetting.address.privateKey);
addKVSInt(kConfigKeyInLength, mSetting.inLength)
addKVSInt(kConfigKeyInQuantity, mSetting.inQuantity)
addKVSInt(kConfigKeyInVariance, mSetting.inVariance)
addKVSInt(kConfigKeyInBackupQuantity, mSetting.inBackupQuantity)
addKVSInt(kConfigKeyOutLength, mSetting.outLength)
addKVSInt(kConfigKeyOutQuantity, mSetting.outQuantity)
addKVSInt(kConfigKeyOutVariance, mSetting.outVariance)
addKVSInt(kConfigKeyOutBackupQuantity, mSetting.outBackupQuantity)
#ifdef RS_I2P_SAM3_BOB_COMPAT
// these allow SAMv3 users to switch back to BOB
// remove after some time
addKVS(kConfigKeyBOBEnable, mSetting.enable ? "TRUE" : "FALSE")
addKVS(kConfigKeyBOBKey, mSetting.address.privateKey)
addKVS(kConfigKeyBOBAddr, mSetting.address.base32)
#endif
lst.push_back(vitem);
return true;
}
#undef addKVS
#undef addKVSUInt
#define getKVSUInt(_kit, _key, _value) \
else if (_kit->key == _key) {\
std::istringstream is(_kit->value);\
int tmp;\
is >> tmp;\
_value = (int8_t)tmp;\
}
bool p3I2pSam3::loadList(std::list<RsItem *> &load)
{
RS_DBG4();
std::string priv;
priv.clear();
for(std::list<RsItem*>::const_iterator it = load.begin(); it!=load.end(); ++it) {
RsConfigKeyValueSet *vitem = dynamic_cast<RsConfigKeyValueSet*>(*it);
if(vitem != NULL) {
RS_STACK_MUTEX(mLock);
for(std::list<RsTlvKeyValue>::const_iterator kit = vitem->tlvkvs.pairs.begin(); kit != vitem->tlvkvs.pairs.end(); ++kit) {
if (kit->key == kConfigKeySAM3Enable)
mSetting.enable = kit->value == "TRUE";
else if (kit->key == kConfigKeyDestPriv)
priv = kit->value;
getKVSUInt(kit, kConfigKeyInLength, mSetting.inLength)
getKVSUInt(kit, kConfigKeyInQuantity, mSetting.inQuantity)
getKVSUInt(kit, kConfigKeyInVariance, mSetting.inVariance)
getKVSUInt(kit, kConfigKeyInBackupQuantity, mSetting.inBackupQuantity)
getKVSUInt(kit, kConfigKeyOutLength, mSetting.outLength)
getKVSUInt(kit, kConfigKeyOutQuantity, mSetting.outQuantity)
getKVSUInt(kit, kConfigKeyOutVariance, mSetting.outVariance)
getKVSUInt(kit, kConfigKeyOutBackupQuantity, mSetting.outBackupQuantity)
#ifdef RS_I2P_SAM3_BOB_COMPAT
// import BOB settings
else if (kit->key == kConfigKeyBOBEnable)
mSetting.enable = kit->value == "TRUE";
else if (kit->key == kConfigKeyBOBKey) {
// don't overwirte, just import when not set already!
if (priv.empty())
priv = kit->value;
}
#endif
else
RS_INFO("unknown key:", kit->key);
}
}
delete vitem;
}
// get the pub key
std::string pub = i2p::publicKeyFromPrivate(priv);
if (pub.empty() || priv.empty())
RS_DBG("no destination to load");
else {
RS_STACK_MUTEX(mLock);
mSetting.address.publicKey = pub;
mSetting.address.privateKey = priv;
mSetting.address.base32 = i2p::keyToBase32Addr(pub);
}
RS_STACK_MUTEX(mLock);
mConfigLoaded = true;
return true;
}
#undef getKVSUInt
bool p3I2pSam3::startSession()
{
RS_DBG4();
constexpr size_t len = 8;
const std::string location = RsRandom::alphaNumeric(len);
const std::string nick = "RetroShare-" + location;
std::vector<std::string> params;
{
RS_STACK_MUTEX(mLock);
// length
params.push_back(i2p::makeOption("inbound.length", mSetting.inLength));
params.push_back(i2p::makeOption("outbound.length", mSetting.outLength));
// variance
params.push_back(i2p::makeOption("inbound.lengthVariance", + mSetting.inVariance));
params.push_back(i2p::makeOption("outbound.lengthVariance", + mSetting.outVariance));
// quantity
params.push_back(i2p::makeOption("inbound.quantity", + mSetting.inQuantity));
params.push_back(i2p::makeOption("outbound.quantity", + mSetting.outQuantity));
// backup quantity
params.push_back(i2p::makeOption("inbound.backupQuantity", + mSetting.inBackupQuantity));
params.push_back(i2p::makeOption("outbound.backupQuantity", + mSetting.outBackupQuantity));
}
std::string paramsStr;
for (auto &&p : params)
paramsStr.append(p + " ");
// keep trailing space for easier extending when necessary
int ret;
if (mSetting.session) {
stopSession();
}
auto session = (Sam3Session*)rs_malloc(sizeof (Sam3Session));
// add nick
paramsStr.append("inbound.nickname=" + nick); // leading space is already there
{
RS_STACK_MUTEX(mLockSam3Access);
if(!mSetting.address.privateKey.empty()) {
RS_DBG3("with destination");
ret = sam3CreateSession(session, SAM3_HOST_DEFAULT, SAM3_PORT_DEFAULT, mSetting.address.privateKey.c_str(), Sam3SessionType::SAM3_SESSION_STREAM, Sam3SigType::EdDSA_SHA512_Ed25519, paramsStr.c_str());
} else {
RS_DBG("without destination");
ret = sam3CreateSession(session, SAM3_HOST_DEFAULT, SAM3_PORT_DEFAULT, SAM3_DESTINATION_TRANSIENT, Sam3SessionType::SAM3_SESSION_STREAM, Sam3SigType::EdDSA_SHA512_Ed25519, paramsStr.c_str());
}
}
if (ret != 0) {
delete session;
session = nullptr;
return false;
}
#if 0 // this check is useless. For non i2p hidden locations the public key is temporal anyway and for i2p hidden ones, it is part of the (fixed) private key.
if (!mSetting.address.publicKey.empty() && mSetting.address.publicKey != session->pubkey)
// This should be ok for non hidden locations. This should be a problem for hidden i2p locations...
RS_DBG("public key changed! Yet unsure if this is ok or a problem. Should be fine for non i2p hidden locations or clear net.");
#endif
/*
* Note: sam3CreateSession will issue a name looup of "ME" to receive its public key, thus it is always correct.
* No need to use i2p::publicKeyFromPrivate()
*/
RS_STACK_MUTEX(mLock);
mSetting.session = session;
mSetting.address.publicKey = session->pubkey;
mSetting.address.base32 = i2p::keyToBase32Addr(session->pubkey);
// do not overwrite the private key, if any!!
RS_DBG1("nick: ", nick, " address: ", mSetting.address.base32);
RS_DBG2(" myDestination.pub ", mSetting.address.publicKey);
RS_DBG2(" myDestination.priv ", mSetting.address.privateKey);
return true;
}
bool p3I2pSam3::startForwarding()
{
RS_DBG4();
if(mSetting.address.privateKey.empty()) {
RS_DBG3("no private key set");
// IMPORANT: return true here!
// since there is no forward session for non hidden nodes, this funtion is successfull by doing nothing
return true;
}
if (!mSetting.session) {
RS_WARN("no session found!");
return false;
}
peerState ps;
mPeerMgr->getOwnNetStatus(ps);
RS_STACK_MUTEX(mLockSam3Access);
mSetting.session->silent = true;
int ret = sam3StreamForward(mSetting.session, sockaddr_storage_iptostring(ps.localaddr).c_str(), sockaddr_storage_port(ps.localaddr));
if (ret < 0) {
RS_DBG("forward failed, due to", mSetting.session->error);
return false;
}
return true;
}
void p3I2pSam3::stopSession()
{
RS_DBG4();
{
RS_STACK_MUTEX(mLock);
if (!mSetting.session)
return;
// swap connections
mInvalidConnections = mValidConnections;
mValidConnections.clear();
RS_STACK_MUTEX(mLockSam3Access);
sam3CloseSession(mSetting.session);
free(mSetting.session);
mSetting.session = nullptr;
mState = samStatus::samState::offline;
}
// At least i2pd doesn't like to instantaniously stop and (re)start a session, wait here just a little bit.
// Not ideal but does the trick.
// (This happens when using the "restart" button in the settings.)
doSleep(std::chrono::seconds(10));
}
void p3I2pSam3::stopForwarding()
{
// nothing to do here, forwarding is stop when closing the seassion
}
bool p3I2pSam3::generateKey(std::string &pub, std::string &priv)
{
RS_DBG4();
pub.clear();
priv.clear();
// The session is only usef for transporting the data
Sam3Session ss;
if (0 > sam3GenerateKeys(&ss, SAM3_HOST_DEFAULT, SAM3_PORT_DEFAULT, Sam3SigType::EdDSA_SHA512_Ed25519)) {
RS_DBG("got error: ", ss.error);
return false;
}
pub = std::string(ss.pubkey);
priv = std::string(ss.privkey);
// sanity check
auto p = i2p::publicKeyFromPrivate(priv);
if (p != pub) {
RS_WARN("public key does not match private key! fixing ...");
pub = p;
}
RS_DBG2("publuc key / address ", pub);
RS_DBG2("private key ", priv);
return true;
}
void p3I2pSam3::lookupKey(taskTicket *ticket)
{
// this can be called independend of the main SAM session!
auto addr = static_cast<i2p::address*>(ticket->data);
if (addr->base32.empty()) {
RS_ERR("lookupKey: called with empty address");
rsAutoProxyMonitor::taskError(ticket);
return;
}
RsThread::async([ticket]()
{
auto addr = static_cast<i2p::address*>(ticket->data);
// The session is only usef for transporting the data
Sam3Session ss;
int ret = sam3NameLookup(&ss, SAM3_HOST_DEFAULT, SAM3_PORT_DEFAULT, addr->base32.c_str());
if (ret < 0) {
// get error
RS_DBG("key: ", addr->base32);
RS_DBG("got error: ", ss.error);
rsAutoProxyMonitor::taskError(ticket);
} else {
addr->publicKey = ss.destkey;
rsAutoProxyMonitor::taskDone(ticket, autoProxyStatus::ok);
RS_DBG1("success");
}
});
}
void p3I2pSam3::establishConnection(taskTicket *ticket)
{
if (mState != samStatus::samState::online || !mSetting.session) {
RS_WARN("no session found!");
rsAutoProxyMonitor::taskError(ticket);
return;
}
samEstablishConnectionWrapper *wrapper = static_cast<samEstablishConnectionWrapper*>(ticket->data);
if (wrapper->address.publicKey.empty()) {
RS_ERR("no public key given");
rsAutoProxyMonitor::taskError(ticket);
return;
}
RsThread::async([ticket, this]() {
auto wrapper = static_cast<samEstablishConnectionWrapper*>(ticket->data);
struct Sam3Connection *connection;
{
auto l = this->mLockSam3Access;
RS_STACK_MUTEX(l);
mSetting.session->silent = false;
connection = sam3StreamConnect(this->mSetting.session, wrapper->address.publicKey.c_str());
}
if (!connection) {
// get error
RS_DBG("got error:", this->mSetting.session->error);
rsAutoProxyMonitor::taskError(ticket);
} else {
wrapper->connection = connection;
{
auto l = this->mLockSam3Access;
RS_STACK_MUTEX(l);
this->mValidConnections.push_back(connection);
}
RS_DBG1("success");
rsAutoProxyMonitor::taskDone(ticket, autoProxyStatus::ok);
}
});
}
void p3I2pSam3::closeConnection(taskTicket *ticket)
{
Sam3Connection *conn = static_cast<Sam3Connection*>(ticket->data);
if (mState == samStatus::samState::offline || !mSetting.session) {
// no session found, sam was likely stopped
RS_DBG2("no session found");
auto it = std::find(mInvalidConnections.begin(), mInvalidConnections.end(), conn);
if (it != mInvalidConnections.end()) {
// this is the expected case
mInvalidConnections.erase(it);
} else {
// this is unexpected but not a big deal, just warn
RS_WARN("cannot find connection in mInvalidConnections");
it = std::find(mValidConnections.begin(), mValidConnections.end(), conn);
if (it != mValidConnections.end()) {
mValidConnections.erase(it);
// now it is getting even weirder, still not a big deal, just warn
RS_WARN("found connection in mValidConnections");
}
}
// when libsam3 has already handled closing of the connection - which should be the case here - the memory has been freed already (-> pointer is invalid)
conn = nullptr;
} else {
RS_STACK_MUTEX(mLock);
bool callClose = true;
// search in current connections
auto it = std::find(mValidConnections.begin(), mValidConnections.end(), conn);
if (it != mValidConnections.end()) {
RS_DBG2("found valid connection");
mValidConnections.erase(it);
} else {
// search in old connections
it = std::find(mInvalidConnections.begin(), mInvalidConnections.end(), conn);
if (it != mInvalidConnections.end()) {
// old connection, just ignore. *should* be freed already
mInvalidConnections.erase(it);
RS_DBG2("found old (invalid) connection");
callClose = false;
conn = nullptr;
} else {
// weird
RS_WARN("could'n find connection!");
// best thing we can do here
callClose = false;
conn = nullptr;
}
}
if (callClose) {
RS_DBG2("closing connection");
RS_STACK_MUTEX(mLockSam3Access);
sam3CloseConnection(conn);
conn = nullptr; // freed by above call
}
}
if (conn) {
free(conn);
conn = nullptr;
}
ticket->data = nullptr;
rsAutoProxyMonitor::taskDone(ticket, autoProxyStatus::ok);
return;
}
void p3I2pSam3::updateSettings_locked()
{
RS_DBG4();
IndicateConfigChanged();
#if 0 // TODO recreat session when active, can we just recreat it?
if (mSs) {
stopSession();
startSession();
}
#endif
}

View file

@ -0,0 +1,110 @@
#ifndef P3I2PSAM3_H
#define P3I2PSAM3_H
#include <queue>
#include <list>
#include "services/autoproxy/rsautoproxymonitor.h"
#include "pqi/p3cfgmgr.h"
#include "util/i2pcommon.h"
#include "util/rsthreads.h"
/*
* This class implements I2P SAMv3 (Simple Anonymous Messaging) to allow RS
* to automatically setup tunnel to and from I2P.
* SAMv3 is a simple text-based interface: https://geti2p.net/de/docs/api/samv3
*
* For the actual SAM commands / low level stuff libsam3 (https://github.com/i2p/libsam3)
* is used with some minor adjustments, for exmaple, the FORWARD session is always silent.
*
* SAM in a nutshell works like this:
* 1) setup main/control session which configures everything (destination ID, tunnel number, hops number, and so on)
* 2) setup a forward session, so that I2P will establish a connection to RS for each incoming connection to our i2p destination
* 3a) query/lookup the destination (public key) for a given i2p address
* 3b) connect to the given destination
*
* An established connection (both incoming or outgoing) are then handed over to RS.
* The lifetime of a session (and its subordinates connections) is bound to their tcp socket. When the socket closes, the session is closed, too.
*
*/
class p3PeerMgr;
class Sam3Session;
class Sam3Connection;
typedef Sam3Session samSession;
struct samSettings : i2p::settings {
samSession *session;
};
struct samEstablishConnectionWrapper {
i2p::address address;
Sam3Connection *connection;
};
struct samStatus {
std::string sessionName;
enum samState {
offline,
connectSession,
connectForward,
online
} state; // the name is kinda redundant ...
};
class p3I2pSam3 : public RsTickingThread, public p3Config, public autoProxyService
{
public:
p3I2pSam3(p3PeerMgr *peerMgr);
// autoProxyService interface
public:
bool isEnabled();
bool initialSetup(std::string &addr, uint16_t &port);
void processTaskAsync(taskTicket *ticket);
void processTaskSync(taskTicket *ticket);
// RsTickingThread interface
public:
void threadTick(); /// @see RsTickingThread
// p3Config interface
protected:
RsSerialiser *setupSerialiser();
bool saveList(bool &cleanup, std::list<RsItem *> &);
bool loadList(std::list<RsItem *> &load);
private:
bool startSession();
bool startForwarding();
void stopSession();
void stopForwarding();
bool generateKey(std::string &pub, std::string &priv);
void lookupKey(taskTicket *ticket);
void establishConnection(taskTicket *ticket);
void closeConnection(taskTicket *ticket);
void updateSettings_locked();
bool mConfigLoaded;
samSettings mSetting;
p3PeerMgr *mPeerMgr;
std::queue<taskTicket *> mPending;
// Used to report the state to the gui
// (Since the create session call/will can block and there is no easy way from outside the main thread to see
// what is going on, it is easier to store the current state in an extra variable independen from the main thread)
samStatus::samState mState;
// used to keep track of connections, libsam3 does it internally but it can be unreliable since pointers are shared
std::list<Sam3Connection *> mValidConnections, mInvalidConnections;
// mutex
RsMutex mLock;
RsMutex mLockSam3Access; // libsam3 is not thread safe! (except for key lookup)
};
#endif // P3I2PSAM3_H

View file

@ -329,14 +329,22 @@ autoProxyService *rsAutoProxyMonitor::lookUpService(autoProxyType::autoProxyType
bool rsAutoProxyMonitor::isAsyncTask(autoProxyTask::autoProxyTask_enum t)
{
// Explicit list all values, so that missing ones will be detected by the compiler.
switch (t) {
case autoProxyTask::start:
case autoProxyTask::stop:
case autoProxyTask::receiveKey:
case autoProxyTask::start: [[fallthrough]];
case autoProxyTask::stop: [[fallthrough]];
case autoProxyTask::receiveKey: [[fallthrough]];
case autoProxyTask::lookupKey: [[fallthrough]];
case autoProxyTask::establishConnection: [[fallthrough]];
case autoProxyTask::closeConnection:
return true;
break;
default:
break;
case autoProxyTask::status: [[fallthrough]];
case autoProxyTask::getSettings: [[fallthrough]];
case autoProxyTask::setSettings: [[fallthrough]];
case autoProxyTask::getErrorInfo: [[fallthrough]];
case autoProxyTask::reloadConfig: [[fallthrough]];
case autoProxyTask::proxyStatusCheck:
return false;
}
return false;
}

View file

@ -31,23 +31,27 @@ class autoProxyCallback;
namespace autoProxyType {
enum autoProxyType_enum {
I2PBOB
// I2PBOB,
I2PSAM3
};
}
namespace autoProxyTask {
enum autoProxyTask_enum {
/* async tasks */
start, ///< start up proxy
stop, ///< shut down proxy
receiveKey, ///< renew proxy key (if any)
proxyStatusCheck, ///< use to check if the proxy is still running
start, ///< start up proxy
stop, ///< shut down proxy
receiveKey, ///< renew proxy key (if any)
lookupKey, ///< look up a base32 addr
proxyStatusCheck, ///< use to check if the proxy is still running
establishConnection, ///< create a connection to a given public key or base32 address
closeConnection, ///< closes a connection
/* sync tasks */
status, ///< get status from auto proxy
getSettings, ///< get setting from auto proxy
setSettings, ///< set setting of auto proxy
reloadConfig, ///< signal config reload/rebuild
getErrorInfo ///< get error information from auto proxy
status, ///< get status from auto proxy
getSettings, ///< get setting from auto proxy
setSettings, ///< set setting of auto proxy
reloadConfig, ///< signal config reload/rebuild
getErrorInfo ///< get error information from auto proxy
};
}

View file

@ -90,6 +90,15 @@ rs_broadcast_discovery {
win32-g++|win32-clang-g++:dLibs *= wsock32
}
rs_sam3_libsam3 {
LIBSAM3_SRC_PATH=$$clean_path($${RS_SRC_PATH}/supportlibs/libsam3/)
LIBSAM3_BUILD_PATH=$$clean_path($${RS_BUILD_PATH}/supportlibs/libsam3/)
INCLUDEPATH *= $$clean_path($${LIBSAM3_SRC_PATH}/src/libsam3/)
DEPENDPATH *= $$clean_path($${LIBSAM3_BUILD_PATH})
QMAKE_LIBDIR *= $$clean_path($${LIBSAM3_BUILD_PATH})
LIBS *= -L$$clean_path($${LIBSAM3_BUILD_PATH}) -lsam3
}
static {
sLibs *= $$mLibs
} else {

View file

@ -50,8 +50,10 @@ std::string publicKeyFromPrivate(std::string const &priv)
* https://geti2p.net/spec/common-structures#keysandcert
* https://geti2p.net/spec/common-structures#certificate
*/
if (priv.length() < 884) // base64 ( = 663 bytes = KeyCert + priv Keys)
if (priv.length() < privKeyMinLenth_b64) {
RS_WARN("key to short!");
return std::string();
}
// creat a copy to work on, need to convert it to standard base64
auto priv_copy(priv);
@ -70,13 +72,14 @@ std::string publicKeyFromPrivate(std::string const &priv)
uint8_t certType = 0;
uint16_t len = 0;
uint16_t signingKeyType = 0;
uint16_t cryptKey = 0;
uint16_t cryptKeyType = 0;
// only used for easy break
do {
try {
// jump to certificate
p += publicKeyLen;
// try to read type and length
certType = *p++;
len = readTwoBytesBE(p);
@ -87,7 +90,7 @@ std::string publicKeyFromPrivate(std::string const &priv)
/*
* CertType.Null
* type null is followed by 0x00 0x00 <END>
* so has to be 0!
* so len has to be 0!
*/
RS_DBG("cert is CertType.Null");
publicKeyLen += 3; // add 0x00 0x00 0x00
@ -119,7 +122,7 @@ std::string publicKeyFromPrivate(std::string const &priv)
// likely 7
signingKeyType = readTwoBytesBE(p);
RS_DBG("signing pubkey type ", certType);
RS_DBG("signing pubkey type ", signingKeyType);
if (signingKeyType >= 3 && signingKeyType <= 6) {
RS_DBG("signing pubkey type ", certType, " has oversize");
// calculate oversize
@ -137,18 +140,18 @@ std::string publicKeyFromPrivate(std::string const &priv)
return std::string();
}
publicKeyLen += values.first - 128; // 128 = default DSA key length = the space than can be used before the key must be splitted
publicKeyLen += values.first - 128; // 128 = default DSA key length = the space that can be used before the key must be splitted
}
// Crypto Public Key
// likely 0
cryptKey = readTwoBytesBE(p);
RS_DBG("crypto pubkey type ", cryptKey);
cryptKeyType = readTwoBytesBE(p);
RS_DBG("crypto pubkey type ", cryptKeyType);
// info: these are all smaller than the default 256 bytes, so no oversize calculation is needed
break;
} catch (const std::out_of_range &e) {
RS_DBG("hit exception! ", e.what());
RS_DBG("hit an exception! ", e.what());
return std::string();
}
} while(false);
@ -160,4 +163,107 @@ std::string publicKeyFromPrivate(std::string const &priv)
return pub;
}
bool getKeyTypes(const std::string &key, std::string &signingKey, std::string &cryptoKey)
{
if (key.length() < pubKeyMinLenth_b64) {
RS_WARN("key to short!");
return false;
}
// creat a copy to work on, need to convert it to standard base64
auto key_copy(key);
std::replace(key_copy.begin(), key_copy.end(), '~', '/');
// replacing the - with a + is not necessary, as RsBase64 can handle base64url encoding, too
// std::replace(copy.begin(), copy.end(), '-', '+');
// get raw data
std::vector<uint8_t> data;
RsBase64::decode(key_copy, data);
auto p = data.cbegin();
constexpr size_t publicKeyLen = 256 + 128; // default length (bytes)
uint8_t certType = 0;
uint16_t signingKeyType = 0;
uint16_t cryptKeyType = 0;
// try to read types
try {
// jump to certificate
p += publicKeyLen;
// try to read type and skip length
certType = *p++;
p += 2;
// only 0 and 5 are used / valid at this point
// check for == 0
if (certType == static_cast<typename std::underlying_type<CertType>::type>(CertType::Null)) {
RS_DBG("cert is CertType.Null");
signingKey = "DSA_SHA1";
cryptoKey = "ElGamal";
return true;
}
// check for != 5
if (certType != static_cast<typename std::underlying_type<CertType>::type>(CertType::Key)) {
// unsupported
RS_DBG("cert type ", certType, " is unsupported");
return false;
}
RS_DBG("cert is CertType.Key");
// Signing Public Key
// likely 7
signingKeyType = readTwoBytesBE(p);
RS_DBG("signing pubkey type ", signingKeyType);
// Crypto Public Key
// likely 0
cryptKeyType = readTwoBytesBE(p);
RS_DBG("crypto pubkey type ", cryptKeyType);
} catch (const std::out_of_range &e) {
RS_DBG("hit an exception! ", e.what());
return false;
}
// now convert to string (this would be easier with c++17)
#define HELPER(a, b, c) \
case static_cast<typename std::underlying_type<a>::type>(a::c): \
b = #c; \
break;
switch (signingKeyType) {
HELPER(SigningKeyType, signingKey, DSA_SHA1)
HELPER(SigningKeyType, signingKey, ECDSA_SHA256_P256)
HELPER(SigningKeyType, signingKey, ECDSA_SHA384_P384)
HELPER(SigningKeyType, signingKey, ECDSA_SHA512_P521)
HELPER(SigningKeyType, signingKey, RSA_SHA256_2048)
HELPER(SigningKeyType, signingKey, RSA_SHA384_3072)
HELPER(SigningKeyType, signingKey, RSA_SHA512_4096)
HELPER(SigningKeyType, signingKey, EdDSA_SHA512_Ed25519)
HELPER(SigningKeyType, signingKey, EdDSA_SHA512_Ed25519ph)
HELPER(SigningKeyType, signingKey, RedDSA_SHA512_Ed25519)
default:
RsWarn("unkown signing key type:", signingKeyType);
return false;
}
switch (cryptKeyType) {
HELPER(CryptoKeyType, cryptoKey, ElGamal)
HELPER(CryptoKeyType, cryptoKey, P256)
HELPER(CryptoKeyType, cryptoKey, P384)
HELPER(CryptoKeyType, cryptoKey, P521)
HELPER(CryptoKeyType, cryptoKey, X25519)
default:
RsWarn("unkown crypto key type:", cryptKeyType);
return false;
}
#undef HELPER
return true;
}
} // namespace i2p

View file

@ -186,6 +186,39 @@ static const std::array<std::pair<uint16_t, uint16_t>, 12> signingKeyLengths {
/*SigningKeyType::RedDSA_SHA512_Ed25519 */ std::make_pair<uint16_t, uint16_t>( 32, 32),
};
/*
* Key length infos:
*
* BOB private key
* len b64: 884
* len pln: 663
*
* BOB public key / destination
* len b64: 516
* len pln: 387
*
* SAMv3 private key
* len b64: 908
* len pln: 679
*
* SAMv3 public key
* len b64: 516
* len pln: 387
*
* Example:
* in bytes, public key only
* 384 (Key) + 3 (Null certificate) = 387 bytes
* 384 (Key) + 7 (key certificate) = 391 bytes
*
* in bytes public + private key
* 384 (Key) + 3 (Null certificate) + 256 (ElGamal) + 20 (DSA_SHA1) = 663 bytes
* 384 (Key) + 7 (key certificate) + 256 (ElGamal) + 32 (EdDSA_SHA512_Ed25519) = 679 bytes
*/
constexpr size_t pubKeyMinLenth_b64 = 516;
constexpr size_t pubKeyMinLenth_bin = 387;
constexpr size_t privKeyMinLenth_b64 = 884;
constexpr size_t privKeyMinLenth_bin = 663;
/**
* @brief makeOption Creates the string "lhs=rhs" used by BOB and SAM. Converts rhs
* @param lhs option to set
@ -208,6 +241,15 @@ std::string keyToBase32Addr(const std::string &key);
*/
std::string publicKeyFromPrivate(const std::string &priv);
/**
* @brief getKeyTypes returns the name of the utilized algorithms used by the key
* @param key public key
* @param signingKey name of the signing key, e.g. DSA_SHA1
* @param cryptoKey name of the crpyto key, e.g. ElGamal
* @return true on success, false otherwise
*/
bool getKeyTypes(const std::string &key, std::string &signingKey, std::string &cryptoKey);
} // namespace i2p
#endif // I2PCOMMON_H