forked from ringcentral/RingCentral.Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PubNubExtension.cs
178 lines (158 loc) · 5.97 KB
/
PubNubExtension.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Org.BouncyCastle.Crypto.IO;
using Org.BouncyCastle.Security;
using PubnubApi;
namespace RingCentral.Net.PubNub
{
public class PubNubExtension : SdkExtension
{
public RestClient rc;
public readonly List<Subscription> subscriptions = new List<Subscription>();
public PubNubExtension()
{
var frameworkDescription = RuntimeInformation.FrameworkDescription;
if (!frameworkDescription.StartsWith(".NET Framework "))
{
throw new NotSupportedException(@"Package RingCentral.NET.Pubnub only supports .NET Framework.
Please install package RingCentral.Net.PubnubPCL instead.");
}
}
public override void Install(RestClient restClient)
{
this.rc = restClient;
}
public async void Revoke()
{
foreach (var subscription in subscriptions)
{
await subscription.Revoke();
}
this.subscriptions.Clear();
}
public async Task<Subscription> Subscribe(string[] eventFilters, Action<string> callback)
{
var subscription = new Subscription(this, eventFilters, callback);
await subscription.Subscribe();
this.subscriptions.Add(subscription);
return subscription;
}
}
public class Subscription
{
public readonly PubNubExtension pne;
public readonly string[] eventFilters;
public Action<string> callback;
private Pubnub _pubnub;
private bool _renewScheduled;
private SubscriptionInfo _subscriptionInfo;
public SubscriptionInfo SubscriptionInfo
{
get => _subscriptionInfo;
private set
{
_subscriptionInfo = value;
if (value == null || _renewScheduled) return;
Debug.Assert(_subscriptionInfo.expiresIn != null, "subscriptionInfo.expiresIn != null");
Task.Delay((int) (_subscriptionInfo.expiresIn.Value - 120) * 1000).ContinueWith(
async action =>
{
// 2 minutes before expiration
_renewScheduled = false;
await Refresh();
});
_renewScheduled = true;
}
}
internal Subscription(PubNubExtension pne, string[] eventFilters, Action<string> callback)
{
this.pne = pne;
this.eventFilters = eventFilters;
this.callback = callback;
}
internal async Task<SubscriptionInfo> Subscribe()
{
var requestBody = new
{
eventFilters,
deliveryMode = new {transportType = "PubNub", encryption = true}
};
var httpContent = new StringContent(JsonConvert.SerializeObject(requestBody), Encoding.UTF8,
"application/json");
SubscriptionInfo = await pne.rc.Post<SubscriptionInfo>("/restapi/v1.0/subscription", httpContent);
var pnConfiguration = new PNConfiguration
{
SubscribeKey = SubscriptionInfo.deliveryMode.subscriberKey,
ReconnectionPolicy = PNReconnectionPolicy.LINEAR,
Origin = "ringcentral.pubnubapi.com"
};
_pubnub = new Pubnub(pnConfiguration);
_pubnub.AddListener(new SubscribeCallbackExt(
(pubnubObj, message) => { callback(Decrypt(message.Message.ToString())); },
(pubnubObj, presence) => { },
(pubnubObj, status) => { }
));
_pubnub.Subscribe<string>().Channels(new[]
{
SubscriptionInfo.deliveryMode.address
}).Execute();
return SubscriptionInfo;
}
public async Task<SubscriptionInfo> Refresh()
{
SubscriptionInfo =
await pne.rc.Post<SubscriptionInfo>($"/restapi/v1.0/subscription/{SubscriptionInfo.id}/renew");
return SubscriptionInfo;
}
public async Task<HttpResponseMessage> Revoke()
{
try
{
var r = await pne.rc.Delete($"/restapi/v1.0/subscription/{_subscriptionInfo.id}");
return r;
}
catch (RestException re)
{
if (re.httpResponseMessage.StatusCode == HttpStatusCode.NotFound) // already deleted
{
return re.httpResponseMessage;
}
throw;
}
finally
{
_pubnub.Destroy();
_pubnub = null;
SubscriptionInfo = null;
}
}
private string Decrypt(string dataString)
{
var key = Convert.FromBase64String(SubscriptionInfo.deliveryMode.encryptionKey);
var keyParameter = ParameterUtilities.CreateKeyParameter("AES", key);
var cipher = CipherUtilities.GetCipher("AES/ECB/PKCS7Padding");
cipher.Init(false, keyParameter);
var data = Convert.FromBase64String(dataString);
var memoryStream = new MemoryStream(data, false);
var cipherStream = new CipherStream(memoryStream, cipher, null);
const int bufferSize = 1024;
var buffer = new byte[bufferSize];
int length;
var resultStream = new MemoryStream();
while ((length = cipherStream.Read(buffer, 0, bufferSize)) > 0)
{
resultStream.Write(buffer, 0, length);
}
var resultBytes = resultStream.ToArray();
return Encoding.UTF8.GetString(resultBytes, 0, resultBytes.Length);
}
}
}