Python1320

Python1320

Aug 14th, 2010
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.54 KB | None | 0 0
  1. -- ONLY for Testing...
  2.  
  3. require'oosocks'
  4.  
  5. module("srvq",package.seeall)
  6.  
  7. A2S_INFO_HEADER = "ÿÿÿÿTSource Engine Query\0"
  8.  
  9. QuerySocket = OOSock(IPPROTO_UDP)
  10.  
  11.  
  12. function QueryServer(ip,port,callback)
  13.     QuerySocket:Send(A2S_INFO_HEADER,ip,tonumber(port))
  14.     QuerySocket:ReceiveDatagram()
  15. end
  16.  
  17. QuerySocket:SetBinaryMode(true)
  18.  
  19. Calltypes = {
  20.     [SCKCALL_CONNECT] = "Connect", -- SCKCALL_CONNECT
  21.     [SCKCALL_REC_SIZE] = "Rec Size", -- SCKCALL_REC_SIZE
  22.     [SCKCALL_REC_LINE] = "Rec Line", -- SCKCALL_REC_LINE
  23.     [SCKCALL_SEND] = "Send", -- SCKCALL_SEND
  24.     [SCKCALL_BIND] = "Bind", -- SCKCALL_BIND
  25.     [SCKCALL_ACCEPT] = "Accept", -- SCKCALL_ACCEPT
  26.     [SCKCALL_LISTEN] = "Listen", -- SCKCALL_LISTEN 
  27.     [SCKCALL_REC_DATAGRAM] = "Datagram", -- SCKCALL_REC_DATAGRAM
  28. }
  29.  
  30. Errtypes = {
  31.     [SCKERR_OK] = "Ok", -- SCKERR_OK
  32.     [SCKERR_BAD] = "Bad", -- SCKERR_BAD
  33.     [SCKERR_CONNECTION_RESET] = "Con Rest", -- SCKERR_CONNECTION_RESET
  34.     [SCKERR_NOT_CONNECTED] = "Not Con", -- SCKERR_NOT_CONNECTED
  35.     [SCKERR_TIMED_OUT] = "Timed Out", -- SCKERR_TIMED_OUT
  36. }
  37.  
  38.  
  39. function QueryCallback(sock,call,id,err,binread,peer,peerport)
  40.  
  41.         MsgN("[Server Query] Call: "..Calltypes[call].." - Error: "..Errtypes[err])
  42.      
  43.     if call == SCKCALL_REC_DATAGRAM and err == SCKERR_OK then
  44.         --[[
  45.         local size = binread:GetSize()  
  46.         MsgN("size:",size)
  47.         local rawdata = "";  
  48.        
  49.         for i=1, size do    
  50.             local byte=binread:ReadByte()
  51.             rawdata = rawdata .. (byte!=0 and string.char( byte ) or '#' )
  52.         end  
  53.            
  54.          MsgN("RawData: '"..tostring(rawdata).."'")
  55.        
  56.          MsgN"------------------"
  57.         binread:Rewind()
  58.         --]]
  59.        
  60.        
  61.         local info={peer=peer,peerport=peerport}
  62.         -- Skip Header: Should be 0xFF 0xFF 0xFF 0xFF
  63.         binread:ReadByte()binread:ReadByte()binread:ReadByte()binread:ReadByte()
  64.  
  65.         local byte=binread:ReadByte()  
  66.         info.type=(byte!=0 and string.char( byte ) or '#' )
  67.         -- MsgN("\t".."protocol:",protocol)
  68.        
  69.         info.servername=binread:ReadString()   
  70.         -- MsgN("\t".."servername:",servername)
  71.  
  72.         info.map=binread:ReadString()  
  73.         -- MsgN("\t".."map:",map)
  74.        
  75.         info.gamename=binread:ReadString() 
  76.         -- MsgN("\t".."gamename:",gamename)
  77.        
  78.         info.gamedesc=binread:ReadString() 
  79.         -- MsgN("\t".."gamedesc:",gamedesc)
  80.        
  81.         local b1=binread:ReadByte()
  82.         local b2=binread:ReadByte()
  83.         b2=b2*256
  84.         info.appid=b1+b2
  85.         -- MsgN("\t".."appid:",appid)
  86.        
  87.         info.players=binread:ReadByte()
  88.         -- MsgN("\t".."players:",players)      
  89.        
  90.         info.maxplayers=binread:ReadByte() 
  91.         -- MsgN("\t".."maxplayers:",maxplayers)    
  92.  
  93.         info.bots=binread:ReadByte()   
  94.         -- MsgN("\t".."bots:",bots)    
  95.        
  96.         local byte=binread:ReadByte()  
  97.         info.servertype=(byte!=0 and string.char( byte ) or '#' )
  98.         -- MsgN("\t".."servertype:",servertype)    
  99.  
  100.         local byte=binread:ReadByte()  
  101.         info.ostype=(byte!=0 and string.char( byte ) or '#' )
  102.         -- MsgN("\t".."ostype:",ostype)
  103.  
  104.         info.passworded=binread:ReadByte() 
  105.         -- MsgN("\t".."passworded:",passworded)
  106.        
  107.         info.secure=binread:ReadByte() 
  108.         -- MsgN("\t".."secure:",secure)
  109.        
  110.         info.gameversion=binread:ReadString()  
  111.         -- MsgN("\t".."gameversion:",gameversion)
  112.  
  113.         info.EDF=binread:ReadByte()
  114.         -- MsgN("\t".."EDF:",EDF)
  115.        
  116.         for name,data in pairs(info) do
  117.             MsgN(" > "..name..":\t"..tostring(data))
  118.         end
  119.        
  120.         sock:Close() --asdf. Nee to think of how to make the "resolver" work as in whether we use the same socket for everything/socket for every requested server query instance/etc...       
  121.        
  122.     end
  123.    
  124. end
  125. QuerySocket:SetCallback(QueryCallback)
  126.  
  127.  
  128. MsgN("Querying server...",QueryServer("88.191.102.162",27015))
Add Comment
Please, Sign In to add comment