Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

403 Investigation #124

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions src/Api/PubnubApi/EndPoint/PubSub/SubscribeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ internal void MultiChannelSubscribeInit<T>(PNOperationType responseType, string[
{
bool channelGroupSubscribeOnly = false;
SubscribeDisconnected[PubnubInstance.InstanceId] = false;
SubscribeAccessDenied[PubnubInstance.InstanceId] = false;

if (rawChannels != null && rawChannels.Length > 0)
{
Expand Down Expand Up @@ -555,14 +556,7 @@ internal void MultiChannelSubscribeInit<T>(PNOperationType responseType, string[
ResetInternetCheckSettings(channels, channelGroups);
MultiChannelSubscribeRequest<T>(responseType, channels, channelGroups, 0, 0, false, initialSubscribeUrlParams, externalQueryParam);

if (SubscribeHeartbeatCheckTimer != null)
{
try
{
SubscribeHeartbeatCheckTimer.Change(Timeout.Infinite, Timeout.Infinite);
}
catch { /* ignore */ }
}
CheckAndDisableubscribeHeartbeatCheckTimer();
SubscribeHeartbeatCheckTimer = new Timer(StartSubscribeHeartbeatCheckCallback<T>, null, config.SubscribeTimeout * 500, config.SubscribeTimeout * 500);
}
}
Expand All @@ -572,6 +566,18 @@ internal void MultiChannelSubscribeInit<T>(PNOperationType responseType, string[
}
}

private static void CheckAndDisableubscribeHeartbeatCheckTimer()
{
if (SubscribeHeartbeatCheckTimer != null)
{
try
{
SubscribeHeartbeatCheckTimer.Change(Timeout.Infinite, Timeout.Infinite);
}
catch { /* ignore */ }
}
}

private void MultiChannelSubscribeRequest<T>(PNOperationType type, string[] channels, string[] channelGroups, object timetoken, int region, bool reconnect, Dictionary<string, string> initialSubscribeUrlParams, Dictionary<string, object> externalQueryParam)
{
if (SubscribeDisconnected[PubnubInstance.InstanceId])
Expand Down Expand Up @@ -670,9 +676,11 @@ private void MultiChannelSubscribeRequest<T>(PNOperationType type, string[] chan

// Wait for message
string json = "";
PNStatus pnStatus = null;
UrlProcessRequest<T>(request, pubnubRequestState, false).ContinueWith(r =>
{
json = r.Result.Item1;
pnStatus = r.Result.Item2;
}, TaskContinuationOptions.ExecuteSynchronously).Wait();
if (!string.IsNullOrEmpty(json))
{
Expand Down Expand Up @@ -730,6 +738,22 @@ private void MultiChannelSubscribeRequest<T>(PNOperationType type, string[] chan
}
else
{
if (pnStatus != null && pnStatus.Category == PNStatusCategory.PNAccessDeniedCategory)
{
//Announce 403 and exit subscribe on 403
Announce(pnStatus); //Announce to subscribe callback when 403

///Disabling flags/checks which can invoke resubscribe
SubscribeAccessDenied[PubnubInstance.InstanceId] = true;
Task.Factory.StartNew(() =>
{
TerminateCurrentSubscriberRequest();
}, CancellationToken.None, TaskCreationOptions.PreferFairness, TaskScheduler.Default).ConfigureAwait(false);

CheckAndDisableubscribeHeartbeatCheckTimer();
return;
}

if (multiplexExceptionTimer != null)
{
multiplexExceptionTimer.Change(Timeout.Infinite, Timeout.Infinite);
Expand Down Expand Up @@ -1027,6 +1051,12 @@ internal void StartSubscribeHeartbeatCheckCallback<T>(object state)
LoggingMethod.WriteToLog(pubnubLog, string.Format("DateTime {0}, SubscribeManager - SubscribeDisconnected. No heartbeat check.", DateTime.Now.ToString(CultureInfo.InvariantCulture)), config.LogVerbosity);
return;
}
if (SubscribeAccessDenied[PubnubInstance.InstanceId])
{
CheckAndDisableubscribeHeartbeatCheckTimer();
LoggingMethod.WriteToLog(pubnubLog, string.Format("DateTime {0}, SubscribeManager - SubscribeAccessDenied. Exiting StartSubscribeHeartbeatCheckCallback.", DateTime.Now.ToString(CultureInfo.InvariantCulture)), config.LogVerbosity);
return;
}

string[] channels = GetCurrentSubscriberChannels();
string[] chananelGroups = GetCurrentSubscriberChannelGroups();
Expand Down
4 changes: 4 additions & 0 deletions src/Api/PubnubApi/EndPoint/PubSub/SubscribeOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ internal void CurrentPubnubInstance(Pubnub instance)
{
SubscribeDisconnected.GetOrAdd(instance.InstanceId, false);
}
if (!SubscribeAccessDenied.ContainsKey(instance.InstanceId))
{
SubscribeAccessDenied.GetOrAdd(instance.InstanceId, false);
}
if (!LastSubscribeTimetoken.ContainsKey(instance.InstanceId))
{
LastSubscribeTimetoken.GetOrAdd(instance.InstanceId, 0);
Expand Down
1 change: 1 addition & 0 deletions src/Api/PubnubApi/PubnubCoreBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public abstract class PubnubCoreBase

private bool clientNetworkStatusInternetStatus = true;
protected static ConcurrentDictionary<string, bool> SubscribeDisconnected { get; set; } = new ConcurrentDictionary<string, bool>();
protected static ConcurrentDictionary<string, bool> SubscribeAccessDenied { get; set; } = new ConcurrentDictionary<string, bool>();

protected Pubnub PubnubInstance { get; set; }

Expand Down
12 changes: 6 additions & 6 deletions src/Examples/PubnubApi.ConsoleExample/PubnubExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2221,12 +2221,12 @@ static public void Main()
.AuthorizedUuid(grantAuthorizedUuiIdEntry)
.Resources(new PNTokenResources()
{
Channels = new Dictionary<string, PNTokenAuthValues>() {
{ grantChannelName, new PNTokenAuthValues() { Read = grantRead, Write = grantWrite, Manage= grantManage, Create = grantCreate, Delete=grantDelete, Get = grantGet, Update = grantUpdate, Join = grantJoin } } },
ChannelGroups = new Dictionary<string, PNTokenAuthValues>() {
{grantChannelGroupName, new PNTokenAuthValues() { Read = grantRead, Write = grantWrite, Manage= grantManage, Create = grantCreate, Delete=grantDelete, Get = grantGet, Update = grantUpdate, Join = grantJoin } } },
Uuids = new Dictionary<string, PNTokenAuthValues>() {
{ grantUuiIdEntry, new PNTokenAuthValues() { Read = grantRead, Write = grantWrite, Manage= grantManage, Create = grantCreate, Delete=grantDelete, Get = grantGet, Update = grantUpdate, Join = grantJoin } } },
Channels = !string.IsNullOrEmpty(grantChannelName) ? new Dictionary<string, PNTokenAuthValues>() {
{ grantChannelName, new PNTokenAuthValues() { Read = grantRead, Write = grantWrite, Manage= grantManage, Create = grantCreate, Delete=grantDelete, Get = grantGet, Update = grantUpdate, Join = grantJoin } } } : null,
ChannelGroups = !string.IsNullOrEmpty(grantChannelGroupName) ? new Dictionary<string, PNTokenAuthValues>() {
{grantChannelGroupName, new PNTokenAuthValues() { Read = grantRead, Write = grantWrite, Manage= grantManage, Create = grantCreate, Delete=grantDelete, Get = grantGet, Update = grantUpdate, Join = grantJoin } } } : null,
Uuids = !string.IsNullOrEmpty(grantUuiIdEntry) ? new Dictionary<string, PNTokenAuthValues>() {
{ grantUuiIdEntry, new PNTokenAuthValues() { Read = grantRead, Write = grantWrite, Manage= grantManage, Create = grantCreate, Delete=grantDelete, Get = grantGet, Update = grantUpdate, Join = grantJoin } } } : null,
})
.Execute(new PNAccessManagerTokenResultExt((result, status) =>
{
Expand Down