From a060a10b55b85e9b35f77ce96e982fd96a79e45b Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Tue, 30 Jul 2019 19:50:56 +0100 Subject: [PATCH] Add user agent string --- PolyFeed/FeedBuilder.cs | 5 ++++- PolyFeed/Helpers/UserAgentHelper.cs | 34 +++++++++++++++++++++++++++++ PolyFeed/PolyFeed.csproj | 1 + 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 PolyFeed/Helpers/UserAgentHelper.cs diff --git a/PolyFeed/FeedBuilder.cs b/PolyFeed/FeedBuilder.cs index 8724a52..cebae46 100644 --- a/PolyFeed/FeedBuilder.cs +++ b/PolyFeed/FeedBuilder.cs @@ -30,7 +30,10 @@ namespace PolyFeed public async Task AddSource(FeedSource source) { await Console.Error.WriteLineAsync("[Builder] Downloading content"); - WebResponse response = await WebRequest.Create(source.Feed.Url).GetResponseAsync(); + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(source.Feed.Url); + + request.UserAgent = UserAgentHelper.UserAgent; + WebResponse response = await request.GetResponseAsync(); await Console.Error.WriteLineAsync("[Builder] Generating feed header"); diff --git a/PolyFeed/Helpers/UserAgentHelper.cs b/PolyFeed/Helpers/UserAgentHelper.cs new file mode 100644 index 0000000..0a892c5 --- /dev/null +++ b/PolyFeed/Helpers/UserAgentHelper.cs @@ -0,0 +1,34 @@ +using System; +using System.Reflection; + +namespace PolyFeed.Helpers +{ + public static class UserAgentHelper + { + public static string UserAgent { + get { + return $"PolyFeed/{version} ({os_name} {cpu_arch}; +https://github.com/sbrl/PolyFeed) .NET-CLR/{clr_version} {mono_info}"; + } + } + + private static string version => Program.GetProgramVersion(); + private static string os_name => Environment.OSVersion.Platform.ToString().Replace("Unix", "Linux"); + private static string cpu_arch => System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture.ToString(); + + private static string clr_version => Environment.Version.ToString(); + + private static string mono_info { + get { + Type type = Type.GetType("Mono.Runtime"); + if (type == null) + return string.Empty; + + MethodInfo displayName = type.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static); + if (displayName != null) + return $"Mono/{(string)displayName.Invoke(null, null)}"; + + return string.Empty; + } + } + } +} diff --git a/PolyFeed/PolyFeed.csproj b/PolyFeed/PolyFeed.csproj index 7500696..f88c0f4 100644 --- a/PolyFeed/PolyFeed.csproj +++ b/PolyFeed/PolyFeed.csproj @@ -149,6 +149,7 @@ +