Skip to content

Commit

Permalink
forward: remove tproxy
Browse files Browse the repository at this point in the history
Signed-off-by: hexian000 <[email protected]>
  • Loading branch information
hexian000 committed Apr 22, 2023
1 parent 9213d07 commit cd86fe6
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 54 deletions.
13 changes: 13 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
16 changes: 8 additions & 8 deletions m.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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"
;;
Expand All @@ -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")
Expand All @@ -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"
;;
Expand All @@ -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"
;;
Expand Down
87 changes: 59 additions & 28 deletions neox.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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
51 changes: 38 additions & 13 deletions simple_ruleset.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -101,34 +103,56 @@ 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)
end
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))
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions src/forward.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -212,6 +213,7 @@ static void forward_start(
forward_free(ctx);
return;
}
#endif
} else {
FAIL();
}
Expand Down
13 changes: 9 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ static void print_usage(const char *argv0)
" -r, --ruleset <file> load ruleset from Lua file\n"
" --api <bind_address> RESTful API for monitoring\n"
" -t, --timeout <seconds> Maximum time in seconds that a whole request can take (default: 60.0)\n"
" -u, --user <name> switch to the specified limited user, e.g. nobody\n"
" -v, --verbose increase verbosity\n"
" -s, --silence decrease verbosity\n"
" -u, --user <name> 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"
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit cd86fe6

Please sign in to comment.