예제 #1
0
        public override int FindSlot(IPEndPoint endPoint, bool comparePorts)
        {
            for (var i = 0; i < Connections.Length; i++)
            {
                if (Connections[i].State != ConnectionState.OFFLINE &&
                    Connections[i].State != ConnectionState.ERROR &&
                    NetworkCore.CompareEndPoints(Connections[i].EndPoint, endPoint, comparePorts))
                {
                    return(i);
                }
            }

            return(-1);
        }
예제 #2
0
        public override bool Receive(out NetworkChunk packet)
        {
            while (true)
            {
                if (ChunkReceiver.FetchChunk(out packet))
                {
                    return(true);
                }

                if (UdpClient.Available <= 0)
                {
                    return(false);
                }

                var    remote = (IPEndPoint)null;
                byte[] data;

                try
                {
                    data = UdpClient.Receive(ref remote);
                }
                catch
                {
                    continue;
                }

                if (data.Length <= 0)
                {
                    continue;
                }

                if (!NetworkCore.UnpackPacket(data, data.Length, ChunkReceiver.ChunkConstruct))
                {
                    continue;
                }

                var useToken = false;
                var token    = (uint)0;

                if (ChunkReceiver.ChunkConstruct.Flags.HasFlag(PacketFlags.TOKEN))
                {
                    useToken = true;
                    token    = ChunkReceiver.ChunkConstruct.Token;
                }
                else if (ChunkReceiver.ChunkConstruct.Flags.HasFlag(PacketFlags.CONTROL) &&
                         ChunkReceiver.ChunkConstruct.Data[0] == (int)ConnectionMessages.CONNECT &&
                         ChunkReceiver.ChunkConstruct.DataSize >= 1 + 512)
                {
                    useToken = true;
                    token    = ChunkReceiver.ChunkConstruct.Data.ToUInt32(5);
                }

                if (NetworkBan.IsBanned(remote, out var reason))
                {
                    NetworkCore.SendControlMsg(UdpClient, remote, 0, useToken, token,
                                               ConnectionMessages.CLOSE, reason);
                    continue;
                }

                if (ChunkReceiver.ChunkConstruct.Flags.HasFlag(PacketFlags.CONNLESS))
                {
                    packet = new NetworkChunk
                    {
                        ClientId = -1,
                        Flags    = SendFlags.CONNLESS,
                        EndPoint = remote,
                        DataSize = ChunkReceiver.ChunkConstruct.DataSize,
                        Data     = ChunkReceiver.ChunkConstruct.Data
                    };

                    return(true);
                }

                var clientId = FindSlot(remote, true);

                if (ChunkReceiver.ChunkConstruct.Flags.HasFlag(PacketFlags.CONTROL) &&
                    ChunkReceiver.ChunkConstruct.Data[0] == (int)ConnectionMessages.CONNECT)
                {
                    if (clientId != -1)
                    {
                        continue;
                    }

                    if (ChunkReceiver.ChunkConstruct.DataSize >= 1 + 512)
                    {
                        var connectAccept = new byte[4];
                        GetToken(remote).ToByteArray(connectAccept, 0);
                        NetworkCore.SendControlMsg(UdpClient, remote, 0, true, token,
                                                   ConnectionMessages.CONNECTACCEPT, connectAccept);
                        Debug.Log("netserver", "got connect, sending connect+accept challenge");
                    }
                    else if (Config["SvAllowOldClients"].AsBoolean() && string.IsNullOrWhiteSpace(Config["Password"].AsString()))
                    {
                        if (LegacyRateLimit())
                        {
                            Debug.Log("netserver", "dropping legacy connect due to ratelimit");
                            continue;
                        }

                        var packets = new NetworkChunkConstruct[]
                        {
                            new NetworkChunkConstruct(),
                            new NetworkChunkConstruct(),
                        };
                        var legacyToken = GetLegacyToken(remote);

                        ConstructLegacyHandshake(packets[0], packets[1], legacyToken);
                        for (var i = 0; i < 2; i++)
                        {
                            NetworkCore.SendPacket(UdpClient, remote, packets[i]);
                        }
                        Debug.Log("netserver", "got legacy connect, sending legacy challenge");
                    }
                    else
                    {
                        Debug.Log("netserver", $"dropping short connect packet, size={ChunkReceiver.ChunkConstruct.DataSize}");
                    }
                }
                else
                {
                    if (clientId == -1)
                    {
                        if (!useToken || !IsCorrectToken(remote, token))
                        {
                            if (!useToken && Config["SvAllowOldClients"].AsBoolean())
                            {
                                ChunkReceiver.Start(remote, null, -1);
                                var chunk   = new NetworkChunk();
                                var correct = false;

                                while (ChunkReceiver.FetchChunk(out chunk))
                                {
                                    if (DecodeLegacyHandShake(chunk.Data, chunk.DataSize, out var legacyToken))
                                    {
                                        if (IsCorrectLegacyToken(remote, legacyToken))
                                        {
                                            correct = true;
                                            break;
                                        }
                                    }
                                }

                                ChunkReceiver.Clear();

                                if (!correct)
                                {
                                    continue;
                                }
                            }
                            else
                            {
                                Debug.Log("netserver",
                                          !useToken
                                        ? "dropping packet with missing token"
                                        : $"dropping packet with invalid token, token={token}");
                                continue;
                            }
                        }

                        var sameIps = 0;

                        for (var i = 0; i < Connections.Length; i++)
                        {
                            if (Connections[i].State == ConnectionState.OFFLINE)
                            {
                                if (clientId < 0)
                                {
                                    clientId = i;
                                }
                                continue;
                            }

                            if (!NetworkCore.CompareEndPoints(Connections[i].EndPoint, remote, false))
                            {
                                continue;
                            }

                            sameIps++;
                            if (sameIps >= ServerConfig.MaxClientsPerIp)
                            {
                                NetworkCore.SendControlMsg(UdpClient, remote, 0, useToken, token, ConnectionMessages.CLOSE,
                                                           $"Only {ServerConfig.MaxClientsPerIp} players with the same IP are allowed");
                                return(false);
                            }
                        }

                        if (clientId < 0)
                        {
                            for (var i = 0; i < Connections.Length; i++)
                            {
                                if (Connections[i].State == ConnectionState.OFFLINE)
                                {
                                    clientId = i;
                                    break;
                                }
                            }
                        }

                        if (clientId < 0)
                        {
                            NetworkCore.SendControlMsg(UdpClient, remote, 0, useToken, token,
                                                       ConnectionMessages.CLOSE, "This server is full");
                            return(false);
                        }

                        if (useToken)
                        {
                            Connections[clientId].Accept(remote, token);
                        }
                        else
                        {
                            Connections[clientId].AcceptLegacy(remote);
                        }

                        NewClientCallback?.Invoke(clientId, !useToken);
                        if (!useToken)
                        {
                            continue;
                        }

                        Connections[clientId].Feed(ChunkReceiver.ChunkConstruct, remote);
                    }

                    if (Connections[clientId].Feed(ChunkReceiver.ChunkConstruct, remote))
                    {
                        if (ChunkReceiver.ChunkConstruct.DataSize != 0)
                        {
                            ChunkReceiver.Start(remote, Connections[clientId], clientId);
                        }
                    }
                }
            }
        }
