Initialize() 공개 메소드

public Initialize ( ) : void
리턴 void
예제 #1
0
파일: Rcon.cs 프로젝트: aarondl/Project-2Q
        /// <summary>
        /// Authenticates a socket before accepting into
        /// a permanent connection (it's connection attempt
        /// may yet be rejected.)
        /// </summary>
        /// <param name="socketObj">The object to cast into a socket to query.</param>
        public void Authenticate(Object socketObj)
        {
            Socket s = (Socket)socketObj;

            int rconConId = this.NextRconID; //Don't bother sending CreateRootUser this ID. The socket
            if ( rconConId == -1 ) return; //doesn't exist for long enough for us to care. But it's important that it's there.

            if ( rconUsers == null ) {
                CreateRootUser( s );
                return;
            }

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat( "<{0}.{1}@{2}>",
                System.Diagnostics.Process.GetCurrentProcess().Id,
                DateTime.Now.ToString( "ddmmyyhhmmss" ),
                System.Environment.UserDomainName );

            string authmsg = "authenticate " + sb.ToString();
            int offset = 0;
            SocketError errorCode;
            do {
                try {
                    offset += s.Send( IRCProtocol.Ascii.GetBytes( authmsg ),
                        offset, authmsg.Length - offset, SocketFlags.None, out errorCode );
                }
                catch ( SocketException ) {
                    s.Close();
                    return; //Give up. They can reconnect.
                }
            } while ( offset < authmsg.Length );

            if ( errorCode != SocketError.Success ) { s.Close(); return; }

            //Get Auth response.
            byte[] recvBuf = new byte[rconfig.SocketBufferSize];
            offset = s.Receive( recvBuf, 0, rconfig.SocketBufferSize, SocketFlags.None, out errorCode );
            if ( offset == 0 || errorCode != SocketError.Success ) { s.Close(); return; }
            string authReply = IRCProtocol.Ascii.GetString( recvBuf, 0, offset );

            //Parse into name/hash and verify both for correctness.
            string[] unameAndHash = authReply.Split( ' ' );
            if ( unameAndHash.Length != 2 ) { s.Close(); return; }

            //Verify username.
            string username = unameAndHash[0];
            Nullable<RconUserFile.RconUser> rc = rconUsers.GetUser(username);
            if ( rc == null ) { s.Close(); return; }

            //Verify their hash.
            string theirHash = unameAndHash[1];
            string pass = rc.Value.Password;

            HMACMD5 hmac = new HMACMD5( IRCProtocol.Ascii.GetBytes( pass ) );
            hmac.Initialize();
            hmac.TransformFinalBlock( IRCProtocol.Ascii.GetBytes( sb.ToString() ), 0, theirHash.Length );
            string ourHash = RconUserFile.GetHashFromDigest( hmac.Hash );

            if ( !ourHash.Equals( theirHash ) ) { s.Close(); return; }

            authmsg = "success";
            offset = 0;
            do {
                try {
                    offset += s.Send( IRCProtocol.Ascii.GetBytes( authmsg ),
                        offset, authmsg.Length - offset, SocketFlags.None, out errorCode );
                }
                catch ( SocketException ) {
                    s.Close();
                    return; //Give up. They can reconnect.
                }
            } while ( offset < authmsg.Length );

            if ( errorCode != SocketError.Success ) { s.Close(); return; }

            //DO IT MAD AMOUNTS OF ARRAY LOOKUPS FOR NO REASON. L2TEMP VARIABLE PLZ
            rconConnections[rconConId] = new SocketPipe( s, rconConId, 30000, 5000, 0 );
            rconConnections[rconConId].OnReceive += new SocketPipe.ReceiveData( OnReceive );
            rconConnections[rconConId].OnDisconnect += new SocketPipe.NoParams( OnDisconnect );

            Thread t = new Thread( new ThreadStart( rconConnections[rconConId].ConstantPump ) );
            t.Start(); //Start sending anything we can :D
            rconConnections[rconConId].ConstantSiphon(); //Consume current thread generated by ConstantAccept.
        }
예제 #2
0
    string getRequest(string text)
    {
        System.Collections.Generic.SortedList<string, string> paramsOrdered = new SortedList<string, string>();
        paramsOrdered.Add("user", user);
        paramsOrdered.Add("voice", "Philippe");
        paramsOrdered.Add("coding", "lin");
        paramsOrdered.Add("parsing", "tags");
        paramsOrdered.Add("frequency", "48000");
        paramsOrdered.Add("header", "wav-header");

         

        byte[] bytes = Encoding.UTF8.GetBytes(text);
        //Console.Write(BitConverter.ToString(bytes));
        paramsOrdered.Add("text", Encoding.UTF8.GetString(bytes));

        //Calcul de la valeur de hashsage
        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
        byte[] keyByte = encoding.GetBytes(key);

        System.Security.Cryptography.HMACMD5 hmacMD5 = new System.Security.Cryptography.HMACMD5(keyByte);
        hmacMD5.Initialize();

        string concatParams = "";

        for (int i = 0; i < paramsOrdered.Count; i++)
            concatParams += paramsOrdered.ElementAt(i).Key + "=" + paramsOrdered.ElementAt(i).Value;

        byte[] hashByte = hmacMD5.ComputeHash(encoding.GetBytes(concatParams));

        // Construction de l'url d'invocation
        string webRequestString = @"http://ws.voxygen.fr/ws/tts1?";
        for (int i = 0; i < paramsOrdered.Count; i++)
        {
            if (i > 0) webRequestString += "&";
            webRequestString += paramsOrdered.ElementAt(i).Key + "=" + paramsOrdered.ElementAt(i).Value;
        }

        webRequestString += "&hmac=" + ByteToString(hashByte);
        return webRequestString;
    }