#!/opt/local/bin/lua
local PROXY_BINPATH = os.getenv("PROXY_BINPATH") or "/usr/local/sbin/mysql-proxy"
local MYSQL_HOST = os.getenv("MYSQL_HOST") or "127.0.0.1"
local MYSQL_PORT = os.getenv("MYSQL_PORT") or "3306"
local PROXY_HOST = os.getenv("PROXY_HOST") or "127.0.0.1"
local PROXY_PORT = os.getenv("PROXY_PORT") or "4040"
local ADMIN_PORT = os.getenv("ADMIN_PORT") or "4041"
local PROXY_CHAIN_PORT = os.getenv("PROXY_CHAIN_PORT") or "15070"
local PROXY_MASTER_PORT = os.getenv("PROXY_MASTER_PORT") or "15050"
local PROXY_SLAVE_PORT = os.getenv("PROXY_SLAVE_PORT") or "15060"
local ADMIN_MASTER_PORT = os.getenv("ADMIN_MASTER_PORT") or "15051"
local ADMIN_SLAVE_PORT = os.getenv("ADMIN_SLAVE_PORT") or "15061"
local ADMIN_CHAIN_PORT = os.getenv("ADMIN_CHAIN_PORT") or "15071"
local PROXY_PID_DIRECTORY = os.getenv("PROXY_PID_DIRECTORY") or "/tmp"
local PROXY_PID_FILE = os.getenv("PROXY_PID_FILE") or "proxy1.pid"
local PROXY_CHAIN_PID_FILE = os.getenv("PROXY_CHAIN_PID_FILE") or "proxy_chain1.pid"
local PROXY_MASTER_PID_FILE = os.getenv("PROXY_MASTER_PID_FILE") or "proxy_master1.pid"
local PROXY_SLAVE_PID_FILE = os.getenv("PROXY_SLAVE_PID_FILE") or "proxy_slave1.pid"
local PROXY_STOP_SCRIPT = os.getenv("PROXY_STOP_SCRIPT") or 'proxy_chain_stop'
-- this is the path containing the global Lua modules
local GLOBAL_LUA_PATH = os.getenv('LUA_LDIR') or '/usr/share/lua/5.1/?.lua'
-- this is the path containing the Proxy libraries
local PROXY_LUA_PATH = os.getenv('LUA_PATH') or '/usr/local/share/?.lua'
-- Building the final include path
local INCLUDE_PATH =
GLOBAL_LUA_PATH .. ';' ..
PROXY_LUA_PATH
local proxy_list = {}
---
-- chain_proxy()
--[[
starts two proxy instances, with the first one pointing to the
default backend server, and the second one (with default ports)
is pointing at the first proxy
@param first_lua_script
@param second_lua_script
@param use_replication uses a master proxy as backend
--]]
function chain_proxy (first_lua_script, second_lua_script, use_replication, env_opts)
first_proxy_options = {
["proxy-backend-addresses"] = MYSQL_HOST .. ":" .. MYSQL_PORT,
["proxy-address"] = PROXY_HOST .. ":" .. PROXY_CHAIN_PORT,
["admin-address"] = PROXY_HOST .. ":" .. ADMIN_CHAIN_PORT,
["pid-file"] = PROXY_CHAIN_PID_FILE,
["proxy-lua-script"] = first_lua_script ,
}
--
-- if replication was not started, then it is started here
--
if use_replication and (use_replication == true) then
if (proxy_list['master'] == nil) then
simulate_replication()
end
first_proxy_options["proxy-backend-addresses"] = PROXY_HOST .. ':' .. PROXY_MASTER_PORT
end
second_proxy_options = {
["proxy-backend-addresses"] = MYSQL_HOST .. ":" .. PROXY_CHAIN_PORT ,
["proxy-address"] = PROXY_HOST .. ":" .. PROXY_PORT,
["admin-address"] = PROXY_HOST .. ":" .. ADMIN_PORT,
["pid-file"] = PROXY_PID_FILE,
["proxy-lua-script"] = second_lua_script ,
}
if not env_opts then
env_opts = { }
end
start_proxy('first_proxy', first_proxy_options, env_opts['first'] or '')
start_proxy('second_proxy',second_proxy_options, env_opts['second'] or '' )
end
---
-- simulate_replication()
--[[
creates a fake master/slave by having two proxies
pointing at the same backend
you can alter those backends by changing
the starting parameters
@param master_options options for master
@param slave_options options for slave
--]]
function simulate_replication(master_options, slave_options)
if not master_options then
master_options = default_master_options
end
if not master_options['pid-file'] then
master_options['pid-file'] = PROXY_MASTER_PID_FILE
end
if not slave_options then
slave_options = default_slave_options
end
if not slave_options['pid-file'] then
slave_options['pid-file'] = PROXY_SLAVE_PID_FILE
end
start_proxy('master', master_options)
start_proxy('slave', slave_options)
end
---
-- turn a option-table into a string
--
-- the values are encoded and quoted for the shell
--
-- @param tbl a option table
-- @param sep the seperator, defaults to a space
function options_tostring(tbl, sep)
-- default value for sep
sep = sep or " "
assert(type(tbl) == "table")
assert(type(sep) == "string")
local s = ""
for k, v in pairs(tbl) do
local enc_value = v:gsub("\\", "\\\\"):gsub("\"", "\\\"")
s = s .. "--" .. k .. "=\"" .. enc_value .. "\" "
end
return s
end
---
-- file_exists()
--
-- checks if a file exists
--
-- @param fname the name of the file being checked
function file_exists(fname)
local fh=io.open( fname, 'r')
if fh then
fh:close()
return true
else
return false
end
end
---
-- start_proxy()
--
-- starts an instance of MySQL Proxy
--
-- @param proxy_name internal name of the proxy instance, for retrieval
-- @param proxy_options the options to start the Proxy
function start_proxy(proxy_name, proxy_options, env_opts)
-- start the proxy
assert(type(proxy_options) == 'table')
assert(file_exists(proxy_options['proxy-lua-script']) )
proxy_options['pid-file'] = PROXY_PID_DIRECTORY ..
'/' .. proxy_options['pid-file']
assert(os.execute(
env_opts ..
' LUA_PATH="' ..
INCLUDE_PATH .. '" ' ..
' screen -d -m -S ' ..
proxy_name .. ' ' ..
PROXY_BINPATH .. " " ..
options_tostring( proxy_options) .. " &"
))
proxy_list[proxy_name] = proxy_options
end
--
-- PROGRAM STARTS HERE
--
local lua_scripts = {arg[1], arg[2] }
for i, v in pairs( lua_scripts ) do
if not v:match('.lua$') then
v = v .. '.lua'
end
lua_scripts[i] = v
if not file_exists(v) then
print( "file " .. v .. " does not exist")
os.exit(1)
end
end
chain_proxy(lua_scripts[1], lua_scripts[2]) -- , false, {first = arg[3], second = arg[4]} )
local stop_script_name = PROXY_PID_DIRECTORY .. '/' .. PROXY_STOP_SCRIPT
local fh = assert(io.open(stop_script_name, 'w'), "can't open " .. stop_script_name)
--
-- creates a customized script to stop the chained proxies
-- and close the support 'screen' applications
--
fh:write("#!" .. arg[-1] .. "\n")
fh:write([[
function get_pid(pid_file_name)
local fh = assert(io.open(pid_file_name, 'r'),
"error opening " .. pid_file_name)
local pid = assert(fh:read() ,
"PID not found in " .. pid_file_name)
fh:close()
return pid
end
os.execute('screen -list')
]] )
--
-- prints information about the chained proxies
--
for name,data in pairs(proxy_list) do
print (string.format('proxy started on screen "%s" using script "%s" - pid_file : %s ',
name, data['proxy-lua-script'], data['pid-file']
))
fh:write("os.execute('kill -TERM ' .. get_pid('" .. data['pid-file'] .. "'))\n")
fh:write("os.remove('" .. data['pid-file'] .. "' )\n")
end
fh:write("os.execute('sleep 1')\n")
fh:write("os.execute('screen -list')\n")
fh:close()
os.execute('chmod +x ' .. stop_script_name)
print ('stop script is '.. stop_script_name)
os.execute('screen -list')
One simple way to "chain" proxies is to enter the information for one proxy directly into your browser's proxy configuration setup. Then connect to a web anonymizer which is also a proxy, and from there, to the site you want to view. Anyone trying to trace your actual IP would now have to subpoena the logs from two proxy servers. Download Games website