2019-06-07 08:59:07 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
2020-02-17 08:58:14 +00:00
|
|
|
|
using System.Web;
|
2019-12-20 11:17:01 +00:00
|
|
|
|
|
2020-02-17 08:58:14 +00:00
|
|
|
|
using ASC.Common;
|
|
|
|
|
using ASC.Core.Common.Configuration;
|
2019-06-07 08:59:07 +00:00
|
|
|
|
using ASC.FederatedLogin.LoginProviders;
|
2020-02-17 08:58:14 +00:00
|
|
|
|
using ASC.Web.Studio.Utility;
|
|
|
|
|
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
2019-06-07 08:59:07 +00:00
|
|
|
|
namespace ASC.Web.Core.Utility
|
|
|
|
|
{
|
|
|
|
|
public interface IUrlShortener
|
|
|
|
|
{
|
2019-12-20 11:17:01 +00:00
|
|
|
|
string GetShortenLink(string shareLink);
|
2019-06-07 08:59:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-24 10:32:12 +00:00
|
|
|
|
public class UrlShortener
|
2019-06-07 08:59:07 +00:00
|
|
|
|
{
|
2019-09-24 10:32:12 +00:00
|
|
|
|
public bool Enabled { get { return !(Instance is NullShortener); } }
|
2019-06-07 08:59:07 +00:00
|
|
|
|
|
2019-09-24 10:32:12 +00:00
|
|
|
|
private IUrlShortener _instance;
|
|
|
|
|
public IUrlShortener Instance
|
2019-06-07 08:59:07 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_instance == null)
|
|
|
|
|
{
|
2019-12-20 11:17:01 +00:00
|
|
|
|
if (ConsumerFactory.Get<BitlyLoginProvider>().Enabled)
|
2019-06-07 08:59:07 +00:00
|
|
|
|
{
|
2019-12-20 11:17:01 +00:00
|
|
|
|
_instance = new BitLyShortener(ConsumerFactory);
|
2019-06-07 08:59:07 +00:00
|
|
|
|
}
|
2019-09-24 10:32:12 +00:00
|
|
|
|
else if (!string.IsNullOrEmpty(Configuration["web:url-shortener"]))
|
2019-06-07 08:59:07 +00:00
|
|
|
|
{
|
2019-12-20 11:17:01 +00:00
|
|
|
|
_instance = new OnlyoShortener(Configuration, CommonLinkUtility);
|
2019-06-07 08:59:07 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_instance = new NullShortener();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _instance;
|
|
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
|
public ConsumerFactory ConsumerFactory { get; }
|
|
|
|
|
public CommonLinkUtility CommonLinkUtility { get; }
|
|
|
|
|
|
|
|
|
|
public UrlShortener(IConfiguration configuration, ConsumerFactory consumerFactory, CommonLinkUtility commonLinkUtility)
|
|
|
|
|
{
|
|
|
|
|
Configuration = configuration;
|
|
|
|
|
ConsumerFactory = consumerFactory;
|
|
|
|
|
CommonLinkUtility = commonLinkUtility;
|
2019-06-07 08:59:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class BitLyShortener : IUrlShortener
|
2020-02-17 08:58:14 +00:00
|
|
|
|
{
|
|
|
|
|
public BitLyShortener(ConsumerFactory consumerFactory)
|
|
|
|
|
{
|
|
|
|
|
ConsumerFactory = consumerFactory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ConsumerFactory ConsumerFactory { get; }
|
|
|
|
|
|
2019-12-20 11:17:01 +00:00
|
|
|
|
public string GetShortenLink(string shareLink)
|
2019-06-07 08:59:07 +00:00
|
|
|
|
{
|
2019-12-20 11:17:01 +00:00
|
|
|
|
return ConsumerFactory.Get<BitlyLoginProvider>().GetShortenLink(shareLink);
|
2019-06-07 08:59:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class OnlyoShortener : IUrlShortener
|
|
|
|
|
{
|
|
|
|
|
private readonly string url;
|
|
|
|
|
private readonly string internalUrl;
|
|
|
|
|
private readonly string sKey;
|
|
|
|
|
|
2019-12-20 11:17:01 +00:00
|
|
|
|
public OnlyoShortener(IConfiguration configuration, CommonLinkUtility commonLinkUtility)
|
2019-06-07 08:59:07 +00:00
|
|
|
|
{
|
2019-09-24 10:32:12 +00:00
|
|
|
|
url = configuration["web.url-shortener"];
|
|
|
|
|
internalUrl = configuration["web.url-shortener.internal"];
|
|
|
|
|
sKey = configuration["core.machinekey"];
|
2019-06-07 08:59:07 +00:00
|
|
|
|
|
|
|
|
|
if (!url.EndsWith("/"))
|
2020-02-17 08:58:14 +00:00
|
|
|
|
url += '/';
|
|
|
|
|
CommonLinkUtility = commonLinkUtility;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CommonLinkUtility CommonLinkUtility { get; }
|
|
|
|
|
|
2019-12-20 11:17:01 +00:00
|
|
|
|
public string GetShortenLink(string shareLink)
|
2019-06-07 08:59:07 +00:00
|
|
|
|
{
|
2020-02-17 08:58:14 +00:00
|
|
|
|
using var client = new WebClient { Encoding = Encoding.UTF8 };
|
|
|
|
|
client.Headers.Add("Authorization", CreateAuthToken());
|
2019-12-20 11:17:01 +00:00
|
|
|
|
return CommonLinkUtility.GetFullAbsolutePath(url + client.DownloadString(new Uri(internalUrl + "?url=" + HttpUtility.UrlEncode(shareLink))));
|
2019-06-07 08:59:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string CreateAuthToken(string pkey = "urlShortener")
|
|
|
|
|
{
|
2020-02-17 08:58:14 +00:00
|
|
|
|
using var hasher = new HMACSHA1(Encoding.UTF8.GetBytes(sKey));
|
|
|
|
|
var now = DateTime.UtcNow.ToString("yyyyMMddHHmmss");
|
|
|
|
|
var hash = Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(string.Join("\n", now, pkey))));
|
2019-08-15 15:08:40 +00:00
|
|
|
|
return string.Format("ASC {0}:{1}:{2}", pkey, now, hash);
|
2019-06-07 08:59:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class NullShortener : IUrlShortener
|
|
|
|
|
{
|
2019-12-20 11:17:01 +00:00
|
|
|
|
public string GetShortenLink(string shareLink)
|
2019-06-07 08:59:07 +00:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-02-17 08:58:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class UrlShortenerExtension
|
|
|
|
|
{
|
|
|
|
|
public static DIHelper AddUrlShortener(this DIHelper services)
|
|
|
|
|
{
|
|
|
|
|
services.TryAddScoped<UrlShortener>();
|
|
|
|
|
return services
|
|
|
|
|
.AddConsumerFactoryService()
|
|
|
|
|
.AddCommonLinkUtilityService();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-07 08:59:07 +00:00
|
|
|
|
}
|