mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-02-02 02:54:01 +01:00
[BOB] require commands options, fix usage of existent nick and status
Signed-off-by: R4SAS <r4sas@i2pmail.org>
This commit is contained in:
parent
17892238a9
commit
605ccf3e02
|
@ -168,7 +168,7 @@ namespace client
|
|||
m_LocalDestination (localDestination),
|
||||
m_OutboundTunnel (nullptr), m_InboundTunnel (nullptr),
|
||||
m_Nickname(nickname), m_InHost(inhost), m_OutHost(outhost),
|
||||
m_InPort(inport), m_OutPort(outport), m_Quiet(quiet)
|
||||
m_InPort(inport), m_OutPort(outport), m_Quiet(quiet), m_IsRunning(false)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -183,6 +183,7 @@ namespace client
|
|||
{
|
||||
if (m_OutboundTunnel) m_OutboundTunnel->Start ();
|
||||
if (m_InboundTunnel) m_InboundTunnel->Start ();
|
||||
m_IsRunning = true;
|
||||
}
|
||||
|
||||
void BOBDestination::Stop ()
|
||||
|
@ -193,6 +194,7 @@ namespace client
|
|||
|
||||
void BOBDestination::StopTunnels ()
|
||||
{
|
||||
m_IsRunning = false;
|
||||
if (m_OutboundTunnel)
|
||||
{
|
||||
m_OutboundTunnel->Stop ();
|
||||
|
@ -361,7 +363,7 @@ namespace client
|
|||
const auto issetStr = [](const std::string &str) { return str.empty() ? "not_set" : str; }; // for inhost, outhost
|
||||
const auto issetNum = [&issetStr](const int p) { return issetStr(p == 0 ? "" : std::to_string(p)); }; // for inport, outport
|
||||
const auto destExists = [](const BOBDestination * const dest) { return dest != nullptr; };
|
||||
const auto destReady = [](const BOBDestination * const dest) { return dest->GetLocalDestination()->IsReady(); };
|
||||
const auto destReady = [](const BOBDestination * const dest) { return dest->IsRunning(); };
|
||||
const auto bool_str = [](const bool v) { return v ? "true" : "false"; }; // bool -> str
|
||||
|
||||
// tunnel info
|
||||
|
@ -479,26 +481,43 @@ namespace client
|
|||
void BOBCommandSession::SetNickCommandHandler (const char * operand, size_t len)
|
||||
{
|
||||
LogPrint (eLogDebug, "BOB: setnick ", operand);
|
||||
m_Nickname = operand;
|
||||
std::string msg ("Nickname set to ");
|
||||
msg += m_Nickname;
|
||||
SendReplyOK (msg.c_str ());
|
||||
if(*operand)
|
||||
{
|
||||
auto dest = m_Owner.FindDestination (operand);
|
||||
if (!dest)
|
||||
{
|
||||
m_Nickname = operand;
|
||||
std::string msg ("Nickname set to ");
|
||||
msg += m_Nickname;
|
||||
SendReplyOK (msg.c_str ());
|
||||
}
|
||||
else
|
||||
SendReplyError ("tunnel is active");
|
||||
}
|
||||
else
|
||||
SendReplyError ("no nickname has been set");
|
||||
}
|
||||
|
||||
void BOBCommandSession::GetNickCommandHandler (const char * operand, size_t len)
|
||||
{
|
||||
LogPrint (eLogDebug, "BOB: getnick ", operand);
|
||||
m_CurrentDestination = m_Owner.FindDestination (operand);
|
||||
if (m_CurrentDestination)
|
||||
if(*operand)
|
||||
{
|
||||
m_Keys = m_CurrentDestination->GetKeys ();
|
||||
m_Nickname = operand;
|
||||
}
|
||||
if (m_Nickname == operand)
|
||||
{
|
||||
std::string msg ("Nickname set to ");
|
||||
msg += m_Nickname;
|
||||
SendReplyOK (msg.c_str ());
|
||||
m_CurrentDestination = m_Owner.FindDestination (operand);
|
||||
if (m_CurrentDestination)
|
||||
{
|
||||
m_Keys = m_CurrentDestination->GetKeys ();
|
||||
m_IsActive = m_CurrentDestination->IsRunning ();
|
||||
m_Nickname = operand;
|
||||
}
|
||||
if (m_Nickname == operand)
|
||||
{
|
||||
std::string msg ("Nickname set to ");
|
||||
msg += m_Nickname;
|
||||
SendReplyOK (msg.c_str ());
|
||||
}
|
||||
else
|
||||
SendReplyError ("no nickname has been set");
|
||||
}
|
||||
else
|
||||
SendReplyError ("no nickname has been set");
|
||||
|
@ -535,7 +554,7 @@ namespace client
|
|||
void BOBCommandSession::SetkeysCommandHandler (const char * operand, size_t len)
|
||||
{
|
||||
LogPrint (eLogDebug, "BOB: setkeys ", operand);
|
||||
if (m_Keys.FromBase64 (operand))
|
||||
if (*operand && m_Keys.FromBase64 (operand))
|
||||
SendReplyOK (m_Keys.GetPublic ()->ToBase64 ().c_str ());
|
||||
else
|
||||
SendReplyError ("invalid keys");
|
||||
|
@ -562,35 +581,55 @@ namespace client
|
|||
void BOBCommandSession::OuthostCommandHandler (const char * operand, size_t len)
|
||||
{
|
||||
LogPrint (eLogDebug, "BOB: outhost ", operand);
|
||||
m_OutHost = operand;
|
||||
SendReplyOK ("outhost set");
|
||||
if (*operand)
|
||||
{
|
||||
m_OutHost = operand;
|
||||
SendReplyOK ("outhost set");
|
||||
}
|
||||
else
|
||||
SendReplyError ("empty outhost");
|
||||
}
|
||||
|
||||
void BOBCommandSession::OutportCommandHandler (const char * operand, size_t len)
|
||||
{
|
||||
LogPrint (eLogDebug, "BOB: outport ", operand);
|
||||
m_OutPort = std::stoi(operand);
|
||||
if (m_OutPort >= 0)
|
||||
SendReplyOK ("outbound port set");
|
||||
if (*operand)
|
||||
{
|
||||
m_OutPort = std::stoi(operand);
|
||||
if (m_OutPort >= 0)
|
||||
SendReplyOK ("outbound port set");
|
||||
else
|
||||
SendReplyError ("port out of range");
|
||||
}
|
||||
else
|
||||
SendReplyError ("port out of range");
|
||||
SendReplyError ("empty outport");
|
||||
}
|
||||
|
||||
void BOBCommandSession::InhostCommandHandler (const char * operand, size_t len)
|
||||
{
|
||||
LogPrint (eLogDebug, "BOB: inhost ", operand);
|
||||
m_InHost = operand;
|
||||
SendReplyOK ("inhost set");
|
||||
if (*operand)
|
||||
{
|
||||
m_InHost = operand;
|
||||
SendReplyOK ("inhost set");
|
||||
}
|
||||
else
|
||||
SendReplyError ("empty inhost");
|
||||
}
|
||||
|
||||
void BOBCommandSession::InportCommandHandler (const char * operand, size_t len)
|
||||
{
|
||||
LogPrint (eLogDebug, "BOB: inport ", operand);
|
||||
m_InPort = std::stoi(operand);
|
||||
if (m_InPort >= 0)
|
||||
SendReplyOK ("inbound port set");
|
||||
if (*operand)
|
||||
{
|
||||
m_InPort = std::stoi(operand);
|
||||
if (m_InPort >= 0)
|
||||
SendReplyOK ("inbound port set");
|
||||
else
|
||||
SendReplyError ("port out of range");
|
||||
}
|
||||
else
|
||||
SendReplyError ("port out of range");
|
||||
SendReplyError ("empty inport");
|
||||
}
|
||||
|
||||
void BOBCommandSession::QuietCommandHandler (const char * operand, size_t len)
|
||||
|
@ -613,52 +652,62 @@ namespace client
|
|||
void BOBCommandSession::LookupCommandHandler (const char * operand, size_t len)
|
||||
{
|
||||
LogPrint (eLogDebug, "BOB: lookup ", operand);
|
||||
auto addr = context.GetAddressBook ().GetAddress (operand);
|
||||
if (!addr)
|
||||
if (*operand)
|
||||
{
|
||||
SendReplyError ("Address Not found");
|
||||
return;
|
||||
}
|
||||
auto localDestination = m_CurrentDestination ? m_CurrentDestination->GetLocalDestination () : i2p::client::context.GetSharedLocalDestination ();
|
||||
if (addr->IsIdentHash ())
|
||||
{
|
||||
// we might have leaseset already
|
||||
auto leaseSet = localDestination->FindLeaseSet (addr->identHash);
|
||||
if (leaseSet)
|
||||
auto addr = context.GetAddressBook ().GetAddress (operand);
|
||||
if (!addr)
|
||||
{
|
||||
SendReplyOK (leaseSet->GetIdentity ()->ToBase64 ().c_str ());
|
||||
SendReplyError ("Address Not found");
|
||||
return;
|
||||
}
|
||||
}
|
||||
// trying to request
|
||||
auto s = shared_from_this ();
|
||||
auto requstCallback = [s](std::shared_ptr<i2p::data::LeaseSet> ls)
|
||||
auto localDestination = m_CurrentDestination ? m_CurrentDestination->GetLocalDestination () : i2p::client::context.GetSharedLocalDestination ();
|
||||
if (addr->IsIdentHash ())
|
||||
{
|
||||
if (ls)
|
||||
s->SendReplyOK (ls->GetIdentity ()->ToBase64 ().c_str ());
|
||||
else
|
||||
s->SendReplyError ("LeaseSet Not found");
|
||||
};
|
||||
if (addr->IsIdentHash ())
|
||||
localDestination->RequestDestination (addr->identHash, requstCallback);
|
||||
// we might have leaseset already
|
||||
auto leaseSet = localDestination->FindLeaseSet (addr->identHash);
|
||||
if (leaseSet)
|
||||
{
|
||||
SendReplyOK (leaseSet->GetIdentity ()->ToBase64 ().c_str ());
|
||||
return;
|
||||
}
|
||||
}
|
||||
// trying to request
|
||||
auto s = shared_from_this ();
|
||||
auto requstCallback = [s](std::shared_ptr<i2p::data::LeaseSet> ls)
|
||||
{
|
||||
if (ls)
|
||||
s->SendReplyOK (ls->GetIdentity ()->ToBase64 ().c_str ());
|
||||
else
|
||||
s->SendReplyError ("LeaseSet Not found");
|
||||
};
|
||||
if (addr->IsIdentHash ())
|
||||
localDestination->RequestDestination (addr->identHash, requstCallback);
|
||||
else
|
||||
localDestination->RequestDestinationWithEncryptedLeaseSet (addr->blindedPublicKey, requstCallback);
|
||||
}
|
||||
else
|
||||
localDestination->RequestDestinationWithEncryptedLeaseSet (addr->blindedPublicKey, requstCallback);
|
||||
SendReplyError ("empty lookup address");
|
||||
}
|
||||
|
||||
void BOBCommandSession::LookupLocalCommandHandler (const char * operand, size_t len)
|
||||
{
|
||||
LogPrint (eLogDebug, "BOB: lookup local ", operand);
|
||||
auto addr = context.GetAddressBook ().GetAddress (operand);
|
||||
if (!addr)
|
||||
if (*operand)
|
||||
{
|
||||
SendReplyError ("Address Not found");
|
||||
return;
|
||||
auto addr = context.GetAddressBook ().GetAddress (operand);
|
||||
if (!addr)
|
||||
{
|
||||
SendReplyError ("Address Not found");
|
||||
return;
|
||||
}
|
||||
auto ls = i2p::data::netdb.FindLeaseSet (addr->identHash);
|
||||
if (ls)
|
||||
SendReplyOK (ls->GetIdentity ()->ToBase64 ().c_str ());
|
||||
else
|
||||
SendReplyError ("Local LeaseSet Not found");
|
||||
}
|
||||
auto ls = i2p::data::netdb.FindLeaseSet (addr->identHash);
|
||||
if (ls)
|
||||
SendReplyOK (ls->GetIdentity ()->ToBase64 ().c_str ());
|
||||
else
|
||||
SendReplyError ("Local LeaseSet Not found");
|
||||
SendReplyError ("empty lookup address");
|
||||
}
|
||||
|
||||
void BOBCommandSession::ClearCommandHandler (const char * operand, size_t len)
|
||||
|
@ -718,11 +767,11 @@ namespace client
|
|||
std::string statusLine;
|
||||
|
||||
// always prefer destination
|
||||
auto ptr = m_Owner.FindDestination(name);
|
||||
if(ptr != nullptr)
|
||||
auto dest = m_Owner.FindDestination(name);
|
||||
if(dest)
|
||||
{
|
||||
// tunnel destination exists
|
||||
BuildStatusLine(false, ptr, statusLine);
|
||||
BuildStatusLine(false, dest, statusLine);
|
||||
SendReplyOK(statusLine.c_str());
|
||||
}
|
||||
else
|
||||
|
@ -742,7 +791,7 @@ namespace client
|
|||
void BOBCommandSession::HelpCommandHandler (const char * operand, size_t len)
|
||||
{
|
||||
auto helpStrings = m_Owner.GetHelpStrings();
|
||||
if(len == 0)
|
||||
if(!*operand)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "COMMANDS:";
|
||||
|
|
|
@ -163,6 +163,7 @@ namespace client
|
|||
int GetInPort() const { return m_InPort; }
|
||||
int GetOutPort() const { return m_OutPort; }
|
||||
bool GetQuiet() const { return m_Quiet; }
|
||||
bool IsRunning() const { return m_IsRunning; }
|
||||
const i2p::data::PrivateKeys& GetKeys () const { return m_LocalDestination->GetPrivateKeys (); };
|
||||
std::shared_ptr<ClientDestination> GetLocalDestination () const { return m_LocalDestination; };
|
||||
|
||||
|
@ -176,6 +177,7 @@ namespace client
|
|||
std::string m_InHost, m_OutHost;
|
||||
int m_InPort, m_OutPort;
|
||||
bool m_Quiet;
|
||||
bool m_IsRunning;
|
||||
};
|
||||
|
||||
class BOBCommandChannel;
|
||||
|
|
Loading…
Reference in a new issue