Python1320

Source Server Query thingy unfinished

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