mirror of
https://github.com/opengram-server/opengram.git
synced 2026-09-18 00:08:36 +03:00
Initial commit
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
namespace MyTelegram.AuthServer.Handlers;
|
||||
|
||||
public interface IMsgsAckHandler : IObjectHandler
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace MyTelegram.AuthServer.Handlers;
|
||||
|
||||
public interface IReqDhParamsHandler : IObjectHandler
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace MyTelegram.AuthServer.Handlers;
|
||||
|
||||
public interface IReqPqHandler : IObjectHandler
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace MyTelegram.AuthServer.Handlers;
|
||||
|
||||
public interface IReqPqMultiHandler : IObjectHandler
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace MyTelegram.AuthServer.Handlers;
|
||||
|
||||
public interface ISetClientDhParamsHandler : IObjectHandler
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace MyTelegram.AuthServer.Handlers;
|
||||
|
||||
public class MsgsAckHandler : BaseObjectHandler<TMsgsAck, IObject>, IMsgsAckHandler
|
||||
{
|
||||
protected override Task<IObject> HandleCoreAsync(IRequestInput input, TMsgsAck obj)
|
||||
{
|
||||
return Task.FromResult<IObject>(null!);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace MyTelegram.AuthServer.Handlers;
|
||||
|
||||
public class ReqDhParamsHandler(IStep2Helper step2ServerHelper, ILogger<ReqDhParamsHandler> logger)
|
||||
: BaseObjectHandler<RequestReqDHParams, IServerDHParams>,
|
||||
IReqDhParamsHandler
|
||||
{
|
||||
protected override async Task<IServerDHParams> HandleCoreAsync(
|
||||
IRequestInput input,
|
||||
RequestReqDHParams obj
|
||||
)
|
||||
{
|
||||
var dto = await step2ServerHelper.GetServerDhParamsAsync(obj);
|
||||
logger.LogInformation(
|
||||
"[Step2] ReqDhParamsHandler, connectionId: {ConnectionId}, reqMsgId: {ReqMsgId}",
|
||||
input.ConnectionId,
|
||||
input.ReqMsgId
|
||||
);
|
||||
|
||||
return dto.ServerDhParams;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
namespace MyTelegram.AuthServer.Handlers;
|
||||
|
||||
public class ReqPqHandler(
|
||||
IStep1Helper step1ServerHelper,
|
||||
ILogger<ReqPqHandler> logger,
|
||||
ICacheManager<AuthCacheItem> cacheManager
|
||||
) : BaseObjectHandler<RequestReqPq, IResPQ>, IReqPqHandler
|
||||
{
|
||||
protected override async Task<IResPQ> HandleCoreAsync(IRequestInput input, RequestReqPq obj)
|
||||
{
|
||||
var dto = step1ServerHelper.GetResponse(obj.Nonce);
|
||||
var authCacheItem = new AuthCacheItem(obj.Nonce, dto.ServerNonce, dto.P, dto.Q, false);
|
||||
|
||||
var key = AuthCacheItem.GetCacheKey(dto.ServerNonce);
|
||||
|
||||
await cacheManager.SetAsync(
|
||||
key,
|
||||
authCacheItem,
|
||||
MyTelegramConsts.AuthKeyExpireSeconds
|
||||
);
|
||||
logger.LogInformation(
|
||||
"[Step1] ReqPqHandler, connectionId: {ConnectionId}, reqMsgId: {ReqMsgId}",
|
||||
input.ConnectionId,
|
||||
input.ReqMsgId
|
||||
);
|
||||
|
||||
return dto.ResPq;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace MyTelegram.AuthServer.Handlers;
|
||||
|
||||
public class ReqPqMultiHandler(
|
||||
IStep1Helper step1ServerHelper,
|
||||
ILogger<ReqPqMultiHandler> logger,
|
||||
ICacheManager<AuthCacheItem> cacheManager
|
||||
) : BaseObjectHandler<RequestReqPqMulti, IResPQ>, IReqPqMultiHandler
|
||||
{
|
||||
protected override async Task<IResPQ> HandleCoreAsync(
|
||||
IRequestInput input,
|
||||
RequestReqPqMulti obj
|
||||
)
|
||||
{
|
||||
var sw = Stopwatch.StartNew();
|
||||
var dto = step1ServerHelper.GetResponse(obj.Nonce);
|
||||
|
||||
var authCacheItem = new AuthCacheItem(obj.Nonce, dto.ServerNonce, dto.P, dto.Q, false);
|
||||
var key = AuthCacheItem.GetCacheKey(dto.ServerNonce);
|
||||
await cacheManager.SetAsync(
|
||||
key,
|
||||
authCacheItem,
|
||||
MyTelegramConsts.AuthKeyExpireSeconds
|
||||
);
|
||||
sw.Stop();
|
||||
logger.LogInformation(
|
||||
"[Step1] ReqPqMultiHandler, connectionId={ConnectionId}, nonce: {Nonce} reqMsgId: {ReqMsgId}, authKeyId: {AuthKeyId} {TimeSpan}ms",
|
||||
input.ConnectionId,
|
||||
obj.Nonce.ToHexString(),
|
||||
input.ReqMsgId,
|
||||
input.AuthKeyId,
|
||||
sw.Elapsed.TotalMilliseconds
|
||||
);
|
||||
|
||||
return dto.ResPq;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
namespace MyTelegram.AuthServer.Handlers;
|
||||
|
||||
public class SetClientDhParamsHandler(
|
||||
IStep3Helper step3ServerHelper,
|
||||
ILogger<SetClientDhParamsHandler> logger,
|
||||
ICacheManager<AuthKeyCacheItem> cacheManager,
|
||||
IEventBus eventBus
|
||||
) : BaseObjectHandler<RequestSetClientDHParams, ISetClientDHParamsAnswer>, ISetClientDhParamsHandler
|
||||
{
|
||||
protected override async Task<ISetClientDHParamsAnswer> HandleCoreAsync(
|
||||
IRequestInput input,
|
||||
RequestSetClientDHParams obj
|
||||
)
|
||||
{
|
||||
var dto = await step3ServerHelper.SetClientDhParamsAnswerAsync(obj);
|
||||
logger.LogInformation(
|
||||
"[Step3] [{IsPerm}] authKey created successfully, connectionId: {ConnectionId}, authKeyId: {AuthKeyId:x2}, reqMsgId: {ReqMsgId}",
|
||||
input.ConnectionId,
|
||||
dto.IsPermanent ? "Perm" : "Temp",
|
||||
dto.AuthKeyId,
|
||||
input.ReqMsgId
|
||||
);
|
||||
|
||||
// Cached authentication data expires in 120 seconds
|
||||
var cacheKey = AuthKeyCacheItem.GetCacheKey(dto.AuthKeyId);
|
||||
await cacheManager.SetAsync(
|
||||
cacheKey,
|
||||
new AuthKeyCacheItem(dto.AuthKey, dto.ServerSalt, dto.IsPermanent),
|
||||
120
|
||||
);
|
||||
await eventBus.PublishAsync(
|
||||
new AuthKeyCreatedIntegrationEvent(
|
||||
input.ConnectionId,
|
||||
input.ReqMsgId,
|
||||
dto.AuthKey,
|
||||
dto.ServerSalt,
|
||||
dto.IsPermanent,
|
||||
dto.SetClientDhParamsAnswer.ToBytes(),
|
||||
dto.DcId
|
||||
)
|
||||
);
|
||||
|
||||
// The session server will send SetClientDhParamsAnswer to client if the perm auth key created on session server
|
||||
if (!dto.IsPermanent)
|
||||
{
|
||||
return dto.SetClientDhParamsAnswer;
|
||||
}
|
||||
|
||||
return null!;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user