Ejemplo n.º 1
0
        public override int Process(AsyncUser user, byte[] buffer, int size)
        {
            int r = defaultMessage;

            for (int i = 0; i < defaultMessage; i++)
            {
                if (Utils.StartsWith(buffer, size, _data[i]))
                {
                    r = i;

                    break;
                }
            }

            byte[] http = r switch
            {
                0 => GetHttpHtml(opcode200, _message[0]),
                1 => GetHttpText(opcode200, _message[1]),
                2 => GetHttpText(opcode200, _message[2]),
                _ => GetHttpHtml(opcode404, _message[defaultMessage])
            };

            Write(user, http, 0, http.Length);

            return(1);
        }
    }
Ejemplo n.º 2
0
        private void ProcessSend(SocketAsyncEventArgs args)
        {
            if (args.SocketError == SocketError.Success)
            {
                int bytesTransferred = args.BytesTransferred;

                if (bytesTransferred != args.Count)
                {
                    throw new NotImplementedException();
                }

                AsyncUser user = (AsyncUser)args.UserToken;

#if VERBOSE
                Debug.WriteIf(_minimumBufferSize == 0 && args.Buffer[args.Count - 1] == 10, DateTime.Now.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture) + " [Server" + user.Id + "] " + Encoding.ASCII.GetString(args.Buffer, 0, args.Count));
                Debug.WriteLineIf(_minimumBufferSize == 0 && args.Buffer[args.Count - 1] != 10, DateTime.Now.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture) + " [Server" + user.Id + "] " + Encoding.ASCII.GetString(args.Buffer, 0, args.Count));
                Debug.WriteLineIf(_minimumBufferSize > 0, DateTime.Now.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture) + " [Server" + user.Id + "] " + Utils.GetHex(args.Buffer, args.Count));
#endif

                ReturnArgs(user, args);
            }
            else
            {
#if VERBOSE
                Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture) + " ProcessSend() -> CloseArgs(" + ((AsyncUser)args.UserToken).Id + ")");
#endif

                CloseArgs(args);
            }
        }
Ejemplo n.º 3
0
        public override int Process(AsyncUser user, byte[] buffer, int size)
        {
            Contract.Requires(user != null);

            IrcService.Enqueue(user.Id, buffer, size);

            return(1);
        }
Ejemplo n.º 4
0
        public override AsyncUser CreateUser(Socket sock)
        {
            AsyncUser user = new AsyncUser();

            user.InitializeUser(sock);

            return(user);
        }
Ejemplo n.º 5
0
        private void ProcessAccept(SocketAsyncEventArgs acceptArgs)
        {
            if (acceptArgs.SocketError == SocketError.Success)
            {
                Socket sock = acceptArgs.AcceptSocket;

                Contract.Requires(sock != null);

                acceptArgs.AcceptSocket = null;

                //----------------------------------------------------------

                //sock.NoDelay = true;

                sock.ReceiveBufferSize = _maximumBufferSize;
                sock.SendBufferSize    = _maximumBufferSize;

                sock.ReceiveTimeout = 120_000; // ms
                sock.SendTimeout    = 120_000; // ms

                //----------------------------------------------------------

                AsyncUser user = CreateUser(sock);

                GetArgs(user, out SocketAsyncEventArgs receiveArgs);

                try
                {
                    Handshake(user);
                }
                catch (Exception)
                {
#if VERBOSE
                    Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture) + " ProcessAccept() -> CloseArgs(" + user.Id + ")");
#endif

                    CloseArgs(receiveArgs);

                    goto returnArgs;
                }

                receiveArgs.SetBuffer(0, _maximumBufferSize);

                TryReceive(receiveArgs);
            }
            else
            {
#if VERBOSE
                Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture) + " ProcessAccept() -> TryCloseAndContinueAccepting()");
#endif
            }

returnArgs:

            TryCloseAndContinueAccepting(acceptArgs);
        }
Ejemplo n.º 6
0
        private void ReturnArgs(AsyncUser user, SocketAsyncEventArgs args)
        {
            user.ActiveArgs--;

            Contract.Assert(args.UserToken == user);

            args.UserToken = null;

            _args.Enqueue(args);
        }
