master ed4620864949 cached
5 files
7.9 KB
2.2k tokens
10 symbols
1 requests
Download .txt
Repository: thedudeguy/PHP-Minecraft-Rcon
Branch: master
Commit: ed4620864949
Files: 5
Total size: 7.9 KB

Directory structure:
gitextract_mruhq277/

├── .gitignore
├── LICENSE
├── README.md
├── composer.json
└── src/
    └── Rcon.php

================================================
FILE CONTENTS
================================================

================================================
FILE: .gitignore
================================================
composer.phar
/vendor


================================================
FILE: LICENSE
================================================
The MIT License (MIT)

Copyright (c) 2013 Chris Churchwell

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


================================================
FILE: README.md
================================================
PHP-Minecraft-Rcon
==================
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/thedudeguy/PHP-Minecraft-Rcon/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/thedudeguy/PHP-Minecraft-Rcon/?branch=master)

Simple Rcon class for php.

## Installation
### Using Composer
This Rcon library may be installed by issuing the following command:
```bash
$ composer require thedudeguy/rcon
```
### Not Using Composer
If not using Composer, just place the Rcon.php file in your project and include it in your PHP script:
```php
require_once('Rcon.php');
```

## Example
For this script to work, rcon must be enabled on the server, by setting `enable-rcon=true` in the server's `server.properties` file. A password must also be set, and provided in the script.

```php
$host = 'some.minecraftserver.com'; // Server host name or IP
$port = 25575;                      // Port rcon is listening on
$password = 'server-rcon-password'; // rcon.password setting set in server.properties
$timeout = 3;                       // How long to timeout.

use Thedudeguy\Rcon;

$rcon = new Rcon($host, $port, $password, $timeout);

if ($rcon->connect())
{
  $rcon->sendCommand("say Hello World!");
}
```


================================================
FILE: composer.json
================================================
{
	"name": "thedudeguy/rcon",
	"description": "Simple Rcon class for php.",
	"keywords": ["rcon", "minecraft"],
	"homepage": "https://github.com/thedudeguy/PHP-Minecraft-Rcon",
	"license": "MIT",
	"authors": [
		{
			"name": "Chris Churchwell",
			"homepage": "http://chrischurchwell.com",
			"email": "chris@chrischurchwell.com"
		}
	],
	"support": {
		"issues": "https://github.com/thedudeguy/PHP-Minecraft-Rcon/issues",
		"source": "https://github.com/thedudeguy/PHP-Minecraft-Rcon/tree/master"
	},
	"autoload": {
		"psr-4": { 
			"Thedudeguy\\": "src/"
		}
    } 
}


================================================
FILE: src/Rcon.php
================================================
<?php
/**
 * See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol for
 * more information about Source RCON Packets
 *
 * PHP Version 7
 *
 * @copyright 2013-2017 Chris Churchwell
 * @author thedudeguy
 * @link https://github.com/thedudeguy/PHP-Minecraft-Rcon
 */

namespace Thedudeguy;

class Rcon
{
    private $host;
    private $port;
    private $password;
    private $timeout;

    private $socket;

    private $authorized = false;
    private $lastResponse = '';

    const PACKET_AUTHORIZE = 5;
    const PACKET_COMMAND = 6;

    const SERVERDATA_AUTH = 3;
    const SERVERDATA_AUTH_RESPONSE = 2;
    const SERVERDATA_EXECCOMMAND = 2;
    const SERVERDATA_RESPONSE_VALUE = 0;

    /**
     * Create a new instance of the Rcon class.
     *
     * @param string $host
     * @param integer $port
     * @param string $password
     * @param integer $timeout
     */
    public function __construct($host, $port, $password, $timeout)
    {
        $this->host = $host;
        $this->port = $port;
        $this->password = $password;
        $this->timeout = $timeout;
    }

    /**
     * Get the latest response from the server.
     *
     * @return string
     */
    public function getResponse()
    {
        return $this->lastResponse;
    }

    /**
     * Connect to a server.
     *
     * @return boolean
     */
    public function connect()
    {
        $this->socket = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);

        if (!$this->socket) {
            $this->lastResponse = $errstr;
            return false;
        }

        //set timeout
        stream_set_timeout($this->socket, 3, 0);

