Passed
Pull Request — v3 (#657)
by Austin
02:16
created

Raknet::beforeSend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 4
c 2
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of GameQ.
4
 *
5
 * GameQ is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * GameQ is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace GameQ\Protocols;
20
21
use GameQ\Buffer;
22
use GameQ\Exception\Protocol as Exception;
23
use GameQ\Protocol;
24
use GameQ\Result;
25
use GameQ\Server;
26
27
/**
28
 * Raknet Protocol Class
29
 *
30
 * See https://wiki.vg/Raknet_Protocol for more techinal information
31
 *
32
 * @author Austin Bischoff <[email protected]>
33
 */
34
class Raknet extends Protocol
35
{
36
    /**
37
     * The magic string that is sent to get access to the server information
38
     */
39
    const OFFLINE_MESSAGE_DATA_ID = "\x00\xFF\xFF\x00\xFE\xFE\xFE\xFE\xFD\xFD\xFD\xFD\x12\x34\x56\x78";
40
41
    /**
42
     * Expected first part of the response from the server after query
43
     */
44
    const ID_UNCONNECTED_PONG = "\x1C";
45
46
    /**
47
     * Array of packets we want to look up.
48
     * Each key should correspond to a defined method in this or a parent class
49
     *
50
     * @type array
51
     */
52
    protected $packets = [
53
        self::PACKET_STATUS => "\x01%s%s\x02\x00\x00\x00\x00\x00\x00\x00", // Format time, magic,
54
    ];
55
56
    /**
57
     * The query protocol used to make the call
58
     *
59
     * @type string
60
     */
61
    protected $protocol = 'raknet';
62
63
    /**
64
     * String name of this protocol class
65
     *
66
     * @type string
67
     */
68
    protected $name = 'raknet';
69
70
    /**
71
     * Longer string name of this protocol class
72
     *
73
     * @type string
74
     */
75
    protected $name_long = "Raknet Server";
76
77
    /**
78
     * Do some work to build the packet we need to send out to query
79
     *
80
     * @param Server $server
81
     *
82
     * @return void
83
     */
84 12
    public function beforeSend(Server $server)
85
    {
86
        // Update the server status packet before it is sent
87 12
        $this->packets[self::PACKET_STATUS] = sprintf(
88 12
            $this->packets[self::PACKET_STATUS],
89 12
            pack('Q', time()),
90 2
            self::OFFLINE_MESSAGE_DATA_ID
91
        );
92 2
    }
93
94
    /**
95
     * Process the response
96
     *
97
     * @return array
98
     * @throws \GameQ\Exception\Protocol
99
     */
100 12
    public function processResponse()
101
    {
102
        // Merge the response array into a buffer. Unknown if this protocol does split packets or not
103 12
        $buffer = new Buffer(implode($this->packets_response));
104
105
        // Read first character from response. It should match below
106 12
        $header = $buffer->read(1);
107
108
        // Check first character to make sure the header matches
109 12
        if ($header !== self::ID_UNCONNECTED_PONG) {
110
            throw new Exception(sprintf(
111
                'The header returned "%s" does not match the expected header of "%s"!',
112
                $header,
113
                self::ID_UNCONNECTED_PONG
114
            ));
115
        }
116
117
        // Burn the time section
118 12
        $buffer->skip(8);
119
120
        // Server GUID is next
121 12
        $serverGUID = $buffer->readInt64();
122
123
        // Read the next set to check to make sure the "magic" matches
124 12
        $magicCheck = $buffer->read(16);
125
126
        // Magic check fails
127 12
        if ($magicCheck !== self::OFFLINE_MESSAGE_DATA_ID) {
128
            throw new Exception(sprintf(
129
                'The magic value returned "%s" does not match the expected value of "%s"!',
130
                $magicCheck,
131
                self::OFFLINE_MESSAGE_DATA_ID
132
            ));
133
        }
134
135
        // According to docs the next character is supposed to be used for a length and string for the following
136
        // character for the MOTD but it appears to be implemented incorrectly
137
        // Burn the next two characters instead of trying to do anything useful with them
138 12
        $buffer->skip(2);
139
140
        // Set the result to a new result instance
141 12
        $result = new Result();
142
143
        // Here on is server information delimited by semicolons (;)
144 12
        $info = explode(';', $buffer->getBuffer());
145
146 12
        $result->add('edition', $info[0]);
147 12
        $result->add('motd_line_1', $info[1]);
148 12
        $result->add('protocol_version', (int)$info[2]);
149 12
        $result->add('version', $info[3]);
150 12
        $result->add('num_players', (int)$info[4]);
151 12
        $result->add('max_players', (int)$info[5]);
152 12
        $result->add('server_uid', $info[6]);
153 12
        $result->add('motd_line_2', $info[7]);
154 12
        $result->add('gamemode', $info[8]);
155 12
        $result->add('gamemode_numeric', (int)$info[9]);
156 12
        $result->add('port_ipv4', (int)$info[10]);
157 12
        $result->add('port_ipv6', (int)$info[11]);
158 12
        $result->add('dedicated', 1);
159
160 12
        unset($header, $serverGUID, $magicCheck, $info);
161
162 12
        return $result->fetch();
163
    }
164
}
165