Giter Site home page Giter Site logo

rust-rcon's Introduction

rust-rcon Build Status

An RCON implementation in the Rust programming language.

This project aims to at least work with the Minecraft implementation of RCON.

Status

  • basic rcon sessions work
  • multi-packet responses
    • works with minecraft
    • not working with factorio

How to install

Add this your Cargo.toml:

[dependencies]
rcon = "0"

How to use

extern crate rcon;

Examples

See the examples in the examples folder

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be dual licensed as above, without any additional terms or conditions.

rust-rcon's People

Contributors

anatawa12 avatar icewind1991 avatar ironhaven avatar jenrik avatar klemens avatar limeburst avatar panicbit avatar sabrinajewson avatar timvisee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

rust-rcon's Issues

Limit size of sent packages

The maximum allowed packet size seems to be 1428 bytes, which is a payload of 1414 bytes excluding headers and null-terminating bytes. If 1429 bytes or more are sent, the server immediately closes the connection. (Edit: this was tested using Minecraft 1.7.10)

This should probably be rejected in the client right away by returning a PacketTooLarge error from cmd(), whose return type would need to be changed to RconResult<String> (or we could add a new function executeCommand or similar and deprecate cmd).

I can submit a PR if you like.

Tokio Example?

So I tried using rcon with tokio and looks like there's none but I figured out by myself.
For anyone who wondering what is tokio example looks like, here's example code.

use rcon;
use toml;
use std;
use tokio;

#[tokio::main]
async fn main(){
    let config: toml::Value = toml::from_str(include_str!("../config.toml")).unwrap();

    let mut server = <rcon::Connection<tokio::net::TcpStream>>::builder()
    .enable_minecraft_quirks(true)
    .connect(format!("{}:{}", config["server"]["ip"].as_str().expect("Server IP isn't specified"), config["server"]["port"].as_str().unwrap_or("25575")), config["server"]["password"].as_str().unwrap())
    .await.unwrap();

    println!("Connected to the server! ({})\nRun `exit` command to exit the program or CTRL + C!", config["server"]["ip"].as_str().unwrap());

    loop {
        println!("Summoning A Zombie ");
        let res = server.cmd("summon minecraft:zombie -775.65 26.00 -180.72 ").await;
        if res.is_err() {
            println!("Server Error Out!");
            continue
        }
        println!("Server Response: {}", res.unwrap());
    }
    println!("Bye!")
}

unresolved import `async_std::net` with rcon-0.5.0

error[E0432]: unresolved import `async_std::net`
  --> /Users/anatawa12/.cargo/registry/src/github.com-1ecc6299db9ec823/rcon-0.5.0/src/lib.rs:14:16
   |
14 | use async_std::net::{TcpStream, ToSocketAddrs};
   |                ^^^ could not find `net` in `async_std`

error[E0432]: unresolved import `async_std::task`
  --> /Users/anatawa12/.cargo/registry/src/github.com-1ecc6299db9ec823/rcon-0.5.0/src/lib.rs:15:16
   |
15 | use async_std::task::sleep;
   |                ^^^^ could not find `task` in `async_std`

error[E0433]: failed to resolve: could not find `io` in `async_std`
  --> /Users/anatawa12/.cargo/registry/src/github.com-1ecc6299db9ec823/rcon-0.5.0/src/packet.rs:11:16
   |
11 | use async_std::io::{Read, ReadExt, Write, prelude::WriteExt};
   |                ^^ could not find `io` in `async_std`

error[E0432]: unresolved import `async_std::io`
  --> /Users/anatawa12/.cargo/registry/src/github.com-1ecc6299db9ec823/rcon-0.5.0/src/packet.rs:11:16
   |
11 | use async_std::io::{Read, ReadExt, Write, prelude::WriteExt};
   |                ^^ could not find `io` in `async_std`

error: aborting due to 4 previous errors

Some errors have detailed explanations: E0432, E0433.
For more information about an error, try `rustc --explain E0432`.
error: could not compile `rcon`
To learn more, run the command again with --verbose.
warning: build failed, waiting for other jobs to finish...
error: build failed
Process finished with exit code 101

Factorio Compatibility

I'm trying to use rust-rcon to send commands to a Factorio server instead of a Minecraft server.
It works but loops forever instead of returning.

The issue is the following bit of code in lib.rs:70:

        self.send(PacketType::ExecCommand, cmd).await?;