Ejemplo n.º 7
0
        public override int Process(AsyncUser user, byte[] buffer, int size)
        {
            if (Utils.StartsWith(buffer, size, _data[0]) && Utils.EndsWith(buffer, size, _data[1]))
            {
                Write(user, _data[2], 0, _data[2].Length);
            }
            else
            {
                Write(user, _data[3], 0, _data[3].Length);
            }

            return(1);
        }
Ejemplo n.º 8
0
        private void GetArgs(AsyncUser user, out SocketAsyncEventArgs args)
        {
            while (!_args.TryDequeue(out args))
            {
                CreateArgs(_numConnections >> 2);
            }

            Contract.Assert(args.UserToken == null);

            args.UserToken = user;

            user.ActiveArgs++;
        }
Ejemplo n.º 9
0
        private void CloseArgs(SocketAsyncEventArgs args)
        {
            AsyncUser user = (AsyncUser)args.UserToken;

            ReturnArgs(user, args);

            if (Interlocked.Exchange(ref user.Closing, 1) < 1)
            {
                BeginCloseUser(user);

                Contract.Assert(user.Socket != null);

                user.EndCloseUser();
            }
        }
Ejemplo n.º 10
0
        public override int Process(AsyncUser user, byte[] buffer, int size)
        {
            if (Utils.EqualsTo(buffer, size, _data[0]))
            {
                Write(user, _data[1], 0, _data[1].Length);

                return(1);
            }

            if (Utils.EqualsTo(buffer, size, _data[2]))
            {
                Write(user, _data[3], 0, _data[3].Length);

                return(1);
            }

            return(0);
        }
Ejemplo n.º 11
0
        private void TrySend(SocketAsyncEventArgs args)
        {
            AsyncUser user = (AsyncUser)args.UserToken;

            try
            {
                if (!user.Socket.SendAsync(args))
                {
                    ProcessSend(args);
                }
            }
            catch (Exception)
            {
#if VERBOSE
                Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture) + " TrySend() -> CloseArgs(" + user.Id + ")");
#endif

                CloseArgs(args);
            }
        }
Ejemplo n.º 12
0
        public void Write(AsyncUser user, byte[] buffer, int offset, int size)
        {
            Contract.Assert(size >= _minimumBufferSize && size <= _maximumBufferSize);

            GetArgs(user, out SocketAsyncEventArgs args);

            if (user.ActiveArgs < _numConnections >> 3)
            {
                Buffer.BlockCopy(buffer, offset, args.Buffer, 0, size);

                args.SetBuffer(0, size);

                TrySend(args);

                return;
            }

#if VERBOSE
            Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture) + " Write() -> CloseArgs(" + user.Id + ")");
#endif

            CloseArgs(args);
        }
Ejemplo n.º 13
0
        public override int Process(AsyncUser user, byte[] buffer, int size)
        {
            Dictionary <string, string> d = new Dictionary <string, string>();

            Utils.GetArguments(buffer, size, ref d);

            /*
             *  \gamename\sfc2op\gamever\1.6\location\0\validate\Dvz0jxQz\final\
             *  \gamename\sfc2op\gamever\1.6\location\0\validate\Dvz0jxQz\final\\queryid\1.1\
             *  \gamename\sfc2op\gamever\1.6\location\0\validate\Dvz0jxQz\final\\queryid\1.1\\list\cmp\gamename\sfc2op\final\
             */

            if (d.ContainsKey("gamename"))
            {
                if (d.ContainsKey("list"))
                {
                    using MemoryStream m = new MemoryStream();

                    GsService.ListServers(m);

                    m.Write(_data[0], 0, _data[0].Length);

                    // sends the list

                    m.SetLength(m.Position);

                    byte[] b = m.ToArray();

                    Write(user, b, 0, b.Length);
                }

                return(1);
            }

            return(0);
        }
