/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   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/>.
 */

package de.steamwar.velocitycore.mods;

import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.ConnectionHandshakeEvent;
import com.velocitypowered.proxy.connection.client.InitialInboundConnection;
import com.velocitypowered.proxy.connection.client.LoginInboundConnection;
import com.velocitypowered.proxy.protocol.packet.HandshakePacket;
import de.steamwar.linkage.Linked;
import de.steamwar.persistent.Reflection;
import de.steamwar.velocitycore.VelocityCore;
import de.steamwar.velocitycore.listeners.BasicListener;

import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;

@Linked
public class Hostname extends BasicListener {

    private static final Reflection.Field<LoginInboundConnection, InitialInboundConnection> delegate = new Reflection.Field<>(LoginInboundConnection.class, "delegate");

    public static InitialInboundConnection getInitialInboundConnection(LoginInboundConnection loginInboundConnection) {
        return delegate.get(loginInboundConnection);
    }

    private static final Reflection.Field<InitialInboundConnection, HandshakePacket> handshake = new Reflection.Field<>(InitialInboundConnection.class, "handshake");

    public static String getExtraHandshakeData(LoginInboundConnection loginInboundConnection) {
        HandshakePacket handshakePacket = handshake.get(getInitialInboundConnection(loginInboundConnection));
        int i = handshakePacket.getServerAddress().indexOf('\0');
        if (i == -1) return "";

        return handshakePacket.getServerAddress().substring(i);
    }

    private final Set<String> knownHostnames = new HashSet<>();
    private final Set<String> knownExtraData = new HashSet<>();

    public Hostname() {
        knownHostnames.add("steamwar.de");
        knownHostnames.add("178.63.72.88");
        knownHostnames.add("memewar.de"); // Chaoscaot
        knownHostnames.add("dampfkrieg.de"); // Chaoscaot
        knownHostnames.add("steamshrimp.de"); // Chaoscaot
        knownHostnames.add("127.0.0.1"); // Geyser

        knownHostnames.add("@mat:matdoes.dev "); //https://github.com/mat-1/matscan
        knownHostnames.add("wtf.mynx.lol"); //https://discord.com/invite/serverseeker
        knownHostnames.add("masscan");
        knownHostnames.add("aaa");

        knownExtraData.add("");
        knownExtraData.add("\0FML\0");
        knownExtraData.add("\0FML2\0");
        knownExtraData.add("\0FML3\0");
        knownExtraData.add("\0FORGE");
    }

    @Subscribe
    public void onHandshake(ConnectionHandshakeEvent event) {
        String hostname = event.getConnection().getVirtualHost().orElseThrow().getHostName().toLowerCase();
        if (!knownHostnames.contains(hostname) && !hostname.endsWith(".steamwar.de")) {
            VelocityCore.getLogger().log(Level.WARNING, () -> event.getConnection().getRemoteAddress() + " connected with unknown hostname " + hostname);
        }

        String extraData = getExtraHandshakeData((LoginInboundConnection) event.getConnection());
        if (!knownExtraData.contains(extraData)) {
            VelocityCore.getLogger().log(Level.WARNING, () -> event.getConnection().getRemoteAddress() + " connected with unknown extra data " + extraData);
        }
    }
}
