Skip to content

Commit

Permalink
add command line tool for generating countries.json and dockerise it
Browse files Browse the repository at this point in the history
  • Loading branch information
aspriddell committed Jul 11, 2021
1 parent 7112c34 commit 2cadf44
Show file tree
Hide file tree
Showing 17 changed files with 160 additions and 10 deletions.
26 changes: 25 additions & 1 deletion .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:

- name: Setup PowerShell Policy
run: Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force

- name: Download Publish Script
run: powershell Invoke-WebRequest -Uri "https://raw.githubusercontent.com/dragonfruitnetwork/publish-script/main/nuget.ps1" -OutFile ".\nuget.ps1"

Expand All @@ -29,3 +29,27 @@ jobs:
- name: Nuget Build and Publish (Beta)
run: .\nuget.ps1 -TargetName $env:name -TargetProject $env:project -ApiKey ${{ secrets.NUGET_KEY }} -Suffix "-beta"
if: "github.event.release.prerelease"

- name: DockerHub Login
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push (Release)
uses: docker/build-push-action@v2
if: "!github.event.release.prerelease"
with:
push: true
context: .
tags: dragonfruitdotnet/onionfruit-country-engine:latest
file: ./DragonFruit.OnionFruit.Status.Generator/Dockerfile

- name: Build and push (Beta)
uses: docker/build-push-action@v2
if: "github.event.release.prerelease"
with:
push: true
context: .
tags: dragonfruitdotnet/onionfruit-country-engine:beta
file: ./DragonFruit.OnionFruit.Status.Generator/Dockerfile
25 changes: 25 additions & 0 deletions DragonFruit.OnionFruit.Status.Generator/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/.idea
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
10 changes: 10 additions & 0 deletions DragonFruit.OnionFruit.Status.Generator/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
COPY . .
ARG Version
RUN dotnet publish "./DragonFruit.OnionFruit.Status.Generator/DragonFruit.OnionFruit.Status.Generator.csproj" -c Release -o /app/publish /p:AssemblyVersion=$Version

FROM mcr.microsoft.com/dotnet/net:5.0 AS final
WORKDIR /app
COPY --from=build /app/publish .
ENV OUTPUT_FILE=/output/countries.json
ENTRYPOINT ["dotnet", "DragonFruit.OnionFruit.Status.Generator.dll"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\DragonFruit.OnionFruit.Status\DragonFruit.OnionFruit.Status.csproj" />
</ItemGroup>

</Project>
27 changes: 27 additions & 0 deletions DragonFruit.OnionFruit.Status.Generator/OnionFruitCountriesList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// OnionFruit.Status Copyright 2021 DragonFruit Network <inbox@dragonfruit.network>
// Licensed under MIT. Please refer to the LICENSE file for more info

using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;

namespace DragonFruit.OnionFruit.Status.Generator
{
public class OnionFruitCountriesList
{
[JsonProperty("in")]
public string[] In { get; set; }

[JsonProperty("out")]
public string[] Out { get; set; }

private static IEnumerable<string> GetCountriesWithFlag(IEnumerable<IGrouping<string, TorRelay>> info, RelayFlags flag) =>
info.Where(x => x.Any(y => y.Flags.HasFlag(flag))).Select(x => x.Key);

public static explicit operator OnionFruitCountriesList(IGrouping<string, TorRelay>[] info) => new()
{
In = GetCountriesWithFlag(info, RelayFlags.Guard).ToArray(),
Out = GetCountriesWithFlag(info, RelayFlags.Exit).ToArray()
};
}
}
38 changes: 38 additions & 0 deletions DragonFruit.OnionFruit.Status.Generator/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// OnionFruit.Status Copyright 2021 DragonFruit Network <inbox@dragonfruit.network>
// Licensed under MIT. Please refer to the LICENSE file for more info

using System;
using System.IO;
using System.Linq;
using Bia.Countries.Iso3166;
using DragonFruit.Common.Data;
using DragonFruit.Common.Data.Services;

namespace DragonFruit.OnionFruit.Status.Generator
{
internal static class Program
{
private static readonly ApiClient Client = new();

private static void Main(string[] args)
{
Console.WriteLine("Fetching data...");
var nodes = Client.GetServerInfo().Relays;
var countries = nodes.GroupBy(x => x.CountryCode.ToUpper()).Where(x => Countries.GetCountryByAlpha2(x.Key) is not null).ToArray();

Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine($"Downloaded {nodes.Length} relay metadata packets over {countries.Length} countries");
var response = (OnionFruitCountriesList)countries;

Console.WriteLine($"Response contains {response.In.Length} entry countries and {response.Out.Length} exit countries");

var saveLocation = Environment.GetEnvironmentVariable("OUTPUT_FILE") ?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "countries.json");
Console.Write($"Writing to {saveLocation}...");

FileServices.WriteFile(saveLocation, response);

Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Complete");
}
}
}
2 changes: 1 addition & 1 deletion DragonFruit.OnionFruit.Status.Tests/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// OnionFruit.Status Copyright 2020 DragonFruit Network <inbox@dragonfruit.network>
// OnionFruit.Status Copyright 2021 DragonFruit Network <inbox@dragonfruit.network>
// Licensed under MIT. Please refer to the LICENSE file for more info

using System;
Expand Down
6 changes: 6 additions & 0 deletions DragonFruit.OnionFruit.Status.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
readme.md = readme.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DragonFruit.OnionFruit.Status.Generator", "DragonFruit.OnionFruit.Status.Generator\DragonFruit.OnionFruit.Status.Generator.csproj", "{BF2C5F17-9621-4822-9C00-4C0C8DB5F8EA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +29,10 @@ Global
{617C360C-4282-4212-8295-8F37C57D7C20}.Debug|Any CPU.Build.0 = Debug|Any CPU
{617C360C-4282-4212-8295-8F37C57D7C20}.Release|Any CPU.ActiveCfg = Release|Any CPU
{617C360C-4282-4212-8295-8F37C57D7C20}.Release|Any CPU.Build.0 = Release|Any CPU
{BF2C5F17-9621-4822-9C00-4C0C8DB5F8EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BF2C5F17-9621-4822-9C00-4C0C8DB5F8EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BF2C5F17-9621-4822-9C00-4C0C8DB5F8EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BF2C5F17-9621-4822-9C00-4C0C8DB5F8EA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
5 changes: 4 additions & 1 deletion DragonFruit.OnionFruit.Status.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/CodeCleanup/Profiles/=Code_0020Cleanup/@EntryIndexedValue">&lt;?xml version="1.0" encoding="utf-16"?&gt;&lt;Profile name="Code Cleanup"&gt;&lt;CSArrangeThisQualifier&gt;True&lt;/CSArrangeThisQualifier&gt;&lt;CSUseVar&gt;&lt;BehavourStyle&gt;CAN_CHANGE_TO_EXPLICIT&lt;/BehavourStyle&gt;&lt;LocalVariableStyle&gt;ALWAYS_EXPLICIT&lt;/LocalVariableStyle&gt;&lt;ForeachVariableStyle&gt;ALWAYS_EXPLICIT&lt;/ForeachVariableStyle&gt;&lt;/CSUseVar&gt;&lt;CSOptimizeUsings&gt;&lt;OptimizeUsings&gt;True&lt;/OptimizeUsings&gt;&lt;EmbraceInRegion&gt;False&lt;/EmbraceInRegion&gt;&lt;RegionName&gt;&lt;/RegionName&gt;&lt;/CSOptimizeUsings&gt;&lt;CSShortenReferences&gt;True&lt;/CSShortenReferences&gt;&lt;CSReformatCode&gt;True&lt;/CSReformatCode&gt;&lt;CSUpdateFileHeader&gt;True&lt;/CSUpdateFileHeader&gt;&lt;CSCodeStyleAttributes ArrangeTypeAccessModifier="False" ArrangeTypeMemberAccessModifier="False" SortModifiers="True" RemoveRedundantParentheses="True" AddMissingParentheses="False" ArrangeBraces="False" ArrangeAttributes="False" ArrangeArgumentsStyle="False" /&gt;&lt;XAMLCollapseEmptyTags&gt;False&lt;/XAMLCollapseEmptyTags&gt;&lt;CSFixBuiltinTypeReferences&gt;True&lt;/CSFixBuiltinTypeReferences&gt;&lt;CSArrangeQualifiers&gt;True&lt;/CSArrangeQualifiers&gt;&lt;IDEA_SETTINGS&gt;&amp;lt;profile version="1.0"&amp;gt;&#xD;
&amp;lt;option name="myName" value="Code Cleanup" /&amp;gt;&#xD;
&amp;lt;/profile&amp;gt;&lt;/IDEA_SETTINGS&gt;&lt;/Profile&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderText/@EntryValue">OnionFruit.Status Copyright $CURRENT_YEAR$ DragonFruit Network &lt;inbox@dragonfruit.network&gt;&#xD;
Licensed under MIT. Please refer to the LICENSE file for more info</s:String>
<s:Boolean x:Key="/Default/CodeInspection/ExcludedFiles/FileMasksToSkip/=_002A_002Efnt/@EntryIndexedValue">True</s:Boolean>
Expand Down Expand Up @@ -235,7 +238,7 @@ Licensed under MIT. Please refer to the LICENSE file for more info</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=VirtualMemberNeverOverridden_002EGlobal/@EntryIndexedValue">HINT</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=VirtualMemberNeverOverridden_002ELocal/@EntryIndexedValue">WARNING</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=WrongIndentSize/@EntryIndexedValue">WARNING</s:String>
<s:String x:Key="/Default/CodeStyle/CodeCleanup/Profiles/=Code_0020Cleanup_0020_0028peppy_0029/@EntryIndexedValue">&lt;?xml version="1.0" encoding="utf-16"?&gt;&lt;Profile name="Code Cleanup (peppy)"&gt;&lt;CSArrangeThisQualifier&gt;True&lt;/CSArrangeThisQualifier&gt;&lt;CSUseVar&gt;&lt;BehavourStyle&gt;CAN_CHANGE_TO_EXPLICIT&lt;/BehavourStyle&gt;&lt;LocalVariableStyle&gt;ALWAYS_EXPLICIT&lt;/LocalVariableStyle&gt;&lt;ForeachVariableStyle&gt;ALWAYS_EXPLICIT&lt;/ForeachVariableStyle&gt;&lt;/CSUseVar&gt;&lt;CSOptimizeUsings&gt;&lt;OptimizeUsings&gt;True&lt;/OptimizeUsings&gt;&lt;EmbraceInRegion&gt;False&lt;/EmbraceInRegion&gt;&lt;RegionName&gt;&lt;/RegionName&gt;&lt;/CSOptimizeUsings&gt;&lt;CSShortenReferences&gt;True&lt;/CSShortenReferences&gt;&lt;CSReformatCode&gt;True&lt;/CSReformatCode&gt;&lt;CSUpdateFileHeader&gt;True&lt;/CSUpdateFileHeader&gt;&lt;CSCodeStyleAttributes ArrangeTypeAccessModifier="False" ArrangeTypeMemberAccessModifier="False" SortModifiers="True" RemoveRedundantParentheses="True" AddMissingParentheses="False" ArrangeBraces="False" ArrangeAttributes="False" ArrangeArgumentsStyle="False" /&gt;&lt;XAMLCollapseEmptyTags&gt;False&lt;/XAMLCollapseEmptyTags&gt;&lt;CSFixBuiltinTypeReferences&gt;True&lt;/CSFixBuiltinTypeReferences&gt;&lt;CSArrangeQualifiers&gt;True&lt;/CSArrangeQualifiers&gt;&lt;/Profile&gt;</s:String>

<s:String x:Key="/Default/CodeStyle/CodeCleanup/RecentlyUsedProfile/@EntryValue">Code Cleanup (peppy)</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/BRACES_FOR_DOWHILE/@EntryValue">RequiredForMultiline</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/BRACES_FOR_FIXED/@EntryValue">RequiredForMultiline</s:String>
Expand Down
2 changes: 1 addition & 1 deletion DragonFruit.OnionFruit.Status/Helpers/Bandwidth.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// OnionFruit.Status Copyright 2020 DragonFruit Network <inbox@dragonfruit.network>
// OnionFruit.Status Copyright 2021 DragonFruit Network <inbox@dragonfruit.network>
// Licensed under MIT. Please refer to the LICENSE file for more info

using System;
Expand Down
2 changes: 1 addition & 1 deletion DragonFruit.OnionFruit.Status/RelayFlags.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// OnionFruit.Status Copyright 2020 DragonFruit Network <inbox@dragonfruit.network>
// OnionFruit.Status Copyright 2021 DragonFruit Network <inbox@dragonfruit.network>
// Licensed under MIT. Please refer to the LICENSE file for more info

using System;
Expand Down
2 changes: 1 addition & 1 deletion DragonFruit.OnionFruit.Status/TorRelay.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// OnionFruit.Status Copyright 2020 DragonFruit Network <inbox@dragonfruit.network>
// OnionFruit.Status Copyright 2021 DragonFruit Network <inbox@dragonfruit.network>
// Licensed under MIT. Please refer to the LICENSE file for more info

using System;
Expand Down
2 changes: 1 addition & 1 deletion DragonFruit.OnionFruit.Status/TorRelayInformation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// OnionFruit.Status Copyright 2020 DragonFruit Network <inbox@dragonfruit.network>
// OnionFruit.Status Copyright 2021 DragonFruit Network <inbox@dragonfruit.network>
// Licensed under MIT. Please refer to the LICENSE file for more info

using System;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// OnionFruit.Status Copyright 2020 DragonFruit Network <inbox@dragonfruit.network>
// OnionFruit.Status Copyright 2021 DragonFruit Network <inbox@dragonfruit.network>
// Licensed under MIT. Please refer to the LICENSE file for more info

using System.Threading;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// OnionFruit.Status Copyright 2020 DragonFruit Network <inbox@dragonfruit.network>
// OnionFruit.Status Copyright 2021 DragonFruit Network <inbox@dragonfruit.network>
// Licensed under MIT. Please refer to the LICENSE file for more info

using DragonFruit.Common.Data;
Expand Down
6 changes: 5 additions & 1 deletion codecutter.json
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{"solution":"DragonFruit.OnionFruit.Status.sln","displayLevel":3,"errorLevel":3}
{
"solution": "DragonFruit.OnionFruit.Status.sln",
"displayLevel": 3,
"errorLevel": 3
}
File renamed without changes.

0 comments on commit 2cadf44

Please sign in to comment.