Skip to content

Commit d76612c

Browse files
committed
Allow gz format for firewall uri rules
1 parent 734a261 commit d76612c

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

IPBan/IPBanMain.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2525
using DigitalRuby.IPBanCore;
2626

2727
using System;
28+
using System.Net;
29+
using System.Net.Http;
2830
using System.Threading;
2931
using System.Threading.Tasks;
3032

IPBanCore/Core/IPBan/IPBanUriFirewallRule.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2525
using System;
2626
using System.Collections.Generic;
2727
using System.IO;
28+
using System.IO.Compression;
2829
using System.Linq;
2930
using System.Net.Http;
3031
using System.Text;
@@ -168,7 +169,18 @@ public async Task Update(CancellationToken cancelToken = default)
168169
string filePath = Uri.LocalPath;
169170
if (File.Exists(filePath))
170171
{
171-
await ProcessResult(await File.ReadAllTextAsync(filePath, cancelToken), cancelToken);
172+
string text;
173+
if (filePath.EndsWith(".gz", StringComparison.OrdinalIgnoreCase))
174+
{
175+
using var fs = File.OpenRead(filePath);
176+
var bytes = DecompressBytes(fs);
177+
text = Encoding.UTF8.GetString(bytes);
178+
}
179+
else
180+
{
181+
text = await File.ReadAllTextAsync(filePath, cancelToken);
182+
}
183+
await ProcessResult(text, cancelToken);
172184
}
173185
}
174186
else
@@ -237,5 +249,15 @@ private Task ProcessResult(string text, CancellationToken cancelToken)
237249

238250
return firewall.BlockIPAddresses(RulePrefix, ranges, null, cancelToken);
239251
}
252+
253+
private static byte[] DecompressBytes(Stream input)
254+
{
255+
MemoryStream decompressdStream = new();
256+
{
257+
using GZipStream gz = new(input, CompressionMode.Decompress, true);
258+
gz.CopyTo(decompressdStream);
259+
}
260+
return decompressdStream.ToArray();
261+
}
240262
}
241263
}

IPBanCore/Core/Interfaces/IHttpRequestMaker.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2424

2525
using System;
2626
using System.Collections.Generic;
27+
using System.IO.Compression;
28+
using System.IO;
2729
using System.Net;
2830
using System.Net.Cache;
2931
using System.Net.Http;
@@ -107,8 +109,10 @@ public async Task<byte[]> MakeRequestAsync(Uri uri, string postJson = null, IEnu
107109
versionAssembly = GetType().Assembly;
108110
}
109111
}
110-
HttpRequestMessage msg = new();
111-
msg.RequestUri = uri;
112+
HttpRequestMessage msg = new()
113+
{
114+
RequestUri = uri
115+
};
112116
msg.Headers.Add("User-Agent", versionAssembly.GetName().Name);
113117
if (headers != null)
114118
{
@@ -135,6 +139,22 @@ public async Task<byte[]> MakeRequestAsync(Uri uri, string postJson = null, IEnu
135139
{
136140
throw new WebException("Request to url " + uri + " failed, status: " + responseMsg.StatusCode + ", response: " + Encoding.UTF8.GetString(response));
137141
}
142+
if (uri.AbsolutePath.EndsWith(".gz", StringComparison.OrdinalIgnoreCase))
143+
{
144+
try
145+
{
146+
// in case response somehow got gzip decompressed already, catch exception and keep response as is
147+
MemoryStream decompressdStream = new();
148+
{
149+
using GZipStream gz = new(new MemoryStream(response), CompressionMode.Decompress, true);
150+
gz.CopyTo(decompressdStream);
151+
}
152+
response = decompressdStream.ToArray();
153+
}
154+
catch
155+
{
156+
}
157+
}
138158
return response;
139159
}
140160
}

0 commit comments

Comments
 (0)