~starkingdoms/starkingdoms

e049da31d21e364dbfc36185d6f89afa9ba07b00 — c0repwn3r 2 years ago a53b7bc
add 0x0c AuthenticateAndBeamOut to the protocol
M client/play.html => client/play.html +1 -0
@@ 15,6 15,7 @@
      <p id="pos">Position: NaN, NaN</p>
      <p id="vel">Velocity: NaN</p>
      <p id="pos-moon">Relative to Moon: NaN, NaN</p>
      <button style="display: none;" id="beamout">Beam out!</button>
    </div>
  </body>
</html>

M client/src/index.ts => client/src/index.ts +15 -1
@@ 2,7 2,12 @@ import {Logger, logSetup} from "./logger";
import {gateway_connect, GatewayClient} from "./gateway";
import {Player} from "./protocol/player";
import {Planet, PlanetType} from "./protocol/planet";
import {MessageC2SInput, MessageC2SInput_packetInfo} from "./protocol/message_c2s";
import {
    MessageC2SAuthenticateAndBeamOut,
    MessageC2SAuthenticateAndBeamOut_packetInfo,
    MessageC2SInput,
    MessageC2SInput_packetInfo
} from "./protocol/message_c2s";
import {encode} from "./serde";

logSetup();


@@ 53,6 58,15 @@ async function client_main(server: string, username: string, texture_quality: st

    if (window.localStorage.getItem("token") !== null && window.localStorage.getItem("user") !== null) {
        global.can_beam_out = true;
        document.getElementById("beamout")!.style.setProperty("display", "block");
        document.getElementById("beamout")!.addEventListener("click", () => {
            let msg = MessageC2SAuthenticateAndBeamOut.encode({
                userId: window.localStorage.getItem("user")!,
                token: window.localStorage.getItem("token")!
            }).finish();

            global.client?.socket.send(encode(MessageC2SAuthenticateAndBeamOut_packetInfo.type, msg));
        })
    }

    logger.info("Loading textures");

M client/src/protocol/message_c2s.ts => client/src/protocol/message_c2s.ts +119 -0
@@ 199,6 199,50 @@ export function messageC2SInput_packetInfoToJSON(object: MessageC2SInput_packetI
  }
}

export interface MessageC2SAuthenticateAndBeamOut {
  /** The user ID that the client is authenticating as */
  userId: string;
  /** The token from the authentication server that the user is authenticating as */
  token: string;
}

export enum MessageC2SAuthenticateAndBeamOut_packetInfo {
  unknown = 0,
  type = 12,
  UNRECOGNIZED = -1,
}

export function messageC2SAuthenticateAndBeamOut_packetInfoFromJSON(
  object: any,
): MessageC2SAuthenticateAndBeamOut_packetInfo {
  switch (object) {
    case 0:
    case "unknown":
      return MessageC2SAuthenticateAndBeamOut_packetInfo.unknown;
    case 12:
    case "type":
      return MessageC2SAuthenticateAndBeamOut_packetInfo.type;
    case -1:
    case "UNRECOGNIZED":
    default:
      return MessageC2SAuthenticateAndBeamOut_packetInfo.UNRECOGNIZED;
  }
}

export function messageC2SAuthenticateAndBeamOut_packetInfoToJSON(
  object: MessageC2SAuthenticateAndBeamOut_packetInfo,
): string {
  switch (object) {
    case MessageC2SAuthenticateAndBeamOut_packetInfo.unknown:
      return "unknown";
    case MessageC2SAuthenticateAndBeamOut_packetInfo.type:
      return "type";
    case MessageC2SAuthenticateAndBeamOut_packetInfo.UNRECOGNIZED:
    default:
      return "UNRECOGNIZED";
  }
}

function createBaseMessageC2SHello(): MessageC2SHello {
  return { version: 0, requestedUsername: "", nextState: 0 };
}


@@ 536,6 580,81 @@ export const MessageC2SInput = {
  },
};

function createBaseMessageC2SAuthenticateAndBeamOut(): MessageC2SAuthenticateAndBeamOut {
  return { userId: "", token: "" };
}

export const MessageC2SAuthenticateAndBeamOut = {
  encode(message: MessageC2SAuthenticateAndBeamOut, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
    if (message.userId !== "") {
      writer.uint32(10).string(message.userId);
    }
    if (message.token !== "") {
      writer.uint32(18).string(message.token);
    }
    return writer;
  },

  decode(input: _m0.Reader | Uint8Array, length?: number): MessageC2SAuthenticateAndBeamOut {
    const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
    let end = length === undefined ? reader.len : reader.pos + length;
    const message = createBaseMessageC2SAuthenticateAndBeamOut();
    while (reader.pos < end) {
      const tag = reader.uint32();
      switch (tag >>> 3) {
        case 1:
          if (tag != 10) {
            break;
          }

          message.userId = reader.string();
          continue;
        case 2:
          if (tag != 18) {
            break;
          }

          message.token = reader.string();
          continue;
      }
      if ((tag & 7) == 4 || tag == 0) {
        break;
      }
      reader.skipType(tag & 7);
    }
    return message;
  },

  fromJSON(object: any): MessageC2SAuthenticateAndBeamOut {
    return {
      userId: isSet(object.userId) ? String(object.userId) : "",
      token: isSet(object.token) ? String(object.token) : "",
    };
  },

  toJSON(message: MessageC2SAuthenticateAndBeamOut): unknown {
    const obj: any = {};
    message.userId !== undefined && (obj.userId = message.userId);
    message.token !== undefined && (obj.token = message.token);
    return obj;
  },

  create<I extends Exact<DeepPartial<MessageC2SAuthenticateAndBeamOut>, I>>(
    base?: I,
  ): MessageC2SAuthenticateAndBeamOut {
    return MessageC2SAuthenticateAndBeamOut.fromPartial(base ?? {});
  },

  fromPartial<I extends Exact<DeepPartial<MessageC2SAuthenticateAndBeamOut>, I>>(
    object: I,
  ): MessageC2SAuthenticateAndBeamOut {
    const message = createBaseMessageC2SAuthenticateAndBeamOut();
    message.userId = object.userId ?? "";
    message.token = object.token ?? "";
    return message;
  },
};

type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;

export type DeepPartial<T> = T extends Builtin ? T

M protocol/src/lib.rs => protocol/src/lib.rs +9 -2
@@ 1,6 1,6 @@
use std::error::Error;
use protobuf::{Enum, Message};
use crate::message_c2s::{MessageC2SChat, MessageC2SGoodbye, MessageC2SHello, MessageC2SInput, MessageC2SPing};
use crate::message_c2s::{MessageC2SAuthenticateAndBeamOut, MessageC2SChat, MessageC2SGoodbye, MessageC2SHello, MessageC2SInput, MessageC2SPing};
use crate::message_s2c::{MessageS2CChat, MessageS2CGoodbye, MessageS2CHello, MessageS2CPlanetData, MessageS2CPlayersUpdate, MessageS2CPong};
use crate::planet::PlanetType;
use crate::starkingdoms_protocol::PacketWrapper;


@@ 14,7 14,8 @@ pub enum MessageC2S {
    Goodbye(MessageC2SGoodbye),
    Chat(MessageC2SChat),
    Ping(MessageC2SPing),
    Input(MessageC2SInput)
    Input(MessageC2SInput),
    AuthenticateAndBeamOut(MessageC2SAuthenticateAndBeamOut)
}

#[derive(Debug)]


@@ 48,6 49,9 @@ impl TryFrom<&[u8]> for MessageC2S {
            },
            _id if _id == message_c2s::message_c2sinput::Packet_info::type_.value() as i64 => {
                MessageC2S::Input(MessageC2SInput::parse_from_bytes(&pkt.packet_data)?)
            },
            _id if _id == message_c2s::message_c2sauthenticate_and_beam_out::Packet_info::type_.value() as i64 => {
                MessageC2S::AuthenticateAndBeamOut(MessageC2SAuthenticateAndBeamOut::parse_from_bytes(&pkt.packet_data)?)
            }
            _id => { return Err(format!("Unrecognized C2S packet {}", _id).into()); }
        };


@@ 75,6 79,9 @@ impl TryInto<Vec<u8>> for MessageC2S {
            }
            MessageC2S::Input(p) => {
                (message_c2s::message_c2sping::Packet_info::type_.value(), p.write_to_bytes()?)
            },
            MessageC2S::AuthenticateAndBeamOut(p) => {
                (message_c2s::message_c2sauthenticate_and_beam_out::Packet_info::type_.value(), p.write_to_bytes()?)
            }
        };


M protocol/src/pbuf/message_c2s.proto => protocol/src/pbuf/message_c2s.proto +7 -0
@@ 35,4 35,11 @@ message MessageC2SInput {
  bool down_pressed = 2;
  bool left_pressed = 3;
  bool right_pressed = 4;
}

message MessageC2SAuthenticateAndBeamOut {
  enum packet_info { unknown = 0; type = 0x0c; }

  string user_id = 1; // The user ID that the client is authenticating as
  string token = 2;   // The token from the authentication server that the user is authenticating as
}
\ No newline at end of file

M server/src/handler.rs => server/src/handler.rs +3 -0
@@ 208,6 208,9 @@ pub async fn handle_client(mgr: ClientManager, data: Arc<RwLock<PhysicsData>>, r
                            me.input.down = p.down_pressed;
                            me.input.left = p.left_pressed;
                            me.input.right = p.right_pressed;
                        },
                        MessageC2S::AuthenticateAndBeamOut(p) => {
                            info!("[{}] * Beaming out {} as {} with realm token {}", remote_addr, username, p.user_id, p.token);
                        }
                    }
                }