        // check authorization
        return $this->authorize();
    }

    /**
     * Disconnect from server.
     *
     * @return void
     */
    public function disconnect()
    {
        if ($this->socket) {
                    fclose($this->socket);
        }
    }

    /**
     * True if socket is connected and authorized.
     *
     * @return boolean
     */
    public function isConnected()
    {
        return $this->authorized;
    }

    /**
     * Send a command to the connected server.
     *
     * @param string $command
     *
     * @return boolean|mixed
     */
    public function sendCommand($command)
    {
        if (!$this->isConnected()) {
                    return false;
        }

        // send command packet
        $this->writePacket(self::PACKET_COMMAND, self::SERVERDATA_EXECCOMMAND, $command);

        // get response
        $response_packet = $this->readPacket();
        if ($response_packet['id'] == self::PACKET_COMMAND) {
            if ($response_packet['type'] == self::SERVERDATA_RESPONSE_VALUE) {
                $this->lastResponse = $response_packet['body'];

                return $response_packet['body'];
            }
        }

        return false;
    }

    /**
     * Log into the server with the given credentials.
     *
     * @return boolean
     */
    private function authorize()
    {
        $this->writePacket(self::PACKET_AUTHORIZE, self::SERVERDATA_AUTH, $this->password);
        $response_packet = $this->readPacket();

        if ($response_packet['type'] == self::SERVERDATA_AUTH_RESPONSE) {
            if ($response_packet['id'] == self::PACKET_AUTHORIZE) {
                $this->authorized = true;

                return true;
            }
        }

        $this->disconnect();
        return false;
    }

    /**
     * Writes a packet to the socket stream.
     *
     * @param $packetId
     * @param $packetType
     * @param string $packetBody
     *
     * @return void
     */
    private function writePacket($packetId, $packetType, $packetBody)
    {
        /*
		Size			32-bit little-endian Signed Integer	 	Varies, see below.
		ID				32-bit little-endian Signed Integer		Varies, see below.
		Type	        32-bit little-endian Signed Integer		Varies, see below.
		Body		    Null-terminated ASCII String			Varies, see below.
		Empty String    Null-terminated ASCII String			0x00
		*/

        //create packet
        $packet = pack('VV', $packetId, $packetType);
        $packet = $packet.$packetBody."\x00";
        $packet = $packet."\x00";

        // get packet size.
        $packet_size = strlen($packet);

        // attach size to packet.
        $packet = pack('V', $packet_size).$packet;

        // write packet.
        fwrite($this->socket, $packet, strlen($packet));
    }

    /**
     * Read a packet from the socket stream.
     *
     * @return array
     */
    private function readPacket()
    {
        //get packet size.
        $size_data = fread($this->socket, 4);
        $size_pack = unpack('V1size', $size_data);
        $size = $size_pack['size'];

        // if size is > 4096, the response will be in multiple packets.
        // this needs to be address. get more info about multi-packet responses
        // from the RCON protocol specification at
        // https://developer.valvesoftware.com/wiki/Source_RCON_Protocol
        // currently, this script does not support multi-packet responses.

        $packet_data = fread($this->socket, $size);
        $packet_pack = unpack('V1id/V1type/a*body', $packet_data);

        return $packet_pack;
    }
}
Download .txt
gitextract_mruhq277/

├── .gitignore
├── LICENSE
├── README.md
├── composer.json
└── src/
    └── Rcon.php
Download .txt
SYMBOL INDEX (10 symbols across 1 files)

FILE: src/Rcon.php
  class Rcon (line 15) | class Rcon
    method __construct (line 43) | public function __construct($host, $port, $password, $timeout)
    method getResponse (line 56) | public function getResponse()
    method connect (line 66) | public function connect()
    method disconnect (line 87) | public function disconnect()
    method isConnected (line 99) | public function isConnected()
    method sendCommand (line 111) | public function sendCommand($command)
    method authorize (line 138) | private function authorize()
    method writePacket (line 164) | private function writePacket($packetId, $packetType, $packetBody)
    method readPacket (line 194) | private function readPacket()
Condensed preview — 5 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (9K chars).
[
  {
    "path": ".gitignore",
    "chars": 22,
    "preview": "composer.phar\n/vendor\n"
  },
  {
    "path": "LICENSE",
    "chars": 1083,
    "preview": "The MIT License (MIT)\n\nCopyright (c) 2013 Chris Churchwell\n\nPermission is hereby granted, free of charge, to any person "
  },
  {
    "path": "README.md",
    "chars": 1212,
    "preview": "PHP-Minecraft-Rcon\n==================\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/thedudeguy/PHP-Minecraft"
  },
  {
    "path": "composer.json",
    "chars": 570,
    "preview": "{\n\t\"name\": \"thedudeguy/rcon\",\n\t\"description\": \"Simple Rcon class for php.\",\n\t\"keywords\": [\"rcon\", \"minecraft\"],\n\t\"homepa"
  },
  {
    "path": "src/Rcon.php",
    "chars": 5251,
    "preview": "<?php\n/**\n * See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol for\n * more information about Source RCON"
  }
]

About this extraction

This page contains the full source code of the thedudeguy/PHP-Minecraft-Rcon GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 5 files (7.9 KB), approximately 2.2k tokens, and a symbol index with 10 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!