public NetBuffer Decrypt(NetBuffer inBuf) { NetworkLog.Debug(NetworkLogFlags.Encryption, "Decrypting message using RSA"); //Log.Debug(NetworkLogFlags.Encryption, "Encrypted: ", Convert.ToBase64String(inBuf.Data, 0, inBuf.LengthBytes)); byte[] encrypted = inBuf.Data.SubArray(1, inBuf.LengthBytes - 1); byte[] decrypted; try { decrypted = _rsa.Decrypt(encrypted, false); } catch (Exception ex) { NetworkLog.Debug(NetworkLogFlags.Encryption, "Failed to decrypt message using RSA: ", ex); return(null); } var outBuf = new NetBuffer(); outBuf.Data = decrypted; outBuf.LengthBytes = decrypted.Length; //Log.Debug(NetworkLogFlags.Encryption, "Decrypted: ", Convert.ToBase64String(outBuf.Data, 0, outBuf.LengthBytes)); return(outBuf); }
public object Execute(object instance, params object[] parameters) { // TODO: catch reflection exceptions and NetworkException, what about other exceptions? can we rethrow them without unity crashing? try { return(info.Invoke(instance, parameters)); } catch (Exception e) { string paramstr; if (parameters.Length != 0) { paramstr = parameters[0].ToString(); for (int i = 1; i < parameters.Length; i++) { paramstr += ", " + parameters[i]; } } else { paramstr = String.Empty; } NetworkLog.Error(NetworkLogFlags.RPC, "Failed to invoke ", this, " with ", parameters.Length, " parameter(s): ", paramstr); NetworkLog.Error(NetworkLogFlags.RPC, e); // swallow the exception, so that we don't actually stop the execution flow in uLink. return(null); } }
public NetBuffer Encrypt(NetBuffer inBuf) { NetworkLog.Debug(NetworkLogFlags.Encryption, "Encrypting message using RSA"); //Log.Debug(NetworkLogFlags.Encryption, "PlainData: ", Convert.ToBase64String(inBuf.Data, 0, inBuf.LengthBytes)); byte[] unencrypted = inBuf.Data.SubArray(0, inBuf.LengthBytes); byte[] encrypted; try { encrypted = _rsa.Encrypt(unencrypted, false); } catch (Exception ex) { NetworkLog.Debug(NetworkLogFlags.Encryption, "Failed to encrypt message using RSA: ", ex); return(null); } ArrayUtility.Push(ref encrypted, NetworkMessage.ENCRYPTED_SIGNATURE); var outBuf = new NetBuffer(); outBuf.Data = encrypted; outBuf.LengthBytes = encrypted.Length; //Log.Debug(NetworkLogFlags.Encryption, "Encrypted: ", Convert.ToBase64String(outBuf.Data, 0, outBuf.LengthBytes)); return(outBuf); }
public static string GetHostName() { #if UNITY_BUILD if (UnityEngine.Application.webSecurityEnabled) { NetworkLog.Debug(NetworkLogFlags.Utility, "Can't get local host name when running as a webplayer"); return("localhost"); } #endif return(Dns.GetHostName()); }
public static IPAddress TryGetLocalIP() { IPAddress ip = null; try { ip = NetUtility.Resolve(GetHostName()); } catch (Exception ex) { NetworkLog.Debug(NetworkLogFlags.Utility, "Failed to get local ip address:", ex); } return(ip ?? IPAddress.Loopback); }
public static IPAddress Resolve(string host) { IPAddress ip = null; try { ip = NetUtility.Resolve(host); } catch (Exception ex) { NetworkLog.Debug(NetworkLogFlags.Utility, "Failed to resolve host '", host, "':", ex); } Assert(ip != null, "Unable to resolve host"); return(ip); }
public NetBuffer Encrypt(NetBuffer inBuf) { NetworkLog.Debug(NetworkLogFlags.Encryption, "Encrypting message using AES"); //Log.Debug(NetworkLogFlags.Encryption, "PlainData: ", Convert.ToBase64String(inBuf.Data, 0, inBuf.LengthBytes)); MemoryStream ms = null; CryptoStream cs = null; try { ms = new MemoryStream(); ms.WriteByte(NetworkMessage.ENCRYPTED_SIGNATURE); var iv = new byte[IV_SIZE]; NetRandom.Instance.NextBytes(iv); ms.Write(iv, 0, IV_SIZE); var encryptor = _aes.CreateEncryptor(_aes.Key, iv); cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write); cs.Write(inBuf.Data, 0, inBuf.LengthBytes); cs.FlushFinalBlock(); } catch (Exception ex) { NetworkLog.Debug(NetworkLogFlags.Encryption, "Failed to encrypt message using AES: ", ex); return(null); } finally { if (cs != null) { cs.Close(); } if (ms != null) { ms.Close(); } } var encrypted = ms.ToArray(); var outBuf = new NetBuffer(encrypted, 0, encrypted.Length * 8); //Log.Debug(NetworkLogFlags.Encryption, "Encrypted: ", Convert.ToBase64String(outBuf.Data, 0, outBuf.LengthBytes)); return(outBuf); }
private void _Initialize(Component source, UnityEngine.MonoBehaviour[] components, string rpcName, RuntimeTypeHandle messageInfoHandle) { string type = ""; int typeIndex = rpcName.IndexOf(':'); if (typeIndex != -1) { type = rpcName.Substring(0, typeIndex); rpcName = rpcName.Substring(typeIndex + 1); } foreach (var component in components) { if (!String.IsNullOrEmpty(type) && component.GetType().Name != type) { continue; } var candidate = new RPCMethod(component.GetType(), rpcName, true); if (candidate.isExecutable) { if (instance.IsNotNull()) { NetworkLog.Error(NetworkLogFlags.RPC, "Found two or more RPCs named '", rpcName, "' inside the receivers at ", source); instance = null; return; } instance = component; method = candidate; } } if (instance.IsNotNull()) { reader = new ParameterReader(method, messageInfoHandle); } else { Log.Error(NetworkLogFlags.RPC, "No receiver found for RPC '", rpcName, "' at ", source); } }
public NetBuffer Decrypt(NetBuffer inBuf) { NetworkLog.Debug(NetworkLogFlags.Encryption, "Decrypting message using AES"); //Log.Debug(NetworkLogFlags.Encryption, "Encrypted: ", Convert.ToBase64String(inBuf.Data, 0, inBuf.LengthBytes)); var ms = new MemoryStream(inBuf.Data, 1, inBuf.LengthBytes - 1); CryptoStream cs = null; byte[] decrypted; int decryptedCount; try { var iv = new byte[IV_SIZE]; ms.Read(iv, 0, IV_SIZE); var decryptor = _aes.CreateDecryptor(_aes.Key, iv); cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read); decrypted = new byte[inBuf.LengthBytes]; decryptedCount = cs.Read(decrypted, 0, decrypted.Length); } catch (Exception ex) { NetworkLog.Debug(NetworkLogFlags.Encryption, "Failed to decrypt message using AES: ", ex); return(null); } finally { if (cs != null) { cs.Close(); } ms.Close(); } var outBuf = new NetBuffer(decrypted, 0, decryptedCount * 8); //Log.Debug(NetworkLogFlags.Encryption, "Decrypted: ", Convert.ToBase64String(outBuf.Data, 0, outBuf.LengthBytes)); return(outBuf); }
public RPCMethod(Type instanceType, string name, bool isRPC) { try { const BindingFlags flags = BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; info = instanceType.GetMethod(name, flags); } catch (AmbiguousMatchException ex) { Log.Error(NetworkLogFlags.RPC, "Method name ", name, " declared in ", instanceType, " is ambiguous due to multiple matches: ", ex); info = null; } catch (Exception ex) { Log.Error(NetworkLogFlags.RPC, "Couldn't find method ", name, " declared in ", instanceType, ": ", ex); info = null; } if (info == null) { return; } if (isRPC && !name.StartsWith("RPC") && !Utility.HasAttributeWithPrefix(info, "RPC")) { NetworkLog.Warning(NetworkLogFlags.RPC, "Ignoring matched method ", name, " declared in ", instanceType, " because it is missing the RPC attribute or prefix"); info = null; } #if NO_COROUTINE_RPC if (info.ReturnType == typeof(IEnumerator)) { Log.Warning(NetworkLogFlags.RPC, "Ignoring matched method " + name + " declared in " + type + " because it is a coroutine"); info = null; } #endif }