...
        // the server processes packets in order, so send an empty packet and
        // remember its id to detect the end of a multi-packet response
        let end_id = self.send(PacketType::ExecCommand, "").await?;
        let mut result = String::new();

        loop {
            let received_packet = self.recv().await?;

            if received_packet.get_id() == end_id {
                // This is the response to the end-marker packet
                break;
            }

            result += received_packet.get_body();
        }

If I add logging I see that end_id is 3 the the only package ever received is 2. I guess Factorio ignores the empty send and only returns confirmation for the original send. I fixed the issue by removing the original end_id line and instead use the real send call:

let end_id = self.send(PacketType::ExecCommand, cmd).await?;

Could you please add this difference in behaviour as an additional feature toggle?

cargo build: unable to compile rcon 0.3.0

After adding rcon = "0.3.0" to the [dependencies] section in Cargo.toml my project refuses to compile:

$ cargo build
    Blocking waiting for file lock on package cache
...

   Compiling rcon v0.3.0
error[E0599]: no method named `write_i32_le` found for struct `std::vec::Vec<_>` in the current scope
  --> C:\Users\Jens\.cargo\registry\src\github.com-1ecc6299db9ec823\rcon-0.3.0\src\packet.rs:71:13
   |
71 |         buf.write_i32_le(self.length).await?;
   |             ^^^^^^^^^^^^ method not found in `std::vec::Vec<_>`

error[E0599]: no method named `write_i32_le` found for struct `std::vec::Vec<_>` in the current scope
  --> C:\Users\Jens\.cargo\registry\src\github.com-1ecc6299db9ec823\rcon-0.3.0\src\packet.rs:72:13
   |
72 |         buf.write_i32_le(self.id).await?;
   |             ^^^^^^^^^^^^ method not found in `std::vec::Vec<_>`

error[E0599]: no method named `write_i32_le` found for struct `std::vec::Vec<_>` in the current scope
  --> C:\Users\Jens\.cargo\registry\src\github.com-1ecc6299db9ec823\rcon-0.3.0\src\packet.rs:73:13
   |
73 |         buf.write_i32_le(self.ptype.to_i32()).await?;
   |             ^^^^^^^^^^^^ method not found in `std::vec::Vec<_>`

error[E0599]: no method named `read_i32_le` found for mutable reference `&mut T` in the current scope
  --> C:\Users\Jens\.cargo\registry\src\github.com-1ecc6299db9ec823\rcon-0.3.0\src\packet.rs:83:24
   |
83 |         let length = r.read_i32_le().await?;
   |                        ^^^^^^^^^^^ method not found in `&mut T`

error[E0599]: no method named `read_i32_le` found for mutable reference `&mut T` in the current scope
  --> C:\Users\Jens\.cargo\registry\src\github.com-1ecc6299db9ec823\rcon-0.3.0\src\packet.rs:84:20
   |
84 |         let id = r.read_i32_le().await?;
   |                    ^^^^^^^^^^^ method not found in `&mut T`

error[E0599]: no method named `read_i32_le` found for mutable reference `&mut T` in the current scope
  --> C:\Users\Jens\.cargo\registry\src\github.com-1ecc6299db9ec823\rcon-0.3.0\src\packet.rs:85:23
   |
85 |         let ptype = r.read_i32_le().await?;
   |                       ^^^^^^^^^^^ method not found in `&mut T`

error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0599`.
error: could not compile `rcon`.

To learn more, run the command again with --verbose.
warning: build failed, waiting for other jobs to finish...
error: build failed
$ rustc --version
rustc 1.46.0 (04488afe3 2020-08-24)

$ cargo --version
cargo 1.46.0 (149022b1d 2020-07-17)

Relicense under dual MIT/Apache-2.0

Why?

The MIT license requires reproducing countless copies of the same copyright
header with different names in the copyright field, for every MIT library in
use. The Apache license does not have this drawback, and has protections from
patent trolls and an explicit contribution licensing clause. However, the
Apache license is incompatible with GPLv2. This is why Rust is dual-licensed as
MIT/Apache (the "primary" license being Apache, MIT only for GPLv2 compat), and
doing so would be wise for this project. This also makes this crate suitable
for inclusion in the Rust standard distribution and other project using dual
MIT/Apache.

How?

To do this, get explicit approval from each contributor of copyrightable work
(as not all contributions qualify for copyright) and then add the following to
your README:

## License

Licensed under either of
 * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
 * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.

### Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you shall be dual licensed as above, without any
additional terms or conditions.

and in your license headers, use the following boilerplate (based on that used in Rust):

// Copyright (c) 2015 t developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.

And don't forget to update the license metadata in your Cargo.toml!

Contributor checkoff

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.