diff --git a/docs/changes.txt b/docs/changes.txt index e54980a..a90eca7 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -143,3 +143,4 @@ v0.8.0 * zapret-lib: tls dissector/reconstructor * zapret-antidpi: tls_client_hello_clone * zapret-antidpi: "optional" arg to blob taking functions +* nfqws2: support gzipped lua file. auto use script.lua.gz diff --git a/nfq2/gzip.c b/nfq2/gzip.c index a3e4eb7..6e4805d 100644 --- a/nfq2/gzip.c +++ b/nfq2/gzip.c @@ -7,7 +7,7 @@ #define BUFMIN 128 #define BUFCHUNK (1024*128) -int z_readfile(FILE *F, char **buf, size_t *size) +int z_readfile(FILE *F, char **buf, size_t *size, size_t extra_alloc) { z_stream zs; int r; @@ -38,7 +38,7 @@ int z_readfile(FILE *F, char **buf, size_t *size) if ((bufsize - *size) < BUFMIN) { bufsize += BUFCHUNK; - newbuf = *buf ? realloc(*buf, bufsize) : malloc(bufsize); + newbuf = *buf ? realloc(*buf, bufsize + extra_alloc) : malloc(bufsize + extra_alloc); if (!newbuf) { r = Z_MEM_ERROR; @@ -57,7 +57,7 @@ int z_readfile(FILE *F, char **buf, size_t *size) if (*size < bufsize) { // free extra space - if ((newbuf = realloc(*buf, *size))) *buf = newbuf; + if ((newbuf = realloc(*buf, *size + extra_alloc))) *buf = newbuf; } inflateEnd(&zs); diff --git a/nfq2/gzip.h b/nfq2/gzip.h index 15e30d2..541ba1a 100644 --- a/nfq2/gzip.h +++ b/nfq2/gzip.h @@ -4,5 +4,5 @@ #include #include -int z_readfile(FILE *F,char **buf,size_t *size); +int z_readfile(FILE *F, char **buf, size_t *size, size_t extra_alloc); bool is_gzip(FILE* F); diff --git a/nfq2/hostlist.c b/nfq2/hostlist.c index 7a72be2..e514e09 100644 --- a/nfq2/hostlist.c +++ b/nfq2/hostlist.c @@ -61,7 +61,7 @@ bool AppendHostList(hostlist_pool **hostlist, const char *filename) if (is_gzip(F)) { - r = z_readfile(F,&zbuf,&zsize); + r = z_readfile(F,&zbuf,&zsize,0); fclose(F); if (r==Z_OK) { diff --git a/nfq2/ipset.c b/nfq2/ipset.c index f8fd516..679724e 100644 --- a/nfq2/ipset.c +++ b/nfq2/ipset.c @@ -76,7 +76,7 @@ static bool AppendIpset(ipset *ips, const char *filename) if (is_gzip(F)) { - r = z_readfile(F,&zbuf,&zsize); + r = z_readfile(F,&zbuf,&zsize,0); fclose(F); if (r==Z_OK) { diff --git a/nfq2/lua.c b/nfq2/lua.c index bfac2ed..0e8fc6a 100644 --- a/nfq2/lua.c +++ b/nfq2/lua.c @@ -13,6 +13,7 @@ #include "lua.h" #include "params.h" +#include "gzip.h" #include "helpers.h" #include "conntrack.h" #include "crypto/sha.h" @@ -2899,17 +2900,43 @@ static bool lua_desync_functions_exist() return true; } +static bool lua_file_open_test(const char *filename, bool *b_gzip, char *fname) +{ + FILE *F = fopen(filename,"rb"); + if (F) + { + if (fname) snprintf(fname,PATH_MAX,"%s",filename); + } + else + { + size_t l = strlen(filename); + char *fngz = malloc(l+4); + if (!fngz) return false; + memcpy(fngz, filename, l); + memcpy(fngz+l,".gz",4); + if (fname) snprintf(fname,PATH_MAX,"%s",fngz); + F = fopen(fngz,"rb"); + free(fngz); + } + if (F) + { + if (b_gzip) *b_gzip = is_gzip(F); + fclose(F); + } + return !!F; +} + bool lua_test_init_script_files(void) { struct str_list *str; LIST_FOREACH(str, ¶ms.lua_init_scripts, next) { - if (str->str[0]=='@' && !file_open_test(str->str+1, O_RDONLY)) + if (str->str[0]=='@' && !lua_file_open_test(str->str+1, NULL, NULL)) { #ifndef __CYGWIN__ int e = errno; #endif - DLOG_ERR("LUA file '%s' not accessible\n", str->str+1); + DLOG_ERR("LUA file '%s' or '%s.gz' not accessible\n", str->str+1, str->str+1); #ifndef __CYGWIN__ if (e==EACCES) DLOG_ERR("I drop my privileges and do not run Lua as root\ncheck file permissions and +x rights on all directories in the path\n"); @@ -2920,6 +2947,33 @@ bool lua_test_init_script_files(void) return true; } +static int luaL_doZfile(lua_State *L, const char *filename) +{ + bool b_gzip; + char fname[PATH_MAX]; + if (!lua_file_open_test(filename, &b_gzip, fname)) + luaL_error(L, "could not open lua file '%s' or '%s.gz'", filename, filename); + if (b_gzip) + { + size_t size; + char *buf; + int r; + FILE *F = fopen(fname, "rb"); + if (!F) + luaL_error(L, "could not open lua file '%s'", fname); + r = z_readfile(F, &buf, &size, 1); + fclose(F); + if (r != Z_OK) + luaL_error(L, "could not unzip lua file '%s'", fname); + buf[size] = 0; + r = luaL_dostring(L, buf); + free(buf); + return r; + } + else + return luaL_dofile(L, filename); +} + static bool lua_init_scripts(void) { struct str_list *str; @@ -2938,7 +2992,7 @@ static bool lua_init_scripts(void) DLOG("LUA RUN STR: %s\n",s); } } - if ((status = str->str[0]=='@' ? luaL_dofile(params.L, str->str+1) : luaL_dostring(params.L, str->str))) + if ((status = str->str[0]=='@' ? luaL_doZfile(params.L, str->str+1) : luaL_dostring(params.L, str->str))) { lua_perror(params.L); return false;