Skip to content

Commit

Permalink
fix: do not log SocketException
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Aug 22, 2023
1 parent e0f369f commit dbd8bc6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
15 changes: 7 additions & 8 deletions src/GZCTF/Controllers/ProxyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public ProxyController(ILogger<ProxyController> logger, IDistributedCache cache,
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(RequestResponse), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(RequestResponse), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(RequestResponse), StatusCodes.Status418ImATeapot)]
public async Task<IActionResult> ProxyForInstance(string id, CancellationToken token = default)
{
if (!_enablePlatformProxy)
Expand Down Expand Up @@ -122,6 +123,7 @@ public async Task<IActionResult> ProxyForInstance(string id, CancellationToken t
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(RequestResponse), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(RequestResponse), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(RequestResponse), StatusCodes.Status418ImATeapot)]
public async Task<IActionResult> ProxyForNoInstance(string id, CancellationToken token = default)
{
if (!_enablePlatformProxy)
Expand Down Expand Up @@ -166,17 +168,14 @@ internal async Task<IActionResult> DoContainerProxy(string id, IPEndPoint client
await socket.ConnectAsync(target, token);

if (!socket.Connected)
{
_logger.SystemLog($"容器连接失败,请检查网络配置 -> {target.Address}:{target.Port}", TaskStatus.Failed, LogLevel.Warning);
return BadRequest(new RequestResponse("容器连接失败"));
}
throw new SocketException((int)SocketError.NotConnected);

stream = new CapturableNetworkStream(socket, metadata, options);
}
catch (Exception e)
catch (SocketException e)
{
_logger.LogError(e, "容器连接失败");
return BadRequest(new RequestResponse("容器连接失败"));
_logger.SystemLog($"容器连接失败({e.SocketErrorCode}),可能正在启动中或请检查网络配置 -> {target.Address}:{target.Port}", TaskStatus.Failed, LogLevel.Warning);
return new JsonResult(new RequestResponse($"容器连接失败{e.SocketErrorCode})", 418)) { StatusCode = 418 };
}

using var ws = await HttpContext.WebSockets.AcceptWebSocketAsync();
Expand All @@ -188,7 +187,7 @@ internal async Task<IActionResult> DoContainerProxy(string id, IPEndPoint client
}
catch (Exception e)
{
_logger.LogError(e, "代理错误");
_logger.LogError(e, "代理过程发生错误");
}
finally
{
Expand Down
13 changes: 5 additions & 8 deletions src/GZCTF/Repositories/InstanceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,17 +250,10 @@ public async Task<VerifyResult> VerifyAnswer(Submission submission, Cancellation

try
{

var instance = await _context.Instances
.IgnoreAutoIncludes()
.Include(i => i.FlagContext)
var instance = await _context.Instances.IgnoreAutoIncludes().Include(i => i.FlagContext)
.SingleOrDefaultAsync(i => i.ChallengeId == submission.ChallengeId &&
i.ParticipationId == submission.ParticipationId, token);

// submission is from the queue, do not modify it directly
// we need to requery the entity to ensure it is being tracked correctly
var updateSub = await _context.Submissions.SingleAsync(s => s.Id == submission.Id, token);

var ret = SubmissionType.Unaccepted;

if (instance is null)
Expand All @@ -269,6 +262,10 @@ public async Task<VerifyResult> VerifyAnswer(Submission submission, Cancellation
return new(SubmissionType.Unaccepted, AnswerResult.NotFound);
}

// submission is from the queue, do not modify it directly
// we need to requery the entity to ensure it is being tracked correctly
var updateSub = await _context.Submissions.SingleAsync(s => s.Id == submission.Id, token);

if (instance.FlagContext is null && submission.Challenge.Type.IsStatic())
{
updateSub.Status = await _context.FlagContexts
Expand Down

0 comments on commit dbd8bc6

Please sign in to comment.