Skip to content

Commit 10216d9

Browse files
committed
feat(cli): add protocol filtering to fetch command
1 parent 8961a84 commit 10216d9

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/Proxxi.Cli/Commands/Fetch/FetchCommand.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Proxxi.Core.ProxyWriters;
77
using Proxxi.Plugin.Loader.Extensions;
88
using Proxxi.Plugin.Loader.PluginLoaders;
9+
using Proxxi.Plugin.Sdk.Models;
910
using Proxxi.Plugin.Sdk.ProxySources;
1011

1112
using Spectre.Console;
@@ -29,6 +30,8 @@ public override async Task<int> ExecuteAsync(CommandContext context, FetchComman
2930
Stream stream;
3031
OutputFormat format;
3132

33+
var protocols = settings.Protocols ?? Protocols.None;
34+
3235
if (settings.Output != null)
3336
{
3437
stream = File.OpenWrite(settings.Output);
@@ -54,14 +57,14 @@ public override async Task<int> ExecuteAsync(CommandContext context, FetchComman
5457
if (writer is not IStreamProxyWriter streamProxyWriter)
5558
throw new InvalidOperationException($"{format} output does not support stream mode.");
5659

57-
await FetchAndWriteProxiesAsync(streamProxySource, streamProxyWriter, ct);
60+
await FetchAndWriteProxiesAsync(streamProxySource, streamProxyWriter, protocols, ct);
5861
}
5962
else
6063
{
6164
if (writer is not IBatchProxyWriter batchProxyWriter)
6265
throw new InvalidOperationException($"{format} output does not support stream mode.");
6366

64-
await FetchAndWriteProxiesAsync(batchProxySource, batchProxyWriter, ct);
67+
await FetchAndWriteProxiesAsync(batchProxySource, batchProxyWriter, protocols, ct);
6568
}
6669

6770
return 0;
@@ -105,24 +108,30 @@ public override async Task<int> ExecuteAsync(CommandContext context, FetchComman
105108
}
106109

107110
private static async Task FetchAndWriteProxiesAsync(IBatchProxySource? batchProxySource,
108-
IBatchProxyWriter writer, CancellationToken ct)
111+
IBatchProxyWriter writer, Protocols protocols, CancellationToken ct)
109112
{
110113
if (batchProxySource == null)
111114
throw new InvalidOperationException("The plugin does not support batch mode.");
112115

113116
var proxies = await batchProxySource.FetchAsync(ct);
114117

118+
if (protocols != Protocols.None)
119+
proxies = proxies.Where(p => (p.Protocols & protocols) != Protocols.None);
120+
115121
await writer.WriteAsync(proxies, ct);
116122
}
117123

118124
private static async Task FetchAndWriteProxiesAsync(IStreamProxySource? streamProxySource,
119-
IStreamProxyWriter writer, CancellationToken ct)
125+
IStreamProxyWriter writer, Protocols protocols, CancellationToken ct)
120126
{
121127
if (streamProxySource == null)
122128
throw new InvalidOperationException("The plugin does not support stream mode.");
123129

124130
var proxies = streamProxySource.FetchAsync(ct);
125131

132+
if (protocols != Protocols.None)
133+
proxies = proxies.Where(p => (p.Protocols & protocols) != Protocols.None);
134+
126135
await writer.WriteAsync(proxies, ct);
127136
}
128137

src/Proxxi.Cli/Commands/Fetch/FetchCommandSettings.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.ComponentModel;
22

33
using Proxxi.Core.Models;
4+
using Proxxi.Plugin.Sdk.Models;
45

56
using Spectre.Console.Cli;
67

@@ -24,6 +25,10 @@ public class FetchCommandSettings : CommandSettings
2425
[Description("Fetch proxies using streaming mode instead of batch mode")]
2526
public bool Stream { get; init; }
2627

28+
[CommandOption("-p|--protocols <PROTOCOLS>"), DefaultValue(Proxxi.Plugin.Sdk.Models.Protocols.None)]
29+
[Description("Filter proxies by protocol: http, https, socks4, socks5")]
30+
public Protocols? Protocols { get; init; }
31+
2732
[CommandOption("--pretty")]
2833
[Description("Write human-readable, pretty-formatted output")]
2934
public bool Pretty { get; init; }

0 commit comments

Comments
 (0)