Example #1
0
 protected StreamProviderBase(StreamProviderBase other)
 {
     contextNode            = other.contextNode;
     credentialCache        = other.credentialCache;
     credential             = other.credential;
     protocol               = other.protocol;
     credentialsNeeded      = other.credentialsNeeded;
     credentialsInitialized = other.credentialsInitialized;
     silent = other.silent;
 }
            public override bool Pass(INodeEndpointProtocolRequest request)
            {
                lock (this.stepLock)
                {
                    switch (this.currentStep)
                    {
                    case Step.WaitingForAuthData:
                    {
                        this.currentStep = Step.WaitingForAuthResult;
                        byte[] authData          = request.Message;
                        byte[] encryptedAuthData = Md5AuthDataAndPassword(authData, this.passwordMD5);
                        this.client.Send(encryptedAuthData);
                    }
                        return(false);

                    case Step.WaitingForAuthResult:
                    {
                        this.currentStep = Step.WaitingForAesKey;
                        if (request.RequestMessage() != "PASS")
                        {
                            this.client.Disconnect();
                        }
                        else
                        {
                            byte[] publicKey, privateKey;
                            Crypting.RsaGenerateKey(out publicKey, out privateKey);

                            this.client.Send(publicKey);
                            this.privateKey = privateKey;
                        }
                    }
                        return(false);

                    case Step.WaitingForAesKey:
                    {
                        this.currentStep = Step.Done;

                        byte[] encryptedMessage = request.Message;
                        byte[] message          = Crypting.RsaDecrypt(encryptedMessage, this.privateKey);
                        this.privateKey = null;

                        int keyLength = StreamProtocol <Stream> .ReadLeadBytes(message);

                        byte[] key = new byte[keyLength];
                        byte[] iv  = new byte[message.Length - sizeof(int) - keyLength];
                        Array.Copy(message, sizeof(int), key, 0, key.Length);
                        Array.Copy(message, sizeof(int) + keyLength, iv, 0, iv.Length);
                        SetKey(key, iv);
                        this.connectionEvent.Set();
                    }
                        return(false);
                    }
                }
                return(true);
            }
Example #3
0
        /// <summary>
        /// Shorthand to send a request for a default instance of this protocol's data from the server.
        /// Requires `SetServerDefaults()` to be implemented.
        /// </summary>
        protected static void QuickRequestToServer <T>(int retries) where T : PacketProtocol             //, new()
        {
            if (Main.netMode != 1)
            {
                throw new ModHelpersException("Not a client.");
            }

            T t = (T)StreamProtocol.CreateInstance(typeof(T));

            //T t = (T)Activator.CreateInstance( typeof(T), BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { }, null );

            t.SendRequestToServer(retries);
        }
Example #4
0
        /// <summary>
        /// Shorthand to send a request for a default instance of this protocol's data from a client.
        /// Requires `SetClientDefaults()` to be implemented.
        /// </summary>
        /// <param name="toWho">Main.player index of player (client) being requested for this data. -1 for all clients.</param>
        /// <param name="ignoreWho">Main.player index of player (client) being ignored. -1 for no client.</param>
        /// <param name="retries"></param>
        protected static void QuickRequestToClient <T>(int toWho, int ignoreWho, int retries) where T : PacketProtocol
        {
            if (Main.netMode != 2)
            {
                throw new ModHelpersException("Not server.");
            }

            T t = (T)StreamProtocol.CreateInstance(typeof(T));

            //T t = (T)Activator.CreateInstance( typeof(T), BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { }, null );

            t.SendRequestToClient(toWho, ignoreWho, retries);
        }
Example #5
0
        private static void QuickSendToServerBase <T>(bool syncToClients)
            where T : PacketProtocol                        //, new()
        {
            if (Main.netMode != 1)
            {
                throw new ModHelpersException("Can only send as client.");
            }

            T t = (T)StreamProtocol.CreateInstance(typeof(T));

            //T t = (T)Activator.CreateInstance( typeof(T), BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { }, null );
            t.SetClientDefaults();
            t.OnClone();

            t.SendToServer(syncToClients);
        }
Example #6
0
        /// <summary>
        /// Shorthand to send a default instance of this protocol's data to a client. Requires `SetServerDefaults()`
        /// to be implemented.
        /// </summary>
        /// <param name="toWho">Main.player index of player (client) receiving this data. -1 for all clients.</param>
        /// <param name="ignoreWho">Main.player index of player (client) being ignored. -1 for no client.</param>
        protected static void QuickSendToClient <T>(int toWho, int ignoreWho)
            where T : PacketProtocol                        //, new()
        {
            if (Main.netMode != 2)
            {
                throw new ModHelpersException("Can only send as client.");
            }

            T t = (T)StreamProtocol.CreateInstance(typeof(T));

            //T t = (T)Activator.CreateInstance( typeof(T), BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { }, null );

            t.SetServerDefaults(toWho);
            t.OnClone();

            t.SendToClient(toWho, ignoreWho);
        }
