Skip to content

Commit f61437b

Browse files
committed
Suggestion for implementation
1 parent 65cfa5e commit f61437b

5 files changed

Lines changed: 84 additions & 51 deletions

File tree

.github/workflows/update-cloudflare-proxies.yml

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ on:
55
- cron: '0 0 1 * *' # runs at 00:00 UTC on the 1st day of every month
66
workflow_dispatch:
77

8-
env:
9-
IP_MERGED_FILE: Common/cloudflare-ips.txt
10-
118
jobs:
129
update-proxies:
1310
runs-on: ubuntu-latest
@@ -17,23 +14,16 @@ jobs:
1714
with:
1815
ref: ${{ github.ref }}
1916

20-
- name: Fetch Cloudflare IPs and Update Files
21-
env:
22-
IPV4_URL: https://www.cloudflare.com/ips-v4
23-
IPV6_URL: https://www.cloudflare.com/ips-v6
24-
run: |
25-
set -euo pipefail
17+
- uses: actions/setup-dotnet@v4
2618

27-
echo "Fetching Cloudflare IP lists and merging"
28-
curl -s $IPV4_URL > $IP_MERGED_FILE
29-
echo "" >> $IP_MERGED_FILE
30-
curl -s $IPV6_URL >> $IP_MERGED_FILE
19+
- name: Build to regenerate Cloudflare IPs
20+
run: dotnet build Common/Common.csproj
3121

3222
- name: Commit and Push Changes
3323
run: |
3424
git config user.name "github-actions[bot]"
3525
git config user.email "github-actions[bot]@users.noreply.github.com"
36-
git add ${{ env.IP_MERGED_FILE }}
26+
git add Common/Utils/CloudflareNetworks.g.cs
3727
3828
if git diff --cached --quiet; then
3929
echo "No changes detected."

Common/Common.csproj

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,51 @@
3535
<PackageReference Include="Z.EntityFramework.Plus.EFCore" />
3636
</ItemGroup>
3737

38-
<!-- Files to copy -->
39-
<ItemGroup>
40-
<EmbeddedResource Include="cloudflare-ips.txt">
41-
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
42-
</EmbeddedResource>
43-
</ItemGroup>
38+
<!-- Fetch Cloudflare IPs at build time and generate C# source -->
39+
<UsingTask TaskName="GenerateCloudflareIPsSource" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)/Microsoft.Build.Tasks.Core.dll">
40+
<ParameterGroup>
41+
<IPv4File ParameterType="System.String" Required="true" />
42+
<IPv6File ParameterType="System.String" Required="true" />
43+
<OutputFile ParameterType="System.String" Required="true" />
44+
</ParameterGroup>
45+
<Task>
46+
<Code Type="Fragment" Language="cs"><![CDATA[
47+
var lines = new List<string>();
48+
lines.AddRange(File.ReadAllLines(IPv4File).Where(l => !string.IsNullOrWhiteSpace(l)));
49+
lines.AddRange(File.ReadAllLines(IPv6File).Where(l => !string.IsNullOrWhiteSpace(l)));
50+
51+
var sb = new System.Text.StringBuilder();
52+
sb.AppendLine("// <auto-generated/>");
53+
sb.AppendLine("using System.Net;");
54+
sb.AppendLine();
55+
sb.AppendLine("namespace OpenShock.Common.Utils;");
56+
sb.AppendLine();
57+
sb.AppendLine("public static partial class TrustedProxiesFetcher");
58+
sb.AppendLine("{");
59+
sb.AppendLine(" private static readonly IPNetwork[] CloudflareNetworks =");
60+
sb.AppendLine(" [");
61+
foreach (var line in lines)
62+
sb.AppendLine($" IPNetwork.Parse(\"{line.Trim()}\"),");
63+
sb.AppendLine(" ];");
64+
sb.AppendLine("}");
65+
66+
File.WriteAllText(OutputFile, sb.ToString());
67+
]]></Code>
68+
</Task>
69+
</UsingTask>
70+
71+
<Target Name="FetchCloudflareIPs" BeforeTargets="PrepareForBuild">
72+
<MakeDir Directories="$(IntermediateOutputPath)" />
73+
<DownloadFile SourceUrl="https://www.cloudflare.com/ips-v4"
74+
DestinationFolder="$(IntermediateOutputPath)"
75+
DestinationFileName="cf-v4.txt" />
76+
<DownloadFile SourceUrl="https://www.cloudflare.com/ips-v6"
77+
DestinationFolder="$(IntermediateOutputPath)"
78+
DestinationFileName="cf-v6.txt" />
79+
<GenerateCloudflareIPsSource IPv4File="$(IntermediateOutputPath)cf-v4.txt"
80+
IPv6File="$(IntermediateOutputPath)cf-v6.txt"
81+
OutputFile="$(MSBuildProjectDirectory)/Utils/CloudflareNetworks.g.cs" />
82+
</Target>
4483

