Skip to content

Instantly share code, notes, and snippets.

@findstr
Last active September 6, 2017 14:27
Show Gist options
  • Save findstr/ade126f92808f2831f5a76c6a766fe02 to your computer and use it in GitHub Desktop.
Save findstr/ade126f92808f2831f5a76c6a766fe02 to your computer and use it in GitHub Desktop.
revert-proxy
daemon = 0
bootstrap = "test/testhttp.lua"
lualib_path = "test/?.lua;lualib/?.lua"
lualib_cpath = "test/?.so;luaclib/?.so"
ctrl="192.168.2.1@8002"
tunnel="192.168.2.1@8003"
local socket = require "socket"
local core = require "silly.core"
local env = require "silly.env"
local function direct(src, dst)
return function()
while true do
local d = socket.read(src, 1)
if not d then
socket.close(dst)
return
end
local ok = socket.write(dst, d)
if not ok then
socket.close(src)
return
end
end
end
end
local function bridge(ip, src)
local fd = socket.connect(ip)
local srcfd = socket.connect(env.get("tunnel"))
assert(fd)
assert(srcfd)
socket.write(srcfd, src)
core.fork(direct(fd, srcfd))
core.fork(direct(srcfd, fd))
end
return function (ip)
local fd = socket.connect(env.get("ctrl"))
print("connect ctrl", fd)
assert(fd)
while true do
local src = socket.readline(fd)
print("new come", src)
if not src then
return
end
bridge(ip, src)
end
end
daemon = 0
bootstrap = "test/proxyd.lua"
lualib_path = "test/?.lua;lualib/?.lua"
lualib_cpath = "luaclib/?.so"
listen = "@8001"
ctrl = "@8002"
tunnel = "@8003"
local socket = require "socket"
local core = require "silly.core"
local env = require "silly.env"
local T = nil
socket.listen(env.get("ctrl"), function(fd, addr)
print("ctrl", fd)
T = fd;
end)
socket.listen(env.get("listen"), function(fd, addr)
assert(T)
local ok = socket.write(T, fd .. "\n")
assert(ok)
end)
local function direct(src, dst)
return function()
while true do
local d = socket.read(src, 1)
if not d then
socket.close(dst)
return
end
local ok = socket.write(dst, d)
if not ok then
socket.close(src)
return
end
end
end
end
socket.listen(env.get("tunnel"), function(fd, addr)
local to = socket.readline(fd)
to = tonumber(to)
core.fork(direct(fd, to))
core.fork(direct(to, fd))
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment