Skip to content

fix: transport module error log level #256

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

Open
wants to merge 6 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
4 changes: 2 additions & 2 deletions src/Api/PubnubApi/ClientNetworkStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ internal static bool CheckInternetStatus<T>(bool systemActive, PNOperationType
} catch (AggregateException ae) {
foreach (var ie in ae.InnerExceptions)
{
PubnubConfiguation?.Logger?.Error($"AggregateException CheckInternetStatus : {ie.GetType().Name} {ie.Message}");
PubnubConfiguation?.Logger?.Warn($"AggregateException CheckInternetStatus : {ie.GetType().Name} {ie.Message}");
}
} catch (Exception ex)
{
PubnubConfiguation?.Logger?.Error($"CheckInternetStatus Exception: {ex.Message}");
PubnubConfiguation?.Logger?.Warn($"CheckInternetStatus Exception: {ex.Message}");
}

return networkStatus;
Expand Down
253 changes: 1 addition & 252 deletions src/Api/PubnubApi/EndPoint/TokenManager.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Reflection;
using Newtonsoft.Json;
using PeterO.Cbor;
// using PeterO.Cbor;
using System.Globalization;
#if !NET35 && !NET40
using System.Collections.Concurrent;
Expand Down Expand Up @@ -67,113 +63,9 @@ private static string GetDisplayableBytes(byte[] currentBytes)
public PNTokenContent ParseToken(string token)
{
PNTokenContent result = null;
try
{
if (!string.IsNullOrEmpty(token) && token.Trim().Length > 0)
{
string refinedToken = token.Replace('_', '/').Replace('-', '+');
byte[] tokenByteArray = Convert.FromBase64String(refinedToken);
logger?.Debug($"TokenManager Token Bytes = {GetDisplayableBytes(tokenByteArray)}");
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(tokenByteArray))
{
CBORObject cborObj = CBORObject.DecodeFromBytes(tokenByteArray);
logger?.Debug($"RAW CBOR {cborObj.ToJSONString()}");

if (cborObj != null)
{
result = new PNTokenContent();
result.Meta = new Dictionary<string, object>();
result.Resources = new PNTokenResources {
Channels = new Dictionary<string, PNTokenAuthValues>(),
ChannelGroups = new Dictionary<string, PNTokenAuthValues>(),
Uuids = new Dictionary<string, PNTokenAuthValues>(),
Users = new Dictionary<string, PNTokenAuthValues>(),
Spaces = new Dictionary<string, PNTokenAuthValues>()
};
result.Patterns = new PNTokenPatterns {
Channels = new Dictionary<string, PNTokenAuthValues>(),
ChannelGroups = new Dictionary<string, PNTokenAuthValues>(),
Uuids = new Dictionary<string, PNTokenAuthValues>(),
Users = new Dictionary<string, PNTokenAuthValues>(),
Spaces = new Dictionary<string, PNTokenAuthValues>()
};
ParseCBOR(cborObj, "", ref result);
}

}
}
}
catch (Exception ex)
{
logger?.Error($"Error parsing token: {ex.Message} \n {ex.StackTrace}");
}
return result;
}

private void ParseCBOR(CBORObject cbor, string parent, ref PNTokenContent pnGrantTokenDecoded)
{
foreach (KeyValuePair<CBORObject, CBORObject> kvp in cbor.Entries)
{
if (kvp.Key.Type.ToString().Equals("ByteString", StringComparison.OrdinalIgnoreCase))
{
#if NETSTANDARD10 || NETSTANDARD11
UTF8Encoding utf8 = new UTF8Encoding(true, true);
byte[] keyBytes = kvp.Key.GetByteString();
string key = utf8.GetString(keyBytes, 0, keyBytes.Length);
#else
string key = Encoding.ASCII.GetString(kvp.Key.GetByteString());
#endif
ParseCBORValue(key, parent, kvp, ref pnGrantTokenDecoded);
}
else if (kvp.Key.Type.ToString().Equals("TextString", StringComparison.OrdinalIgnoreCase))
{
logger?.Debug($"ParseCBOR TextString Key {kvp.Key}-{kvp.Value}-{kvp.Value.Type}");
ParseCBORValue(kvp.Key.ToString(), parent, kvp, ref pnGrantTokenDecoded);
}
else
{
logger?.Debug($"Others Key {kvp.Key}-{kvp.Key.Type}-{kvp.Value}-{kvp.Value.Type}");
}

}
}

