|
35 | 35 | <PackageReference Include="Z.EntityFramework.Plus.EFCore" /> |
36 | 36 | </ItemGroup> |
37 | 37 |
|
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> |
44 | 83 |
|
45 | 84 | <!-- Capture git commit only if we're in a git repo; pipe output back to MSBuild --> |
46 | 85 | <Target Name="SetSourceRevisionId" BeforeTargets="InitializeSourceControlInformation"> |
|
0 commit comments