| 1 | # Minecraft implementation of the RCON protocol. | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package Net::RCON::Minecraft; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 11 | ▶ | use Mouse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 11 | ▶ | use Mouse::Util::TypeConstraints; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use Net::RCON::Minecraft::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 11 | ▶ | use IO::Socket::IP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 11 | ▶ | use IO::Select; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 11 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 11 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 11 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 11 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | # Packet types | ||||||||||||||||||||||||||||
| 20 | 11 | AUTH => 3, # Minecraft RCON login packet type | |||||||||||||||||||||||||||
| 21 | AUTH_RESPONSE => 2, # Server auth response | ||||||||||||||||||||||||||||
| 22 | AUTH_FAIL => -1, # Auth failure (password invalid) | ||||||||||||||||||||||||||||
| 23 | COMMAND => 2, # Command packet type | ||||||||||||||||||||||||||||
| 24 | RESPONSE_VALUE => 0, # Server response | ||||||||||||||||||||||||||||
| 25 | 11 | }; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 11 | class_type 'IP' => { class => 'IO::Socket::IP' }; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 11 | has host => ( is => 'ro', default => 'localhost', isa => 'Str' ); | |||||||||||||||||||||||||||
| 30 | 11 | has port => ( is => 'ro', default => 25575, isa => 'Int' ); | |||||||||||||||||||||||||||
| 31 | 11 | has password => ( is => 'ro', default => '', isa => 'Str' ); | |||||||||||||||||||||||||||
| 32 | 11 | has timeout => ( is => 'rw', default => 30, isa => 'Num' ); | |||||||||||||||||||||||||||
| 33 | 47 | ▶ | has _select => ( is => 'ro', default => sub { IO::Select->new } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 11 | has _socket => ( is => 'rw', default => undef, isa => 'Maybe[IP]' ); | |||||||||||||||||||||||||||
| 35 | 11 | has _next_id => ( is => 'rw', default => 1, isa => 'Int' ); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 53 | ▶ | after _next_id => sub { $_[0]->{_next_id} = ($_[0]->{_next_id} + 1) % 2**31 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 33 | ▶ | sub connect { | ||||||||||||||||||||||||||
|
Subroutine
connect: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 33 | my ($s) = @_; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 33 | ▶ | return 1 if $s->connected; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 32 | ▶ | croak 'Password required' unless length $s->password; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 30 | ▶ | $s->_socket(IO::Socket::IP->new( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | PeerAddr => $s->host, | ||||||||||||||||||||||||||||
| 48 | PeerPort => $s->port, | ||||||||||||||||||||||||||||
| 49 | Proto => 'tcp', | ||||||||||||||||||||||||||||
| 50 | 1 | )) or croak "Connection to @{[$s->host]}:@{[$s->port]} failed: $!"; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 29 | $s->_socket->autoflush(1); | |||||||||||||||||||||||||||
| 53 | 29 | $s->_select->remove($s->_select->handles); | |||||||||||||||||||||||||||
| 54 | 29 | $s->_select->add($s->_socket); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 29 | my $id = $s->_next_id(1); | |||||||||||||||||||||||||||
| 57 | 29 | $s->_send_encode(AUTH, $id, $s->password); | |||||||||||||||||||||||||||
| 58 | 29 | my ($size,$res_id,$type,$payload) = $s->_read_decode; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # Force a reconnect if we're about to error out | ||||||||||||||||||||||||||||
| 61 | 28 | ▶ | $s->disconnect unless $type == AUTH_RESPONSE and $id == $res_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$type == 2 and $id == $res_id
MC/DC100%2/2
$type == 2 and $id == $res_id
$type == 2
$id == $res_id
Truth table100%3/3
A and B
A= $type == 2B= $id == $res_id
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 28 | ▶ | croak "RCON authentication failed" if $res_id == AUTH_FAIL; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 27 | ▶ | croak "Expected AUTH_RESPONSE(2), got $type" if $type != AUTH_RESPONSE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 24 | ▶ | croak "Expected ID $id, got $res_id" if $id != $res_id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 23 | ▶ | croak "Non-blank payload <$payload>" if length $payload; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 22 | return 1; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 13 | ▶ | sub disconnect { | ||||||||||||||||||||||||||
|
Subroutine
disconnect: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 13 | my $s = shift; | |||||||||||||||||||||||||||
| 73 | 13 | ▶ | $s->_socket->shutdown(2) if $s->_socket and $s->_socket->connected; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$s->_socket and $s->_socket->connected
MC/DC100%2/2
$s->_socket and $s->_socket->connected
$s->_socket
$s->_socket->connected
Truth table100%3/3
A and B
A= $s->_socketB= $s->_socket->connected
| |||||||||||||||||||||||||||||
| 74 | 13 | 1; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 37 | ▶ | sub connected { $_[0]->_socket and $_[0]->_socket->connected } | ||||||||||||||||||||||||||
|
Subroutine
connected: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 16 | ▶ | sub command { | ||||||||||||||||||||||||||
|
Subroutine
command: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 16 | my ($s, $command, $mode) = @_; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 16 | ▶ | croak 'Command required' unless length $command; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 15 | $s->connect; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 15 | my $id = $s->_next_id; | |||||||||||||||||||||||||||
| 86 | 15 | my $nonce = 16 + int rand(2**15 - 16); # Extra insurance | |||||||||||||||||||||||||||
| 87 | 15 | $s->_send_encode(COMMAND, $id, $command); | |||||||||||||||||||||||||||
| 88 | 15 | $s->_send_encode($nonce, $id, 'nonce'); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 15 | my $raw = ''; | |||||||||||||||||||||||||||
| 91 | 15 | while (1) { | |||||||||||||||||||||||||||
| 92 | 31 | my ($size,$res_id,$type,$payload) = $s->_read_decode; | |||||||||||||||||||||||||||
| 93 | 31 | ▶ | if ($id != $res_id) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 1 | $s->disconnect; | |||||||||||||||||||||||||||
| 95 | 1 | croak sprintf( | |||||||||||||||||||||||||||
| 96 | 'Desync. Expected %d (0x%04x), got %d (0x%04x). Disconnected.', | ||||||||||||||||||||||||||||
| 97 | $id, $id, $res_id, $res_id | ||||||||||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 30 | ▶ | croak "size:$size id:$id got type $type, not RESPONSE_VALUE(0)" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | if $type != RESPONSE_VALUE; | ||||||||||||||||||||||||||||
| 102 | 29 | ▶ | last if $payload eq sprintf 'Unknown request %x', $nonce; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 16 | $raw .= $payload; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 13 | $raw =~ s!\r\n!\n!g; # \R would be nice, but requires 5.010 | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 13 | Net::RCON::Minecraft::Response->new(raw => $raw, id => $id); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | sub DESTROY { $_[0]->disconnect } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # | ||||||||||||||||||||||||||||
| 114 | # Private methods -- Not for external use | ||||||||||||||||||||||||||||
| 115 | # | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # Grab a complete response from the Minecraft server and decode it, | ||||||||||||||||||||||||||||
| 118 | # returning $id, $type, and $payload | ||||||||||||||||||||||||||||
| 119 | 65 | ▶ | sub _read_decode { | ||||||||||||||||||||||||||
|
Subroutine
_read_decode: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 120 | 65 | my ($s) = @_; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 65 | local $_ = $s->_read_with_timeout(4); | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 62 | my $size = unpack 'V'; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 62 | ▶ | croak 'Packet too short. Size field = ' . $size . ' (10 is smallest)' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | if $size < 10; | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 61 | $_ = $s->_read_with_timeout($size); | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 60 | ▶ | croak 'Server response missing terminator' unless s/\0\0$//; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 59 | $size, unpack 'V!V(A*)'; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # Read with timeout. Implemented with select. Guarantees either $len bytes are | ||||||||||||||||||||||||||||
| 137 | # read, or we croak() trying. Returns the bytes read. | ||||||||||||||||||||||||||||
| 138 | 126 | ▶ | sub _read_with_timeout { | ||||||||||||||||||||||||||
|
Subroutine
_read_with_timeout: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 139 | 126 | my ($s, $len) = @_; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 126 | my $ret = ''; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 126 | ▶ | while ($len > length $ret) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 127 | ▶ | if ($s->_select->can_read($s->timeout)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 124 | my $buf = ''; | |||||||||||||||||||||||||||
| 146 | 124 | my $read = $s->_socket->sysread($buf, $len - length $ret); | |||||||||||||||||||||||||||
| 147 | 124 | ▶ | croak "Socket read error: $!" if not defined $read; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 123 | $ret .= $buf; | |||||||||||||||||||||||||||
| 149 | } else { | ||||||||||||||||||||||||||||
| 150 | 3 | $s->disconnect; | |||||||||||||||||||||||||||
| 151 | 3 | croak "Server timeout. Got " .length($ret)."/".$len." bytes"; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 122 | $ret; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # Form and send a packet of the specified $type, $req_id and $payload | ||||||||||||||||||||||||||||
| 159 | 61 | ▶ | sub _send_encode { | ||||||||||||||||||||||||||
|
Subroutine
_send_encode: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 160 | 61 | my ($s, $type, $id, $payload) = @_; | |||||||||||||||||||||||||||
| 161 | 61 | ▶ | $payload = "" unless defined $payload; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 61 | my $data = pack('V!V' => $id, $type) . $payload . "\0\0"; | |||||||||||||||||||||||||||
| 163 | 61 | eval { $s->_socket->send(pack(V => length $data) . $data) }; | |||||||||||||||||||||||||||
| 164 | 61 | ▶ | croak "Socket write failed: $@" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 11 | __PACKAGE__->meta->make_immutable(); | |||||||||||||||||||||||||||