private void ParseCBORValue(string key, string parent, KeyValuePair<CBORObject, CBORObject> kvp, ref PNTokenContent pnGrantTokenDecoded)
{
if (kvp.Value.Type.ToString().Equals("Map", StringComparison.OrdinalIgnoreCase))
{
logger?.Debug($"ParseCBORValue Map Key {key}");
var p = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", parent, string.IsNullOrEmpty(parent) ? "" : ":", key);
ParseCBOR(kvp.Value, p, ref pnGrantTokenDecoded);
}
else if (kvp.Value.Type.ToString().Equals("ByteString", StringComparison.OrdinalIgnoreCase))
{
#if NETSTANDARD10 || NETSTANDARD11
UTF8Encoding utf8 = new UTF8Encoding(true, true);
byte[] valBytes = kvp.Value.GetByteString();
string val = utf8.GetString(valBytes, 0, valBytes.Length);
#else
string val = Encoding.ASCII.GetString(kvp.Value.GetByteString());
#endif
logger?.Debug($"ByteString Value {key}-{val}");
FillGrantToken(parent, key, kvp.Value, typeof(string), ref pnGrantTokenDecoded);
}
else if (kvp.Value.Type.ToString().Equals("Integer", StringComparison.OrdinalIgnoreCase))
{
logger?.Debug($" Integer Value {key}-{kvp.Value}");
FillGrantToken(parent, key, kvp.Value, typeof(int), ref pnGrantTokenDecoded);
}
else if (kvp.Value.Type.ToString().Equals("TextString", StringComparison.OrdinalIgnoreCase))
{
logger?.Debug($"TextString Value {key}-{kvp.Value}");
FillGrantToken(parent, key, kvp.Value, typeof(string), ref pnGrantTokenDecoded);
}
else
{
logger?.Debug($"Others Key Value {kvp.Key.Type}-{kvp.Value.Type}-{key}-{kvp.Value}");
}
}

private string ReplaceBoundaryQuotes(string key)
{
if (key != null && !string.IsNullOrEmpty(key) && key.Length >= 2
Expand All @@ -185,149 +77,6 @@ private string ReplaceBoundaryQuotes(string key)
}
return key;
}

