2019-05-15 14:56:09 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* (c) Copyright Ascensio System Limited 2010-2018
|
|
|
|
*
|
|
|
|
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
|
|
|
|
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
|
|
|
|
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
|
|
|
|
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
|
|
|
|
*
|
|
|
|
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
|
|
|
|
*
|
|
|
|
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
|
|
|
|
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
|
|
|
|
*
|
|
|
|
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
|
|
|
|
* relevant author attributions when distributing the software. If the display of the logo in its graphic
|
|
|
|
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
|
|
|
|
* in every copy of the program you distribute.
|
|
|
|
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2021-10-12 10:14:33 +00:00
|
|
|
using System.IO;
|
2019-05-15 14:56:09 +00:00
|
|
|
using System.Linq;
|
2021-10-12 10:14:33 +00:00
|
|
|
using System.Net.Http;
|
2019-05-15 14:56:09 +00:00
|
|
|
using System.Security.Cryptography;
|
|
|
|
using System.ServiceModel;
|
2020-02-17 08:58:14 +00:00
|
|
|
using System.Text;
|
2020-02-07 10:29:16 +00:00
|
|
|
|
2020-02-17 08:58:14 +00:00
|
|
|
using ASC.Common;
|
2019-05-15 14:56:09 +00:00
|
|
|
using ASC.Common.Logging;
|
2020-02-17 08:58:14 +00:00
|
|
|
using ASC.Core.Common.Notify.Jabber;
|
2020-09-15 13:11:05 +00:00
|
|
|
using ASC.Security.Cryptography;
|
|
|
|
|
2019-11-06 15:03:09 +00:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2020-02-17 08:58:14 +00:00
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
2019-05-15 14:56:09 +00:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
namespace ASC.Core.Notify.Signalr
|
2020-10-28 12:26:27 +00:00
|
|
|
{
|
|
|
|
[Scope]
|
2020-02-17 08:58:14 +00:00
|
|
|
public class ConfigureSignalrServiceClient : IConfigureNamedOptions<SignalrServiceClient>
|
|
|
|
{
|
2020-08-12 09:58:08 +00:00
|
|
|
internal TenantManager TenantManager { get; }
|
2020-09-15 13:11:05 +00:00
|
|
|
internal CoreSettings CoreSettings { get; }
|
2020-09-25 10:47:21 +00:00
|
|
|
internal MachinePseudoKeys MachinePseudoKeys { get; }
|
2020-08-12 09:58:08 +00:00
|
|
|
internal IConfiguration Configuration { get; }
|
2022-01-13 11:19:39 +00:00
|
|
|
internal IOptionsMonitor<ILog> Options { get; }
|
|
|
|
internal IHttpClientFactory ClientFactory { get; }
|
2020-02-17 08:58:14 +00:00
|
|
|
|
|
|
|
public ConfigureSignalrServiceClient(
|
|
|
|
TenantManager tenantManager,
|
2020-09-15 13:11:05 +00:00
|
|
|
CoreSettings coreSettings,
|
|
|
|
MachinePseudoKeys machinePseudoKeys,
|
2020-02-17 08:58:14 +00:00
|
|
|
IConfiguration configuration,
|
2022-01-13 11:19:39 +00:00
|
|
|
IOptionsMonitor<ILog> options,
|
|
|
|
IHttpClientFactory clientFactory)
|
2020-02-17 08:58:14 +00:00
|
|
|
{
|
|
|
|
TenantManager = tenantManager;
|
2020-09-15 13:11:05 +00:00
|
|
|
CoreSettings = coreSettings;
|
|
|
|
MachinePseudoKeys = machinePseudoKeys;
|
2020-02-17 08:58:14 +00:00
|
|
|
Configuration = configuration;
|
2022-01-13 11:19:39 +00:00
|
|
|
Options = options;
|
|
|
|
ClientFactory = clientFactory;
|
2020-02-17 08:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Configure(string name, SignalrServiceClient options)
|
|
|
|
{
|
2020-02-13 15:12:27 +00:00
|
|
|
options.Log = Options.CurrentValue;
|
|
|
|
options.hub = name.Trim('/');
|
|
|
|
options.TenantManager = TenantManager;
|
2022-01-13 11:19:39 +00:00
|
|
|
options.CoreSettings = CoreSettings;
|
|
|
|
options.ClientFactory = ClientFactory;
|
2020-09-15 13:11:05 +00:00
|
|
|
options.SKey = MachinePseudoKeys.GetMachineConstant();
|
2020-02-13 15:12:27 +00:00
|
|
|
options.Url = Configuration["web:hub:internal"];
|
|
|
|
options.EnableSignalr = !string.IsNullOrEmpty(options.Url);
|
2019-05-15 14:56:09 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-02-13 15:12:27 +00:00
|
|
|
var replaceSetting = Configuration["jabber:replace-domain"];
|
2019-05-15 14:56:09 +00:00
|
|
|
if (!string.IsNullOrEmpty(replaceSetting))
|
|
|
|
{
|
2020-02-13 15:12:27 +00:00
|
|
|
options.JabberReplaceDomain = true;
|
2019-05-15 14:56:09 +00:00
|
|
|
var q =
|
2019-08-15 12:04:42 +00:00
|
|
|
replaceSetting.Split(new[] { "->" }, StringSplitOptions.RemoveEmptyEntries)
|
2019-05-15 14:56:09 +00:00
|
|
|
.Select(s => s.Trim().ToLowerInvariant())
|
|
|
|
.ToList();
|
2020-02-13 15:12:27 +00:00
|
|
|
options.JabberReplaceFromDomain = q.ElementAt(0);
|
|
|
|
options.JabberReplaceToDomain = q.ElementAt(1);
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
2020-02-17 08:58:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Configure(SignalrServiceClient options)
|
|
|
|
{
|
|
|
|
Configure("default", options);
|
|
|
|
}
|
|
|
|
}
|
2020-10-19 15:53:15 +00:00
|
|
|
|
|
|
|
[Scope(typeof(ConfigureSignalrServiceClient))]
|
2020-02-13 15:12:27 +00:00
|
|
|
public class SignalrServiceClient
|
|
|
|
{
|
2021-12-28 16:01:12 +00:00
|
|
|
private static readonly TimeSpan Timeout = TimeSpan.FromSeconds(1);
|
2020-02-13 15:12:27 +00:00
|
|
|
internal ILog Log;
|
|
|
|
private static DateTime lastErrorTime;
|
2022-01-21 09:19:57 +00:00
|
|
|
public bool EnableSignalr { get; set; }
|
2020-09-15 13:11:05 +00:00
|
|
|
internal byte[] SKey;
|
2020-02-13 15:12:27 +00:00
|
|
|
internal string Url;
|
|
|
|
internal bool JabberReplaceDomain;
|
|
|
|
internal string JabberReplaceFromDomain;
|
|
|
|
internal string JabberReplaceToDomain;
|
|
|
|
|
|
|
|
internal string hub;
|
|
|
|
|
2020-08-12 09:58:08 +00:00
|
|
|
internal TenantManager TenantManager { get; set; }
|
2022-01-13 11:19:39 +00:00
|
|
|
internal CoreSettings CoreSettings { get; set; }
|
|
|
|
internal IHttpClientFactory ClientFactory { get; set; }
|
2020-02-13 15:12:27 +00:00
|
|
|
|
2020-02-17 08:58:14 +00:00
|
|
|
public SignalrServiceClient()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2020-02-13 15:12:27 +00:00
|
|
|
|
2019-05-15 14:56:09 +00:00
|
|
|
|
|
|
|
public void SendMessage(string callerUserName, string calleeUserName, string messageText, int tenantId,
|
|
|
|
string domain)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
domain = ReplaceDomain(domain);
|
|
|
|
var tenant = tenantId == -1
|
2019-09-17 12:42:32 +00:00
|
|
|
? TenantManager.GetTenant(domain)
|
|
|
|
: TenantManager.GetTenant(tenantId);
|
2022-01-12 15:42:03 +00:00
|
|
|
var isTenantUser = callerUserName.Length == 0;
|
2019-05-15 14:56:09 +00:00
|
|
|
var message = new MessageClass
|
|
|
|
{
|
2019-10-10 10:59:22 +00:00
|
|
|
UserName = isTenantUser ? tenant.GetTenantDomain(CoreSettings) : callerUserName,
|
2019-05-15 14:56:09 +00:00
|
|
|
Text = messageText
|
|
|
|
};
|
|
|
|
|
2019-08-15 12:04:42 +00:00
|
|
|
MakeRequest("send", new { tenantId = tenant.TenantId, callerUserName, calleeUserName, message, isTenantUser });
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SendInvite(string chatRoomName, string calleeUserName, string domain)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
domain = ReplaceDomain(domain);
|
|
|
|
|
2019-09-17 12:42:32 +00:00
|
|
|
var tenant = TenantManager.GetTenant(domain);
|
2019-05-15 14:56:09 +00:00
|
|
|
|
|
|
|
var message = new MessageClass
|
|
|
|
{
|
2019-10-10 10:59:22 +00:00
|
|
|
UserName = tenant.GetTenantDomain(CoreSettings),
|
2019-05-15 14:56:09 +00:00
|
|
|
Text = chatRoomName
|
|
|
|
};
|
|
|
|
|
2019-08-15 12:04:42 +00:00
|
|
|
MakeRequest("sendInvite", new { tenantId = tenant.TenantId, calleeUserName, message });
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SendState(string from, byte state, int tenantId, string domain)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
domain = ReplaceDomain(domain);
|
|
|
|
|
|
|
|
if (tenantId == -1)
|
|
|
|
{
|
2019-09-17 12:42:32 +00:00
|
|
|
tenantId = TenantManager.GetTenant(domain).TenantId;
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 12:04:42 +00:00
|
|
|
MakeRequest("setState", new { tenantId, from, state });
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SendOfflineMessages(string callerUserName, List<string> users, int tenantId)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-08-15 12:04:42 +00:00
|
|
|
MakeRequest("sendOfflineMessages", new { tenantId, callerUserName, users });
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SendUnreadCounts(Dictionary<string, int> unreadCounts, string domain)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
domain = ReplaceDomain(domain);
|
|
|
|
|
2019-09-17 12:42:32 +00:00
|
|
|
var tenant = TenantManager.GetTenant(domain);
|
2019-05-15 14:56:09 +00:00
|
|
|
|
2019-08-15 12:04:42 +00:00
|
|
|
MakeRequest("sendUnreadCounts", new { tenantId = tenant.TenantId, unreadCounts });
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SendUnreadUsers(Dictionary<int, Dictionary<Guid, int>> unreadUsers)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
MakeRequest("sendUnreadUsers", unreadUsers);
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SendUnreadUser(int tenant, string userId, int count)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-08-15 12:04:42 +00:00
|
|
|
MakeRequest("updateFolders", new { tenant, userId, count });
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SendMailNotification(int tenant, string userId, int state)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-08-15 12:04:42 +00:00
|
|
|
MakeRequest("sendMailNotification", new { tenant, userId, state });
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void EnqueueCall(string numberId, string callId, string agent)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
MakeRequest("enqueue", new { numberId, callId, agent });
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void IncomingCall(string callId, string agent)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
MakeRequest("incoming", new { callId, agent });
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void MissCall(string numberId, string callId, string agent)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
MakeRequest("miss", new { numberId, callId, agent });
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Reload(string numberId, string agentId = null)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-09-17 12:42:32 +00:00
|
|
|
var numberRoom = TenantManager.GetCurrentTenant().TenantId + numberId;
|
2019-05-15 14:56:09 +00:00
|
|
|
MakeRequest("reload", new { numberRoom, agentId });
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void FilesChangeEditors(int tenantId, string fileId, bool finish)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
MakeRequest("changeEditors", new { tenantId, fileId, finish });
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public T GetAgent<T>(string numberId, List<Guid> contactsResponsibles)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return MakeRequest<T>("GetAgent", new { numberId, contactsResponsibles });
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
|
|
|
ProcessError(error);
|
|
|
|
}
|
|
|
|
|
2019-08-15 13:00:20 +00:00
|
|
|
return default;
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private string ReplaceDomain(string domain)
|
|
|
|
{
|
|
|
|
if (JabberReplaceDomain && domain.EndsWith(JabberReplaceFromDomain))
|
|
|
|
{
|
|
|
|
var place = domain.LastIndexOf(JabberReplaceFromDomain);
|
|
|
|
if (place >= 0)
|
|
|
|
{
|
|
|
|
return domain.Remove(place, JabberReplaceFromDomain.Length).Insert(place, JabberReplaceToDomain);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ProcessError(Exception e)
|
|
|
|
{
|
|
|
|
Log.ErrorFormat("Service Error: {0}, {1}, {2}", e.Message, e.StackTrace,
|
|
|
|
(e.InnerException != null) ? e.InnerException.Message : string.Empty);
|
|
|
|
if (e is CommunicationException || e is TimeoutException)
|
|
|
|
{
|
|
|
|
lastErrorTime = DateTime.Now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private string MakeRequest(string method, object data)
|
|
|
|
{
|
|
|
|
if (!IsAvailable()) return "";
|
2021-10-12 10:14:33 +00:00
|
|
|
|
|
|
|
var request = new HttpRequestMessage();
|
|
|
|
request.Headers.Add("Authorization", CreateAuthToken());
|
|
|
|
request.Method = HttpMethod.Post;
|
|
|
|
request.RequestUri = new Uri(GetMethod(method));
|
|
|
|
|
2019-11-06 15:03:09 +00:00
|
|
|
var jsonData = JsonConvert.SerializeObject(data);
|
2021-10-12 10:14:33 +00:00
|
|
|
Log.DebugFormat("Method:{0}, Data:{1}", method, jsonData);
|
|
|
|
|
|
|
|
request.Content = new StringContent(jsonData, Encoding.UTF8, "application/json");
|
|
|
|
|
2022-01-13 11:19:39 +00:00
|
|
|
var httpClient = ClientFactory.CreateClient();
|
|
|
|
|
2021-11-24 19:34:39 +00:00
|
|
|
using (var response = httpClient.Send(request))
|
2021-10-12 10:14:33 +00:00
|
|
|
using (var stream = response.Content.ReadAsStream())
|
|
|
|
using (var streamReader = new StreamReader(stream))
|
|
|
|
{
|
|
|
|
return streamReader.ReadToEnd();
|
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private T MakeRequest<T>(string method, object data)
|
|
|
|
{
|
|
|
|
var resultMakeRequest = MakeRequest(method, data);
|
|
|
|
return JsonConvert.DeserializeObject<T>(resultMakeRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool IsAvailable()
|
|
|
|
{
|
|
|
|
return EnableSignalr && lastErrorTime + Timeout < DateTime.Now;
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetMethod(string method)
|
|
|
|
{
|
2022-01-14 13:12:37 +00:00
|
|
|
return $"{Url.TrimEnd('/')}/controller/{hub}/{method}";
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 10:32:12 +00:00
|
|
|
public string CreateAuthToken(string pkey = "socketio")
|
2019-05-15 14:56:09 +00:00
|
|
|
{
|
2020-09-15 13:11:05 +00:00
|
|
|
using var hasher = new HMACSHA1(SKey);
|
2019-11-06 15:03:09 +00:00
|
|
|
var now = DateTime.UtcNow.ToString("yyyyMMddHHmmss");
|
|
|
|
var hash = Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(string.Join("\n", now, pkey))));
|
2022-01-14 13:12:37 +00:00
|
|
|
return $"ASC {pkey}:{now}:{hash}";
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
}
|
2019-05-15 14:56:09 +00:00
|
|
|
}
|