1
0
mirror of https://github.com/lxsang/antd-lua-plugin synced 2025-07-16 13:59:46 +02:00

10 Commits

Author SHA1 Message Date
1ed346bcfc fix: user correct path to find module
All checks were successful
gitea-sync/antd-lua-plugin/pipeline/head This commit looks good
2023-01-25 22:38:03 +01:00
b0812d1e14 update to support new version of httpd
All checks were successful
gitea-sync/antd-lua-plugin/pipeline/head This commit looks good
2023-01-09 19:54:51 +01:00
22f62fcf77 fix: SSL socket polling bug
All checks were successful
gitea-sync/antd-lua-plugin/pipeline/head This commit looks good
2022-10-21 12:04:41 +02:00
243e778ac7 sqlite module: infer database path from parameter
All checks were successful
gitea-sync/antd-lua-plugin/pipeline/head This commit looks good
More detail on issue #4
2022-10-03 18:30:28 +02:00
b69d98d205 Update Jenkinsfile
All checks were successful
gitea-sync/antd-lua-plugin/pipeline/head This commit looks good
2022-10-03 09:50:35 +02:00
f19bd01847 Use older toolchain
Some checks failed
gitea-sync/antd-lua-plugin/pipeline/head There was a failure building this commit
2022-09-30 11:58:43 +02:00
869f13129a fix logging API bug
All checks were successful
gitea-sync/antd-lua-plugin/pipeline/head This commit looks good
2022-09-15 11:55:20 +02:00
7a1c58009b add random salt string generation in core API 2022-09-02 20:10:35 +02:00
e6004d8ccd improvement and add more lua C wrappers 2022-08-30 16:31:45 +02:00
b83beb5a16 update 2022-08-27 22:36:22 +02:00
7 changed files with 257 additions and 62 deletions

View File

@ -1,4 +1,4 @@
math.randomseed(os.clock())
package.cpath = __api__.apiroot..'/?.so'
require("antd")
std = modules.std()
@ -76,11 +76,11 @@ if HEADER["User-Agent"] and HEADER["User-Agent"]:match("Mobi") then
end
function LOG_INFO(fmt,...)
ulib.syslog(5,string.format(fmt or "LOG", table.unpack({...}) or ""))
ulib.syslog(5,string.format(fmt or "LOG",...))
end
function LOG_ERROR(fmt,...)
ulib.syslog(3,string.format(fmt or "ERROR", table.unpack({...}) or ""))
ulib.syslog(3,string.format(fmt or "ERROR",...))
end
function has_module(m)
@ -231,6 +231,8 @@ if code ~= 0 then
return
end
-- LOG_INFO(JSON.encode(REQUEST))
-- OOP support
--require("OOP")
-- load sqlite helper
@ -240,17 +242,18 @@ end
-- run the file
local m, s, p = has_module(HTTP_REQUEST.request.RESOURCE_PATH)
local m, s, p = has_module(HTTP_REQUEST.request.REQUEST_URI)
if m then
-- run the correct module
if s then
local r,e = loadscript(p)
if r then r() else unknow(e) end
else
LOG_INFO("RUNNING MODULE %s", p)
require(p)
end
else
unknow("Resource not found for request "..HTTP_REQUEST.request.RESOURCE_PATH)
unknow("Resource not found for request "..HTTP_REQUEST.request.REQUEST_URI)
end

View File

@ -4,8 +4,15 @@ array = modules.array()
modules.sqlite = function()
if not sqlite then
sqlite = require("sqlitedb")
sqlite.getdb = function(s)
return sqlite._getdb(__api__.dbpath.."/"..s..".db")
sqlite.getdb = function(name)
if name:find("%.db$") then
return sqlite._getdb(name)
elseif name:find("/") then
LOG_ERROR("Invalid database name %s", name)
return nil
else
return sqlite._getdb(__api__.dbpath.."/"..name..".db")
end
end
end
return sqlite

View File

