Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add hello cmd #119

Merged
merged 10 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/base_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ const std::string kCmdNameDebug = "debug";
const std::string kSubCmdNameDebugHelp = "help";
const std::string kSubCmdNameDebugOOM = "oom";
const std::string kSubCmdNameDebugSegfault = "segfault";
const std::string kCmdNameHello = "hello";
const std::string kCmdNameInfo = "info";
const std::string kCmdNameSort = "sort";

Expand Down
3 changes: 2 additions & 1 deletion src/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ int PClient::HandlePacket(std::string&& data) {
}

if (!auth_) {
if (params[0][0] == kCmdNameAuth) {
// auth and hello command can be executed without auth
if (params[0][0] == kCmdNameAuth || params[0][0] == kCmdNameHello) {
auto now = ::time(nullptr);
if (now <= last_auth_ + 1) {
// avoid guess password.
Expand Down
2 changes: 1 addition & 1 deletion src/client_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ class ClientMap {
bool KillClientByAddrPort(const std::string& addr_port);
};

} // namespace kiwi
} // namespace kiwi
98 changes: 97 additions & 1 deletion src/cmd_admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
#include <string>
#include <vector>
#include "cmd_admin.h"
#include "config.h"
#include "db.h"

#include "braft/raft.h"
#include "log.h"
#include "pstd_string.h"
#include "rocksdb/version.h"

Expand All @@ -36,7 +38,6 @@
#include "pstd/env.h"

#include "client_map.h"
#include "cmd_table_manager.h"
#include "slow_log.h"
#include "store.h"

Expand Down Expand Up @@ -161,6 +162,101 @@ bool PingCmd::DoInitial(PClient* client) { return true; }

void PingCmd::DoCmd(PClient* client) { client->SetRes(CmdRes::kPong, "PONG"); }

HelloCmd::HelloCmd(const std::string& name, int16_t arity)
: BaseCmd(name, arity, kCmdFlagsFast, kAclCategoryFast | kAclCategoryConnection) {}

bool HelloCmd::DoInitial(PClient* client) {
size_t argc = client->argv_.size();
int resp_version = 2;

if (!client->GetAuth()) {
authed_ = false;
}

if (argc > 1) {
if (pstd::String2int(client->argv_[1].data(), client->argv_[1].size(), &resp_version) == 0) {
client->SetRes(CmdRes::kErrOther, "Protocol version is not an integer or out of range");
return false;
}
}

if (resp_version != 2) {
client->SetRes(CmdRes::kErrOther, "unsupported protocol version");
return false;
}

return true;
}

void HelloCmd::DoCmd(PClient* client) {
size_t argc = client->argv_.size();
size_t next_arg = 2;

for (; next_arg < argc; next_arg++) {
size_t more_args = argc - next_arg;
const std::string& arg = client->argv_[next_arg];
// TODO(marsevilspirit): support auth acl
// like: hello 2 auth username password
// now only support hello auth password
// do not support username (need acl)
if ((strcasecmp(arg.data(), "SETNAME") == 0) && more_args) {
client->SetName(client->argv_[next_arg + 1]);
next_arg++;
} else if (strcasecmp(arg.data(), "AUTH") == 0 && more_args) {
authed_ = true;
if (client->GetAuth()) {
continue;
}
if (client->argv_[next_arg + 1] != g_config.password) {
client->SetRes(CmdRes::kErrOther, "invalid password");
return;
} else {
client->SetAuth();
}
next_arg++;
} else {
client->SetRes(CmdRes::kSyntaxErr, "Syntax error");
return;
}
}

if (!authed_) {
client->SetRes(CmdRes::kErrOther,
"NOAUTH HELLO must be called with the client already authenticated, \
otherwise the HELLO <proto> AUTH <pass> option can be used to authenticate the client and \
select the RESP protocol version at the same time");
return;
}

Hello(client);
}

void HelloCmd::Hello(PClient* client) {
client->AppendArrayLen(static_cast<int64_t>(12));
client->AppendString("server");
client->AppendString("kiwi");
client->AppendString("version");
client->AppendString(Kkiwi_VERSION);
client->AppendString("proto");
client->AppendInteger(static_cast<int64_t>(2));
client->AppendString("id");
client->AppendInteger(static_cast<int64_t>(client->GetUniqueID()));
client->AppendString("mode");

if (!g_config.use_raft) {
client->AppendString("standalone");
} else {
client->AppendString("cluster");
}

client->AppendString("role");
if (client->GetAuth()) {
client->AppendString("master");
} else {
client->AppendString("slave");
}
}

EchoCmd::EchoCmd(const std::string& name, int16_t arity) : BaseCmd(name, arity, kCmdFlagsFast, kAclCategoryFast) {}

bool EchoCmd::DoInitial(PClient* client) { return true; }
Expand Down
23 changes: 20 additions & 3 deletions src/cmd_admin.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#pragma once

#include <cstdint>
#include <optional>
#include <variant>
#include "base_cmd.h"
Expand Down Expand Up @@ -135,7 +136,7 @@ class CmdClientId : public BaseCmd {

class CmdClientList : public BaseCmd {
private:
enum class Type { DEFAULT, IDLE, ADDR, ID } list_type_;
enum class Type : std::int8_t { DEFAULT, IDLE, ADDR, ID } list_type_;
std::string info_;

public:
Expand All @@ -150,7 +151,7 @@ class CmdClientList : public BaseCmd {

class CmdClientKill : public BaseCmd {
private:
enum class Type { ALL, ADDR, ID } kill_type_;
enum class Type : std::int8_t { ALL, ADDR, ID } kill_type_;

public:
CmdClientKill(const std::string& name, int16_t arity);
Expand Down Expand Up @@ -195,6 +196,22 @@ class PingCmd : public BaseCmd {
void DoCmd(PClient* client) override;
};

class HelloCmd : public BaseCmd {
marsevilspirit marked this conversation as resolved.
Show resolved Hide resolved
public:
HelloCmd(const std::string& name, int16_t arity);

protected:
bool DoInitial(PClient* client) override;

private:
void DoCmd(PClient* client) override;

void Hello(PClient* client);

private:
bool authed_ = true;
};

class EchoCmd : public BaseCmd {
public:
EchoCmd(const std::string& name, int16_t arity);
Expand All @@ -216,7 +233,7 @@ class InfoCmd : public BaseCmd {
private:
void DoCmd(PClient* client) override;

enum InfoSection {
enum InfoSection : std::uint8_t {
kInfoErr = 0x0,
kInfoServer,
kInfoStats,
Expand Down
3 changes: 3 additions & 0 deletions src/cmd_table_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ void CmdTableManager::InitCmdTable() {
ADD_COMMAND(Select, 2);
ADD_COMMAND(Shutdown, 1);

// hello
ADD_COMMAND(Hello, -1);

// info
ADD_COMMAND(Info, -1);

Expand Down
Loading