mc_query 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4b2f4ccbe968e103681ebfd6e843e614ccdb94ec36316273674a3b9fd8ac9031
4
+ data.tar.gz: 88337d11fa1394a7f30ecfe99bda94bbfe54e073e76bc93ba389ddd13fa1c7ef
5
+ SHA512:
6
+ metadata.gz: 64fdd0b76b5f0f194246c0a9596dcce1dc8cbebe6341563775e7ddb08ea485e90ac283a6cdf1906aa42f135ce1f0cbcbe646ab33628411794f616b4e4615e035
7
+ data.tar.gz: 17bb53e152eb73df0076970710ad27c22ef4fbdc6f222b01e087dee55fd614ade61b784fd9ff9815bbc133694096ae4e5b58af875af18226c7b0af1c2e2333f3
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ /.idea/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mc_query.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Taylor Blau
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,45 @@
1
+ # mc-query
2
+
3
+ MCQuery is a Rubygem that includes ruby bindings to the Minecraft RCON protocol.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'mc_query'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mc_query
18
+
19
+ ## Usage
20
+
21
+ Using `mc_query` is really simple. Simply stick `require mc_query` at the top of your Ruby file, and then make a query. Here, I will query the [Overcast Network](http://oc.tc).
22
+
23
+ ```ruby
24
+ require 'mc_query'
25
+
26
+ mc = MCQuery.new({:ip => 'us.oc.tc'})
27
+ mc.simple_query
28
+
29
+ # => {
30
+ # :name => "My Server",
31
+ # :gametype => "SMP",
32
+ # :world_name => "world",
33
+ # :online_players => "0",
34
+ # :max_players => "150",
35
+ # :ip => "10.0.0.1",
36
+ # }
37
+ ```
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it ( https://github.com/[my-github-username]/mc_query/fork )
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,5 @@
1
+ require "mc_query/version"
2
+
3
+ module MCQuery
4
+ require "mc_query/query"
5
+ end
@@ -0,0 +1,92 @@
1
+ require 'net/http'
2
+
3
+ module MCQuery
4
+ class Query
5
+ @@MAGIC = "\xFE\xFD"
6
+ @@REQUEST = "#{@@MAGIC}\x00"
7
+ @@HANDSHAKE = "#{@@MAGIC}\x09"
8
+
9
+ def initialize(opts = {})
10
+ # Merge in the default options
11
+ opts = {:ip => 'localhost', :port => '25565', :timeout => 8}.merge(opts)
12
+
13
+ @ip = opts[:ip]
14
+ @port = opts[:port]
15
+ @timeout = opts[:timeout]
16
+
17
+ # Connect to the server socket (based on the options)
18
+ @socket = TCPSocket.new(@ip, @port)
19
+
20
+ # Generate a session id and store it off
21
+ @session_id = get_session_id
22
+ end
23
+
24
+ # Public: Do the Minecraft dance (according to the protocol) and
25
+ # send back a hash of the result
26
+ #
27
+ # Examples
28
+ #
29
+ # simple_query
30
+ # # => {
31
+ # :name => "My Server",
32
+ # :gametype => "SMP",
33
+ # :world_name => "world",
34
+ # :online_players => "0",
35
+ # :max_players => "150",
36
+ # :ip => "10.0.0.1",
37
+ # }
38
+ #
39
+ # Returns the named hash of the data contained in the simple query
40
+ def simple_query
41
+ # Store off the challenge key (we'll need this for querying)
42
+ @challenge = get_challenge_key
43
+
44
+ timeout @timeout do
45
+ query = @socket.send(encode_data("#{@@REQUEST}#{@session_id}") + @challenge.to_s, 0)
46
+ buffer = recieve_data
47
+ parsed = buffer.split("\0", 6)
48
+ puts parsed
49
+ {
50
+ :name => parsed[0],
51
+ :gametype => parsed[1],
52
+ :world_name => parsed[2],
53
+ :online_players => parsed[3],
54
+ :max_players => parsed[4],
55
+ :ip => parsed[5]
56
+ }
57
+ end
58
+ end
59
+
60
+ def get_challenge_key
61
+ timeout @timeout do
62
+ # Send the magic bytes, the handshake bytes, and the session id
63
+ send_data("#{@@HANDSHAKE}#{@session_id}")
64
+
65
+ # Get the raw data (splice out the headers, and convert to an int32)
66
+ raw_key = recieve_data.to_i
67
+
68
+ # Pack it as big endian and return it
69
+ [raw_key].pack("N")
70
+ end
71
+ end
72
+
73
+ private
74
+
75
+ def get_session_id
76
+ [(rand(32) + 1) & 0x0F0F0F0F].pack("N")
77
+ end
78
+
79
+ def send_data(data)
80
+ @socket.send(encode_data(data), 0)
81
+ end
82
+
83
+ def encode_data(data)
84
+ data.force_encoding(Encoding::ASCII_8BIT)
85
+ end
86
+
87
+ def recieve_data
88
+ # Recieve the data, splicing out the headers
89
+ @socket.recvfrom(1460)[0][5...-1]
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,92 @@
1
+ require 'net/http'
2
+
3
+ module MCQuery
4
+ class Query
5
+ @@MAGIC = "\xFE\xFD"
6
+ @@REQUEST = "#{@@MAGIC}\x00"
7
+ @@HANDSHAKE = "#{@@MAGIC}\x09"
8
+
9
+ def initialize(opts = {})
10
+ # Merge in the default options
11
+ opts = {:ip => 'localhost', :port => '25565', :timeout => 8}.merge(opts)
12
+
13
+ @ip = opts[:ip]
14
+ @port = opts[:port]
15
+ @timeout = opts[:timeout]
16
+
17
+ # Connect to the server socket (based on the options)
18
+ @socket = UDPSocket.new
19
+ @socket.connect(@ip, @port)
20
+
21
+ # Generate a session id and store it off
22
+ @session_id = get_session_id
23
+ end
24
+
25
+ # Public: Do the Minecraft dance (according to the protocol) and
26
+ # send back a hash of the result
27
+ #
28
+ # Examples
29
+ #
30
+ # simple_query
31
+ # # => {
32
+ # :name => "My Server",
33
+ # :gametype => "SMP",
34
+ # :world_name => "world",
35
+ # :online_players => "0",
36
+ # :max_players => "150",
37
+ # :ip => "10.0.0.1",
38
+ # }
39
+ #
40
+ # Returns the named hash of the data contained in the simple query
41
+ def simple_query
42
+ # Store off the challenge key (we'll need this for querying)
43
+ @challenge = get_challenge_key
44
+
45
+ timeout @timeout do
46
+ query = @socket.send(encode_data("#{@@REQUEST}#{@session_id}") + @challenge.to_s, 0)
47
+ buffer = recieve_data
48
+ parsed = buffer.split("\0", 6)
49
+ puts parsed
50
+ {
51
+ :name => parsed[0],
52
+ :gametype => parsed[1],
53
+ :world_name => parsed[2],
54
+ :online_players => parsed[3],
55
+ :max_players => parsed[4],
56
+ :ip => parsed[5]
57
+ }
58
+ end
59
+ end
60
+
61
+ def get_challenge_key
62
+ timeout @timeout do
63
+ # Send the magic bytes, the handshake bytes, and the session id
64
+ send_data("#{@@HANDSHAKE}#{@session_id}")
65
+
66
+ # Get the raw data (splice out the headers, and convert to an int32)
67
+ raw_key = recieve_data.to_i
68
+
69
+ # Pack it as big endian and return it
70
+ [raw_key].pack("N")
71
+ end
72
+ end
73
+
74
+ private
75
+ def get_session_id
76
+ [(rand(32) + 1) & 0x0F0F0F0F].pack("N")
77
+ end
78
+
79
+ def send_data(data)
80
+ @socket.send(encode_data(data), 0)
81
+ end
82
+
83
+ def encode_data(data)
84
+ data.force_encoding(Encoding::ASCII_8BIT)
85
+ end
86
+
87
+ def recieve_data
88
+ # Recieve the data, splicing out the headers
89
+ @socket.recvfrom(1460)[0][5...-1]
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,3 @@
1
+ module MCQuery
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mc_query/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mc_query"
8
+ spec.version = MCQuery::VERSION
9
+ spec.authors = ["Taylor Blau"]
10
+ spec.email = ["me@ttaylorr.com"]
11
+ spec.summary = %q{Ruby bindings to query Minecraft servers based on the RCON protocol.}
12
+ spec.homepage = "http://mc.ttaylorr.com"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.6"
21
+ spec.add_development_dependency "rake"
22
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mc_query
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Taylor Blau
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-09-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - me@ttaylorr.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/mc_query.rb
54
+ - lib/mc_query/connect.rb
55
+ - lib/mc_query/query.rb
56
+ - lib/mc_query/version.rb
57
+ - mc_query.gemspec
58
+ homepage: http://mc.ttaylorr.com
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.7.6
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Ruby bindings to query Minecraft servers based on the RCON protocol.
82
+ test_files: []