Half-Life Server Class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MattF
Forum Contributor
Posts: 225
Joined: Sun May 19, 2002 9:58 am
Location: Sussex, UK

Half-Life Server Class

Post by MattF »

I am making a half-life server class to send RCON commands to a half-life server (and those running on the same protocol eg CS). Here is the basic information of what needs to be done:
Note to those writing remote admin programs that issue rcon commands (the in-client rcon commands work as before), you will need to change your rcon tools to use the following revised protocol. Remote App sends a UDP packet to the server on the server's port (e.g., 127.0.0.1:27015): The packet should start with 4 consecutive bytes of 255 (32-bit integer -1) and the string:

"challenge rcon\n"

The server will respond to the requesting system on the purported remote IP address and port with four 255's and:

"challenge rcon number\n"

...where number is an unsigned int32 number. To issue the actual rcon, the remote App then responds with a UDP packet containing 4 255s and:

"rcon number \"password\" rconcommands"

where password is the rcon_password ( should be enclosed in quotes as noted so that multiple word passwords will continue to work ), number is the unsigned int32 number received from the server and rconcommands is the actual rcon command string. If the remote App fails to send the appropriate challenge number, waits too long to send the challenge, or uses an invalid password more than a few times in the course of a few seconds, the remote App will be assumed to be malicious and the actual ip address used by the remote host will be permanently and automatically banned from the server (as with the addip command). You can use listip to see the list of banned ip addresses on a server.
Here is my class:

Code: Select all

<?php
//Half-Life Server Connection Class
/* See send-rcon.txt for more info */
class hlConnection
{
var $connection;
var $key;
var $errno;
var $errstr;

	function connect($ip,$port)
	{
		$this->connection = fsockopen ("udp://".$ip, $port, $errno, $errstr, 10);
		if (!$this->connection) {
			$this->errno = $errno;
			$this->errstr = $errstr;
			return false;
		} else {
			fputs(chr(255).chr(255).chr(255).chr(255)."challenge rcon\n",$this->connection);
			$temp_data = "";
			while(!feof($connection)) {
				$temp_data .= fread($this->connection,1);
			}
			$this->key = stristr($temp_data,"rcon");
			list($trash,$this->key) = split(" ",$this->key);
			$this->key = substr($this->key,0,strlen($this->key)-2);
			return $this->connection;
		}
	}

	function returnKey()
	{
		return $this->key;
	}

	function error()
	{
		return "Error: ".$this->errstr." (".$this->errno.")";
	}

	function close()
	{
		if(fclose($this->connection)) {
			return true;
		} else {
			return false;
		}
	}
	 
}
?>
Here is my test page:

Code: Select all

<?php
require "server.class.php";
$conn = new hlConnection or die("Could not create class");
if($conn->connect("62.60.48.102",27015)) {
	echo "Connected<br>";
	$conn->close();
} else {
	echo $conn->error();
}
?>
The problem is that when I go to the page nothing happens, it just sits there with a blank screen for ages doing nothing at all. The IP address is one of a simple dedicated server on my dialup connection so if anyone wants to try this they will have to substitute that IP for a public server's IP. Any advice is very welcome.
MattF
Forum Contributor
Posts: 225
Joined: Sun May 19, 2002 9:58 am
Location: Sussex, UK

Post by MattF »

Any thoughts, any suggestions, any spam? Please help me!
MattF
Forum Contributor
Posts: 225
Joined: Sun May 19, 2002 9:58 am
Location: Sussex, UK

Post by MattF »

Oh come on! People say "How do I print text onto a page" and you reply with about 50,000 replies but someone has a real question and you go and ignore it! Please help me :(
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Hey Matt,
Oh come on! People say "How do I print text onto a page" and you reply with about 50,000 replies but someone has a real question and you go and ignore it! Please help me
Relax. It's not as though you're a leper or something. I was looking very closely at this before, but I've also got work to do! Additionaly, this is not how most people use PHP. That's something you need to keep in mind. I've been working on a load balancer among other things in PHP and got NO HELP from the php community with issues concerning shared memory, sockets, and round robin scheduling.

However, there is a good deal of data out there about various types of programming. As an example, I was able to find a good deal about shared memory, do a little testing with what's provided, and come up with a solution.

You can do it too! It'll make you a stronger developer.
The problem is that when I go to the page nothing happens, it just sits there with a blank screen for ages doing nothing at all.
What is it supposed to do? Is there something you can write into that class that can give you some idea of what's going on? Like some debugging output?

Now seriously, is this the kind of thing that is implemented in a web page? The description you provided alone isn't the kind of behaviour you're going to see in a web page. Once a server has returned a page to a user, it's done! Therefore, this
The server will respond to the requesting system on the purported remote IP address and port with four 255's and:
"challenge rcon number\n"
...where number is an unsigned int32 number. To issue the actual rcon, the remote App then responds with a UDP packet containing 4 255s and:
"rcon number \"password\" rconcommands"
will never happen.

That said, rather then just giving you the answers, I'm going to tell you to look at the examples in the manual concerning sockets and even fsockopen(). There is a lot of good information there.

Make yourself strong!

BDKR
MattF
Forum Contributor
Posts: 225
Joined: Sun May 19, 2002 9:58 am
Location: Sussex, UK

Post by MattF »

Thank you very much for taking the time to reply. I have already looked extensively through the manual and found nothing to help me with this problem (but I may be blind). I tried adding some simple HTML to the top of the page something like

Code: Select all

<h1>This is the page</h1>
Now that should work even if nothing else does. What my class is supposed to do is provide an interface between script and server. When I say that the user responds with a command I didn't explain myself well, what I meant was the user specifys a command and then a script connects to the server and does the verifification and then runs the command. Then returns output to the user. After that the user may then send another command and so on.
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post by dusty »

phpRCON is for hl. i might try one for q3, i made a server status script awhile back.. this might be a good addition
MattF
Forum Contributor
Posts: 225
Joined: Sun May 19, 2002 9:58 am
Location: Sussex, UK

Post by MattF »

I also made a HL server status script. I will look into this phpRCON. Thanks for the replies.
Post Reply