PDA

View Full Version : [Essentials script] [Script] Edit of the Sockets script to receive sockets instead of only sending


Ho-oh 112
February 13th, 2012, 07:32 PM
Alright, so I've been looking up on a safer way to use SQL servers with essentials, and I think I found one, Desbrina indirectly gave me the idea.

This is a discussion over how we can do this.

It's designed for php scripts to send a socket with a gifts attributes (game code, code, message) but wasn't tested.
Right so I've developed a sample:

def pbGiftRequest(host, request, filename=nil, depth=0)
if depth>10
raise "Redirection level too deep"
end
socket=::TCPSocket.new(host, 80)
time=Time.now.to_i
begin
socket.send(request)
info=socket.accept # accepts incomming sockets
info.listen("Gift") # Waits for the socket
result=info.read(64)# When a socket comes in it reads it's first 64 chars...
data=""
# Get the HTTP result
if result[/^HTTP\/1\.[01] (\d+).*/]
errorcode=$1.to_i
if errorcode>=400 && errorcode<500
raise "HTTP Error #{errorcode}"
end
headers={}
# Get the response headers
while true
result=socket.gets.sub(/\r$/,"")
break if result==""
if result[/^([^:]+):\s*(.*)/]
headers[$1]=$2
end
end
length=-1
chunked=false
if headers["Content-Length"]
length=headers["Content-Length"].to_i
end
if headers["Transfer-Encoding"]=="chunked"
chunked=true
end
if headers["Location"] && errorcode >= 300 && errorcode < 400
socket.close rescue socket=nil
return pbDownloadData(headers["Location"],filename,depth+1)
end
if chunked==true
# Chunked content
while true
lengthline=socket.gets.sub(/\r$/,"")
length=lengthline.to_i(16)
break if length==0
while Time.now.to_i-time>=5 || socket.select(10)==0
time=Time.now.to_i
Graphics.update
end
data+=socket.recv(length)
socket.gets
end
elsif length==-1
# No content length specified
while true
if socket.select(500)==0
break
else
while Time.now.to_i-time>=5 || socket.select(10)==0
time=Time.now.to_i
Graphics.update
end
data+=socket.recv(1)
end
end
else
# Content length specified
while length>0
chunk=[length,4096].min
while Time.now.to_i-time>=5 || socket.select(10)==0
time=Time.now.to_i
Graphics.update
end
data+=socket.recv(chunk)
length-=chunk
end
end
end
if filename
File.open(filename,"wb"){|f|
f.write(data)
}
else
return data
end
ensure
socket.close rescue socket=nil
end
return ""
end

def pbCheckGift(url, filename=nil, depth=0)
userAgent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.14) Gecko/2009082707 Firefox/3.0.14"
if depth>10
raise "Redirection level too deep"
end
if url[/^http:\/\/([^\/]+)(.*)$/]
host=$1
path=$2
path="/" if path.length==0
request="GET #{path} HTTP/1.1\nUser-Agent: #{userAgent}\nPragma: no-cache\nHost: #{host}\nProxy-Connection: Close\n\n"
return pbGiftRequest(host, request, filename, depth)
end
return ""
end


this sample is to show people how you could possiblly receive sockets, it's not tested but, I guess it's a matter of trail & error.

I was testing it for mySQL mystery gift, but I remembered I have no actual site to test a .php file on which this script is made for....

Anyone with an idea can post it here.