From 8c535a0198c132928fb87e1cf949adaeb5fcc60c Mon Sep 17 00:00:00 2001 From: Paul Kulchenko Date: Sun, 31 Oct 2021 17:10:43 -0700 Subject: [PATCH] Add redbean Lua IsLoopbackClient function --- tool/net/help.txt | 4 ++++ tool/net/redbean.c | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/tool/net/help.txt b/tool/net/help.txt index 3bc82887b..9c21645e6 100644 --- a/tool/net/help.txt +++ b/tool/net/help.txt @@ -761,6 +761,10 @@ FUNCTIONS Returns true if IP address is part of a private network (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16). + IsLoopbackClient() → bool + Returns true if the client IP address (returned by GetRemoteAddr) + is part of the localhost network (127.0.0.0/8). + IsLoopbackIp(uint32) → bool Returns true if IP address is part of the localhost network (127.0.0.0/8). diff --git a/tool/net/redbean.c b/tool/net/redbean.c index 583e54f7a..54dd1269b 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -3980,6 +3980,12 @@ static int LuaIsLoopbackIp(lua_State *L) { return LuaIsIp(L, IsLoopbackIp); } +static int LuaIsLoopbackClient(lua_State *L) { + OnlyCallDuringRequest(L, "IsLoopbackClient"); + lua_pushboolean(L, IsLoopbackClient()); + return 1; +} + static int LuaCategorizeIp(lua_State *L) { lua_pushstring(L, GetIpCategoryName(CategorizeIp(luaL_checkinteger(L, 1)))); return 1; @@ -5369,6 +5375,7 @@ static const luaL_Reg kLuaFuncs[] = { {"IsCompressed", LuaIsCompressed}, // {"IsDaemon", LuaIsDaemon}, // {"IsHiddenPath", LuaIsHiddenPath}, // + {"IsLoopbackClient", LuaIsLoopbackClient}, // {"IsLoopbackIp", LuaIsLoopbackIp}, // {"IsPrivateIp", LuaIsPrivateIp}, // {"IsPublicIp", LuaIsPublicIp}, //