Skip to content

Commit e385956

Browse files
committed
Some Update
1 parent 29bb059 commit e385956

File tree

125 files changed

+4012
-1643
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

125 files changed

+4012
-1643
lines changed

.github/workflows/ci-plugin.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ jobs:
2525
${{ runner.os }}-nuget-
2626
2727
- name: Setup .NET
28-
uses: actions/setup-dotnet@v3
28+
uses: actions/setup-dotnet@v4
2929
with:
30-
dotnet-version: 9.x
30+
dotnet-version: 9
3131
cache: true
3232
cache-dependency-path: src/Plugins/**/packages.lock.json
3333

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System.Net;
2+
using Impostor.Api.Data;
3+
using Impostor.Api.Extension.Utils;
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace Impostor.Api.Extension.Commands;
7+
8+
public class BanIpContent(ILogger<BanIpContent> logger) : IContent
9+
{
10+
public string Name => "BanIp";
11+
public bool Enable => true;
12+
13+
public class BanIpInfo(IPAddress ip, DateTime startTime, DateTime endTime)
14+
{
15+
public IPAddress IP { get; set; } = ip;
16+
public DateTime StartTime { get; set; } = startTime;
17+
public DateTime EndTime { get; set; } = endTime;
18+
}
19+
20+
21+
public List<BanIpInfo> _banIps = [];
22+
public Task<string> SerialiseAsync()
23+
{
24+
return Task.FromResult(JsonUtils.Serialize(_banIps));
25+
}
26+
27+
public Task BanIpAsync(IPAddress address, TimeSpan span)
28+
{
29+
if (_banIps.Any(ban => ban.IP.Equals(address)))
30+
{
31+
logger.LogWarning("Ip {0} 已被 Ban", address);
32+
return Task.CompletedTask;
33+
}
34+
35+
var start = DateTime.Now;
36+
var end = start.Add(span);
37+
_banIps.Add(new BanIpInfo(address, start, end));
38+
logger.LogInformation("Ban Ip {ip} {span} 从 {start} 到 {end}", address, span, start, end);
39+
return Task.CompletedTask;
40+
}
41+
42+
public bool IsBan(string ip) => CheckIsBan(IPAddress.Parse(ip));
43+
44+
public void UnBan(string ip) => UnBan(IPAddress.Parse(ip));
45+
46+
public void UnBan(IPAddress ip)
47+
{
48+
var info = _banIps.FirstOrDefault(ban => ban.IP.Equals(ip));
49+
if (info == null)
50+
{
51+
return;
52+
}
53+
54+
_banIps.Remove(info);
55+
logger.LogInformation("UnBan Ip {ip}", ip);
56+
}
57+
58+
public bool CheckIsBan(IPAddress ip)
59+
{
60+
var info = _banIps.FirstOrDefault(ban => ban.IP.Equals(ip));
61+
if (info == null)
62+
{
63+
return false;
64+
}
65+
66+
if (info.EndTime < DateTime.Now)
67+
{
68+
_banIps.Remove(info);
69+
return false;
70+
}
71+
72+
return true;
73+
}
74+
75+
public Task DeserializeAsync(string data)
76+
{
77+
var json = JsonUtils.Deserialize<List<BanIpInfo>>(data);
78+
if (json == null)
79+
{
80+
return Task.CompletedTask;
81+
}
82+
83+
_banIps = json;
84+
return Task.CompletedTask;
85+
}
86+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
using Impostor.Api.Extension.Utils;
2+
using Impostor.Api.Utils;
3+
14
namespace Impostor.Api.Extension.Commands;
25

3-
public class CommandEventArgs(ICommandManager manager, string[] args) : EventArgs
6+
public class CommandEventArgs(ICommandManager manager, string[] args) : EventArgs, IArgUtils
47
{
58
public ICommandManager Sender { get; set; } = manager;
9+
public string? SubCommand { get; set; }
610
public string[] Args { get; set; } = args;
711
}
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
using Impostor.Api.Events;
2+
13
namespace Impostor.Api.Extension.Commands;
24

3-
public interface ICommand;
5+
public interface ICommand
6+
{
7+
public string GetDescription()
8+
{
9+
return string.Empty;
10+
}
11+
}
412

513
public interface ISystemCommand : ICommand
614
{
@@ -10,5 +18,5 @@ public interface ISystemCommand : ICommand
1018
public interface ISingleCommand : ICommand
1119
{
1220
public string Command { get; }
13-
public Task InvokeAsync(CommandEventArgs args);
21+
public Task<EventTypeResult> InvokeAsync(CommandEventArgs args);
1422
}

src/Impostor.Api.Extension/Commands/ICommandManager.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using Impostor.Api.Config;
2+
13
namespace Impostor.Api.Extension.Commands;
24

35
public interface ICommandManager
@@ -11,4 +13,8 @@ public interface ICommandManager
1113
public ICommandManager RegisterCommand<T>() where T : ICommand;
1214

1315
public Task HandleCommandAsync(string commandString);
16+
17+
public Task HandleStringAsync();
18+
19+
public List<IConfigSet> ConfigSets { get; }
1420
}

src/Impostor.Api.Extension/Commands/NextCommand.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
using Impostor.Api.Events;
2+
13
namespace Impostor.Api.Extension.Commands;
24

3-
public class NextCommand(string command, Func<CommandEventArgs, Task> onInvoke) : ISingleCommand
5+
public class NextCommand(string command, Func<CommandEventArgs, Task<EventTypeResult>> onInvoke) : ISingleCommand
46
{
5-
private Func<CommandEventArgs, Task> OnInvoke { get; } = onInvoke;
7+
private Func<CommandEventArgs, Task<EventTypeResult>> OnInvoke { get; } = onInvoke;
68
public string Command { get; } = command;
79

8-
public Task InvokeAsync(CommandEventArgs args)
10+
public Task<EventTypeResult> InvokeAsync(CommandEventArgs args)
911
{
1012
return OnInvoke(args);
1113
}

src/Impostor.Api.Extension/Impostor.Api.Extension.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
<ItemGroup>
2424
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.3.0"/>
25-
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="9.0.6"/>
25+
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="9.0.7" />
2626
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.3.0"/>
2727
</ItemGroup>
2828
</Project>

src/Impostor.Api.Extension/Net/ClientAuthInfo.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Net;
22
using Impostor.Api.Innersloth;
3+
using Impostor.Api.Net;
34

45
namespace Impostor.Api.Extension.Net;
56

@@ -12,10 +13,15 @@ public class ClientAuthInfo(
1213
IPAddress targetIp
1314
)
1415
{
16+
public string? CacheName { get; set; }
1517
public IPAddress TargetIp { get; set; } = targetIp;
1618
public uint LastId { get; set; } = lastId;
1719
public GameVersion Version { get; set; } = version;
1820
public Platforms Platform { get; set; } = platform;
1921
public string MatchmakerToken { get; set; } = matchmakerToken;
2022
public string FriendCode { get; set; } = friendCode;
23+
24+
public IClient? Client { get; set; }
25+
26+
public bool IsBanned { get; set; }
2127
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System.Net;
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization;
4+
5+
namespace Impostor.Api.Extension.Utils;
6+
7+
public static class JsonUtils
8+
{
9+
private static readonly IpAddressConverter IpAddressConverter = new();
10+
private static readonly TimeConverter TimeConverter = new();
11+
private static JsonSerializerOptions? _options;
12+
13+
public static JsonSerializerOptions GetConverterOptions()
14+
{
15+
if (_options != null)
16+
{
17+
return _options;
18+
}
19+
20+
_options = new JsonSerializerOptions
21+
{
22+
AllowTrailingCommas = true,
23+
WriteIndented = true,
24+
Converters = { IpAddressConverter,TimeConverter },
25+
};
26+
return _options;
27+
}
28+
29+
public static string Serialize<T>(T obj)
30+
{
31+
return JsonSerializer.Serialize(obj, GetConverterOptions());
32+
}
33+
34+
public static T? Deserialize<T>(string json)
35+
{
36+
return JsonSerializer.Deserialize<T>(json, GetConverterOptions());
37+
}
38+
}
39+
40+
internal class IpAddressConverter : JsonConverter<IPAddress>
41+
{
42+
public override IPAddress Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
43+
{
44+
if (reader.TokenType == JsonTokenType.String)
45+
return IPAddress.Parse(reader.GetString() ?? string.Empty);
46+
throw new JsonException();
47+
}
48+
49+
public override void Write(Utf8JsonWriter writer, IPAddress value, JsonSerializerOptions options)
50+
{
51+
writer.WriteStringValue(value.ToString());
52+
}
53+
}
54+
55+
internal class TimeConverter : JsonConverter<DateTime>
56+
{
57+
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
58+
{
59+
if (reader.TokenType == JsonTokenType.String)
60+
return DateTime.ParseExact(reader.GetString() ?? string.Empty, "yyyy:MM:dd:HH:mm", null);
61+
throw new JsonException();
62+
}
63+
64+
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
65+
{
66+
writer.WriteStringValue(value.ToString("yyyy:MM:dd:HH:mm"));
67+
}
68+
}

src/Impostor.Api.Extension/Utils/ServiceUtils.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
using Impostor.Api.Config;
2+
using Impostor.Api.Data;
3+
using Impostor.Api.Extension.Commands;
4+
using Impostor.Api.Utils;
15
using Microsoft.Extensions.Configuration;
26
using Microsoft.Extensions.DependencyInjection;
37

@@ -10,4 +14,63 @@ public static IServiceCollection ConfigureSection<T>(this IServiceCollection col
1014
{
1115
return collection.Configure<T>(configuration.GetSection(section));
1216
}
17+
18+
public static IServiceCollection AddBanIpContent(this IServiceCollection services, ServerConfig config)
19+
{
20+
if (config.EnableContent)
21+
{
22+
services.AddRequiredSingleton<IContent, BanIpContent>();
23+
return services;
24+
}
25+
26+
services.AddSingleton<BanIpContent>();
27+
return services;
28+
}
29+
30+
public class ServiceCacheGet<T>(IServiceProvider provider) where T : class
31+
{
32+
private T? _value;
33+
public T? Value => Get();
34+
35+
public IServiceProvider Provider { get; private set; } = provider;
36+
37+
public ServiceCacheGet<T> SetProvider(IServiceProvider provider)
38+
{
39+
Provider = provider;
40+
_noHasValue = false;
41+
return this;
42+
}
43+
44+
public Func<ServiceCacheGet<T>, bool>? HasUpdate { get; set; }
45+
46+
public Func<IServiceProvider, ServiceCacheGet<T>, T>? OnUpdate { get; set; }
47+
48+
public bool _noHasValue;
49+
50+
private T? Get()
51+
{
52+
if (HasUpdate?.Invoke(this) ?? false)
53+
{
54+
_value = OnUpdate != null ? OnUpdate.Invoke(Provider, this) : Provider.GetService<T>();
55+
56+
return _value;
57+
}
58+
59+
if (_noHasValue)
60+
{
61+
return null;
62+
}
63+
64+
if (_value != null)
65+
{
66+
return _value;
67+
}
68+
69+
var get = Provider.GetService<T>();
70+
_noHasValue = get == null;
71+
return _value = get;
72+
}
73+
74+
public static implicit operator T?(ServiceCacheGet<T> serviceCacheGet) => serviceCacheGet.Get();
75+
}
1376
}

0 commit comments

Comments
 (0)