@ -145,4 +145,17 @@ end
function firstToUpper(str)
return (str:gsub("^%l", string.upper))
end
local charset = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890"
function utils.generate_salt(length)
local ret = {}
local r
for i = 1, length do
r = math.random(1, #charset)
table.insert(ret, charset:sub(r, r))
end
return table.concat(ret)
end

17
Jenkinsfile vendored
View File

@ -9,7 +9,7 @@ def build_plugin()
aclocal
autoconf
automake --add-missing
search_path=$(realpath $WORKSPACE/../ant-http/build/$arch/usr)
search_path=$(realpath antd/build/$arch/usr)
CFLAGS="-I$search_path/include" LDFLAGS="-L$search_path/lib" ./configure --prefix=/opt/www
CFLAGS="-I$search_path/include" LDFLAGS="-L$search_path/lib" make
DESTDIR=$WORKSPACE/build/$arch make install
@ -36,11 +36,16 @@ pipeline{
}
stages
{
stage('Prepare dependencies')
{
steps {
copyArtifacts(projectName: 'gitea-sync/ant-http/master', target: 'antd');
}
}
stage('Build AMD64') {
agent {
docker {
image 'xsangle/ci-tools:latest-amd64'
args '-v /var/jenkins_home/workspace/ant-http:/var/jenkins_home/workspace/ant-http'
image 'xsangle/ci-tools:bionic-amd64'
// Run the container on the node specified at the
// top-level of the Pipeline, in the same workspace,
// rather than on a new node entirely:
@ -58,8 +63,7 @@ pipeline{
stage('Build ARM64') {
agent {
docker {
image 'xsangle/ci-tools:latest-arm64'
args '-v /var/jenkins_home/workspace/ant-http:/var/jenkins_home/workspace/ant-http'
image 'xsangle/ci-tools:bionic-arm64'
// Run the container on the node specified at the
// top-level of the Pipeline, in the same workspace,
// rather than on a new node entirely:
@ -77,8 +81,7 @@ pipeline{
stage('Build ARM') {
agent {
docker {
image 'xsangle/ci-tools:latest-arm'
args '-v /var/jenkins_home/workspace/ant-http:/var/jenkins_home/workspace/ant-http'
image 'xsangle/ci-tools:bionic-arm'
// Run the container on the node specified at the
// top-level of the Pipeline, in the same workspace,
// rather than on a new node entirely:

View File

@ -1,5 +1,15 @@
#include <antd/ws.h>
#include <antd/base64.h>
#include <poll.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <time.h>
#include <fcntl.h>
#include "../lualib.h"
// add a length field, and
void lua_new_byte_array(lua_State *L, int n)
@ -724,6 +734,148 @@ static int l_is_dir(lua_State *L)
return 1;
}
static int l_pfork(lua_State* L)
{
antd_client_t* client = (antd_client_t*)lua_touserdata(L,1);
int sock = client->sock;
// create domain socket
struct sockaddr_un address;
address.sun_family = AF_UNIX;
(void)snprintf(address.sun_path, sizeof(address.sun_path), "/tmp/antd_pfork_%d.sock",sock);
unlink(address.sun_path);
int pfd = socket(AF_UNIX, SOCK_STREAM, 0);
if(pfd == -1)
{
ERROR( "Unable to create Unix domain socket: %s", strerror(errno));
lua_pushnumber(L, -1);
return 1;
}
if (bind(pfd, (struct sockaddr *)(&address), sizeof(address)) == -1)
{
ERROR("Unable to bind name: %s to a socket: %s", address.sun_path, strerror(errno));
lua_pushnumber(L, -1);
return 1;
}
// mark the socket as passive mode
if (listen(pfd, 10) == -1)
{
ERROR("Unable to listen to socket: %d (%s): %s", pfd, address.sun_path, strerror(errno));
lua_pushnumber(L, -1);
close(pfd);
return 1;
}
LOG( "Socket %s is created successfully", address.sun_path);
set_nonblock(pfd);
int pid = fork();
if(pid == 0)
{
int child = socket(AF_UNIX, SOCK_STREAM, 0);
if(child == -1)
{
ERROR( "Unable to create Unix domain socket: %s", strerror(errno));
lua_pushnumber(L, -1);
return -1;
}
if(connect(child, (struct sockaddr*)(&address), sizeof(address)) == -1)
{
ERROR( "Unable to connect to socket '%s': %s", address.sun_path, strerror(errno));
lua_pushnumber(L, -1);
close(child);
return -1;
}
client->sock = child;
client->z_level = ANTD_CNONE;
lua_pushnumber(L, 0);
}
else
{
int ret, status;
struct pollfd pfds[2];
pfds[0].fd = pfd;
pfds[0].events = POLLIN;
ret = poll(pfds, 1, -1);
if(ret < 0 || (pfds[0].revents & (POLLERR | POLLHUP)))
{
ERROR("Unable to wait for child process");
lua_pushnumber(L, -1);
close(pfd);
return 1;
}
// now wait for child process
int cfd = accept(pfd, NULL, NULL);
if(cfd == -1)
{
ERROR("Unable to connect to child process");
lua_pushnumber(L, -1);
close(pfd);
return 1;
}
LOG("Child process %d is accessible from %d", pid, cfd);
set_nonblock(cfd);
uint8_t buff[BUFFLEN];
pfds[0].fd = sock;
pfds[0].events = POLLIN;
pfds[1].fd = cfd;
pfds[1].events = POLLIN;
memset(buff, 0, sizeof(buff));
while((ret = poll(pfds, 2, 200)) != -1)
{
if(waitpid(pid, &status, WNOHANG) != 0)
{
break;
}
if(ret == 0)
{
continue;
}
if(pfds[0].revents & POLLIN)
{
ret = read(client->sock,buff, BUFFLEN);
if(ret <= 0)
{
LOG("antd_recv_upto() on %d: %s",sock, strerror(errno));
break;
}
// write data to the other side
if(write(cfd,buff, ret) != ret)
{
ERROR("Error on send(): %s", strerror(errno));
break;
}
}
else if (pfds[0].revents &(POLLERR | POLLHUP))
{
break;
}
if(pfds[1].revents & POLLIN)
{
ret = read(cfd, buff, sizeof(buff));
if(ret <= 0)
{
ERROR("error read() on %d: %s",cfd, strerror(errno));
break;
}
if(antd_send(client,buff, ret) != ret)
{
ERROR("Error atnd_send(): %s", strerror(errno));
break;
}
}
else if (pfds[1].revents &(POLLERR | POLLHUP))
{
break;
}
}
close(cfd);
}
close(pfd);
unlink(address.sun_path);
lua_pushnumber(L, pid);
return 1;
}
static int l_std_error(lua_State *L)
{
void *client = lua_touserdata(L, 1);
@ -804,6 +956,7 @@ static const struct luaL_Reg standard[] = {
{"ws_b", l_ws_bin},
{"ws_close", l_ws_close},
{"is_dir", l_is_dir},
{"pfork", l_pfork},
{NULL, NULL}};
int luaopen_std(lua_State *L)

View File

@ -37,7 +37,7 @@ static int l_check_login (lua_State *L) {
if (pwd == NULL)
{
lua_pushboolean(L,0);
printf("Cannot find pwd record of %s\n", username );
ERROR("Cannot find pwd record of %s", username );
return 1;
}
spwd = getspnam(username);
@ -63,25 +63,23 @@ static int l_check_login (lua_State *L) {
if (encrypted == NULL)
{
lua_pushboolean(L,0);
printf("Cant crypt \n" );
ERROR("Cant crypt %s", strerror(errno) );
return 1;
}
if(strcmp(encrypted, pwd->pw_passwd) == 0)
{
lua_pushboolean(L,1);
printf("%s\n","Successful login" );
return 1;
} else
{
lua_pushboolean(L,0);
printf("Password incorrect \n" );
return 1;
}
#else
// macos
// just pass the check, for test only
lua_pushboolean(L,1);
printf("%s\n","Successful login" );
lua_pushboolean(L,0);
ERROR("Login by shadow passd is not supported on this system");
return 1;
#endif
}
@ -117,6 +115,7 @@ static int l_fork(lua_State* L)
lua_pushnumber(L, pid);
return 1;
}
static int l_waitpid(lua_State* L)
{
int pid = luaL_checknumber(L,1);
@ -138,7 +137,7 @@ static int l_kill(lua_State* L)
{
int pid = luaL_checknumber(L,1);
if(pid == -1) pid = getpid();
int status = kill(pid, SIGKILL);
int status = kill(pid, SIGHUP);
lua_pushnumber(L, status);
return 1;
}
@ -149,7 +148,7 @@ static int l_setuid(lua_State* L)
{
if(setuid(uid) < 0)
{
printf("UID set problem: %s\n", strerror(errno));
ERROR("UID set problem: %s", strerror(errno));
lua_pushboolean(L,0);
}
else
@ -169,7 +168,7 @@ static int l_setgid(lua_State* L)
{
if(setgid(gid) < 0)
{
printf("GID set problem: %s\n", strerror(errno));
ERROR("GID set problem: %s", strerror(errno));
lua_pushboolean(L,0);
}
else
@ -214,12 +213,12 @@ static int l_getuid(lua_State* L)
/* Retrieve group list */
groups = malloc(ngroups * sizeof (gid_t));
if (groups == NULL) {
LOG("malloc eror \n");
LOG("malloc eror");
return 1;
}
if (getgrouplist(name, gid, groups, &ngroups) == -1) {
free(groups);
LOG("getgrouplist() returned -1; ngroups = %d\n", ngroups);
LOG("getgrouplist() returned -1; ngroups = %d", ngroups);
return 1;
}
/* retrieved groups, along with group names */

View File

@ -12,8 +12,8 @@
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <time.h>
#include <poll.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/time.h>
@ -25,7 +25,6 @@
#define MAX_SOCK_NAME 64
#define SOCKET_NAME "lua.sock"
#define MAX_SESSION_TIMEOUT (15u * 60u) //15 min
#define PING_INTERVAL 10u // 10s
#define PROCESS_TIMEOUT 200u //100ms
@ -72,12 +71,14 @@ static int mk_socket()
if (bind(fd, (struct sockaddr *)(&address), sizeof(address)) == -1)
{
ERROR("Unable to bind name: %s to a socket: %s", address.sun_path, strerror(errno));
close(fd);
return -1;
}
// mark the socket as passive mode
if (listen(fd, 500) == -1)
{
ERROR("Unable to listen to socket: %d (%s): %s", fd, sock_path, strerror(errno));
close(fd);
return -1;
}
LOG("Socket %s is created successfully: %d", sock_path, fd);
@ -123,25 +124,25 @@ static void lua_serve()
ERROR("Unable to set reuse address on %d - setsockopt: %s", socket, strerror(errno));
}
LOG("LUA server online");
/*set log level*/
const char * enable_debug = getenv("ANTD_DEBUG");
int log_level = LOG_ERR;
if(enable_debug)
{
if(atoi(enable_debug))
{
LOG("LUA Debug is enabled");
log_level = LOG_NOTICE;
}
}
setlogmask(LOG_UPTO(log_level));
/*set log level*/
const char * enable_debug = getenv("ANTD_DEBUG");
int log_level = LOG_ERR;
if(enable_debug)
{
if(atoi(enable_debug))
{
LOG("LUA Debug is enabled");
log_level = LOG_NOTICE;
}
}
setlogmask(LOG_UPTO(log_level));
while((fd = accept(socket, NULL, NULL)) > 0)
{
pthread_t thread;
lua_thread_data_t* data = (lua_thread_data_t*)malloc(sizeof(lua_thread_data_t));
data->__plugin__ = &__plugin__;
data->fd = fd;
set_nonblock(fd);
//set_nonblock(fd);
if (pthread_create(&thread, NULL, (void *(*)(void*))handle_fn, (void *)data) != 0)
{
ERROR("pthread_create: cannot create lua thread: %s", strerror(errno));
@ -168,7 +169,7 @@ static void lua_serve()
void init()
{
(void)snprintf(sock_path, sizeof(sock_path), "%s/%s", __plugin__.tmpdir, SOCKET_NAME);
(void)snprintf(sock_path, sizeof(sock_path), "%s/%s", __plugin__.tmpdir, SOCKET_NAME);
LOG("Lua socket will be stored in %s", sock_path);
pid = fork();
if (pid == 0)
@ -203,37 +204,39 @@ static void push_dict_to_socket(antd_client_t* cl, char* name, char* parent_name
static void *process(void *data)
{
fd_set fd_in;
antd_request_t *rq = (antd_request_t *)data;
antd_client_t* cl = (antd_client_t* ) dvalue(rq->request, "LUA_CL_DATA");
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = PROCESS_TIMEOUT;
FD_ZERO(&fd_in);
FD_SET(rq->client->sock, &fd_in);
FD_SET(cl->sock, &fd_in);
int max_fdm = rq->client->sock > cl->sock ? rq->client->sock : cl->sock;
int rc = select(max_fdm + 1, &fd_in, NULL, NULL, &timeout);
struct pollfd pfds[2];
pfds[0].fd = rq->client->sock;
pfds[0].events = POLLIN;
if(rq->client->ssl)
{
pfds[0].events = POLLIN | POLLOUT;
}
pfds[1].fd = cl->sock;
pfds[1].events = POLLIN;
int rc = poll(pfds, 2, PROCESS_TIMEOUT);
antd_task_t* task;
uint8_t buff[BUFFLEN];
int ret;
switch (rc)
{
case -1:
ERROR("Error on select(): %s", strerror(errno));
ERROR("Error on poll(): %s", strerror(errno));
antd_close(cl);
dput(rq->request, "LUA_CL_DATA", NULL);
dput(rq->request, "LUA_CL_DATA", NULL);
return antd_create_task(NULL, data, NULL, rq->client->last_io);
case 0:
// time out
task = antd_create_task(process, (void *)rq, NULL, time(NULL));
//antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_WRITABLE | TASK_EVT_ON_READABLE);
//antd_task_bind_event(task, cl->sock, 0, TASK_EVT_ON_WRITABLE | TASK_EVT_ON_READABLE);
antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_WRITABLE | TASK_EVT_ON_READABLE);
antd_task_bind_event(task, cl->sock, 0, TASK_EVT_ON_READABLE);
return task;
// we have data
default:
// If data is on webserver
if (FD_ISSET(rq->client->sock, &fd_in))
if ((pfds[0].revents & POLLIN) || (rq->client->ssl && (pfds[0].revents & POLLOUT)) )
{
while((ret = antd_recv_upto(rq->client,buff, BUFFLEN)) > 0)
{
@ -242,7 +245,7 @@ static void *process(void *data)
{
ERROR("Error on atnd_send(): %s", strerror(errno));
antd_close(cl);
dput(rq->request, "LUA_CL_DATA", NULL);
dput(rq->request, "LUA_CL_DATA", NULL);
return antd_create_task(NULL, data, NULL, rq->client->last_io);
}
}
@ -250,11 +253,18 @@ static void *process(void *data)
{
LOG("antd_recv_upto() on %d: %s",rq->client->sock, strerror(errno));
antd_close(cl);
dput(rq->request, "LUA_CL_DATA", NULL);
dput(rq->request, "LUA_CL_DATA", NULL);
return antd_create_task(NULL, data, NULL, rq->client->last_io);
}
}
else if(FD_ISSET(cl->sock, &fd_in))
else if(pfds[0].revents &(POLLERR | POLLHUP))
{
ERROR("POLLERR or POLLHUP received on %d", rq->client->sock);
antd_close(cl);
dput(rq->request, "LUA_CL_DATA", NULL);
return antd_create_task(NULL, data, NULL, rq->client->last_io);
}
if(pfds[1].revents & POLLIN)
{
while((ret = antd_recv_upto(cl,buff, BUFFLEN)) > 0)
{
@ -263,7 +273,7 @@ static void *process(void *data)
{
ERROR("Error atnd_send(): %s", strerror(errno));
antd_close(cl);
dput(rq->request, "LUA_CL_DATA", NULL);
dput(rq->request, "LUA_CL_DATA", NULL);
return antd_create_task(NULL, data, NULL, rq->client->last_io);
}
}
@ -271,13 +281,20 @@ static void *process(void *data)
{
LOG("antd_recv_upto() on %d: %s", cl->sock, strerror(errno));
antd_close(cl);
dput(rq->request, "LUA_CL_DATA", NULL);
dput(rq->request, "LUA_CL_DATA", NULL);
return antd_create_task(NULL, data, NULL, rq->client->last_io);
}
}
else if(pfds[1].revents &(POLLERR | POLLHUP))
{
ERROR("POLLERR or POLLHUP received on %d", cl->sock);
antd_close(cl);
dput(rq->request, "LUA_CL_DATA", NULL);
return antd_create_task(NULL, data, NULL, rq->client->last_io);
}
task = antd_create_task(process, (void *)rq, NULL, time(NULL));
antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_WRITABLE | TASK_EVT_ON_READABLE);
antd_task_bind_event(task, cl->sock, 0, TASK_EVT_ON_WRITABLE | TASK_EVT_ON_READABLE);
antd_task_bind_event(task, cl->sock, 0, TASK_EVT_ON_READABLE);
return task;
}
}
@ -304,7 +321,7 @@ void* handle(void* data)
cl->z_status = 0;
cl->z_level = ANTD_CNONE;
cl->zstream = NULL;
rq->client->z_level = ANTD_CNONE;
rq->client->z_level = ANTD_CNONE;
push_dict_to_socket(cl, "request","HTTP_REQUEST", rq->request);
antd_send(cl,"\r\n", 2);
dput(rq->request, "LUA_CL_DATA", cl);