/// <summary> /// Gets the server info. /// </summary> /// <param name="callback">Callback.</param> public virtual void GetServerInfo(Action <bool, CombuServerInfo> callback) { var form = CreateForm(); form.AddField("action", "info"); CallWebservice(GetUrl("server.php"), form, (string text, string error) => { bool success = false; Hashtable data = new Hashtable(); if (string.IsNullOrEmpty(error) && !string.IsNullOrEmpty(text)) { data = text.hashtableFromJson(); if (data != null) { success = true; } } CombuServerInfo info = null; if (success) { info = new CombuServerInfo(data); } if (callback != null) { callback(success, info); } }); }
/// <summary> /// Initializes the connection to Combu server: retrieves the RSA Public Key generated from the server, then sends the AES Key and IV generated from the client (encrypted by RSA). /// </summary> void InitializeServer(Action <bool> callback) { if (string.IsNullOrEmpty(appId) || string.IsNullOrEmpty(appSecret)) { Debug.LogError("Combu initialization failed: missing App Id or Secret Key"); return; } Debug.Log("Connecting to Combu server..."); WWWForm form = new WWWForm(); form.AddField("action", "token"); form.AddField("version", COMBU_VERSION); CallWebservice(GetUrl("server.php"), form, (string text, string error) => { string token = string.Empty, xml = string.Empty, modulus = string.Empty, exponent = string.Empty; if (string.IsNullOrEmpty(error) && !string.IsNullOrEmpty(text)) { Hashtable data = text.hashtableFromJson(); if (data != null) { if (data.ContainsKey("token") && !string.IsNullOrEmpty("" + data["token"])) { token = data["token"].ToString().Trim(); } if (data.ContainsKey("xml") && !string.IsNullOrEmpty("" + data["xml"])) { xml = data["xml"].ToString().Trim(); } if (data.ContainsKey("modulus") && !string.IsNullOrEmpty("" + data["modulus"])) { modulus = data["modulus"].ToString().Trim(); } if (data.ContainsKey("exponent") && !string.IsNullOrEmpty("" + data["exponent"])) { exponent = data["exponent"].ToString().Trim(); } } } if (!string.IsNullOrEmpty(token) && (!string.IsNullOrEmpty(xml) || (!string.IsNullOrEmpty(modulus) && !string.IsNullOrEmpty(exponent)))) { if (!string.IsNullOrEmpty(xml)) { encryption.LoadRSA(token, xml); } else { encryption.LoadRSA(token, modulus, exponent); } form = new WWWForm(); form.AddField("action", "authorize"); form.AddField("token", token); form.AddField("app", encryption.EncryptRSA(appId)); form.AddField("secret", encryption.EncryptRSA(appSecret)); form.AddField("key", encryption.EncryptRSA(encryption.Key)); form.AddField("iv", encryption.EncryptRSA(encryption.IV)); CallWebservice(GetUrl("server.php"), form, (string textAuthorize, string errorAuthorize) => { bool successAuthorize = false; if (string.IsNullOrEmpty(errorAuthorize) && !string.IsNullOrEmpty(textAuthorize)) { if (string.IsNullOrEmpty(errorAuthorize) && !string.IsNullOrEmpty(textAuthorize)) { var result = textAuthorize.hashtableFromJson(); if (result != null) { if (result.ContainsKey("success")) { if (!bool.TryParse(result["success"].ToString(), out successAuthorize)) { successAuthorize = false; } } if (!successAuthorize && result.ContainsKey("message")) { errorAuthorize = result["message"] + ""; } } } } if (!successAuthorize) { Debug.LogError("Failed authorization from Combu server" + (string.IsNullOrEmpty(errorAuthorize) ? "" : ": " + errorAuthorize)); if (callback != null) { callback(false); } } else { // Get the server info GetServerInfo((bool success, CombuServerInfo loadedInfo) => { if (success) { _serverInfo = loadedInfo; Debug.Log(_serverInfo.ToString()); } else { _serverInfo = new CombuServerInfo(); Debug.LogError("Failed to get Combu server info"); } if (callback != null) { callback(success); } }); } }); } else { Debug.LogError("Failed connection to Combu server" + (string.IsNullOrEmpty(error) ? "" : ": " + error)); if (callback != null) { callback(false); } } }); }