Jump to content

[Sockets]How to query a CS Server?


Sponz

Recommended Posts

If you want to e.g. get the number and names of all player who are currently playing on a CS-server, you have to send a so-called "query" to this server (using UDP) and handle everything the server sends back to you. This isn't as easy as it sounds. At first, you would have to 'learn', or, at least, understand the protocol a CS-server uses (e.g. what do I have to send to the server to get a 'good' reply and what's the structure of this reply).

The following script echos the output to your active window. You will have to modify it to be able to use it as 'bot'. It's a more complex script and you don't need to understand it (completely) to be able to use it.

; HL server query snippet by Saturn
;
; 16-04-2009: update to protocol version 48 by NaNg
; 29-01-2011: update to fix UTF8 problem in mIRC 7.* and fixed name problems if last char is space by NaNg
;
; usage:
; /hlinfo <ip> <port>
; or: /hlinfo <ip>:<port>
; /hlplay <ip> <port>
; or: /hlplay <ip>:<port>

; the alias that opens a new socket if you want to get general information about a running CS-server
alias hlinfo {
 if (!$2) tokenize 58 $1

 ; using a dynamic socket name so we can use multiple instances at once
 var %sock = $+(hlinfo-,$ticks)
 ; construct the query to send to the server
 bset &t 1 255 255 255 255
 bset -t &t 5 TSource Engine Query
 bset &t 25 0
 ; actually open the socket, and send the query
 sockudp -k %sock $1-2 &t
 ; closing the socket after 60seconds if there is no response (else it will be closed by another event (see below))
 .timer $+ %sock 1 60 sockclose %sock
}

; this event will get all data sent back from the server
on *:UDPREAD:hlinfo-*:{
 ; save it in a binary variable
 sockread -f &reply

 ; set some local variables that we will use below
 var %offset, %name, %map, %game, %num, %max, %ip, %dir

 ; following the protocol, this "m" shows us that we queried a Half-Life 1 server
 if ($chr($bvar(&reply,5)) == m) {
   ; Half-Life 1 info reply

   %offset = 6

   ; save the ip
   %ip = $bvar(&reply,%offset,128).text
   inc %offset $calc($len($bvar(&reply,%offset,128).text) + 1)

   ; the same
   %name = $bvar(&reply,%offset,128).text
   inc %offset $calc($len($bvar(&reply,%offset,128).text) + 1)

   ; the current map
   %map = $bvar(&reply,%offset,128).text
   inc %offset $calc($len($bvar(&reply,%offset,128).text) + 1)

   ; the game directory
   %dir = $bvar(&reply,%offset,128).text
   inc %offset $calc($len($bvar(&reply,%offset,128).text) + 1)

   ; the name of the game
   %game = $bvar(&reply,%offset,128).text
   inc %offset $calc($len($bvar(&reply,%offset,128).text) + 1)

   ; current and maximum players
   %num = $bvar(&reply,%offset)
   %max = $bvar(&reply,$calc(%offset + 1))
 }

 ; else we get data for a CS:Source game
 else {
   ; Source info reply
   ; we do the same as above
   %offset = 7

   %name = $bvar(&reply,%offset,128).text
   inc %offset $calc($len($bvar(&reply,%offset,128).text) + 1)

   %map = $bvar(&reply,%offset,128).text
   inc %offset $calc($len($bvar(&reply,%offset,128).text) + 1)

   %dir = $bvar(&reply,%offset,128).text
   inc %offset $calc($len($bvar(&reply,%offset,128).text) + 1)

   %game = $bvar(&reply,%offset,128).text
   inc %offset $calc($len($bvar(&reply,%offset,128).text) + 1)

   %num = $bvar(&reply,$calc(%offset + 2))
   %max = $bvar(&reply,$calc(%offset + 3))
 }

 ; now we echo all stored details to the active window
 echo -a Info for $sock($sockname).saddr $+ : $+ $sock($sockname).sport
 echo -a Name: %name
 echo -a Map: %map
 echo -a Game: %game
 echo -a Players: %num $+ / $+ %max

 ; and turn off the timer closing the socket
 .timer $+ $sockname off
 ; as we can close it now manually
 sockclose $sockname
}


