diff --git a/API.md b/API.md index 3f705f1..cf7f6e3 100644 --- a/API.md +++ b/API.md @@ -263,3 +263,16 @@ neosocksd.invoke([[printf("test rpc")]], "neosocksd.lan:80", "127.0.0.1:1080") **Description** Run Lua code on another neosocksd. This function returns immediately. On failure, the invocation is lost. + + +### _G.NDEBUG + +**Synopsis** + +```Lua +printf("some debug log") +``` + +**Description** + +Will be set to true if the log level allows printing debug logs. The log level depends on command line argument `-s`/`-v`. diff --git a/m.sh b/m.sh index d5dffa3..e25f13a 100755 --- a/m.sh +++ b/m.sh @@ -12,7 +12,7 @@ case "$1" in -DCMAKE_FIND_ROOT_PATH="${SYSROOT}" \ -DCMAKE_EXPORT_COMPILE_COMMANDS=1 \ -S . -B "xbuild" - cmake --build "xbuild" --parallel + nice cmake --build "xbuild" --parallel ls -lh "xbuild/src/neosocksd" ;; "xs") @@ -24,7 +24,7 @@ case "$1" in -DCMAKE_EXPORT_COMPILE_COMMANDS=1 \ -DLINK_STATIC_LIBS=TRUE \ -S . -B "xbuild" - cmake --build "xbuild" --parallel + nice cmake --build "xbuild" --parallel ls -lh "xbuild/src/neosocksd" ;; "r") @@ -33,7 +33,7 @@ case "$1" in -DCMAKE_BUILD_TYPE="Release" \ -DCMAKE_EXPORT_COMPILE_COMMANDS=1 \ -S . -B "build" - cmake --build "build" --parallel + nice cmake --build "build" --parallel ls -lh "build/src/neosocksd" ;; "s") @@ -44,7 +44,7 @@ case "$1" in -DCMAKE_EXPORT_COMPILE_COMMANDS=1 \ -DLINK_STATIC_LIBS=TRUE \ -S . -B "build" - cmake --build "build" --parallel + nice cmake --build "build" --parallel ls -lh "build/src/neosocksd" ;; "p") @@ -53,7 +53,7 @@ case "$1" in -DCMAKE_BUILD_TYPE="RelWithDebInfo" \ -DCMAKE_EXPORT_COMPILE_COMMANDS=1 \ -S . -B "build" - cmake --build "build" --parallel + nice cmake --build "build" --parallel (cd "build/src" && objdump -drwS "neosocksd" >"neosocksd.S") ls -lh "build/src/neosocksd" ;; @@ -65,7 +65,7 @@ case "$1" in -DCMAKE_EXPORT_COMPILE_COMMANDS=1 \ -DPOSIX=1 \ -S . -B "build" - cmake --build "build" --parallel + nice cmake --build "build" --parallel ls -lh "build/src/neosocksd" ;; "clang") @@ -77,7 +77,7 @@ case "$1" in -DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=lld" \ -DCMAKE_EXPORT_COMPILE_COMMANDS=1 \ -S . -B "build" - cmake --build "build" --parallel + nice cmake --build "build" --parallel (cd "build/src" && llvm-objdump -drwS "neosocksd" >"neosocksd.S") ls -lh "build/src/neosocksd" ;; @@ -91,7 +91,7 @@ case "$1" in -DCMAKE_EXPORT_COMPILE_COMMANDS=1 \ -S . -B "build" ln -sf build/compile_commands.json compile_commands.json - cmake --build "build" --parallel + nice cmake --build "build" --parallel # cd "build/src/tests" && ctest ls -lh "build/src/neosocksd" ;; diff --git a/neox.sh b/neox.sh old mode 100644 new mode 100755 index ac64edc..b6dcf17 --- a/neox.sh +++ b/neox.sh @@ -1,30 +1,61 @@ #!/bin/sh set -e -case "$1" in -"-e" | "--invoke") - URI="/ruleset/invoke" - shift - ;; -"-u" | "--update") - URI="/ruleset/update" - shift - ;; -"--gc") - URI="/ruleset/gc" - shift - ;; -*) - echo "usage: $0 -e '_G.route_default = {\"127.0.6.22:1081\", \"127.0.6.2:1081\"}'" - echo " $0 -u @ruleset.lua" - echo " $0 --gc" - exit 1 - ;; -esac -if [ -z "$1" ]; then - set -x - curl -0vX POST "http://127.0.1.1:9080${URI}" -else - set -x - curl -0v "http://127.0.1.1:9080${URI}" \ - --data-binary "$*" -fi +ADDR="127.0.1.1:9080" +METHOD="POST" +CONTENT="" +PROXY="" + +make_content() { + if [ -z "${CONTENT}" ]; then + curl "$@" + else + curl "$@" --data-binary "${CONTENT}" + fi +} + +make_call() { + echo " >>> ${METHOD} ${URI} ${CONTENT}" + if [ -n "${PROXY}" ]; then + make_content -0vX "${METHOD}" -x "socks4a://${PROXY}" "http://neosocksd.lan${URI}" + else + make_content -0vX "${METHOD}" "http://${ADDR}${URI}" + fi + echo +} + +while [ $# -gt 0 ]; do + case "$1" in + "-c" | "--connect") + ADDR="$2" + shift 2 + ;; + "-x" | "--proxy") + PROXY="$2" + shift 2 + ;; + "-e" | "--invoke") + URI="/ruleset/invoke" + CONTENT="$2" + make_call + shift 2 + ;; + "-u" | "--update") + URI="/ruleset/update" + CONTENT="$2" + make_call + shift 2 + ;; + "--gc") + URI="/ruleset/gc" + CONTENT="" + make_call + shift + ;; + *) + echo "usage: $0 -e '_G.route_default = {\"127.0.6.22:1081\", \"127.0.6.2:1081\"}'" + echo " $0 -u @ruleset.lua" + echo " $0 -x 192.168.1.1:1080 -u @ruleset.lua --gc" + exit 1 + ;; + esac +done diff --git a/simple_ruleset.lua b/simple_ruleset.lua index b02fe49..3c273ac 100644 --- a/simple_ruleset.lua +++ b/simple_ruleset.lua @@ -9,6 +9,8 @@ function string:endswith(sub) return string.sub(self, -n) == sub end +_G.MAX_RECENT_EVENTS = 10 + local function event_add(tstamp, msg) local p = _G.recent_events if p and p.msg == msg then @@ -23,7 +25,7 @@ local function event_add(tstamp, msg) next = p } _G.recent_events = p - for i = 1, 10 do + for i = 1, MAX_RECENT_EVENTS do if not p then return end @@ -101,8 +103,9 @@ end -- [[ simple route functions ]] -- local function simple_route(addr) - -- redirect - for _, rule in ipairs(redirect) do + -- check redirect table + local redirtab = _G.redirect or {} + for _, rule in ipairs(redirtab) do local pattern, target = table.unpack(rule) if addr:find(pattern) then return table.unpack(target) @@ -110,25 +113,46 @@ local function simple_route(addr) end local host, port = splithostport(addr) -- check route table - for _, rule in ipairs(route) do - local pattern, route = table.unpack(rule) + local routetab = _G.route or {} + for _, rule in ipairs(routetab) do + local pattern, dest = table.unpack(rule) if host:find(pattern) then - return addr, table.unpack(route) + return addr, table.unpack(dest) end end -- default route - return addr, table.unpack(route_default) + local default = route_default or {} + return addr, table.unpack(default) end local function simple_route6(addr) + -- check redirect table + local redirtab = _G.redirect6 or {} + for _, rule in ipairs(redirtab) do + local pattern, target = table.unpack(rule) + if addr:find(pattern) then + return table.unpack(target) + end + end + local host, port = splithostport(addr) + -- check route table + local routetab = _G.route6 or {} + for _, rule in ipairs(routetab) do + local pattern, dest = table.unpack(rule) + if host:find(pattern) then + return addr, table.unpack(dest) + end + end -- default route - return addr, table.unpack(route_default) + local default = route6_default or route_default or {} + return addr, table.unpack(default) end local function simple_resolve(addr) local host, port = splithostport(addr) host = string.lower(host) -- lookup in hosts table + local hosts = _G.hosts or {} local entry = hosts[host] if entry then return simple_route(string.format("%s:%s", entry, port)) @@ -145,8 +169,9 @@ end -- [[ ruleset callbacks, see API.md for details ]] -- local ruleset = {} -_G.stat_requests = _G.stat_requests or {} _G.num_requests = _G.num_requests or 0 +_G.stat_requests = _G.stat_requests or {} +_G.MAX_STAT_REQUESTS = 60 function ruleset.resolve(addr) num_requests = num_requests + 1 @@ -181,7 +206,7 @@ end function ruleset.tick(now) printf("ruleset.tick: %.03f", now) table.insert(stat_requests, num_requests) - if stat_requests[61] then + if stat_requests[MAX_STAT_REQUESTS + 1] then table.remove(stat_requests, 1) end end @@ -206,7 +231,7 @@ local function render_stats() end for y = 4, 0, -1 do local line = {} - for x = 1, 60 do + for x = 1, MAX_STAT_REQUESTS do if requests[x] and requests[x] > y then table.insert(line, "|") else @@ -217,7 +242,7 @@ local function render_stats() end local card = #requests local line = {} - for x = 1, 60 do + for x = 1, MAX_STAT_REQUESTS do if x < card then table.insert(line, "-") elseif x == card then @@ -237,7 +262,7 @@ function ruleset.stats(dt) table.insert(w, string.format(s, ...)) end local p = recent_events - for i = 1, 10 do + for i = 1, MAX_RECENT_EVENTS do if not p then break end diff --git a/src/config.h.in b/src/config.h.in index 0bf62c1..31e4137 100644 --- a/src/config.h.in +++ b/src/config.h.in @@ -5,7 +5,7 @@ #define PROJECT_VER "@PROJECT_VERSION_STRING@" #define PROJECT_HOMEPAGE "@PROJECT_HOMEPAGE_URL@" -#cmakedefine01 HAVE_IP_TRANSPARENT +//#cmakedefine01 HAVE_IP_TRANSPARENT #cmakedefine01 HAVE_SO_BINDTODEVICE #cmakedefine01 HAVE_SO_REUSEPORT diff --git a/src/forward.c b/src/forward.c index 884e7e1..64c3830 100644 --- a/src/forward.c +++ b/src/forward.c @@ -204,6 +204,7 @@ static void forward_start( forward_free(ctx); return; } +#if WITH_TPROXY } else if (conf->transparent) { if (getsockname(ctx->accepted_fd, &addr.sa, &len) != 0) { const int err = errno; @@ -212,6 +213,7 @@ static void forward_start( forward_free(ctx); return; } +#endif } else { FAIL(); } diff --git a/src/main.c b/src/main.c index d3892b5..8a5f756 100644 --- a/src/main.c +++ b/src/main.c @@ -77,9 +77,10 @@ static void print_usage(const char *argv0) " -r, --ruleset load ruleset from Lua file\n" " --api RESTful API for monitoring\n" " -t, --timeout Maximum time in seconds that a whole request can take (default: 60.0)\n" - " -u, --user switch to the specified limited user, e.g. nobody\n" - " -v, --verbose increase verbosity\n" - " -s, --silence decrease verbosity\n" + " -u, --user switch to the specified limited user, e.g. \"nobody\"\n" + " -v, --verbose increase logging verbosity, can be specified more than once\n" + " e.g. \"-v -v\" prints verbose messages\n" + " -s, --silence decrease logging verbosity\n" "\n" "example:\n" " neosocksd -l 0.0.0.0:1080 # start a SOCKS 4/4a/5 server\n" @@ -266,8 +267,12 @@ int main(int argc, char **argv) } serve_fn serve_cb = socks_serve; - if (args.forward != NULL || args.tproxy) { + if (args.forward != NULL) { serve_cb = forward_serve; +#if WITH_TPROXY + } else if (args.tproxy) { + serve_cb = forward_serve; +#endif } else if (args.http) { serve_cb = http_proxy_serve; }