Created
September 24, 2012 01:23
-
-
Save soccermitchy/3773722 to your computer and use it in GitHub Desktop.
LuaProxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--LuaProxy 0.0.1 Development | |
domainTypes={"com","net","org","gov","edu","uk","me","cc"} | |
ipNumberList={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255} | |
customHeaders={ | |
server="", --What the proxy should send to the remote client (eg. a browser) | |
client="User-Agent: LuaProxy/0.0.1Development" --What the proxy should send to the remote srever | |
} | |
operatingSystem="unix" --Unix (this also means unix-like) or windows | |
badURL="<html><head><title>[Proxy] Bad HTTP URL </title></head><body><h1>Invalid URL</h1><br>Please try another URL.<br><hr><br>LuaProxy 0.0.1 built by Mitchell Monahan</body></html>" --What to send on a bad URL | |
badProtocol="<html><head><title>[Proxy] Only HTTP!</title></head><body><h1>Invalid protocol</h1><br>Please, only use HTTP<br><hr><br>LuaProxy 0.0.1 built by Mitchell Moanah</body></html>" --What to send if a bad protocol is used | |
socket=require "socket" | |
function split(str,pat) | |
local t = {} -- NOTE: use {n = 0} in Lua-5.0 | |
local fpat = "(.-)" .. pat | |
local last_end = 1 | |
local s, e, cap = str:find(fpat, 1) | |
while s do | |
if s ~= 1 or cap ~= "" then | |
table.insert(t,cap) | |
end | |
last_end = e+1 | |
s, e, cap = str:find(fpat, last_end) | |
end | |
if last_end <= #str then | |
cap = str:sub(last_end) | |
table.insert(t, cap) | |
end | |
return t | |
end | |
server=socket.bind("*",8090) | |
function clear() | |
if operatingSystem == "unix" then | |
os.execute("clear") | |
elseif operatingSystem == "windows" then | |
os.execute("cls") | |
else | |
print("Unable to clear terminal. Check variable operatingSystem (Current value: "..operatingSystem..".)") | |
end | |
end | |
while true do | |
clear() | |
print("Ready") | |
client=server:accept() | |
clear() | |
print("Proxying...") | |
data=client:receive() | |
tmp=true | |
i=1 | |
headertable={} | |
while tmp do | |
client:settimeout(1) | |
headertable[i]=client:receive() | |
if headertable[i] == nil then tmp=nil end | |
i=i+1 | |
end | |
splitData=split(data," ") | |
method=splitData[1] | |
if method == "GET" or method == "get" then | |
--Start assembling URL | |
url=splitData[2] --Heh, assembling by splitting...irony. | |
httpver=splitData[3] | |
splitUrl=split(url,".") | |
c=1 | |
urlData={} | |
urlData.ipsec={} | |
for i=1,#splitUrl do | |
for z=1,#domainTypes do | |
if splitUrl[i]==domainTypes[z] then | |
urlData={ | |
domainType=domainTypes[z], | |
part=i, | |
isIP=false | |
} | |
i=#splitUrl | |
z=#domainTypes | |
end | |
end | |
for z=1,#ipNumberList do | |
if splitUrl[i]==ipNumberList[z] then | |
urlData={ | |
ipsec[c], | |
isIp=true | |
} | |
c=c+1 | |
end | |
end | |
end | |
if urlData==nil then | |
client:send("HTTP/1.0 400 CLIENT_ERROR\r\nDate=Sun, 23 Sep 2012 16:01:50 EST\r\nContent-Type: text/html\r\nContent-Length: "..#badURL.."\r\n\r\n"..badURL) | |
else | |
if isIP==true then | |
if url:sub(1,7)=="http://" then | |
url=url:sub(8,#url) | |
port=split(url,":")[2] | |
port=split(port,"/")[1] | |
ip=ipsec[1].."."..ipsec[2].."."..ipsec[3].."."..ipsec[4] | |
dir=split(url,ip)[1] | |
remoteServer,err=socket:connect(ip,port) | |
if remoteServer then | |
if dir then | |
remoteServer:send("GET "..dir.." HTTP/1.0\r\n"..customHeaders.client.."\r\n\r\n") | |
else | |
remoteServer:send("GET / HTTP/1.0\r\n"..customHeaders.client.."\r\n\r\n") | |
end | |
remoteServer:settimeout(3) | |
remoteData={} | |
tmp=true | |
i=1 | |
while tmp do | |
remoteData[i]=remoteServer:receive() | |
if remoteData[i] == nil then tmp=nil end | |
i=i+1 | |
end | |
sendToClient="" | |
for i=1,#remoteData do | |
sendToClient=sendToClient..remoteData[i] | |
end | |
client:send(sendToClient) | |
else | |
client:send("HTTP/1.0 400 CLIENT_ERROR\r\nDate=Sun, 23 Sep 2012 16:01:50 EST\r\nContent-Type: text/html\r\nContent-Length: "..#badURL+#"<br>"+#err"\r\n\r\n"..badURL.."<br>"..err) | |
end | |
else | |
client:send("HTTP/1.0 400 CLIENT_ERROR\r\nDate=Sun, 23 Sep 2012 16:01:50 EST\r\nContent-Type: text/html\r\nContent-Length: "..#badProtocol.."\r\n\r\n"..badProtocol) | |
end | |
else | |
if url:sub(1,7)=="http://" then | |
url=url:sub(8,#url) | |
port=split(url,":")[2] | |
port=split(port,"/")[1] | |
domain=split(url,urlData.domainType)[1]..urldata.domainType | |
dir=split(url,domain) | |
remoteServer,err=socket:connect(domain,port) | |
if remoteServer then | |
if dir then | |
remoteServer:send("GET "..dir.." HTTP/1.0\r\n"..customHeaders.client.."\r\n\r\n") | |
else | |
remoteServer:send("GET / HTTP/1.0\r\n"..customHeaders.client.."\r\n\r\n") | |
end | |
remoteServer:settimeout(3) | |
remoteData={} | |
tmp=true | |
i=1 | |
while tmp do | |
remoteData[i]=remoteServer:reveive() | |
if remoteData[i] == nil then tmp=nil end | |
i=i+1 | |
end | |
sendToClient="" | |
for i=1,#remoteData do | |
sendToClient=sendToClient..remoteData[i] | |
end | |
client:send(sendToClient) | |
else | |
client:send("HTTP/1.0 400 CLIENT_ERROR\r\nDate=Sun, 23 Sep 2012 16:01:50 EST\r\nContent-Type: text/html\r\nContent-Length: "..#badURL+#"<br>"+#err"\r\n\r\n"..badURL.."<br>"..err) | |
end | |
else | |
client:send("HTTP/1.0 400 CLIENT_ERROR\r\nDate=Sun, 23 Sep 2012 16:01:50 EST\r\nContent-Type: text/html\r\nContent-Length: "..#badProtocol.."\r\n\r\n"..badProtocol) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment