amethyst/lib/servers/handhsake.ex

73 lines
2.4 KiB
Elixir
Raw Normal View History

2024-07-07 11:47:48 +02:00
# Amethyst - An experimental Minecraft server written in Elixir.
# Copyright (C) 2024 KodiCraft
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2024-07-08 16:57:49 +02:00
defmodule Amethyst.Server.Handshake do
@moduledoc """
2024-07-08 16:57:49 +02:00
This module contains the logic for the Handshake stage of the server.
"""
2024-07-07 22:46:18 +02:00
require Logger
use Amethyst.Server
2024-07-07 19:05:32 +02:00
alias Amethyst.Minecraft.Read
2024-07-07 19:05:32 +02:00
2024-07-10 09:11:45 +02:00
@impl true
def init(state) do
state
end
2024-07-07 19:05:32 +02:00
## DESERIALIZATION
2024-07-10 09:11:45 +02:00
@impl true
2024-07-07 19:05:32 +02:00
# Handshake https://wiki.vg/Protocol#Handshake
2024-07-08 14:45:13 +02:00
@spec deserialize(0, binary()) ::
{:handshake, any(), any(), any(), :login | :status | :transfer}
2024-07-07 19:05:32 +02:00
def deserialize(0x00, <<data::binary>>) do
{[ver, addr, port, next], ""} = Read.start(data) |> Read.varint() |> Read.string() |> Read.ushort() |> Read.varint() |> Read.stop()
next = case next do
1 -> :status
2 -> :login
3 -> :transfer
_ -> raise RuntimeError, "Client requested moving to an unknown state!"
end
2024-07-07 22:46:18 +02:00
{:handshake, ver, addr, port, next}
2024-07-07 19:05:32 +02:00
end
2024-07-07 22:46:18 +02:00
def deserialize(type, _) do
2024-07-07 19:05:32 +02:00
raise RuntimeError, "Got unknown packet type #{type}!"
end
2024-07-08 14:45:13 +02:00
## SERIALIZATION
2024-07-10 09:11:45 +02:00
@impl true
def serialize(_) do
raise RuntimeError, "No packets can be transmitted while still in the handshake stage!"
end
2024-07-08 14:45:13 +02:00
## HANDLING
2024-07-10 09:11:45 +02:00
@impl true
2024-07-08 14:45:13 +02:00
# Handshake https://wiki.vg/Protocol#Handshake
2024-07-09 18:57:09 +02:00
@spec handle(any(), any(), any()) :: no_return()
2024-07-10 09:11:45 +02:00
def handle({:handshake, 767, addr, port, next}, client, state) do
2024-07-09 14:18:48 +02:00
Logger.info("Got handshake, version 767 on #{addr}:#{port}. Wants to move to #{next}")
2024-07-08 14:45:13 +02:00
case next do
2024-07-10 09:11:45 +02:00
:status -> Amethyst.Server.Status.serve(client, state)
:login -> Amethyst.Server.Login.serve(client, state)
2024-07-08 14:45:13 +02:00
_ -> raise RuntimeError, "Unhandled move to next mode #{next}"
end
end
2024-07-09 18:57:09 +02:00
def handle(tuple, _, state) do
2024-07-08 14:45:13 +02:00
Logger.error("Unhandled but known packet #{elem(tuple, 0)}")
2024-07-09 18:57:09 +02:00
{:unhandled, state}
2024-07-08 14:45:13 +02:00
end
end