Beispiel #1
0
        /// <summary>
        /// Create a message by a clients authentication state and authentication method
        /// </summary>
        /// <param name="data">Input data received from client</param>
        /// <param name="authenticationState">Clients current authentication state</param>
        /// <param name="authenticationMethod">If authentication state is Authenticating, a authenticationMethod is required to return a message according to sub negotation</param>
        /// <returns>A socks message, or null if Parsing failed</returns>
        private Plugin.ISocksMessage GetMessage(byte[] data, Socks.Constants.AuthenticationState authenticationState, Socks.Constants.AuthenticationMethod authenticationMethod)
        {
            Plugin.ISocksMessage message = null;

            using (MemoryStream stream = new MemoryStream(data))
                using (BinaryReader reader = new BinaryReader(stream))
                {
                    if (authenticationState == Socks.Constants.AuthenticationState.Awaiting)
                    {
                        message = this.SocksMessageFactory.Create <Socks.Message.Awaiting>(reader);
                    }
                    else if (authenticationState == Socks.Constants.AuthenticationState.Authenticating &&
                             authenticationMethod == Socks.Constants.AuthenticationMethod.UsernamePassword)
                    {
                        message = this.SocksMessageFactory.Create <Socks.Message.Authentication.UsernamePassword>(reader);
                    }
                    else if (authenticationState == Socks.Constants.AuthenticationState.Authenticated)
                    {
                        message = this.SocksMessageFactory.Create <Socks.Message.Command>(reader);
                    }
                }

            return(message);
        }
Beispiel #2
0
        /// <summary>
        /// EventHandler for AsyncTCPLib.Server.OnClientDataReceived
        /// </summary>
        private async void OnClientDataReceived(object sender, OnClientDataReceivedEventArgs <Socks.Client> e)
        {
            var localEvent = new OnClientDataReceivedEventArgs <VirtualClient>(e.Client, e.Data, e.RemoteEndPoint);


            Socks.Constants.AuthenticationState currentState = e.Client.Data.AuthenticationState;
            Plugin.ISocksMessage message = this.GetMessage(e.Data, currentState, e.Client.Data.AuthenticationMethod);

            if (currentState != Socks.Constants.AuthenticationState.Transmitting && message == null)
            {
                this.Log.Debug("Received invalid data from ");
                e.Client.Disconnect();
                return;
            }

            // go through each plugin to process data
            foreach (Plugin.IPlugin plugin in this.Plugins)
            {
                if (plugin.HasRawHandler())
                {
                    plugin.RawHandler.OnClientDataReceived(localEvent);
                }
                if (plugin.HasStateDependentHandler())
                {
                    // AuthenticationState.Awaiting
                    if (currentState == Socks.Constants.AuthenticationState.Awaiting)
                    {
                        if (!await plugin.StateDependentHandler.Awaiting(e.Client, message))
                        {
                            e.Client.Disconnect();
                            return;
                        }
                    }
                    // AuthenticationState.Authenticating, awaiting sub negotiation
                    else if (currentState == Socks.Constants.AuthenticationState.Authenticating)
                    {
                        if (!await plugin.StateDependentHandler.Authenticating(e.Client, message))
                        {
                            e.Client.Disconnect();
                            return;
                        }
                    }
                    // AuthenticationState.Authenticated
                    else if (currentState == Socks.Constants.AuthenticationState.Authenticated)
                    {
                        if (!await plugin.StateDependentHandler.Command(e.Client, message))
                        {
                            e.Client.Disconnect();
                            return;
                        }
                    }
                    else if (currentState == Socks.Constants.AuthenticationState.Transmitting)
                    {
                        if (!await plugin.StateDependentHandler.Transmission(e.Client, e.Data))
                        {
                            e.Client.Disconnect();
                            return;
                        }
                    }
                }
            }
        }