summaryrefslogtreecommitdiff
path: root/ThirdParty/luasocket/etc/forward.lua
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-10-20 13:40:34 +0800
committerchai <chaifix@163.com>2021-10-20 13:40:34 +0800
commitff0f488c97fe8b554b909a0057cebc4c860eac8f (patch)
tree4e47262b52ffce7e9cfeaaeeab46371243bcaa78 /ThirdParty/luasocket/etc/forward.lua
parentdde719dd575090b36aaa3ad85bb3cabf33f36c5a (diff)
+luasocket src
Diffstat (limited to 'ThirdParty/luasocket/etc/forward.lua')
-rw-r--r--ThirdParty/luasocket/etc/forward.lua65
1 files changed, 65 insertions, 0 deletions
diff --git a/ThirdParty/luasocket/etc/forward.lua b/ThirdParty/luasocket/etc/forward.lua
new file mode 100644
index 0000000..05ced1a
--- /dev/null
+++ b/ThirdParty/luasocket/etc/forward.lua
@@ -0,0 +1,65 @@
+-- load our favourite library
+local dispatch = require("dispatch")
+local handler = dispatch.newhandler()
+
+-- make sure the user knows how to invoke us
+if #arg < 1 then
+ print("Usage")
+ print(" lua forward.lua <iport:ohost:oport> ...")
+ os.exit(1)
+end
+
+-- function to move data from one socket to the other
+local function move(foo, bar)
+ local live
+ while 1 do
+ local data, error, partial = foo:receive(2048)
+ live = data or error == "timeout"
+ data = data or partial
+ local result, error = bar:send(data)
+ if not live or not result then
+ foo:close()
+ bar:close()
+ break
+ end
+ end
+end
+
+-- for each tunnel, start a new server
+for i, v in ipairs(arg) do
+ -- capture forwarding parameters
+ local _, _, iport, ohost, oport = string.find(v, "([^:]+):([^:]+):([^:]+)")
+ assert(iport, "invalid arguments")
+ -- create our server socket
+ local server = assert(handler.tcp())
+ assert(server:setoption("reuseaddr", true))
+ assert(server:bind("*", iport))
+ assert(server:listen(32))
+ -- handler for the server object loops accepting new connections
+ handler:start(function()
+ while 1 do
+ local client = assert(server:accept())
+ assert(client:settimeout(0))
+ -- for each new connection, start a new client handler
+ handler:start(function()
+ -- handler tries to connect to peer
+ local peer = assert(handler.tcp())
+ assert(peer:settimeout(0))
+ assert(peer:connect(ohost, oport))
+ -- if sucessful, starts a new handler to send data from
+ -- client to peer
+ handler:start(function()
+ move(client, peer)
+ end)
+ -- afte starting new handler, enter in loop sending data from
+ -- peer to client
+ move(peer, client)
+ end)
+ end
+ end)
+end
+
+-- simply loop stepping the server
+while 1 do
+ handler:step()
+end