Example #7
0
        internal static void HandlePacketOnServer(int protocolCode, BinaryReader reader, int playerWho)
        {
            var  mymod = ModHelpersMod.Instance;
            bool isRequest, isSyncedToClients;

            Type protocolType = mymod.PacketProtocolMngr.GetProtocolType(protocolCode);

            if (protocolType == null)
            {
                throw new ModHelpersException("Invalid protocol (hash: " + protocolCode + ")");
            }

            try {
                isRequest         = reader.ReadBoolean();
                isSyncedToClients = reader.ReadBoolean();
            } catch (Exception e) {
                throw new ModHelpersException("Could not read data for protocol " + protocolType.Namespace + "." + protocolType.Name + " - " + e.ToString());
            }

            try {
                var protocol = (PacketProtocol)StreamProtocol.CreateInstance(protocolType);
                //var protocol = (PacketProtocol)Activator.CreateInstance( protocolType, BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { }, null );

                if (isRequest)
                {
                    protocol.ReceiveRequestWithServerBase(playerWho);
                    protocol.OnClone();
                }
                else
                {
                    if (protocol.IsAsync)
                    {
                        ThreadPool.QueueUserWorkItem(_ => {
                            protocol.HandleServer_Core(reader, playerWho, isSyncedToClients);
                        });
                    }
                    else
                    {
                        protocol.HandleServer_Core(reader, playerWho, isSyncedToClients);
                    }
                }
            } catch (Exception e) {
                throw new ModHelpersException("Error handling " + protocolType.Namespace + "." + protocolType.Name + " - " + e.ToString());
            }
        }
Example #8
0
 public StreamProvider(StreamProvider other)
 {
     parent                 = other.parent;
     contextNode            = other.contextNode;
     fullName               = other.fullName;
     virtualName            = other.virtualName;
     virtualRoot            = other.virtualRoot;
     relativeName           = other.relativeName;
     uri                    = other.uri;
     credentialCache        = other.credentialCache;
     protocol               = other.protocol;
     lastModUtc             = other.lastModUtc;
     attributes             = other.attributes;
     size                   = other.size;
     credentialsNeeded      = other.credentialsNeeded;
     credentialsInitialized = other.credentialsInitialized;
     silent                 = other.silent;
     isDir                  = other.isDir;
 }
            public override bool Pass(INodeEndpointProtocolRequest request)
            {
                lock (this.stepLock)
                {
                    switch (this.currentStep)
                    {
                    case Step.WaitingForUserName:
                    {
                        this.currentStep = Step.WaitingForEncryptedAuthData;
                        string userName    = request.RequestMessage();
                        byte[] passwordMD5 = this.authenticationProvider.GetUserNamePasswordMD5(userName);
                        byte[] authData    = new byte[128];
                        new Random().NextBytes(authData);

                        if (passwordMD5 != null)
                        {
                            this.expectedMixedAuthDataAndPassword = Md5AuthDataAndPassword(authData, passwordMD5);
                        }
                        request.Respond(authData);
                    }
                        return(false);

                    case Step.WaitingForEncryptedAuthData:
                    {
                        if (this.expectedMixedAuthDataAndPassword == null)
                        {
                            request.Respond("FAIL");
                        }
                        else
                        {
                            this.currentStep = Step.WaitingForPublicKey;
                            byte[] expected = this.expectedMixedAuthDataAndPassword;
                            byte[] actual   = request.Message;
                            this.expectedMixedAuthDataAndPassword = null;

                            bool fail = false;
                            if (expected.Length != actual.Length)
                            {
                                fail = true;
                            }
                            else
                            {
                                for (int i = 0; i < expected.Length; i++)
                                {
                                    if (expected[i] != actual[i])
                                    {
                                        fail = true;
                                        break;
                                    }
                                }
                            }

                            request.Respond(fail ? "FAIL" : "PASS");
                        }
                    }
                        return(false);

                    case Step.WaitingForPublicKey:
                    {
                        this.currentStep = Step.Done;
                        byte[] publicKey = request.Message;
                        byte[] key, iv;
                        Crypting.AesGenerateKey(out key, out iv);

                        byte[] message = new byte[sizeof(int) + key.Length + iv.Length];
                        StreamProtocol <Stream> .WriteLeadBytes(key.Length, message);

                        Array.Copy(key, 0, message, sizeof(int), key.Length);
                        Array.Copy(iv, 0, message, sizeof(int) + key.Length, iv.Length);

                        byte[] encryptedMessage = Crypting.RsaEncrypt(message, publicKey);
                        request.Respond(encryptedMessage);

                        SetKey(key, iv);
                    }
                        return(false);
                    }
                }
                return(true);
            }
Example #10
0
 public StreamProtocolRequest(StreamProtocol <StreamType> protocolBase, byte[] message)
 {
     this.protocolBase = protocolBase;
     this.message      = message;
 }