<commit_before>﻿using System;
using System.Collections.Concurrent;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using log4net;
using Pdelvo.Minecraft.Protocol.Helper;
using Pdelvo.Minecraft.Protocol.Packets;
using Pdelvo.Minecraft.Proxy.Library;
using Pdelvo.Minecraft.Proxy.Library.Connection;
using Pdelvo.Minecraft.Proxy.Library.Plugins;
using Pdelvo.Minecraft.Proxy.Library.Plugins.Events;

namespace ServerChangePlugin
{
    public class ServerChangePlugin : PluginBase
    {
        ServerChangePacketListener _listener;
        PluginManager _manager;
        public ConcurrentDictionary<IProxyConnection, Tuple<int, int>> EntityIDMapping { get; set; }

        internal ILog Logger = LogManager.GetLogger("Server Change plugin");

        public ServerChangePlugin()
        {
            EntityIDMapping = new ConcurrentDictionary<IProxyConnection, Tuple<int, int>>();
        }

        public override string Name
        {
            get { return "Server Change Plug in"; }
        }

        public override Version Version
        {
            get { return System.Version.Parse("0.0.0.1"); }
        }

        public override string Author
        {
            get { return "pdelvo"; }
        }

        public override void Load(PluginManager manager)
        {
            _manager = manager;
            _listener = new ServerChangePacketListener(this);
            _manager.RegisterPacketListener(_listener);
        }

        public override void OnConnectionLost(UserEventArgs args)
        {
            //clean up
            TryRemoveConnection(args.Connection);
        }

        public bool TryRemoveConnection(IProxyConnection connection)
        {
            Tuple<int, int> result;
            return EntityIDMapping.TryRemove(connection, out result);
        }
    }

    public class ServerChangePacketListener : IPacketListener
    {
        public ServerChangePlugin Plugin { get; set; }

        Regex endPointRegex = new Regex("^\\[Redirect\\].*?\\:\\s*(?<ip>[a-zA-Z0-9\\.\\-]+)\\s*(\\:\\s*(?<port>[0-9]+))?\\s*;(?<version>[0-9]+)?\\s*$");

        public ServerChangePacketListener(ServerChangePlugin plugin)
        {
            Plugin = plugin;
        }

        public void ClientPacketReceived(PacketReceivedEventArgs e)
        {
            ApplyClientEntityIDFixes(e);
        }
        public async void ServerPacketReceived(PacketReceivedEventArgs e)
        {
            var packet = e.Packet as DisconnectPacket;
            if (packet != null)
            {
                var result = endPointRegex.Match(packet.Reason);

                if (result.Success)
                {
                    e.Handled = true;
                    var version = 0;
                    if (result.Groups["version"].Success)
                        version = int.Parse(result.Groups["version"].Value);
                    RemoteServerInfo info = new RemoteServerInfo(result.ToString(), 
                        new IPEndPoint(IPAddress.Parse(result.Groups["ip"].Value), int.Parse(result.Groups["port"].Value)), version);

                    var connection = e.Connection;<commit_msg>New server change regex is now fully implemented<commit_after>﻿using System;
using System.Collections.Concurrent;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using log4net;
using Pdelvo.Minecraft.Protocol.Helper;
using Pdelvo.Minecraft.Protocol.Packets;
using Pdelvo.Minecraft.Proxy.Library;
using Pdelvo.Minecraft.Proxy.Library.Connection;
using Pdelvo.Minecraft.Proxy.Library.Plugins;
using Pdelvo.Minecraft.Proxy.Library.Plugins.Events;

namespace ServerChangePlugin
{
    public class ServerChangePlugin : PluginBase
    {
        ServerChangePacketListener _listener;
        PluginManager _manager;
        public ConcurrentDictionary<IProxyConnection, Tuple<int, int>> EntityIDMapping { get; set; }

        internal ILog Logger = LogManager.GetLogger("Server Change plugin");

        public ServerChangePlugin()
        {
            EntityIDMapping = new ConcurrentDictionary<IProxyConnection, Tuple<int, int>>();
        }

        public override string Name
        {
            get { return "Server Change Plug in"; }
        }

        public override Version Version
        {
            get { return System.Version.Parse("0.0.0.1"); }
        }

        public override string Author
        {
            get { return "pdelvo"; }
        }

        public override void Load(PluginManager manager)
        {
            _manager = manager;
            _listener = new ServerChangePacketListener(this);
            _manager.RegisterPacketListener(_listener);
        }

        public override void OnConnectionLost(UserEventArgs args)
        {
            //clean up
            TryRemoveConnection(args.Connection);
        }

        public bool TryRemoveConnection(IProxyConnection connection)
        {
            Tuple<int, int> result;
            return EntityIDMapping.TryRemove(connection, out result);
        }
    }

    public class ServerChangePacketListener : IPacketListener
    {
        public ServerChangePlugin Plugin { get; set; }

        Regex endPointRegex = new Regex("^\\[Redirect\\].*?\\:\\s*(?<ip>[a-zA-Z0-9\\.\\-]+)\\s*(\\:\\s*(?<port>[0-9]+))?\\s*;(?<version>[0-9]+)?\\s*$");

        public ServerChangePacketListener(ServerChangePlugin plugin)
        {
            Plugin = plugin;
        }

        public void ClientPacketReceived(PacketReceivedEventArgs e)
        {
            ApplyClientEntityIDFixes(e);
        }
        public async void ServerPacketReceived(PacketReceivedEventArgs e)
        {
            var packet = e.Packet as DisconnectPacket;
            if (packet != null)
            {
                var result = endPointRegex.Match(packet.Reason);

                if (result.Success)
                {
                    e.Handled = true;
                    var version = 0;
                    if (result.Groups["version"].Success)
                        version = int.Parse(result.Groups["version"].Value);
                    RemoteServerInfo info = new RemoteServerInfo(result.ToString(), 
                        new IPEndPoint((await Dns.GetHostEntryAsync(result.Groups["ip"].Value)).AddressList[0], result.Groups["port"].Success ? int.Parse(result.Groups["port"].Value) : 25565), version);

                    var connection = e.Connection;
{'fraction_non_alphanumeric': 0.10198696057125116, 'fraction_numerical': 0.0046569388388699165, 'mean_word_length': 2.161432777232581, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2768144', 'n_tokens_mistral': 1717, 'n_tokens_neox': 1630, 'n_words': 347}