Skip to content

Commit

Permalink
not way better
Browse files Browse the repository at this point in the history
  • Loading branch information
HandyS11 committed May 18, 2024
1 parent e000dc9 commit b445ae6
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 37 deletions.
5 changes: 3 additions & 2 deletions RustPlusApi/Examples/Fcm/FcmListener/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using RustPlusApi.Fcm;
using RustPlusApi.Fcm.Data;
using Newtonsoft.Json;

var credentials = new Credentials
{
Expand All @@ -23,11 +24,11 @@
}
};

var listener = new FcmListener(credentials, new List<string>());
var listener = new FcmListener(credentials, []);

listener.NotificationReceived += (_, message) =>
{
Console.WriteLine(message);
Console.WriteLine(JsonConvert.SerializeObject(message, Formatting.Indented));
};

await listener.ConnectAsync();
7 changes: 4 additions & 3 deletions RustPlusApi/Examples/Fcm/FcmRegister/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using RustPlusApi.Fcm;
using Newtonsoft.Json;

var register = new FcmRegister();
var credentials = await register.RegisterAsync("976529667804");
var senderId = "976529667804";
var credentials = await FcmRegister.RegisterAsync(senderId);

Console.ReadKey();
Console.WriteLine(JsonConvert.SerializeObject(credentials, Formatting.Indented));
20 changes: 10 additions & 10 deletions RustPlusApi/RustPlusApi.Fcm/Data/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,42 @@ namespace RustPlusApi.Fcm.Data
public sealed class Credentials
{
[JsonProperty(PropertyName = "keys")]
public Keys Keys { get; set; }
public Keys Keys { get; set; } = null!;
[JsonProperty(PropertyName = "fcm")]
public FcmCredentials Fcm { get; set; }
public FcmCredentials Fcm { get; set; } = null!;
[JsonProperty(PropertyName = "gcm")]
public GcmCredentials Gcm { get; set; }
public GcmCredentials Gcm { get; set; } = null!;
}

public sealed class Keys
{
[JsonProperty(PropertyName = "privateKey")]
public string PrivateKey { get; set; }
public string PrivateKey { get; set; } = null!;

[JsonProperty(PropertyName = "publicKey")]
public string PublicKey { get; set; }
public string PublicKey { get; set; } = null!;

[JsonProperty(PropertyName = "authSecret")]
public string AuthSecret { get; set; }
public string AuthSecret { get; set; } = null!;
}

public sealed class FcmCredentials
{
[JsonProperty(PropertyName = "token")]
public string Token { get; set; }
public string Token { get; set; } = null!;
[JsonProperty(PropertyName = "pushSet")]
public string PushSet { get; set; }
public string PushSet { get; set; } = null!;
}

public sealed class GcmCredentials
{
[JsonProperty(PropertyName = "token")]
public string Token { get; set; }
public string Token { get; set; } = null!;
[JsonProperty(PropertyName = "androidId")]
public ulong AndroidId { get; set; }
[JsonProperty(PropertyName = "securityToken")]
public ulong SecurityToken { get; set; }
[JsonProperty(PropertyName = "appId")]
public string AppId { get; set; }
public string AppId { get; set; } = null!;
}
}
12 changes: 6 additions & 6 deletions RustPlusApi/RustPlusApi.Fcm/FcmListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
using System.Net.Sockets;
using System.Numerics;

using Google.Protobuf;

using McsProto;

using ProtoBuf;

using RustPlusApi.Fcm.Data;
using RustPlusApi.Fcm.Tools;
using RustPlusApi.Fcm.Utils;

using static RustPlusApi.Fcm.Data.Constants;
using static System.GC;


namespace RustPlusApi.Fcm
{
public class FcmListener(Credentials credentials, ICollection<string> persistentIds) : IDisposable
Expand Down Expand Up @@ -42,13 +44,11 @@ public async Task ConnectAsync()

try
{
Connected?.Invoke(this, EventArgs.Empty);

var checkIn = await GcmTools.CheckInAsync(credentials.Gcm.AndroidId, credentials.Gcm.SecurityToken);

var buffer = LoginBuffer();
await _sslStream.WriteAsync(buffer);

Connected?.Invoke(this, EventArgs.Empty);

await ReceiveMessagesAsync();
}
catch (Exception ex)
Expand Down Expand Up @@ -106,7 +106,7 @@ private byte[] LoginBuffer()
ReceivedPersistentId = { persistentIds },
};
using var stream = new MemoryStream();
Serializer.Serialize(stream, loginRequest);
Serializer.Serialize(stream, loginRequest.ToByteArray());
var buffer = stream.ToArray();

var header = new byte[] { KMcsVersion, (byte)McsProtoTag.KLoginRequestTag };
Expand Down
2 changes: 1 addition & 1 deletion RustPlusApi/RustPlusApi.Fcm/FcmRegister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace RustPlusApi.Fcm
{
public class FcmRegister
{
public async Task<Credentials> RegisterAsync(string senderId)
public static async Task<Credentials> RegisterAsync(string senderId)
{
var appId = $"wp:receiver.push.com#{Guid.NewGuid()}";

Expand Down
39 changes: 24 additions & 15 deletions RustPlusApi/RustPlusApi.Fcm/Tools/GcmTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,35 @@ public static async Task<GcmCredentials> RegisterAsync(string appId)

public static async Task<AndroidCheckinResponse> CheckInAsync(ulong? androidId = null, ulong? securityToken = null)
{
var id = (androidId != null) ? (long)androidId : (long?)null;
var requestBody = GetCheckInRequest(id, securityToken);

var request = new HttpRequestMessage(HttpMethod.Post, CheckInUrl)
try
{
Headers =
{
{ "Content-Type", "application/x-protobuf" },
},
Content = new ByteArrayContent(requestBody.ToByteArray())
};
var response = await HttpClient.SendAsync(request);
var data = await response.Content.ReadAsByteArrayAsync();
var id = (androidId != null) ? (long)androidId : (long?)null;
var requestBody = GetCheckInRequest(id, securityToken);

var request = new HttpRequestMessage(HttpMethod.Post, CheckInUrl);

var content = new ByteArrayContent(requestBody.ToByteArray());
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-protobuf");

request.Content = content;

using var stream = new MemoryStream(data);
var message = Serializer.Deserialize<AndroidCheckinResponse>(stream);
var response = await HttpClient.SendAsync(request);
response.EnsureSuccessStatusCode();

var data = await response.Content.ReadAsByteArrayAsync();

return message;
using var stream = new MemoryStream(data);
var message = Serializer.Deserialize<AndroidCheckinResponse>(stream);

return message;
}
catch (Exception ex)
{
throw new ApplicationException("Error during check-in request.", ex);
}
}


private static AndroidCheckinRequest GetCheckInRequest(long? androidId = null, ulong? securityToken = null)
{
return new AndroidCheckinRequest
Expand Down

0 comments on commit b445ae6

Please sign in to comment.