Ejemplo n.º 14
0
        public override int Process(AsyncUser user, byte[] buffer, int size)
        {
            // \valid\\email\[email protected]\final\

            Dictionary <string, string> d = new Dictionary <string, string>();

            Utils.GetArguments(buffer, size, ref d);

            if (d.ContainsKey("valid") && d.ContainsKey("email"))
            {
                if (GsService.ContainsEmail(d["email"]))
                {
                    Write(user, _data[1], 0, _data[1].Length);
                }
                else
                {
                    Write(user, _data[2], 0, _data[2].Length);
                }

                return(1);
            }

            return(0);
        }
Ejemplo n.º 15
0
 public override void Handshake(AsyncUser user)
 {
 }
Ejemplo n.º 16
0
 public abstract void BeginCloseUser(AsyncUser user);
Ejemplo n.º 17
0
 public override void BeginCloseUser(AsyncUser user)
 {
 }
Ejemplo n.º 18
0
        public override int Process(AsyncUser user, byte[] buffer, int size)
        {
            _gameServer.EnqueueMessage((Client27000)user, buffer, size);

            return(1);
        }
Ejemplo n.º 19
0
 public override void BeginCloseUser(AsyncUser user)
 {
     _gameServer.RemoveClient(user.Id);
 }
Ejemplo n.º 20
0
 public abstract void Handshake(AsyncUser user);
Ejemplo n.º 21
0
 public override void BeginCloseUser(AsyncUser user)
 {
     IrcService.TryQuitClient((IrcClient)user);
 }
Ejemplo n.º 22
0
        public override int Process(AsyncUser user, byte[] buffer, int size)
        {
            Contract.Requires(user != null);

            Dictionary <string, string> d = new Dictionary <string, string>();

            Utils.GetArguments(buffer, size, ref d);

            // \newuser\\email\[email protected]\nick\JohnDoe2\password\sfcrulz\id\1\final\

            if (d.ContainsKey("newuser") && d.ContainsKey("email") && d.ContainsKey("nick") && d.ContainsKey("password"))
            {
                if (GsService.ContainsEmail(d["email"]))
                {
                    Write(user, _data[2], 0, _data[2].Length);
                }
                else if (GsService.ContainsNick(d["nick"]))
                {
                    Write(user, _data[3], 0, _data[3].Length);
                }
                else
                {
                    GsService.AddProfile(d["email"], d["nick"], d["password"], out GsProfile profile);

                    // \nur\\userid\12617\profileid\19465\id\1\final\

                    StringBuilder s = new StringBuilder(1024);

                    s.Append("\\nur\\\\userid\\");
                    s.Append(profile.Id);
                    s.Append("\\profileid\\");
                    s.Append(profile.Id);
                    s.Append("\\id\\1\\final\\");

                    byte[] b = Encoding.ASCII.GetBytes(s.ToString());

                    Write(user, b, 0, b.Length);
                }

                return(1);
            }

            /*
             *  \login\\challenge\IoWIbSnjMf2pv9iUioRgF9ySYLV2r72p\user\JohnDoe2@[email protected]\userid\12617\profileid\19465\response\08eeb76f1241ac0777a18aac726c03d1\firewall\1\port\0\id\1\final\
             *  \login\\challenge\PKPNiLeCH1D71pCwSndbT36zrkQLaG2w\user\JohnDoe2@[email protected]\response\a7d6e62a66f96565966f96ab475a44cf\firewall\1\port\0\id\1\final\
             */

            if (d.ContainsKey("login") && d.ContainsKey("challenge") && d.ContainsKey("user") && d.ContainsKey("response"))
            {
                GsService.GetProfile(d["user"], out GsProfile profile);

                if (profile != null)
                {
                    using MD5 md5 = MD5.Create();

                    string password = GetHash(md5, profile.Password);
                    string userData = password + "                                                " + profile.Username;

                    string clientChallenge = d["challenge"];

                    string clientResponse  = GetHash(md5, userData + clientChallenge + _serverChallenge + password);
                    string serviceResponse = GetHash(md5, userData + _serverChallenge + clientChallenge + password);

                    if (clientResponse.Equals(d["response"], StringComparison.Ordinal))
                    {
                        // \lc\2\sesskey\239289\proof\8f1295968d534a3c6816d24dc172d92e\userid\12617\profileid\19465\uniquenick\JohnDoe2@[email protected]\lt\x4TGX3[SbkAk]NIJt3Hc4N__\id\1\final\

                        StringBuilder s = new StringBuilder(1024);

                        s.Append("\\lc\\2\\sesskey\\");
                        s.Append(user.Id);
                        s.Append("\\proof\\");
                        s.Append(serviceResponse);
                        s.Append("\\userid\\");
                        s.Append(profile.Id);
                        s.Append("\\profileid\\");
                        s.Append(profile.Id);
                        s.Append("\\uniquenick\\");
                        s.Append(profile.Username);
                        s.Append("\\lt\\x4TGX3[SbkAk]NIJt3Hc4N__\\id\\1\\final\\");

                        byte[] b = Encoding.ASCII.GetBytes(s.ToString());

                        Write(user, b, 0, b.Length);

                        if (d.ContainsKey("userid") && d.ContainsKey("profileid"))
                        {
                            return(1);
                        }
                        else
                        {
                            return(0);
                        }
                    }
                }

                Write(user, _data[4], 0, _data[4].Length);

                return(1);
            }

            // \logout\\sesskey\239288\final\

            if (d.ContainsKey("logout"))
            {
                return(1);
            }

            return(0);
        }