; this is the alias that will be called from hlplay to open the socket
alias hlchal {
 ; $1 = ip, $2 = port, $3- = what to call when received
 ; use dynamic socket name as above
 var %sock = $+(hlchal-,$ticks)
 ; construct the query to send to the server
 bset &t 1 255 255 255 255 85 255 255 255 255
 ; open the socket, and ask for a player challenge number
 sockudp -k %sock $1-2 &t
 ; mark it with the ip and port (and the query we want to call when we've received something from the server)
 sockmark %sock $3- $1-2
 ; and make a new timer to close it after 60 seconds
 .timer $+ %sock 1 60 sockclose %sock
}

; read everything that comes back from the hlchal-* query
on *:UDPREAD:hlchal-*:{
 ; and save it in the binary variable called &reply
 sockread -f &reply
 ; actually call the alias saved in the sockmark (hlplay_query)
 $sock($sockname).mark $bvar(&reply,6,4)
 ; and turn the timer off because we can close it manually
 .timer $+ $sockname off
 sockclose $sockname
}

; the alias we can call to make everything easier
; it will call other aliases to open sockets etc.
alias hlplay {
 if (!$2) tokenize 58 $1
 hlchal $1-2 hlplay_query
}

; the alias that opens the _real_ socket to receive data (everything before was just to challenge the server (read the website mentioned above))
alias hlplay_query {
 ; set a binary variable
 bset &query 1 255 255 255 255 85 $3-
 ; use dynamic socket names again
 var %sock = $+(hlplay-,$ticks)
 ; open the socket
 sockudp -k %sock $1-2 &query
 ; and turn on the timer closing the socket after 60seconds
 .timer $+ %sock 1 60 sockclose %sock
}

; this event will receive all data from the server as response to our query
on *:UDPREAD:hlplay-*:{
 ; we save it in &reply
 sockread -f &reply

 ; set some local variables we will use later
 var %i = 1, %num = $bvar(&reply,6), %offset = 7
 var %count = 0, %name, %kills

 ; and echo the number of players
 echo -a Players on $sock($sockname).saddr $+ : $+ $sock($sockname).sport

 ; now we can loop through all player and save some details about them
 while (%i <= %num) {
   inc %offset

   ; the name
   %name = $bvar(&reply,%offset,128).text
   inc %offset $calc($len($bvar(&reply,%offset,128).text) + 1)

   ; the kills
   %kills = $bvar(&reply,%offset).long
   if ($isbit(%kills,32)) dec %kills $calc(2^32)
   inc %offset 8

   ; and if there was a player
   if (%name != $null) {
     ; increase the variable that shows us the number of the player
     inc %count
     ; and echo it to the active window with the kills saved above
     echo -a %count $+ . %name - %kills
   }

   ; increase %i so we go on with the next player
   inc %i
 }

 ; if there are no players online, echo it aswell
 if (%count == 0) echo -a No players found!

 ; turn off the timer
 .timer $+ $sockname off
 ; and close the socket manually
 sockclose $sockname
}

Example:

example.png

i hope i helped ya..

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Posts

    • DISCORD : utchiha_market telegram  https://t.me/utchiha_market SELLIX STORE : https://utchiha-market.mysellix.io/ Join our server for more products : https://discord.gg/hoodservices  
    • Hello friend does skillgrp have Encrypt? I'm trying to open it to use in another version I play and it's not working, good job  
    • Σας καλωσορίζουμε θερμά στο ολοκαίνουργιο κοινωνικό φόρουμ της Ελλάδος! Εγγραφείτε τώρα εντελώς ΔΩΡΕΆΝ στο Chatter.gr.   Τα πρώτα 50 μέλη που θα εγγραφούν θα λάβουν ΔΩΡΕΆΝ Premium αναβάθμιση! https://chatter.gr/ Κάντε Like και Follow τη σελίδα μας στο Facebook και προωθήστε την σε φίλους και συγγενείς σας! https://www.facebook.com/chatter.gr/ Επικοινωνήστε σε πραγματικό χρόνο και πραγματοποιήστε βιντεοκλήσεις ή φωνητικές κλήσεις με τα μέλη της κοινότητάς μας! Συνδεθείτε τώρα στον διακομιστή μας! https://discord.gg/BbCv6deT
    • The most authentic love spells are the most reliable source you can rely on. The most authentic of all these spells is cast in a variety of ways. Love can be mended or broken and so anyone can choose how you want to use the spells. You might want to bring back your ex-girlfriend or boyfriend or divorce her. There are A lot of differences in the world of love. First of all, we need to know that there is a special spiritual being around us. A real man that follows us up and finds what lucks in our life and hence finds a better solution to help us. Most of us are naïve and ignorant about this and hence less of us believe in all this. Contact me right now so that we begin the rituals right away. Authentic love spells (973) 384-3997! Get Back your Ex / lost lover Permanently! The most authentic love spells are spread the universal over e to those who apply to get them. They have helped mend broken hearts that have no hope all over. My most efficient and authentic love spells contain the most meditated magical powers. The powers take a bit of time to work. My physical powers are so strong that I have come through meditation and concentration to work for you. Most Powerful love spells (973) 384-3997! Get Back your Ex / lost lover Permanently. The authentic love spells have a package for married people and so they will also grant you the love you deserve and happiness in marriage. Many marriages are breaking and facing hardships, but you have failed to get ways to make it out. Contact me so that your marriage life is helped not to fall and restore the happiness that you once shared. My love spells are easily made through marriage portions and oils that you can easily perform on your own. Call: (973)384-3997
    • 📌(San Diego, CA Love Spells) 📱 (973) 384-3997 Quickest love spells Welcome to the World of Enchantments: Unlock the Power of Love Spells  +1 (973)-384-3997 Are you curious about the mystical world of love spells and their profound effects on human psychology? Look no further! We dedicated to exploring the fascinating realm of enchantments and the psychology behind them. You are a few clicks away from a prompt resolution of your problem: We will our spiritual powers to bring him/her back. Get back your Ex-lover this service has been the reason of so many happy endings that you should consider it as a serious solution. Let us show you our method with zero chances of rejection. Don’t waste your precious time; get your lover back When you work with an expert, this spell works toward dissipating the negative energies that were kicked up between you and your ex so that reconciliation becomes a possibility. When you break up with someone, especially if things ended badly, it’s like having a thick fog between the two of you that prevents you from seeing the truth of each other. Anger, hurt, and resentment linger in the air and it’s difficult to remember the reasons you fell in love in the first place With this spell, you can restore your broken relationship. The fog will clear, and the sun will shine on both your hearts, warming you up and preparing you for a reunion. You love someone but this isn’t mutual? Don’t wait for the deluge and make him or her love you now. This service will create a great alchemy between this person and you. In just a few weeks, you can make the person you dream of falling in love with you. We recommend you to combine this service with a Marriage ritual if you want this person to commit you. Your husband or you wife is thinking about divorce but you don’t want this to happen? Order this service now to reinforce the bonds of your relationship and save your marriage. Get back your Ex-lover This service will make him/her realize that a divorce would be a mistake and will strengthen love and passion. With permanent results, this service will guarantee a long lasting marriage and will make you happy. The perfect service to break up a relationship you don’t think legitimate. Your lover has gone with someone else? You love someone but this person is already involved in a relationship? Don’t hesitate to break them up as this ritual and prayer is very powerful and will give very good results in a few weeks only.☎️ (973) 384-3997 love spells in Alabama love spells in Alaska love spells in Arizona love spells in Arkansas love spells in California  love spells in Colorado love spells in Connecticut Delaware love spells in Florida love spells in Georgia love spells in Hawaii love spells in Idaho love spells in Illinois love spells in Indiana love spells in Iowa love spells in Kansas love spells in Kentucky love spells in Louisiana love spells in Maine love spells in Maryland love spells in Massachusetts love spells in Michigan love spells in Minnesota love spells in Mississippi love spells in Missouri love spells in Montana love spells in Nebraska love spells in Nevada love spells in New Hampshire love spells in New Jersey love spells in New Mexico love spells in New York love spells in North Carolina love spells in North Dakota love spells in Ohio love spells in Oklahoma love spells in Oregon love spells in Pennsylvania love spells in Rhode Island love spells in South Carolina love spells in South Dakota love spells in Tennessee love spells in Texas love spells in Utah love spells in Vermont love spells in Virginia love spells in Washington love spells in West Virginia love spells in Wisconsin love spells in Wyoming
  • Topics

×
×
  • Create New...