Пример #1
0
        protected bool StartCompression(TelnetOpcodes type)
        {
            if (this is NetworkAardwolf)
            {
                throw new Exception("Trying to start compression on Aardwolf connection!");
            }

            if (zStream != null)
            {
                return(false);
            }

            if (type == TelnetOpcodes.MCCP_V1)
            {
                Send(new[] { (byte)TelnetOpcodes.IAC, (byte)TelnetOpcodes.SB, (byte)TelnetOpcodes.MCCP_V1,
                             (byte)TelnetOpcodes.WILL, (byte)TelnetOpcodes.SE });
            }
            else
            {
                Send(new[] { (byte)TelnetOpcodes.IAC, (byte)TelnetOpcodes.SB, (byte)TelnetOpcodes.MCCP_V2,
                             (byte)TelnetOpcodes.IAC, (byte)TelnetOpcodes.SE });
            }

            zStream = new ZStream();
            zStream.deflateInit(6);
            return(true);
        }
Пример #2
0
        protected override void HandlePacket(TelnetPacket pkt)
        {
            base.HandlePacket(pkt);

            if (pkt.Header == TelnetOpcodes.SB && pkt.Type == TelnetOpcodes.GMCP && pkt.Data.Length > 0)
            {
                string Msg = Encoding.Default.GetString(pkt.Data.ToArray()).ToLower();
                if (!Msg.StartsWith("core.supports.") && !Msg.StartsWith("core.hello"))
                {
                    if (Server.Aardwolf != null)
                    {
                        Server.Aardwolf.SendGMCP(pkt.Data.ToArray());
                    }
                }
                else if (Msg.StartsWith("core.supports."))
                {
                    try
                    {
                        Msg = Msg.Substring("core.supports.".Length);
                        if (Msg.StartsWith("add "))
                        {
                            string[] v =
                                Msg.ToLower().Substring(Msg.IndexOf(' ') + 1).Replace("[", "").Replace("]", "").Replace(
                                    "\"", "").Replace(" ", "").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                            foreach (string x in v)
                            {
                                if (!x.EndsWith("0"))
                                {
                                    if (!GMCPModules.Contains(x.Substring(0, x.Length - 1)))
                                    {
                                        GMCPModules.Add(x.Substring(0, x.Length - 1));
                                        string b = x.Substring(0, x.Length - 1);
                                        if (b.Contains('.'))
                                        {
                                            b = b.Substring(0, b.IndexOf('.'));
                                        }
                                        if (!Server.GMCPModules.ContainsKey(b) || Server.GMCPModules[b] == 0)
                                        {
                                            if (Server.Aardwolf != null)
                                            {
                                                Server.Aardwolf.SendGMCP(Encoding.Default.GetBytes("Core.Supports.Add [ \"" + b + " " + x[x.Length - 1].ToString() + "\" ]"));
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    GMCPModules.Remove(x.Substring(0, x.Length - 1));
                                }
                            }
                        }
                        else if (Msg.StartsWith("set "))
                        {
                            string[] v =
                                Msg.ToLower().Substring(Msg.IndexOf(' ') + 1).Replace("[", "").Replace("]", "").Replace(
                                    "\"", "").Replace(" ", "").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                            foreach (string x in v)
                            {
                                if (!x.EndsWith("0"))
                                {
                                    if (!GMCPModules.Contains(x.Substring(0, x.Length - 1)))
                                    {
                                        GMCPModules.Add(x.Substring(0, x.Length - 1));
                                        string b = x.Substring(0, x.Length - 1);
                                        if (b.Contains('.'))
                                        {
                                            b = b.Substring(0, b.IndexOf('.'));
                                        }
                                        if (!Server.GMCPModules.ContainsKey(b) || Server.GMCPModules[b] == 0)
                                        {
                                            if (Server.Aardwolf != null)
                                            {
                                                Server.Aardwolf.SendGMCP(Encoding.Default.GetBytes("Core.Supports.Add [ \"" + b + " " + x[x.Length - 1].ToString() + "\" ]"));
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    GMCPModules.Remove(x.Substring(0, x.Length - 1));
                                }
                            }
                        }
                        else if (Msg.StartsWith("remove "))
                        {
                            string[] v =
                                Msg.ToLower().Substring(Msg.IndexOf(' ') + 1).Replace("[", "").Replace("]", "").Replace(
                                    "\"", "").Replace(" ", "").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                            foreach (string x in v)
                            {
                                GMCPModules.Remove(x);
                            }
                        }
                    }
                    catch
                    {
                    }
                }
            }

            if (pkt.Type == TelnetOpcodes.GMCP)
            {
                return;
            }

            if (pkt.Type == TelnetOpcodes.MCCP_V2)
            {
                if (pkt.Header == TelnetOpcodes.DO)
                {
                    if (StartCompression(TelnetOpcodes.MCCP_V2))
                    {
                        CompressionType = TelnetOpcodes.MCCP_V2;
                    }
                }
                else if (pkt.Header == TelnetOpcodes.DONT)
                {
                    if (CompressionType == TelnetOpcodes.MCCP_V2)
                    {
                        EndCompression();
                        CompressionType = TelnetOpcodes.IAC;
                    }
                }
                return;
            }

            if (pkt.Type == TelnetOpcodes.MCCP_V1)
            {
                if (pkt.Header == TelnetOpcodes.DO)
                {
                    if (StartCompression(TelnetOpcodes.MCCP_V1))
                    {
                        CompressionType = TelnetOpcodes.MCCP_V1;
                    }
                }
                else if (pkt.Header == TelnetOpcodes.DONT)
                {
                    if (CompressionType == TelnetOpcodes.MCCP_V1)
                    {
                        EndCompression();
                        CompressionType = TelnetOpcodes.IAC;
                    }
                }
                return;
            }

            if (Server.Aardwolf != null)
            {
                Server.Aardwolf.Send(pkt);
            }
        }