예제 #3
0
        public override bool Feed(NetworkChunkConstruct packet, IPEndPoint remote)
        {
            if (packet.Flags.HasFlag(PacketFlags.RESEND))
            {
                Resend();
            }

            if (UseToken)
            {
                if (!packet.Flags.HasFlag(PacketFlags.TOKEN))
                {
                    if (!packet.Flags.HasFlag(PacketFlags.CONTROL) || packet.DataSize < 1)
                    {
                        Debug.Log("connection", "dropping msg without token");
                        return(false);
                    }

                    if (packet.Data[0] == (int)ConnectionMessages.CONNECTACCEPT)
                    {
                        if (!Config["ClAllowOldServers"])
                        {
                            Debug.Log("connection", "dropping connect+accept without token");
                            return(false);
                        }
                    }
                    else
                    {
                        Debug.Log("connection", "dropping ctrl msg without token");
                        return(false);
                    }
                }
                else
                {
                    if (packet.Token != Token)
                    {
                        Debug.Log("connection", $"dropping msg with invalid token, wanted={Token} got={packet.Token}");
                        return(false);
                    }
                }
            }

            if (Sequence >= PeerAck)
            {
                if (packet.Ack < PeerAck || packet.Ack > Sequence)
                {
                    return(false);
                }
            }
            else
            {
                if (packet.Ack < PeerAck && packet.Ack > Sequence)
                {
                    return(false);
                }
            }

            PeerAck = packet.Ack;
            if (packet.Flags.HasFlag(PacketFlags.RESEND))
            {
                Resend();
            }

            if (packet.Flags.HasFlag(PacketFlags.CONTROL))
            {
                var msg = (ConnectionMessages)packet.Data[0];
                if (msg == ConnectionMessages.CLOSE)
                {
                    if (!NetworkCore.CompareEndPoints(EndPoint, remote, true))
                    {
                        return(false);
                    }

                    State        = ConnectionState.ERROR;
                    RemoteClosed = true;

                    var reason = "";
                    if (packet.DataSize > 1)
                    {
                        reason = Encoding.UTF8.GetString(packet.Data, 1, Math.Clamp(packet.DataSize - 1, 1, 128));
                    }

                    Error = reason;
                    Debug.Log("connection", $"closed reason='{reason}'");
                    return(false);
                }
                else
                {
                    if (State == ConnectionState.CONNECT)
                    {
                        if (msg == ConnectionMessages.CONNECTACCEPT)
                        {
                            if (packet.Flags.HasFlag(PacketFlags.TOKEN))
                            {
                                if (packet.DataSize < 1 + 4)
                                {
                                    Debug.Log("connection", $"got short connect+accept, size={packet.DataSize}");
                                    return(true);
                                }

                                Token = packet.Data.ToUInt32(1);
                            }
                            else
                            {
                                UseToken = false;
                            }

                            LastReceiveTime = Time.Get();
                            State           = ConnectionState.ONLINE;
                            Debug.Log("connection", "got connect+accept, sending accept. connection online");
                        }
                    }
                }
            }

            if (State == ConnectionState.ONLINE)
            {
                LastReceiveTime = Time.Get();
                AckChunks(packet.Ack);
            }

            return(true);
        }