deathcap / wsmc

WebSocket proxy to Minecraft
34 stars 10 forks source link

wsmc

WebSocket proxy to Minecraft servers

Allows you to write Minecraft clients connecting over WebSockets

Proxy Usage

WSMC for Java

Install Maven and build with:

mvn clean install

Current protocol support: 1.8.9, 1.9 snapshot 15w40b (details)

For Bukkit servers

To load the plugin with software implementing the Bukkit API, simply copy the jar into the plugins directory. Tested with:

For Sponge servers

WSMC also includes Sponge API support, simply copy the jar to the mods folder. For use with:

For other servers (standalone mode)

If your server does not support Bukkit or Sponge plugins, then WSMC can be ran standalone from the command-line:

java -jar target/wsmc*.jar 0.0.0.0 24444 localhost 25565

Tested with:

User authentication is not supported in this mode.

Configuration

The proxy must be able to connect to the Minecraft server without authentication, i.e., in "offline mode". This does not mean the Minecraft server has to be remotely accessible without authentication (although that is a sufficient condition), it can be behind another proxy in "online mode" such as BungeeCord. See below for how to setup authentication in WSMC.

Configure the plugin in plugins/WSMC/config.yml (Bukkit) or config/wsmc.conf (Sponge):

Authentication

By design, wsmc does not handle user passwords. Several techniques to authenticate are available:

First login through the regular Minecraft client: by default, the wsmc/Java plugin will announce "Web client enabled (click to view)" to each user who logs in. You can click this link to go to a per-user URL to login to the web client. The link can be saved and reused as long as the server is up.

If desired, the join message can be disabled by setting announce-on-join: false in plugins/WSMC/config.yml. Users can manually retrieve this URL by typing the /web command. The URL contains your username and a per-user key; it should be kept secret or users will be able to impersonate each other.

Manual setup by administrator via console: an administrator can type the /web username command to create a new key for a given username. The URL can be distributed however you like, and will be used to login with the specified username. Useful for testing with multiple users.

No authentication: setting allow-anonymous: true will disable authentication completely. Use this setting with caution, as it allows logging in as any user.

WSMC for JavaScript (Node.js script)

The first version of WSMC was written in JavaScript. It is still functional but lacks authentication and requires installing Node.js. Java-based servers will likely find WSMC/Java more useful, but WSMC/JavaScript is also provided as an alternative.

To use it, run:

node wsmc.js

Options default to:

--wshost=           websocket host to listen on
--wsport=24444      websocket port to listen on
--mchost=localhost  minecraft host to connect to
--mcport=25565      minecraft port to connect to
--prefix=webuser-   prefix for usernames of websocket-connecting users

When the proxy receives a WS connection, it will connect to the MC server, perform the handshake, negotiate encryption, then pass raw binary packets between the WS client and MC server using websocket-stream.

Limitation: WSMC/JavaScript doesn't perform user authentication. Users currently can connect with no password and they will be given a username beginning with 'webuser-' followed by a number.

Client Example

mcwebchat - a simple web-based chat client

The WS client is responsible for decoding the packets received and encoding packets sent. Protocol encryption is automatically handled by the proxy, but clients still need to unpack/pack the binary data transmitted over the wire for efficiency.

A nice benefit of using WebSockets is that the protocol is already message-based, so the client doesn't have to concern itself with packet lengths (each WS message contains exactly one MC packet).

WSMC Protocol Differences

Although WSMC proxies the MC protocol over WS mostly unscathed, there are some changes to cope with the WS environment:

MC packets have a varint length field prefix; WSMC packets lack this field because WS is already message-based and it would be redundant. But only in one direction, currently (TBD).

MC login uses a complicated handshake phase; WSMC begins with the WS client sending the username and login key in the initial packet (separated by a colon, or only the username if logging in without authentication), and then immediately switching to the login phase.

MC's login success packet contains fields for the UUID and username. WSMC extends the username field with a \0 byte followed by the JSON response from the server list ping. This information is included because WS otherwise has no mechanism to retrieve the server ping information.

Project Ideas

Now that your Minecraft server is accessible via WebSocket, what can you do with it? The mcwebchat example isn't very useful, so here are a few more ambitious project ideas possible with a WSMC backend, for anyone up for the challenge:

For a very incomplete WebGL/voxel.js-based client which doesn't implement any of these ideas or work very well at the moment, but could be a good place to start or for reference purposes, see voxel-clientmc (any help welcome).

License

MIT