Пример #1
0
        private void DOConnectionAccepter(IAsyncResult iar)
        {
            Socket socket = null;

            m_manualResetEvent.Set();

            try {
                socket = m_listenerSocket.EndAccept(iar);
            } catch (SocketException e) {
                throw new Exception("[TCPServer::DOConnectionAccepter()] -> Could not bind/listen/BeginAccept socket! " + e.ToString());
            } catch (ObjectDisposedException e) {
                throw new Exception("[TCPServer::DOConnectionAccepter()] -> ObjectDisposedException while EndAccept " + e.ToString());
            }

            SROClient client = new SROClient(socket);

            if (this.m_serviceComponent.OnClientConnected != null)
            {
                bool result = this.m_serviceComponent.OnClientConnected(client);
                if (result)
                {
                    this.BindClient(client);
                }
                else
                {
                    //TODO: Do not bind implements
                }
            }
            else
            {
                this.BindClient(client);
            }
        }
Пример #2
0
        private void StartFakeClient()
        {
            Thread.Sleep(1000);

            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //socket.Blocking = false;
            //socket.NoDelay = true;

            SROClient fakeClient = new SROClient(socket);

            fakeClientService = new SROServiceContext(fakeClient, this.m_gatewayComponent);

            fakeClientService.DOBind(delegate(SROClient client, SocketError socketError) {
                if (socketError == SocketError.Success)
                {
                    Console.WriteLine("[GATEWAY LISTENER] Clientless socket bind SUCCESS");

                    ConnectFakeClient();
                }
                else
                {
                    Console.WriteLine("[GATEWAY LISTENER] Clientless socket bind failed to host. -- Reason : " + socketError);
                }
            });
        }
Пример #3
0
        public SROServiceContext(SROClient client, SROServiceComponent serviceComponent)
        {
            if (client == null || serviceComponent == null)
            {
                Logger.SERVICE.Print(LogLevel.Allocation, "SROServiceContext or client null");
                this.IsRunning = false;
                return;
            }

            this.ServiceComponent = serviceComponent;
            this.Client           = client;

            this.m_clientSocket = this.Client.Socket;

            this.m_serviceSecurity = new Security();
            this.m_clientSecurity  = client.Security;

            //
            //this.m_serviceSecurity.ChangeIdentity(serviceComponent.Fingerprint);

            this.IsRunning = false;

            if (ServiceComponent.IsDebugMode)
            {
                Logger.SERVICE.Print(LogLevel.Allocation, "SROServiceContext created from Client");
            }
        }
Пример #4
0
 private void BindClient(SROClient client)
 {
     try {
         new SROServiceContext(client, this.m_serviceComponent).DOBind(this.m_serviceComponent.OnServiceSocketStatusChanged);
     } catch { }
 }