Ejemplo n.º 23
0
 public override void Handshake(AsyncUser user)
 {
     Write(user, _data[1], 0, _data[1].Length);
 }
Ejemplo n.º 24
0
 public abstract int Process(AsyncUser user, byte[] buffer, int size);
Ejemplo n.º 25
0
        private void ProcessReceive(SocketAsyncEventArgs args)
        {
            if (args.SocketError == SocketError.Success)
            {
                int bytesTransferred = args.BytesTransferred;

                if (bytesTransferred > 0)
                {
                    int bytesAccumulated = args.Offset + bytesTransferred;

                    AsyncUser user = (AsyncUser)args.UserToken;

                    while (true)
                    {
                        int bytesExpected = GetSize(args.Buffer, bytesAccumulated);

                        if (bytesExpected < _minimumBufferSize || bytesExpected > _maximumBufferSize)
                        {
                            break;
                        }

                        if (bytesAccumulated < bytesExpected)
                        {
                            args.SetBuffer(bytesAccumulated, _maximumBufferSize - bytesAccumulated);

                            TryReceive(args);

                            return;
                        }

#if VERBOSE
                        Debug.WriteIf(_minimumBufferSize == 0 && args.Buffer[bytesExpected - 1] == 10, DateTime.Now.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture) + " [Client" + user.Id + "] " + Encoding.ASCII.GetString(args.Buffer, 0, bytesExpected));
                        Debug.WriteLineIf(_minimumBufferSize == 0 && args.Buffer[bytesExpected - 1] != 10, DateTime.Now.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture) + " [Client" + user.Id + "] " + Encoding.ASCII.GetString(args.Buffer, 0, bytesExpected));
                        Debug.WriteLineIf(_minimumBufferSize > 0, DateTime.Now.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture) + " [Client" + user.Id + "] " + Utils.GetHex(args.Buffer, bytesExpected));
#endif

                        if (Process(user, args.Buffer, bytesExpected) == 0)
                        {
                            break;
                        }

                        if (bytesAccumulated > bytesExpected)
                        {
                            bytesAccumulated -= bytesExpected;

                            Buffer.BlockCopy(args.Buffer, bytesExpected, args.Buffer, 0, bytesAccumulated);

                            continue;
                        }

                        Contract.Assert(bytesAccumulated == bytesExpected);

                        args.SetBuffer(0, _maximumBufferSize);

                        TryReceive(args);

                        return;
                    }
                }
            }

#if VERBOSE
            Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture) + " ProcessReceive() -> CloseArgs(" + ((AsyncUser)args.UserToken).Id + ")");
#endif

            CloseArgs(args);
        }