Skip to content

Commit

Permalink
fix: no attachment for dynamic container challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Sep 2, 2022
1 parent c51bfed commit 7312080
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion GZCTF/Models/Request/Game/ChallengeDetailModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal static ChallengeDetailModel FromInstance(Instance instance)
{
InstanceEntry = instance.Container?.Entry,
CloseTime = instance.Container?.ExpectStopAt,
Url = instance.Challenge.Type.IsDynamic() ?
Url = instance.Challenge.Type == ChallengeType.DynamicAttachment ?
instance.FlagContext?.Attachment?.UrlWithName(instance.Challenge.FileName) :
instance.Challenge.Attachment?.UrlWithName()
}
Expand Down
13 changes: 11 additions & 2 deletions GZCTF/Services/K8sService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using CTFServer.Utils;
using Docker.DotNet.Models;
using k8s;
using k8s.Autorest;
using k8s.Models;
using Microsoft.Extensions.Options;
using System.Net;
using System.Text;

namespace CTFServer.Services;
Expand Down Expand Up @@ -47,7 +49,8 @@ public K8sService(ILogger<K8sService> logger)

public async Task<Container?> CreateContainer(ContainerConfig config, CancellationToken token = default)
{
var name = $"{config.Image.Split("/").LastOrDefault()?.Split(":").FirstOrDefault()}-{Codec.StrMD5(config.Flag ?? Guid.NewGuid().ToString())[..16]}";
// use uuid avoid Conflict
var name = $"{config.Image.Split("/").LastOrDefault()?.Split(":").FirstOrDefault()}-{Guid.NewGuid().ToString("N")[..16]}";
name = name.Replace('_', '-'); // Ensure name is available
var pod = new V1Pod("v1", "Pod")
{
Expand Down Expand Up @@ -99,6 +102,12 @@ public K8sService(ILogger<K8sService> logger)
{
pod = await kubernetesClient.CreateNamespacedPodAsync(pod, "gzctf", cancellationToken: token);
}
catch(HttpOperationException e)
{
logger.SystemLog($"容器 {name} 创建失败, 状态:{e.Response.StatusCode.ToString()}", TaskStatus.Fail, LogLevel.Warning);
logger.SystemLog($"容器 {name} 创建失败, 响应:{e.Response.Content}", TaskStatus.Fail, LogLevel.Error);
return null;
}
catch (Exception e)
{
logger.LogError(e, "创建容器失败");
Expand Down Expand Up @@ -146,7 +155,7 @@ public K8sService(ILogger<K8sService> logger)

try
{
service = await kubernetesClient.CreateNamespacedServiceAsync(service, "gzctf", cancellationToken: token);
service = await kubernetesClient.CoreV1.CreateNamespacedServiceAsync(service, "gzctf", cancellationToken: token);
}
catch (Exception e)
{
Expand Down

0 comments on commit 7312080

Please sign in to comment.