4584
<!-- Capture git commit only if we're in a git repo; pipe output back to MSBuild -->
4685
<Target Name="SetSourceRevisionId" BeforeTargets="InitializeSourceControlInformation">
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// <auto-generated/>
2+
using System.Net;
3+
4+
namespace OpenShock.Common.Utils;
5+
6+
public static partial class TrustedProxiesFetcher
7+
{
8+
private static readonly IPNetwork[] CloudflareNetworks =
9+
[
10+
IPNetwork.Parse("173.245.48.0/20"),
11+
IPNetwork.Parse("103.21.244.0/22"),
12+
IPNetwork.Parse("103.22.200.0/22"),
13+
IPNetwork.Parse("103.31.4.0/22"),
14+
IPNetwork.Parse("141.101.64.0/18"),
15+
IPNetwork.Parse("108.162.192.0/18"),
16+
IPNetwork.Parse("190.93.240.0/20"),
17+
IPNetwork.Parse("188.114.96.0/20"),
18+
IPNetwork.Parse("197.234.240.0/22"),
19+
IPNetwork.Parse("198.41.128.0/17"),
20+
IPNetwork.Parse("162.158.0.0/15"),
21+
IPNetwork.Parse("104.16.0.0/13"),
22+
IPNetwork.Parse("104.24.0.0/14"),
23+
IPNetwork.Parse("172.64.0.0/13"),
24+
IPNetwork.Parse("131.0.72.0/22"),
25+
IPNetwork.Parse("2400:cb00::/32"),
26+
IPNetwork.Parse("2606:4700::/32"),
27+
IPNetwork.Parse("2803:f800::/32"),
28+
IPNetwork.Parse("2405:b500::/32"),
29+
IPNetwork.Parse("2405:8100::/32"),
30+
IPNetwork.Parse("2a06:98c0::/29"),
31+
IPNetwork.Parse("2c0f:f248::/32"),
32+
];
33+
}

Common/Utils/TrustedProxiesFetcher.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace OpenShock.Common.Utils;
44

5-
public static class TrustedProxiesFetcher
5+
public static partial class TrustedProxiesFetcher
66
{
77
private static readonly HttpClient Client = new();
88

@@ -77,14 +77,7 @@ public static async Task<IPNetwork[]> GetTrustedNetworksAsync(bool fetch = true)
7777
cfProxies = await FetchCloudflareIPs();
7878
}
7979

80-
if (cfProxies is null)
81-
{
82-
var assembly = typeof(TrustedProxiesFetcher).Assembly;
83-
var resourceName = assembly.GetName().Name + ".cloudflare-ips.txt";
84-
await using var stream = assembly.GetManifestResourceStream(resourceName) ?? throw new NullReferenceException("Could not open embedded cloudflare-ips.txt file");
85-
using var reader = new StreamReader(stream);
86-
cfProxies = ParseNetworks(await reader.ReadToEndAsync());
87-
}
80+
cfProxies ??= CloudflareNetworks;
8881

8982
return [.. PrivateNetworksParsed, .. cfProxies];
9083
}

Common/cloudflare-ips.txt

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)