Skip to content

Commit

Permalink
Add example to indicate how to use admin port
Browse files Browse the repository at this point in the history
Signed-off-by: hwware <[email protected]>
  • Loading branch information
hwware committed Nov 12, 2024
1 parent 267b6a3 commit 23e662e
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ struct connection {
ConnectionCallbackFunc conn_handler;
ConnectionCallbackFunc write_handler;
ConnectionCallbackFunc read_handler;
int connected_port;
};

#define CONFIG_BINDADDR_MAX 16
Expand Down Expand Up @@ -316,6 +317,9 @@ static inline int connFormatAddr(connection *conn, char *buf, size_t buf_len, in
return -1;
}

if (!remote) {
conn->connected_port = port;
}
return formatAddr(buf, buf_len, ip, port);
}

Expand Down
5 changes: 5 additions & 0 deletions src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -2309,6 +2309,10 @@ int VM_IsModuleNameBusy(const char *name) {
return de != NULL;
}

int VM_GetClientConnectedPort(ValkeyModuleCtx *ctx) {
return getClientConnectedPort(ctx->client);
}

/* Return the current UNIX time in milliseconds. */
mstime_t VM_Milliseconds(void) {
return mstime();
Expand Down Expand Up @@ -13553,6 +13557,7 @@ void moduleRegisterCoreAPI(void) {
REGISTER_API(Strdup);
REGISTER_API(CreateCommand);
REGISTER_API(GetCommand);
REGISTER_API(GetClientConnectedPort);
REGISTER_API(CreateSubcommand);
REGISTER_API(SetCommandInfo);
REGISTER_API(SetCommandACLCategories);
Expand Down
11 changes: 11 additions & 0 deletions src/networking.c
Original file line number Diff line number Diff line change
Expand Up @@ -3294,6 +3294,16 @@ char *getClientSockname(client *c) {
return c->sockname;
}

int getClientConnectedPort(client *c) {
char sockname[NET_ADDR_STR_LEN] = {0};

if (c->sockname == NULL) {
genClientAddrString(c, sockname, sizeof(sockname), 0);
c->sockname = sdsnew(sockname);
}
return c->conn->connected_port;
}

int isClientConnIpV6(client *c) {
/* The cached client peer id is on the form "[IPv6]:port" for IPv6
* addresses, so we just check for '[' here. */
Expand Down Expand Up @@ -3354,6 +3364,7 @@ sds catClientInfoString(sds s, client *client, int hide_user_data) {
replBufBlock *cur = listNodeValue(client->ref_repl_buf_node);
used_blocks_of_repl_buf = last->id - cur->id + 1;
}

sds ret = sdscatfmt(
s,
FMTARGS(
Expand Down
1 change: 1 addition & 0 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -2845,6 +2845,7 @@ void freeClientReplyValue(void *o);
void *dupClientReplyValue(void *o);
char *getClientPeerId(client *client);
char *getClientSockName(client *client);
int getClientConnectedPort(client *client);
int isClientConnIpV6(client *c);
sds catClientInfoString(sds s, client *client, int hide_user_data);
sds getAllClientsInfoString(int type, int hide_user_data);
Expand Down
2 changes: 2 additions & 0 deletions src/valkeymodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,7 @@ VALKEYMODULE_API int (*ValkeyModule_CreateCommand)(ValkeyModuleCtx *ctx,
int keystep) VALKEYMODULE_ATTR;
VALKEYMODULE_API ValkeyModuleCommand *(*ValkeyModule_GetCommand)(ValkeyModuleCtx *ctx,
const char *name)VALKEYMODULE_ATTR;
VALKEYMODULE_API int (*ValkeyModule_GetClientConnectedPort)(ValkeyModuleCtx *ctx) VALKEYMODULE_ATTR;
VALKEYMODULE_API int (*ValkeyModule_CreateSubcommand)(ValkeyModuleCommand *parent,
const char *name,
ValkeyModuleCmdFunc cmdfunc,
Expand Down Expand Up @@ -1666,6 +1667,7 @@ static int ValkeyModule_Init(ValkeyModuleCtx *ctx, const char *name, int ver, in
VALKEYMODULE_GET_API(Strdup);
VALKEYMODULE_GET_API(CreateCommand);
VALKEYMODULE_GET_API(GetCommand);
VALKEYMODULE_GET_API(GetClientConnectedPort);
VALKEYMODULE_GET_API(CreateSubcommand);
VALKEYMODULE_GET_API(SetCommandInfo);
VALKEYMODULE_GET_API(SetCommandACLCategories);
Expand Down
1 change: 1 addition & 0 deletions tests/modules/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ TEST_MODULES = \
mallocsize.so \
aclcheck.so \
list.so \
adminport.so \
subcommands.so \
reply.so \
cmdintrospection.so \
Expand Down
33 changes: 33 additions & 0 deletions tests/modules/adminport.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "valkeymodule.h"

#include <string.h>

int testadminport_runspecificcommand(ValkeyModuleCtx *ctx,
ValkeyModuleString **argv, int argc) {
VALKEYMODULE_NOT_USED(argv);
VALKEYMODULE_NOT_USED(argc);
int port = ValkeyModule_GetClientConnectedPort(ctx);
if (port == 7001) {
ValkeyModule_ReplyWithSimpleString(ctx, "You can execute this command");
} else {
ValkeyModule_ReplyWithSimpleString(
ctx, "You have no permission to execute this command");
}
return VALKEYMODULE_OK;
}

int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx, ValkeyModuleString **argv,
int argc) {
VALKEYMODULE_NOT_USED(argv);
VALKEYMODULE_NOT_USED(argc);
if (ValkeyModule_Init(ctx, "adminport", 1, VALKEYMODULE_APIVER_1) ==
VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;

if (ValkeyModule_CreateCommand(ctx, "testadminport.runspecificcommand",
testadminport_runspecificcommand, "", 0, 0,
0) == VALKEYMODULE_ERR)
return VALKEYMODULE_ERR;

return VALKEYMODULE_OK;
}

0 comments on commit 23e662e

Please sign in to comment.