Compare commits
No commits in common. "7805f3b225c8536eeeb69e846196f084e1d050c8" and "1e7cc32af7e67252d6b4710e4f0a829940dd31f6" have entirely different histories.
7805f3b225
...
1e7cc32af7
@ -19,7 +19,6 @@ defmodule Amethyst.ConnectionHandler do
|
||||
This module is responsible for handling incoming packets and sending outgoing packets. It keeps track of what state the connection is in and which game should
|
||||
receive the packets.
|
||||
"""
|
||||
alias Amethyst.Minecraft.Write
|
||||
require Logger
|
||||
|
||||
@spec child_spec(:gen_tcp.socket()) :: Supervisor.child_spec()
|
||||
@ -64,10 +63,6 @@ defmodule Amethyst.ConnectionHandler do
|
||||
decryption_state = :crypto.crypto_init(:aes_128_cfb8, secret, secret, false)
|
||||
state = state |> Map.put(:encryption_state, encryption_state) |> Map.put(:decryption_state, decryption_state)
|
||||
loop(socket, connstate, version, state)
|
||||
{:set_compression, threshold} ->
|
||||
Logger.debug("Enabling comrpession with threshold #{threshold}")
|
||||
state = Map.put(state, :compression, threshold)
|
||||
loop(socket, connstate, version, state)
|
||||
{:set_position, position} ->
|
||||
prev_position = Map.get(state, :position)
|
||||
state = Map.put(state, :position, position)
|
||||
@ -88,9 +83,9 @@ defmodule Amethyst.ConnectionHandler do
|
||||
if prev_cp == nil do
|
||||
MapSet.new([])
|
||||
else
|
||||
MapSet.new(visible_chunks_from(elem(prev_cp, 0), elem(prev_cp, 1), 16)) # This 16 would be the server view distance!!
|
||||
MapSet.new(visible_chunks_from(elem(prev_cp, 0), elem(prev_cp, 1), Map.get(state, :view_distance, 16)))
|
||||
end
|
||||
chunks = MapSet.new(visible_chunks_from(elem(cp, 0), elem(cp, 1), 16))
|
||||
chunks = MapSet.new(visible_chunks_from(elem(cp, 0), elem(cp, 1), Map.get(state, :view_distance, 16)))
|
||||
new_chunks = MapSet.difference(chunks, prev_chunks)
|
||||
Logger.debug("Sending #{MapSet.size(new_chunks)} chunks...")
|
||||
# We can process all chunks in parallel
|
||||
@ -117,9 +112,6 @@ defmodule Amethyst.ConnectionHandler do
|
||||
{:get_encryption, from} ->
|
||||
send(from, Map.get(state, :decryption_state))
|
||||
loop(socket, connstate, version, state)
|
||||
{:get_compression, from} ->
|
||||
send(from, Map.get(state, :compression))
|
||||
loop(socket, connstate, version, state)
|
||||
{:packet, id, data} ->
|
||||
state = handle_packet(id, data, connstate, version, state)
|
||||
loop(socket, connstate, version, state)
|
||||
@ -258,28 +250,14 @@ defmodule Amethyst.ConnectionHandler do
|
||||
defp send_packet(socket, connstate, packet, version, state) do
|
||||
try do
|
||||
data = connstate.serialize(packet, version)
|
||||
Logger.debug("#{inspect(Map.get(state, :compression))}")
|
||||
container = if Map.get(state, :compression) == nil do
|
||||
# Packet ID is included in data
|
||||
Write.varint(byte_size(data)) <> data
|
||||
else
|
||||
threshold = Map.get(state, :compression, 0)
|
||||
data_length = byte_size(data)
|
||||
if data_length >= threshold do
|
||||
compressed = Write.varint(data_length) <> :zlib.compress(data)
|
||||
Write.varint(byte_size(compressed)) <> compressed
|
||||
else
|
||||
compressed = Write.varint(0) <> data
|
||||
Write.varint(byte_size(compressed)) <> compressed
|
||||
end
|
||||
end
|
||||
Logger.debug(inspect(container))
|
||||
encrypted = if Map.get(state, :encryption_state) == nil do
|
||||
container
|
||||
else
|
||||
Map.get(state, :encryption_state) |> :crypto.crypto_update(container)
|
||||
end
|
||||
length = byte_size(data) |> Amethyst.Minecraft.Write.varint()
|
||||
case Map.get(state, :encryption_state) do
|
||||
nil ->
|
||||
:gen_tcp.send(socket, length <> data)
|
||||
estate ->
|
||||
encrypted = :crypto.crypto_update(estate, length <> data)
|
||||
:gen_tcp.send(socket, encrypted)
|
||||
end
|
||||
rescue
|
||||
e ->
|
||||
Logger.error("Error sending packet #{inspect(packet)} in state #{connstate}: #{Exception.format(:error, e, __STACKTRACE__)}")
|
||||
|
@ -35,7 +35,7 @@ defmodule Amethyst.ConnectionReceiver do
|
||||
{:ok, spawn(fn ->
|
||||
Process.set_label("ConnectionReceiver for #{inspect(socket)}")
|
||||
{:ok, pid} = Amethyst.ConnectionHandler.start_link(socket, Amethyst.ConnectionState.Handshake, 0)
|
||||
receive(socket, pid, nil, nil)
|
||||
receive(socket, pid, nil)
|
||||
end)}
|
||||
end
|
||||
|
||||
@ -44,88 +44,63 @@ defmodule Amethyst.ConnectionReceiver do
|
||||
{:ok, spawn_link(fn ->
|
||||
Process.set_label("ConnectionReceiver for #{inspect(socket)}")
|
||||
{:ok, pid} = Amethyst.ConnectionHandler.start_link(socket, Amethyst.ConnectionState.Handshake, 0)
|
||||
receive(socket, pid, nil, nil)
|
||||
receive(socket, pid, nil)
|
||||
end)}
|
||||
end
|
||||
|
||||
@spec receive(:gen_tcp.socket(), pid(), nil | :crypto.crypto_state(), nil | pos_integer()) :: no_return()
|
||||
def receive(socket, sender, estate, cstate) do
|
||||
case get_packet(socket, estate, cstate) do
|
||||
@spec receive(:gen_tcp.socket(), pid(), nil | :crypto.crypto_state()) :: no_return()
|
||||
def receive(socket, sender, cstate) do
|
||||
case get_packet(socket, cstate) do
|
||||
:closed -> send(sender, :closed)
|
||||
Process.exit(self(), :normal)
|
||||
{:error, error} -> Logger.error("Error reading packet: #{error}")
|
||||
{id, data} -> send(sender, {:packet, id, data})
|
||||
end
|
||||
|
||||
estate = if estate == nil do
|
||||
# Ask the handler if we have encryption now
|
||||
if cstate == nil do
|
||||
# Ask the handler if the encryption state has changed
|
||||
send(sender, {:get_encryption, self()})
|
||||
Logger.debug("Asking for news on encryption...")
|
||||
receive do
|
||||
nil -> nil
|
||||
some -> some
|
||||
nil -> receive(socket, sender, cstate)
|
||||
some ->
|
||||
Logger.debug("Enabling decryption!")
|
||||
receive(socket, sender, some)
|
||||
end
|
||||
else estate end
|
||||
|
||||
cstate = if cstate == nil do
|
||||
# Ask the handler if we have encryption now
|
||||
send(sender, {:get_compression, self()})
|
||||
receive do
|
||||
nil -> nil
|
||||
some -> some
|
||||
else
|
||||
receive(socket, sender, cstate)
|
||||
end
|
||||
else cstate end
|
||||
|
||||
receive(socket, sender, estate, cstate)
|
||||
end
|
||||
|
||||
def get_packet(client, estate, cstate) do
|
||||
case get_varint(client, "", estate) do
|
||||
def get_packet(client, cstate) do
|
||||
case get_varint(client, "", cstate) do
|
||||
:closed -> :closed
|
||||
{:error, error} -> {:error, error}
|
||||
{[length], ""} ->
|
||||
data = :gen_tcp.recv(client, length)
|
||||
case data do
|
||||
{:ok, data} ->
|
||||
# Perform decryption and decompression
|
||||
decrypted = if estate == nil do
|
||||
data
|
||||
else
|
||||
:crypto.crypto_update(estate, data)
|
||||
recv = :gen_tcp.recv(client, length)
|
||||
case recv do
|
||||
{:ok, full_packet} ->
|
||||
full_packet = case cstate do
|
||||
nil -> full_packet
|
||||
ds -> :crypto.crypto_update(ds, full_packet)
|
||||
end
|
||||
|
||||
full_packet = if cstate == nil do
|
||||
# Using "without compression" container
|
||||
decrypted
|
||||
else
|
||||
# Using "with compression" container
|
||||
{[dlength], rest} = Read.start(decrypted) |> Read.varint |> Read.stop
|
||||
if dlength == 0 do
|
||||
# Uncompressed data
|
||||
rest
|
||||
else
|
||||
# Compressed data
|
||||
rest |> :zlib.uncompress()
|
||||
end
|
||||
end
|
||||
|
||||
{[id], data} = Read.start(full_packet) |> Read.varint |> Read.stop
|
||||
{id, data}
|
||||
({[id], data} = Read.start(full_packet) |> Read.varint() |> Read.stop()
|
||||
{id, data})
|
||||
{:error, :closed} -> :closed
|
||||
{:error, error} -> {:error, error}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp get_varint(client, acc, estate) do
|
||||
defp get_varint(client, acc, cstate) do
|
||||
case :gen_tcp.recv(client, 1) do
|
||||
{:ok, byte} ->
|
||||
byte = case estate do
|
||||
byte = case cstate do
|
||||
nil -> byte
|
||||
ds -> :crypto.crypto_update(ds, byte)
|
||||
end
|
||||
case byte do
|
||||
<<0::1, _::7>> -> Read.start(acc <> byte) |> Read.varint() |> Read.stop()
|
||||
<<1::1, _::7>> -> get_varint(client, acc <> byte, estate)
|
||||
<<1::1, _::7>> -> get_varint(client, acc <> byte, cstate)
|
||||
end
|
||||
{:error, :closed} -> :closed
|
||||
{:error, error} -> {:error, error}
|
||||
|
@ -90,13 +90,6 @@ defmodule Amethyst.ConnectionState.Login do
|
||||
verify_token = Amethyst.Keys.decrypt(verify_token)
|
||||
if verify_token == Map.get(state, :verify_token, :never) do
|
||||
send(self(), {:set_encryption, secret})
|
||||
|
||||
if Application.get_env(:amethyst, :compression, nil) != nil do
|
||||
threshold = Application.get_env(:amethyst, :compression, 0)
|
||||
send(self(), {:send_packet, %{packet_type: :set_compression, threshold: threshold}})
|
||||
send(self(), {:set_compression, threshold})
|
||||
end
|
||||
|
||||
send(self(), {:send_packet, %{
|
||||
packet_type: :login_success,
|
||||
uuid: Map.get(state, :uuid),
|
||||
|
@ -18,7 +18,7 @@ defmodule Example.Game do
|
||||
@impl true
|
||||
@spec player_position(any(), {any(), any(), any()}, any()) :: :ok
|
||||
def player_position(from, {x, y, z}, _refs) do
|
||||
# Logger.info("Player at #{inspect(from)} moved to #{x}, #{y}, #{z}")
|
||||
Logger.info("Player at #{inspect(from)} moved to #{x}, #{y}, #{z}")
|
||||
send(from, {:set_position, {x, y, z}})
|
||||
:ok
|
||||
end
|
||||
|
@ -4,5 +4,4 @@ config :amethyst,
|
||||
port: 25599, # Bogus port for testing, avoids unexpected conflicts
|
||||
encryption: true, # Whether or not to request encryption from clients.
|
||||
auth: false, # Whether or not users should be authenticated with Mojang.
|
||||
compression: 256, # Packets larger than this amount are sent compressed. Set to nil to disable compression.
|
||||
default_game: Example.Game # Which game new players should be sent to
|
||||
|
Loading…
Reference in New Issue
Block a user