Skip to content

Commit

Permalink
update middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
area363 committed Oct 6, 2023
1 parent 44553c3 commit 8a6896c
Showing 1 changed file with 80 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Nekoyume.Action;
using Nekoyume.Action.Garages;
using NineChronicles.Headless.Properties;
using Serilog;
using ILogger = Serilog.ILogger;
Expand Down Expand Up @@ -61,107 +62,108 @@ public async Task InvokeAsync(HttpContext context)
var remoteIp = context.Connection.RemoteIpAddress!.ToString();
var body = await new StreamReader(context.Request.Body).ReadToEndAsync();
context.Request.Body.Seek(0, SeekOrigin.Begin);
if (_options.Value.EnableManaging)
if (_options.Value.EnableManaging && body.Contains("stageTransaction"))
{
if (body.Contains("agent(address:\\\"") || body.Contains("agent(address: \\\""))
try
{
try
{
var agent = new Address(body.Split("\\\"")[1].Split("0x")[1]);
UpdateIpSignerList(remoteIp, agent);
}
catch (Exception ex)
{
_logger.Error(
"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Error message: {message} Stacktrace: {stackTrace}",
ex.Message,
ex.StackTrace);
}
}
var pattern = "64313.*6565";
var txPayload = Regex.Match(body, pattern).ToString();
byte[] bytes = ByteUtil.ParseHex(txPayload);
Transaction tx = Transaction.Deserialize(bytes);
var agent = tx.Signer;
var action = NCActionUtils.ToAction(tx.Actions.Actions.First());
_logger.Information("[GRAPHQL-MULTI-ACCOUNT-MANAGER] IP: {IP} Agent: {Agent} Tx: {Path}",
remoteIp, agent, tx.Actions.Actions.FirstOrDefault());

if (body.Contains("stageTransaction"))
{
try
// Only monitoring actions not used in the launcher
if (action is not Stake
and not Stake2
and not Stake0
and not ClaimStakeReward
and not ClaimStakeReward8
and not ClaimStakeReward7
and not ClaimStakeReward6
and not ClaimStakeReward5
and not ClaimStakeReward4
and not ClaimStakeReward3
and not ClaimStakeReward2
and not ClaimStakeReward1
and not TransferAsset
and not TransferAsset4
and not TransferAsset3
and not TransferAsset2
and not TransferAsset0
and not TransferAssets
and not TransferAssets2
and not TransferAssets0
and not UnloadFromMyGarages)
{
var pattern = "64313.*6565";
var txPayload = Regex.Match(body, pattern).ToString();
byte[] bytes = ByteUtil.ParseHex(txPayload);
Transaction tx = Transaction.Deserialize(bytes);
var agent = tx.Signer;
var action = NCActionUtils.ToAction(tx.Actions.Actions.First());

// Only monitoring actions not used in the launcher
if (action is not Stake
and not ClaimStakeReward
and not TransferAsset)
if (_ipSignerList.ContainsKey(remoteIp))
{
if (_ipSignerList.ContainsKey(remoteIp))
if (_ipSignerList[remoteIp].Count > 3)
{
if (_ipSignerList[remoteIp].Count > _options.Value.ThresholdCount)
{
_logger.Information(
"[GRAPHQL-MULTI-ACCOUNT-MANAGER] IP: {IP} List Count: {Count}, AgentAddresses: {Agent}",
remoteIp,
_ipSignerList[remoteIp].Count,
_ipSignerList[remoteIp]);
_logger.Information(
"[GRAPHQL-MULTI-ACCOUNT-MANAGER] IP: {IP} List Count: {Count}, AgentAddresses: {Agent}",
remoteIp,
_ipSignerList[remoteIp].Count,
_ipSignerList[remoteIp]);

if (!MultiAccountManagementList.ContainsKey(agent))
if (!MultiAccountManagementList.ContainsKey(agent))
{
if (!MultiAccountTxIntervalTracker.ContainsKey(agent))
{
if (!MultiAccountTxIntervalTracker.ContainsKey(agent))
{
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Adding agent {agent} to the agent tracker.");
MultiAccountTxIntervalTracker.Add(agent, DateTimeOffset.Now);
}
else
{
if ((DateTimeOffset.Now - MultiAccountTxIntervalTracker[agent]).Minutes >= _options.Value.TxIntervalMinutes)
{
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Resetting Agent {agent}'s time because it has been more than {_options.Value.TxIntervalMinutes} minutes since the last transaction.");
MultiAccountTxIntervalTracker[agent] = DateTimeOffset.Now;
}
else
{
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Managing Agent {agent} for {_options.Value.ManagementTimeMinutes} minutes due to {_ipSignerList[remoteIp].Count} associated accounts.");
ManageMultiAccount(agent);
MultiAccountTxIntervalTracker[agent] = DateTimeOffset.Now;
await CancelRequestAsync(context);
return;
}
}
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Adding agent {agent} to the agent tracker.");
MultiAccountTxIntervalTracker.Add(agent, DateTimeOffset.Now);
}
else
{
var currentManagedTime = (DateTimeOffset.Now - MultiAccountManagementList[agent]).Minutes;
if (currentManagedTime > _options.Value.ManagementTimeMinutes)
if ((DateTimeOffset.Now - MultiAccountTxIntervalTracker[agent]).Minutes >= _options.Value.TxIntervalMinutes)
{
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Restoring Agent {agent} after {_options.Value.ManagementTimeMinutes} minutes.");
RestoreMultiAccount(agent);
MultiAccountTxIntervalTracker[agent] = DateTimeOffset.Now.AddMinutes(-_options.Value.TxIntervalMinutes);
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Current time: {DateTimeOffset.Now} Added time: {DateTimeOffset.Now.AddMinutes(-_options.Value.TxIntervalMinutes)}.");
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Resetting Agent {agent}'s time because it has been more than {_options.Value.TxIntervalMinutes} minutes since the last transaction.");
MultiAccountTxIntervalTracker[agent] = DateTimeOffset.Now;
}
else
{
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Agent {agent} is in managed status for the next {_options.Value.ManagementTimeMinutes - currentManagedTime} minutes.");
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Managing Agent {agent} for {_options.Value.ManagementTimeMinutes} minutes due to {_ipSignerList[remoteIp].Count} associated accounts.");
ManageMultiAccount(agent);
MultiAccountTxIntervalTracker[agent] = DateTimeOffset.Now;
await CancelRequestAsync(context);
return;
}
}
}
}
else
{
UpdateIpSignerList(remoteIp, agent);
else
{
var currentManagedTime = (DateTimeOffset.Now - MultiAccountManagementList[agent]).Minutes;
if (currentManagedTime > _options.Value.ManagementTimeMinutes)
{
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Restoring Agent {agent} after {_options.Value.ManagementTimeMinutes} minutes.");
RestoreMultiAccount(agent);
MultiAccountTxIntervalTracker[agent] = DateTimeOffset.Now.AddMinutes(-_options.Value.TxIntervalMinutes);
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Current time: {DateTimeOffset.Now} Added time: {DateTimeOffset.Now.AddMinutes(-_options.Value.TxIntervalMinutes)}.");
}
else
{
_logger.Information($"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Agent {agent} is in managed status for the next {_options.Value.ManagementTimeMinutes - currentManagedTime} minutes.");
await CancelRequestAsync(context);
return;
}
}
}
}

}
catch (Exception ex)
{
_logger.Error(
"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Error message: {message} Stacktrace: {stackTrace}",
ex.Message,
ex.StackTrace);
else
{
UpdateIpSignerList(remoteIp, agent);
}
}

}
catch (Exception ex)
{
_logger.Error(
"[GRAPHQL-MULTI-ACCOUNT-MANAGER] Error message: {message} Stacktrace: {stackTrace}",
ex.Message,
ex.StackTrace);
}
}
}
Expand Down

0 comments on commit 8a6896c

Please sign in to comment.