Пример #5
0
        private void RegisterListeners()
        {
            if (this.m_gatewayComponent != null)
            {
                this.m_gatewayComponent.OnLocalSocketStatusChanged += new Action <SocketError>(delegate(SocketError error) {
                    if (error == SocketError.Success)
                    {
                        Console.WriteLine("[GATEWAY LISTENER] LOCAL socket bind SUCCESS! : " + this.m_gatewayComponent.LocalEndPoint.ToString());
                    }
                    else
                    {
                        Console.WriteLine("[GATEWAY LISTENER] LOCAL socket bind FAILED!  : " + error);
                    }
                });
                this.m_gatewayComponent.OnServiceSocketStatusChanged += new Action <SROClient, SocketError>(delegate(SROClient client, SocketError error) {
                    if (error == SocketError.Success)
                    {
                        Console.WriteLine("[GATEWAY LISTENER] REMOTE service socket connect SUCCESS! : " + this.m_gatewayComponent.ServiceEndPoint.ToString());
                    }
                    else
                    {
                        Console.WriteLine("[GATEWAY LISTENER] REMOTE service socket connect FAILED!  : " + error);
                    }
                });
                this.m_gatewayComponent.OnClientConnected += new Func <SROClient, bool>(delegate(SROClient client) {
                    Console.WriteLine("[GATEWAY LISTENER] New client connected : " + client.Socket.RemoteEndPoint);
                    this.m_client = client;
                    this.m_client.IsClientless = this.ClientComponent.IsClientless;
                    return(true);
                });
                this.m_gatewayComponent.OnClientDisconnected += new Action <SROClient, ClientDisconnectType>(delegate(SROClient client, ClientDisconnectType disconnectType) {
                    Console.WriteLine("[GATEWAY LISTENER] Client disconnected : " + client.IPAddress + " -- Reason : " + disconnectType);
                });
            }

            if (m_agentComponent != null)
            {
                this.m_agentComponent.OnLocalSocketStatusChanged += new Action <SocketError>(delegate(SocketError error) {
                    if (error == SocketError.Success)
                    {
                        Console.WriteLine("[AGENT LISTENER] LOCAL socket bind SUCCESS! : " + this.m_agentComponent.LocalEndPoint.ToString());
                    }
                    else
                    {
                        Console.WriteLine("[AGENT LISTENER] LOCAL socket bind FAILED!  : " + error);
                    }
                });

                this.m_agentComponent.OnServiceSocketStatusChanged += new Action <SROClient, SocketError>(delegate(SROClient client, SocketError error) {
                    if (error == SocketError.Success)
                    {
                        Console.WriteLine("[AGENT LISTENER] REMOTE service socket connect SUCCESS! : " + this.m_agentComponent.ServiceEndPoint.ToString());
                    }
                    else
                    {
                        Console.WriteLine("[AGENT LISTENER] REMOTE service socket connect FAILED!  : " + error);
                    }
                });

                this.m_agentComponent.OnClientConnected += new Func <SROClient, bool>(delegate(SROClient client) {
                    Console.WriteLine("[AGENT LISTENER] New client connected : " + client.Socket.RemoteEndPoint);
                    return(true);
                });

                this.m_agentComponent.OnClientDisconnected += new Action <SROClient, ClientDisconnectType>(delegate(SROClient client, ClientDisconnectType disconnectType) {
                    Console.WriteLine("[AGENT LISTENER] Client disconnected : " + client.IPAddress + " -- Reason : " + disconnectType);
                });
            }
        }
