Пример #1
0
        internal void Send(TelnetPacket pkt)
        {
            if (Socket == null)
            {
                return;
            }

            if (pkt.Header == TelnetOpcodes.SB && pkt.Data.Length != 0)
            {
                Send(new[] { (byte)TelnetOpcodes.IAC, (byte)TelnetOpcodes.SB, (byte)pkt.Type }.Concat(
                         pkt.Data.ToArray()).Concat(new[] { (byte)TelnetOpcodes.IAC, (byte)TelnetOpcodes.SE }).ToArray());
            }
            else
            {
                Send(new[] { (byte)TelnetOpcodes.IAC, (byte)pkt.Header, (byte)pkt.Type });
            }
        }
Пример #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);
            }
        }
Пример #3
0
 protected virtual void HandlePacket(TelnetPacket pkt)
 {
 }
Пример #4
0
        protected int HandlePacket(byte[] Buf, int Index, int MaxIndex)
        {
            if (Index >= MaxIndex)
            {
                return(MaxIndex);
            }

            if (telnetPacket != null)
            {
                switch (telnetPacket.State)
                {
                case TelnetStates.None:
                    telnetPacket.Header = (TelnetOpcodes)Buf[Index];
                    telnetPacket.State  = TelnetStates.Header;
                    return(HandlePacket(Buf, Index + 1, MaxIndex));

                case TelnetStates.Header:
                    telnetPacket.Type  = (TelnetOpcodes)Buf[Index];
                    telnetPacket.State = telnetPacket.Header == TelnetOpcodes.SB ? TelnetStates.Data : TelnetStates.End;
                    if (telnetPacket.State == TelnetStates.End)
                    {
                        HandlePacket(telnetPacket);
                        telnetPacket = null;
                    }
                    else
                    {
                        telnetPacket.Data = new MemoryStream(512);
                    }
                    return(HandlePacket(Buf, Index + 1, MaxIndex));

                case TelnetStates.Data:
                {
                    for (int i = Index; i < MaxIndex; i++)
                    {
                        if (telnetPacket.HadIAC && Buf[i] == (byte)TelnetOpcodes.SE)
                        {
                            telnetPacket.State = TelnetStates.End;
                            HandlePacket(telnetPacket);
                            if (zStream == null && (telnetPacket.Type == TelnetOpcodes.MCCP_V1 || telnetPacket.Type == TelnetOpcodes.MCCP_V2))
                            {
                                telnetPacket = null;
                                StartDecompression();
                                return(i + 1);
                            }
                            telnetPacket = null;
                            return(HandlePacket(Buf, i + 1, MaxIndex));
                        }
                        if (Buf[i] == (byte)TelnetOpcodes.IAC || (Buf[i] == (byte)TelnetOpcodes.WILL && telnetPacket.Type == TelnetOpcodes.MCCP_V1))
                        {
                            if (!telnetPacket.HadIAC)
                            {
                                if (i - Index > 0)
                                {
                                    telnetPacket.Data.Write(Buf, Index, i - Index);
                                    Index = i;
                                }
                                telnetPacket.HadIAC = true;
                            }
                            else
                            {
                                telnetPacket.Data.Write(new[] { (byte)TelnetOpcodes.IAC }, 0, 1);
                            }
                        }
                        else
                        {
                            if (telnetPacket.HadIAC)
                            {
                                telnetPacket.HadIAC = false;
                                telnetPacket.Data.Write(new[] { (byte)TelnetOpcodes.IAC }, 0, 1);
                            }
                        }
                    }

                    if (Index < MaxIndex)
                    {
                        if (Buf[MaxIndex - 1] != (byte)TelnetOpcodes.IAC)
                        {
                            telnetPacket.Data.Write(Buf, Index, MaxIndex - Index);
                        }
                    }
                    return(MaxIndex);
                }
                }
            }

            for (int i = Index; i < MaxIndex; i++)
            {
                if (Buf[i] == (byte)TelnetOpcodes.IAC)
                {
                    if (i - Index > 0)
                    {
                        WriteInStream(Buf, Index, i);
                    }
                    telnetPacket = new TelnetPacket();
                    return(HandlePacket(Buf, i + 1, MaxIndex));
                }
            }

            if (MaxIndex - Index > 0)
            {
                WriteInStream(Buf, Index, MaxIndex);
            }
            return(MaxIndex);
        }
Пример #5
0
        protected override void HandlePacket(TelnetPacket pkt)
        {
            base.HandlePacket(pkt);

            if (pkt.Type == TelnetOpcodes.GMCP && pkt.Header == TelnetOpcodes.SB && pkt.Data.Length > 0)
            {
                if (inStream.Length != 0)
                {
                    OnReceived(Encoding.Default.GetString(inStream.ToArray()), false);
                    inStream.SetLength(0);
                }
                Server.World._OnReceived(pkt.Data.ToArray());
                return;
            }

            if (pkt.Type == TelnetOpcodes.GMCP && pkt.Header == TelnetOpcodes.WILL)
            {
                Send(new[] { (byte)TelnetOpcodes.IAC, (byte)TelnetOpcodes.DO, (byte)TelnetOpcodes.GMCP });
                SendGMCP(Encoding.Default.GetBytes("Core.Hello { \"client\": \"ProxyMud\", \"version\": \"" + World.Version + "\" }"));
                {
                    if (Server.GMCPModules.Count > 0)
                    {
                        StringBuilder strModule = new StringBuilder();
                        foreach (KeyValuePair <string, int> x in Server.GMCPModules)
                        {
                            if (strModule.Length > 0)
                            {
                                strModule.Append(", ");
                            }
                            strModule.Append("\"" + x.Key + " " + x.Value.ToString() + "\"");
                        }
                        SendGMCP(Encoding.Default.GetBytes("Core.Supports.Set [ " + strModule.ToString() + " ]"));
                    }
                }
                return;
            }

            if (pkt.Type == TelnetOpcodes.TTYPE)
            {
                NetworkClient c = null;
                foreach (NetworkClient x in Server.Clients)
                {
                    if (x.AuthLevel >= 1 && (c == null || x.AuthLevel > c.AuthLevel))
                    {
                        c = x;
                    }
                }
                if (c != null)
                {
                    c.Send(pkt);
                }
                return;
            }

            if (pkt.Type == TelnetOpcodes.MCCP_V2)
            {
                if (pkt.Header == TelnetOpcodes.WILL)
                {
                    Send(new[] { (byte)TelnetOpcodes.IAC, (byte)TelnetOpcodes.DO, (byte)TelnetOpcodes.MCCP_V2 });
                }
                return;
            }

            if (pkt.Type == TelnetOpcodes.MCCP_V1)
            {
                if (pkt.Header == TelnetOpcodes.WILL)
                {
                    Send(new[] { (byte)TelnetOpcodes.IAC, (byte)TelnetOpcodes.DONT, (byte)TelnetOpcodes.MCCP_V1 });
                }
                return;
            }

            foreach (NetworkClient x in Server.Clients)
            {
                if (x.AuthLevel >= 1)
                {
                    x.Send(pkt);
                }
            }
        }