public void findSelfIpAddress() { IPHostEntry Host = Dns.GetHostEntry(Dns.GetHostName()); foreach (IPAddress IP in Host.AddressList) { string tmp = IP.ToString(); if (tmp.Length >= 8 && tmp.Length <= 15) { this._selfIpAddress = tmp; break; } } }
public override byte [] ProcessDataReceived(byte [] data, int length) { string crlf = "" + (char)0xd + "" + (char)0xa; if (World.AllowHttpFrom != "0.0.0.0" && World.AllowHttpFrom != IP.ToString()) { Send403(); return(null); } if (!World.onHttpDataReceived(this, data, length)) { return(null); } try { string str = ""; for (int t = 0; t < length; t++) { str += "" + (char)data[t]; } string [] strs = str.Split(new char[] { ' ' }); if (strs[0] == "POST") { if (strs[1] == "/account.htm") { int iuser = str.IndexOf("username="******"password="******"Username : {0}, Password : {1}", username, password ); if (username.Length > 12 || password.Length > 12) { return(null); } Account newAccount = new Account(username, password, AccessLevels.PlayerLevel); Account myAccount = World.allAccounts.FindByUserName(username.ToUpper()); if (myAccount == null) { World.allAccounts.Add(newAccount); TextReader tx = new StreamReader("./http/accountcreated.htm"); string part2 = tx.ReadToEnd(); tx.Close(); string part1 = "HTTP/1.1 200 OK" + crlf + "Date: " + DateTime.Now.ToString("r") + crlf + "Server: Dr Nexus 0.1a" + crlf + "X-Powered-By: Dr Nexus" + crlf + "Cache-Control: private" + crlf + // "Keep-Alive: timeout=15, max=98" + crlf + "Connection: close" + crlf + //Keep-Alive" + crlf + // "Transfer-Encoding: chunked" + crlf + "Content-Length: " + (part2.Length).ToString() + crlf + "Content-Type: text/html" + crlf + crlf; string pageHtml = part1 + part2; Send(pageHtml); //Send( part2 ); return(null); } else { World.allAccounts.Add(newAccount); TextReader tx = new StreamReader("./http/accounterror.htm"); string part2 = tx.ReadToEnd(); tx.Close(); string part1 = "HTTP/1.1 200 OK" + crlf + "Date: " + DateTime.Now.ToString("r") + crlf + "Server: Dr Nexus 0.1a" + crlf + "X-Powered-By: Dr Nexus" + crlf + "Cache-Control: private" + crlf + // "Keep-Alive: timeout=15, max=98" + crlf + "Connection: close" + crlf + //Keep-Alive" + crlf + // "Transfer-Encoding: chunked" + crlf + "Content-Length: " + (part2.Length).ToString() + crlf + "Content-Type: text/html" + crlf + crlf; string pageHtml = part1 + part2; Send(pageHtml); //Send( part2 ); return(null); } } } if (strs[0] == "GET") { if (strs[1] == "/status.htm") { TextReader tx = new StreamReader("./http/status.htm"); string part2 = tx.ReadToEnd(); tx.Close(); string part3 = ""; string fonct = ""; for (int t = 0; t < part2.Length; t++) { if (part2[t] == '<' && state == 0) { state = 1; } else if (part2[t] == '?' && state == 1) { state = 2; } else if (part2[t] == '?' && state == 2) { state = 3; } else if (part2[t] == '>' && state == 3) { state = 0; fonct = "using System;using System.Collections;using Server.Items;using HelperTools; namespace Server { " + fonct + " }"; CSharpCodeProvider codeProvider = new CSharpCodeProvider(); ICodeCompiler compiler = codeProvider.CreateCompiler(); CompilerParameters parameters = new CompilerParameters(); parameters.GenerateExecutable = false; parameters.GenerateInMemory = true; parameters.MainClass = ""; foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) { parameters.ReferencedAssemblies.Add(asm.Location); } parameters.IncludeDebugInformation = false; //parameters.OutputAssembly = "./res.dll"; CompilerResults results = compiler.CompileAssemblyFromSource(parameters, fonct); if (results.Errors.Count > 0) { string errors = "Compilation failed:\n"; foreach (CompilerError err in results.Errors) { errors += err.ToString() + "\n"; } part3 += "<p>" + errors + "</p>"; } else { Assembly asm = results.CompiledAssembly; ConstructorInfo ct = Utility.FindConstructor("HttpHandler", asm); BaseHttpHandler h = (BaseHttpHandler)ct.Invoke(null); part3 += h.Get(); } } else if (state == 2) { fonct += part2[t]; } else { if (state == 1) { part3 += "<"; } state = 0; part3 += part2[t]; } } string part1 = "HTTP/1.1 200 OK" + crlf + "Date: " + DateTime.Now.ToString("r") + crlf + "Server: Dr Nexus 0.1a" + crlf + "X-Powered-By: Dr Nexus" + crlf + "Cache-Control: private" + crlf + "Connection: close" + crlf + "Content-Length: " + (part3.Length).ToString() + crlf + "Content-Type: text/html" + crlf + crlf; Send(part1 + part3); //Send( part3 ); return(null); } else if (strs[1] == "/account.htm") { TextReader tx = new StreamReader("./http/account.htm"); string part2 = tx.ReadToEnd(); tx.Close(); string part1 = "HTTP/1.1 200 OK" + crlf + "Date: " + DateTime.Now.ToString("r") + crlf + "Server: Dr Nexus 0.1a" + crlf + "X-Powered-By: Dr Nexus" + crlf + "Cache-Control: private" + crlf + // "Keep-Alive: timeout=15, max=98" + crlf + "Connection: close" + crlf + //Keep-Alive" + crlf + // "Transfer-Encoding: chunked" + crlf + "Content-Length: " + (part2.Length).ToString() + crlf + "Content-Type: text/html" + crlf + crlf; // string pageHtml = part1 + part2; Send(part1 + part2); //Send( part2 ); return(null); } Send404(); } } catch (Exception) { } Send403(); return(null); }
public override byte [] ProcessDataReceived(byte [] data, int length) { // Console.WriteLine("Rec {0}", data[ 0 ] ); // HexViewer.View( data, 0, length ); int t; switch (data[0]) { case 0x00: // Logon challenge /*foreach( Account acc in World.allAccounts ) * if ( acc.Ip != null && * ch.IP.Address == acc.Ip.Address && * //ch.Port == acc.Port && * (bool)tryLoggin[ ch.IP.Address.ToString() ] ) * { * Console.WriteLine("wait!!!"); * return new byte[] { 0, 3, 0xEE, 0x1, 0x19 }; * }*/ // Console.WriteLine( "Logon challenge" ); int clientVersion = (((int)data[11]) * 256) + data[12]; byte len = data[33]; userName = new byte[len]; //Console.Write( "User : "******""; for (t = 0; t < len; t++) { userName[t] = data[34 + t]; usern += "" + (char)data[34 + t]; // Console.Write( "{0}", "" + ((char)userName[ t ] ).ToString() ); } // Console.WriteLine( "" ); myAccount = World.allAccounts.FindByUserName(usern); if (World.FreeForAll) { if (myAccount == null) { World.allAccounts.Add(myAccount = new Account(usern, usern)); } } if (myAccount == null) { return new byte[] { 0x1, 0x4 } } ; if (myAccount.SelectedChar != null) { // Console.WriteLine("Already loggin"); return(new byte[] { 1, 0x6 }); // Already logged in } SHA1 sha = new SHA1CryptoServiceProvider(); string pass = "******" + myAccount.Password.ToUpper(); char [] passc = pass.ToCharArray(); byte [] passb = new byte[passc.Length]; int ti = 0; foreach (char c in passc) { passb[ti++] = (byte)c; } byte [] user = Concat(userName, passb); byte [] hash = sha.ComputeHash(user, 0, user.Length); byte [] res = new Byte[hash.Length + salt.Length]; t = 0; rand.NextBytes(salt); foreach (byte s in salt) { res[t++] = s; } foreach (byte s in hash) { res[t++] = s; } byte [] hash2 = sha.ComputeHash(res, 0, res.Length); byte [] x = Reverse(hash2); rN = Reverse(N); rand.NextBytes(b); rb = Reverse(b); BigInteger bi = new BigInteger(x); BigInteger bi2 = new BigInteger(rN); BigInteger g = new BigInteger(new byte[] { 7 }); v = g.modPow(bi, bi2); K = new BigInteger(new Byte[] { 3 }); BigInteger temp1 = K * v; BigInteger temp2 = g.modPow(new BigInteger(rb), new BigInteger(rN)); BigInteger temp3 = temp1 + temp2; B = temp3 % new BigInteger(rN); /* byte []ezfd= B.getBytes(); * Console.WriteLine("B"); * HexViewer.View( ezfd, 0, ezfd.Length ); * BigInteger C = new BigInteger(); * * Console.WriteLine("C/Rn {0}", temp3/new BigInteger( rN ) );*/ //Console.WriteLine("temp1 {0}",temp1.ToHexString()); //Console.WriteLine("temp2 {0}",temp2.ToHexString()); //Console.WriteLine("temp3 {0}",temp3.ToHexString()); /* for(int ll = 0;ll < 6;ll++) * { * C = B; * C += new BigInteger( rN ) * ll; * C -= temp1; * Console.WriteLine("temp3 {0}",C.ToHexString()); * }*/ byte [] pack = new byte[118]; pack[0] = pack[1] = 0; byte [] tB = Reverse(B.getBytes()); for (t = 0; t < tB.Length; t++) { pack[3 + t] = tB[t]; } pack[35] = 1; // g_length pack[36] = 7; // g pack[37] = 32; // n_len for (t = 0; t < N.Length; t++) { pack[38 + t] = N[t]; } for (t = 0; t < salt.Length; t++) { pack[70 + t] = salt[t]; } for (t = 0; t < 16; t++) { pack[102 + t] = 0; } return(pack); case 0x01: // Logon proof { //Console.WriteLine("Logon proof" ); byte [] A = new byte[32]; for (t = 0; t < 32; t++) { A[t] = data[t + 1]; } byte [] kM1 = new byte[20]; for (t = 0; t < 20; t++) { kM1[t] = data[t + 1 + 32]; } //A = new byte[] { 0x23, 0x2f, 0xb1, 0xb8, 0x85, 0x29, 0x64, 0x3d, 0x95, 0xb8, 0xdc, 0xe7, 0x8f, 0x27, 0x50, 0xc7, 0x5b, 0x2d, 0xf3, 0x7a, 0xcb, 0xa8, 0x73, 0xeb, 0x31, 0x07, 0x38, 0x39, 0xed, 0xa0, 0x73, 0x8d }; byte [] rA = Reverse(A); // B = new BigInteger( new byte[] { 0x64, 0x5d, 0x1f, 0x78, 0x97, 0x30, 0x73, 0x70, 0x1e, 0x12, 0xbc, 0x98, 0xaa, 0x38, 0xea, 0x99, 0xb4, 0xbc, 0x43, 0x5c, 0x32, 0xe8, 0x44, 0x7c, 0x73, 0xab, 0x07, 0x7a, 0xe4, 0xd7, 0x59, 0x64 } ); byte [] AB = Concat(A, Reverse(B.getBytes())); SHA1 shaM1 = new SHA1CryptoServiceProvider(); byte [] U = shaM1.ComputeHash(AB); // U = new byte[] { 0x2f, 0x49, 0x69, 0xac, 0x9f, 0x38, 0x7f, 0xd6, 0x72, 0x23, 0x6f, 0x94, 0x91, 0xa5, 0x16, 0x77, 0x7c, 0xdd, 0xe1, 0xc1 }; byte [] rU = Reverse(U); temp1 = v.modPow(new BigInteger(rU), new BigInteger(rN)); temp2 = temp1 * new BigInteger(rA); temp3 = temp2.modPow(new BigInteger(rb), new BigInteger(rN)); byte [] S1 = new byte[16]; byte [] S2 = new byte[16]; byte [] S = new byte[32]; byte [] temp = temp3.getBytes(); /* Console.WriteLine("temp"); * HexViewer.View( temp, 0, temp.Length ); * Console.WriteLine("temp1 {0}", temp1.ToHexString()); * Console.WriteLine("temp2 {0}", temp2.ToHexString()); * Console.WriteLine("temp3 {0}", temp3.ToHexString());*/ Buffer.BlockCopy(temp, 0, S, 0, temp.Length); byte [] rS = Reverse(S); for (t = 0; t < 16; t++) { S1[t] = rS[t * 2]; S2[t] = rS[(t * 2) + 1]; } byte [] hashS1 = shaM1.ComputeHash(S1); byte [] hashS2 = shaM1.ComputeHash(S2); myAccount.SS_Hash = new byte[hashS1.Length + hashS2.Length]; for (t = 0; t < hashS1.Length; t++) { myAccount.SS_Hash[t * 2] = hashS1[t]; myAccount.SS_Hash[(t * 2) + 1] = hashS2[t]; } // SS_Hash = new byte[] { 0x02, 0x61, 0xf4, 0xeb, 0x48, 0x91, 0xb6, 0x6a, 0x1a, 0x82, 0x6e, 0xb7, 0x79, 0x28, 0xd8, 0x64, 0xb7, 0xea, 0x14, 0x54, 0x38, 0xdb, 0x7c, 0xfd, 0x0d, 0x3d, 0x2f, 0xc0, 0x22, 0xce, 0xcc, 0x46, 0x83, 0x79, 0xf2, 0xc0, 0x87, 0x78, 0x7f, 0x14 }; byte [] NHash = shaM1.ComputeHash(N); byte [] GHash = shaM1.ComputeHash(new byte[] { 7 }); byte [] userHash = shaM1.ComputeHash(userName); byte [] NG_Hash = new byte[20]; for (t = 0; t < 20; t++) { NG_Hash[t] = (byte)(NHash[t] ^ GHash[t]); } byte [] Temp = Concat(NG_Hash, userHash); Temp = Concat(Temp, salt); Temp = Concat(Temp, A); Temp = Concat(Temp, B.getBytes()); Temp = Concat(Temp, K.getBytes()); //SS_Hash ); byte [] M1 = shaM1.ComputeHash(Temp); Temp = Concat(A, kM1); Temp = Concat(Temp, myAccount.SS_Hash); byte [] M2 = shaM1.ComputeHash(Temp); byte [] retur = new byte[M2.Length + 4 /*NG_Hash.Length */ + 2]; // byte []retur = new byte[ M2.Length + NG_Hash.Length + 2 ]; retur[0] = 0x1; retur[1] = 0x0; for (t = 0; t < M2.Length; t++) { retur[t + 2] = M2[t]; } //for(t = 0;t < NG_Hash.Length;t++ ) // retur[ t + 2 + 20 ] = NG_Hash[ t ]; // set the account properties Console.WriteLine("Logon proof for {0},{1}", IP.ToString(), myAccount.Username); myAccount.Ip = this.IP; myAccount.Port = 0; myAccount.K = myAccount.SS_Hash; return(retur); } case 0x02: // Reconnect challenge { // Console.WriteLine( "Reconnect challenge" ); byte [] packRecoChallenge = new byte[34]; packRecoChallenge[0] = 0x02; packRecoChallenge[1] = 0x00; for (t = 0; t < 16; t++) { packRecoChallenge[18 + t] = 0; } return(packRecoChallenge); } case 0x03: // Reconnect proof // Console.WriteLine( "Reconnect proof" ); return(new byte[] { 0x03, 0x00 }); case 0x04: // Update server // Console.WriteLine( "Update server" ); break; case 0x10: // Realm List // Console.WriteLine( "Realm lList request" ); string ip = World.ServerIP; /* if ( base.theClientHandler.IP.ToString().StartsWith( "192.168.0" ) ) * { * ip = "192.168.0.2"; * } * else*/ if (IP.ToString() == "127.0.0.1") { ip = "127.0.0.1"; } byte [] retData = new byte[25 + ip.Length + World.ServerName.Length + World.ServerPort.ToString().Length]; /* * byte []retData = new byte[ ]{ 0x10, 45, * 0x00, 0x00, 0x00, 0x00, * 0x00, * 0x01, 0x00, 0x00, 0x00, * 0x00, 0x00, * (byte)'D', (byte)'r', (byte)' ', (byte)'N', (byte)'e', * (byte)'x', (byte)'u', (byte)'s', * 0x00, (byte)'1', (byte)'9', (byte)'2', (byte)'.', * (byte)'1', (byte)'6', (byte)'8', (byte)'.', * (byte)'0', (byte)'.', * (byte)'2', * 0x3a, 0x38, 0x30, 0x38, 0x35, * 0x00, 0x00, 0x00, 0x00, * 0x00, 0x00, * 0x01, 0x00, * 0x02, 0x00 };*/ int offset = 0; Converter.ToBytes((byte)0x10, retData, ref offset); Converter.ToBytes((byte)43, retData, ref offset); Converter.ToBytes(1 /*World.allConnectedChars.Count*/, retData, ref offset); Converter.ToBytes((byte)0, retData, ref offset); Converter.ToBytes(1, retData, ref offset); Converter.ToBytes((short)0, retData, ref offset); Converter.ToBytes(World.ServerName, retData, ref offset); Converter.ToBytes((byte)0, retData, ref offset); Converter.ToBytes(ip, retData, ref offset); Converter.ToBytes((byte)':', retData, ref offset); Converter.ToBytes(World.ServerPort.ToString(), retData, ref offset); Converter.ToBytes((byte)0, retData, ref offset); Converter.ToBytes(0, retData, ref offset); // Converter.ToBytes( (short)0, retData, ref offset );// cr erreir //Converter.ToBytes( (short)1, retData, ref offset ); //Converter.ToBytes( (short)2, retData, ref offset ); Converter.ToBytes((short)World.allConnectedChars.Count, retData, ref offset); Converter.ToBytes((byte)0, retData, ref offset); Converter.ToBytes((short)1, retData, ref offset); int atlen = 1; offset -= 3; Converter.ToBytes(offset, retData, ref atlen); Console.WriteLine("Connected player(s) {0}", World.allConnectedChars.Count); /* if ( World.allConnectedChars.Count < 3 )*/ //Thread.Sleep( 500 ); return(retData); default: Console.WriteLine("Receive unknown command {0}", data[0]); break; } byte [] ret = { 0, 0, 0, 0 }; return(ret); }