Пример #6
0
        private PacketResult GetPacketResultOnClientPacketReceived(SROClient client, SROPacket packet)
        {
            //For ClientlessSwitcher
            if (this.m_client.CanSwitchClient)
            {
                #region Fake Client

                #region 0x2001
                if (packet.Opcode == 0x2001)
                {
                    //[S -> C][2001][16 bytes]
                    //0D 00 47 61 74 65 77 61 79 53 65 72 76 65 72 00   ..GatewayServer.
                    Packet response = new Packet(0x2001);
                    if (!this.m_client.IsConnectedToAgent)
                    {
                        response.WriteAscii("GatewayServer");
                    }
                    else
                    {
                        response.WriteAscii("AgentServer");
                        this.m_client.IsConnectedToAgent = false;
                    }
                    response.WriteByte(0); //Client-Connection
                    response.Lock();

                    client.SendPacket(response);

                    response = new Packet(0x2005, false, true);
                    response.WriteByte(0x01);
                    response.WriteByte(0x00);
                    response.WriteByte(0x01);
                    response.WriteByte(0xBA);
                    response.WriteByte(0x02);
                    response.WriteByte(0x05);
                    response.WriteByte(0x00);
                    response.WriteByte(0x00);
                    response.WriteByte(0x00);
                    response.WriteByte(0x02);
                    response.Lock();

                    client.SendPacket(response);

                    response = new Packet(0x6005, false, true);
                    response.WriteByte(0x03);
                    response.WriteByte(0x00);
                    response.WriteByte(0x02);
                    response.WriteByte(0x00);
                    response.WriteByte(0x02);
                    response.Lock();

                    client.SendPacket(response);
                }
                #endregion

                #region 0x6100
                if (packet.Opcode == 0x6100)
                {
                    byte   local    = packet.ReadByte();
                    string identity = packet.ReadAscii();
                    uint   version  = packet.ReadUInt();

                    //S->P:A100 Data:01
                    Packet response = new Packet(0xA100, false, true);

                    if (local != this.m_client.LocaleID)
                    {
                        response.WriteByte(0x02);
                        response.WriteByte(0x01); //(C4)
                    }
                    else if (identity != "SR_Client")
                    {
                        response.WriteByte(0x02);
                        response.WriteByte(0x03); //(C4)
                    }
                    else if (version != this.m_client.VersionID)
                    {
                        response.WriteByte(0x02);
                        response.WriteByte(0x02);
                    }
                    else
                    {
                        response.WriteByte(0x01); //Success
                    }

                    response.Lock();
                    client.SendPacket(response);
                }
                #endregion

                #region 0x6101

                if (packet.Opcode == 0x6101 && this.m_client.IsConnectedToAgent == false)
                {
                    Packet response = new Packet(0xA102);
                    response.WriteByte(0x01);          //Success
                    response.WriteUInt(uint.MaxValue); //SessionID
                    response.WriteAscii("127.0.0.1");  //AgentIP
                    response.WriteUShort(this.m_client.LocalPort);
                    response.Lock();

                    this.m_client.IsConnectedToAgent = true;
                    client.SendPacket(response);
                }

                #endregion

                #region 0x6103

                if (packet.Opcode == 0x6103)
                {
                    uint   sessionID = packet.ReadUInt();
                    string username  = packet.ReadAscii();
                    string password  = packet.ReadAscii();
                    byte   local     = packet.ReadByte();
                    //byte[] mac = packet.ReadByteArray(6); //No need

                    Packet response = new Packet(0xA103);
                    if (sessionID != uint.MaxValue)
                    {
                        response.WriteByte(0x02);
                        response.WriteByte(0x02);
                    }
                    else if (!string.IsNullOrEmpty(username))
                    {
                        response.WriteByte(0x02);
                        response.WriteByte(0x02);
                    }
                    else if (!string.IsNullOrEmpty(password))
                    {
                        response.WriteByte(0x02);
                        response.WriteByte(0x02);
                    }
                    else if (local != this.m_client.LocaleID)
                    {
                        response.WriteByte(0x02);
                        response.WriteByte(0x02);
                    }
                    else
                    {
                        response.WriteByte(0x01); //Success
                    }
                    response.Lock();
                    client.SendPacket(response);
                }

                #endregion

                #region 0x7007

                if (packet.Opcode == 0x7007)
                {
                    byte type = packet.ReadByte();
                    if (type == 0x02)
                    {
                        Packet responseEndCS = new Packet(0xB001);
                        responseEndCS.WriteByte(0x01);

                        Packet responseInitLoad = new Packet(0x34A5);

                        client.SendPacket(responseEndCS);
                        client.SendPacket(responseInitLoad);
                        this.m_client.IsWaitingForData = true;
                    }
                }

                #endregion

                #endregion
            }
            else
            {
                //Not sure why but after clientless->client the clients preferes to send 0x6103 twice.
                if (packet.Opcode == 0x6103)
                {
                    if (this.m_client.AgentLoginFixCounter > 0)
                    {
                        return(new PacketResult(PacketOperationType.IGNORE));
                    }
                    this.m_client.AgentLoginFixCounter++;
                }

                if (packet.Opcode == 0x6102)
                {
                    this.m_client.AgentLoginFixCounter = 0;
                }

                return(new PacketResult(PacketOperationType.NOTHING));

                //if (this.m_client.IsConnectedToAgent) {
                //    this.m_agentComponent.
                //    m_agentSocket.Send(packet);
                //} else {
                //    m_gatewaySocket.Send(packet);
                //}
            }

            return(new PacketResult(PacketOperationType.NOTHING));
        }