private void FillGrantToken(string parent, string key, object val, Type type, ref PNTokenContent pnGrantTokenDecoded)
{
key = ReplaceBoundaryQuotes(key);
int i = 0;
long l = 0;
switch (type.Name)
{
case "Int32":
if (!int.TryParse(val.ToString(), out i))
{
//do nothing.
}
break;
case "Int64":
if (!long.TryParse(val.ToString(), out l))
{
//do nothing
}
break;
case "String":
// do nothing
break;
default:
break;
}
switch (key)
{
case "v":
pnGrantTokenDecoded.Version = i;
break;
case "t":
pnGrantTokenDecoded.Timestamp = i;
break;
case "ttl":
pnGrantTokenDecoded.TTL = i;
break;
case "meta":
pnGrantTokenDecoded.Meta = val as Dictionary<string, object>;
break;
case "uuid":
pnGrantTokenDecoded.AuthorizedUuid = ((CBORObject)val).AsString();
break;
case "sig":
#if NETSTANDARD10 || NETSTANDARD11
UTF8Encoding utf8 = new UTF8Encoding(true, true);
byte[] keyBytes = (byte[])val;
pnGrantTokenDecoded.Signature = utf8.GetString(keyBytes, 0, keyBytes.Length);
#else
byte[] sigBytes = ((CBORObject)val).GetByteString();
string base64String = Convert.ToBase64String(sigBytes);
pnGrantTokenDecoded.Signature = base64String;
#endif
break;
default:
PNTokenAuthValues rp = GetResourcePermission(i);
switch (parent)
{
case "meta":
if (!pnGrantTokenDecoded.Meta.ContainsKey(key))
{
switch (type.Name)
{
case "Int32":
pnGrantTokenDecoded.Meta.Add(key, ((CBORObject)val).AsInt32());
break;
case "Int64":
pnGrantTokenDecoded.Meta.Add(key, ((CBORObject)val).ToObject<long>());
break;
case "String":
pnGrantTokenDecoded.Meta.Add(key, ((CBORObject)val).AsString());
break;
default:
break;
}
}
break;
case "res:spc":
if (!pnGrantTokenDecoded.Resources.Spaces.ContainsKey(key))
{
pnGrantTokenDecoded.Resources.Spaces.Add(key, rp);
}
break;
case "res:usr":
if (!pnGrantTokenDecoded.Resources.Users.ContainsKey(key))
{
pnGrantTokenDecoded.Resources.Users.Add(key, rp);
}
break;
case "res:uuid":
if (!pnGrantTokenDecoded.Resources.Uuids.ContainsKey(key))
{
pnGrantTokenDecoded.Resources.Uuids.Add(key, rp);
}
break;
case "res:chan":
if (!pnGrantTokenDecoded.Resources.Channels.ContainsKey(key))
{
pnGrantTokenDecoded.Resources.Channels.Add(key, rp);
}
break;
case "res:grp":
if (!pnGrantTokenDecoded.Resources.ChannelGroups.ContainsKey(key))
{
pnGrantTokenDecoded.Resources.ChannelGroups.Add(key, rp);
}
break;
case "pat:spc":
if (!pnGrantTokenDecoded.Patterns.Spaces.ContainsKey(key))
{
pnGrantTokenDecoded.Patterns.Spaces.Add(key, rp);
}
break;
case "pat:usr":
if (!pnGrantTokenDecoded.Patterns.Users.ContainsKey(key))
{
pnGrantTokenDecoded.Patterns.Users.Add(key, rp);
}
break;
case "pat:uuid":
if (!pnGrantTokenDecoded.Patterns.Uuids.ContainsKey(key))
{
pnGrantTokenDecoded.Patterns.Uuids.Add(key, rp);
}
break;
case "pat:chan":
if (!pnGrantTokenDecoded.Patterns.Channels.ContainsKey(key))
{
pnGrantTokenDecoded.Patterns.Channels.Add(key, rp);
}
break;
case "pat:grp":
if (!pnGrantTokenDecoded.Patterns.ChannelGroups.ContainsKey(key))
{
pnGrantTokenDecoded.Patterns.ChannelGroups.Add(key, rp);
}
break;
default:
break;
}
break;
}
}
public void SetAuthToken(string token)
{
dToken.AddOrUpdate(pubnubInstanceId, token, (k, o) => token);
Expand Down
2 changes: 1 addition & 1 deletion src/Api/PubnubApi/EventEngine/Common/EventEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void EmitEvent<T>(object e)
catch (Exception ex)
{
decryptMessage = "**DECRYPT ERROR**";
configuration?.Logger?.Error("Failed to decrypt message on channel {currentMessageChannel} due to exception={ex}.\n Message might be not encrypted");
configuration?.Logger?.Warn("Failed to decrypt message on channel {currentMessageChannel} due to exception={ex}.\n Message might be not encrypted");
}

object decodeMessage = jsonLibrary.DeserializeToObject((decryptMessage == "**DECRYPT ERROR**") ? jsonLibrary.SerializeToJsonString(payload) : decryptMessage);
Expand Down
1 change: 0 additions & 1 deletion src/Api/PubnubApi/PubnubApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3">
<PrivateAssets>None</PrivateAssets>
</PackageReference>
<PackageReference Include="PeterO.Cbor" Version="4.5.5" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net35'">
Expand Down
10 changes: 5 additions & 5 deletions src/Api/PubnubApi/Transport/HttpClientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public async Task<TransportResponse> GetRequest(TransportRequest transportReques
}
catch (Exception e)
{
logger?.Error(
logger?.Warn(
$"HttpClient Service: Exception for http call url {transportRequest.RequestUrl}, exception message: {e.Message}, stacktrace: {e.StackTrace}");
transportResponse = new TransportResponse()
{
Expand Down Expand Up @@ -149,7 +149,7 @@ public async Task<TransportResponse> PostRequest(TransportRequest transportReque
}
catch (Exception e)
{
logger?.Error(
logger?.Warn(
$"Exception for http call url {transportRequest.RequestUrl}, exception message: {e.Message}, stacktrace: {e.StackTrace}");
transportResponse = new TransportResponse()
{
Expand Down Expand Up @@ -228,7 +228,7 @@ public async Task<TransportResponse> PutRequest(TransportRequest transportReques
}
catch (Exception e)
{
logger?.Error(
logger?.Warn(
$"Exception for http call url {transportRequest.RequestUrl}, exception message: {e.Message}, stacktrace: {e.StackTrace}");
transportResponse = new TransportResponse()
{
Expand Down Expand Up @@ -291,7 +291,7 @@ public async Task<TransportResponse> DeleteRequest(TransportRequest transportReq
}
catch (Exception e)
{
logger?.Error(
logger?.Warn(
$"Exception for http call url {transportRequest.RequestUrl}, exception message: {e.Message}, stacktrace: {e.StackTrace}");
transportResponse = new TransportResponse()
{
Expand Down Expand Up @@ -371,7 +371,7 @@ public async Task<TransportResponse> PatchRequest(TransportRequest transportRequ
}
catch (Exception e)
{
logger?.Error(
logger?.Warn(
$"Exception for http call url {transportRequest.RequestUrl}, exception message: {e.Message}, stacktrace: {e.StackTrace}");
transportResponse = new TransportResponse()
{
Expand Down
1 change: 0 additions & 1 deletion src/Api/PubnubApiPCL/PubnubApiPCL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,6 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3">
<PrivateAssets>None</PrivateAssets>
</PackageReference>
<PackageReference Include="PeterO.Cbor" Version="4.5.5" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
</ItemGroup>

Expand Down
1 change: 0 additions & 1 deletion src/Api/PubnubApiUWP/PubnubApiUWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,6 @@
<PackageReference Include="Newtonsoft.Json">
<Version>13.0.3</Version>
</PackageReference>
<PackageReference Include="PeterO.Cbor" Version="4.5.5" />
<PackageReference Include="System.Security.Cryptography.Algorithms" Version="4.3.0" />
<PackageReference Include="System.Collections.Concurrent">
<Version>4.3.0</Version>
Expand Down
1 change: 0 additions & 1 deletion src/Api/PubnubApiUnity/PubnubApiUnity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,6 @@

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="PeterO.Cbor" Version="4.5.5" />
</ItemGroup>

<!--<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.4' ">
Expand Down
Loading
Loading