Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure of actions and hosting #3

Merged
merged 13 commits into from
Jul 3, 2021
Next Next commit
Github pages
Use Github pages for reading last run time and publish hosts file
  • Loading branch information
henrikwidlund committed Jul 3, 2021
commit ceac19202a597153c4bb623efb81b3987119e234
27 changes: 20 additions & 7 deletions .github/workflows/publish-hosts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,30 @@ jobs:

- name: Copy hosts to root of branch
run: |
mkdir ./public
HOSTS=bin/Release/net5.0/hosts
if test -f "$HOSTS"; then
cp $HOSTS .
cp $HOSTS ./public
fi

APPSETTINGS=bin/Release/net5.0/appsettings.json
if test -f "$APPSETTINGS"; then
cp $APPSETTINGS .
MODIFIED=bin/Release/net5.0/MODIFIED
if test -f "$MODIFIED"; then
cp $MODIFIED ./public
fi

- uses: EndBug/add-and-commit@v7
# APPSETTINGS=bin/Release/net5.0/appsettings.json
# if test -f "$APPSETTINGS"; then
# cp $APPSETTINGS .
# fi

- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
message: 'Auto update hosts'
add: 'hosts appsettings.json --force'
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./public
keep_files: true

# - uses: EndBug/add-and-commit@v7
# with:
# message: 'Auto update hosts'
# add: 'hosts appsettings.json --force'
4 changes: 3 additions & 1 deletion Constants.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace HostsParser
{
internal static class Constants
internal readonly struct Constants
{
internal const string PipeSign = "|";
internal const string HatSign = "^";
Expand All @@ -13,5 +13,7 @@ internal static class Constants
internal static readonly int IpFilterLength = IpFilter.Length;
internal const string LoopbackEntry = "0.0.0.0 0.0.0.0";
internal const string WwwPrefix = "www.";
internal const string UseExternalLastRun = "USE_EXTERNAL_LAST_RUN";
internal const string ModifiedFile = "MODIFIED";
}
}
1 change: 1 addition & 0 deletions HostsParser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<TargetFramework>net5.0</TargetFramework>
<Copyright>Henrik Widlund</Copyright>
<License>GPL-3.0 License</License>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
22 changes: 21 additions & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,30 @@
stopWatch.Start();

var settings = JsonSerializer.Deserialize<Settings>(File.ReadAllBytes("appsettings.json"));
if (settings == null)
{
logger.LogError("Couldn't load settings. Terminating...");
return;
}

using var httpClient = new HttpClient();

logger.LogInformation(WithTimeStamp("Start checking if external last run should be used"));
if (settings.LastRunExternalUri != null
&& bool.TryParse(Environment.GetEnvironmentVariable(Constants.UseExternalLastRun), out var useExternalLastRun)
&& useExternalLastRun)
{
var lastRunString = await httpClient.GetStringAsync(settings.LastRunExternalUri);
if (long.TryParse(lastRunString, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out var lastRun))
{
settings = settings with { SourcePreviousUpdateEpoch = lastRun };
logger.LogInformation(WithTimeStamp($"Using external last run: {lastRunString}"));
}
}
logger.LogInformation(WithTimeStamp("Done checking if external last run should be used"));

logger.LogInformation(WithTimeStamp("Start get source hosts"));
var sourceUris = (await httpClient.GetStringAsync(settings!.SourceUri))
var sourceUris = (await httpClient.GetStringAsync(settings.SourceUri))
.Split(Constants.NewLine);
logger.LogInformation(WithTimeStamp("Done get source hosts"));

Expand All @@ -43,6 +62,7 @@

var modifiedDate = DateTime.Parse(modifiedDateString, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AssumeUniversal);
var epoch = new DateTimeOffset(modifiedDate).ToUnixTimeSeconds();
await File.WriteAllTextAsync(Constants.ModifiedFile, epoch.ToString(NumberFormatInfo.InvariantInfo));
if (epoch <= settings.SourcePreviousUpdateEpoch)
{
logger.LogInformation(WithTimeStamp("Source not modified since previous run. Terminating..."));
Expand Down
3 changes: 2 additions & 1 deletion Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ internal record Settings(
Uri AdGuardUri,
string[] SkipLines,
string[] HeaderLines,
string[] KnownBadHosts);
string[] KnownBadHosts,
Uri? LastRunExternalUri);
}
3 changes: 2 additions & 1 deletion appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@
"adtech.fr",
"adtech.us",
"angelcities.com"
]
],
"LastRunExternalUri": "https://henrikwidlund.github.io/MODIFIED"
}