Skip to content

Commit

Permalink
c#
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterbrandsen committed Sep 1, 2024
1 parent 396e2d3 commit 1d91135
Show file tree
Hide file tree
Showing 14 changed files with 309 additions and 215 deletions.
19 changes: 19 additions & 0 deletions src/ScreepsUserTracker/API/API.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="proxies.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
41 changes: 41 additions & 0 deletions src/ScreepsUserTracker/API/HttpClientManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace API
{
public static class HttpClientManager
{
public static HttpClient CreateHttpClient(string? proxyUrl = null)
{
var handler = new HttpClientHandler();

if (!string.IsNullOrEmpty(proxyUrl))
{
handler.Proxy = new WebProxy(proxyUrl)
{
BypassProxyOnLocal = true
};
handler.UseProxy = true;
}
else
{
handler.UseProxy = false;
}

var httpClient = new HttpClient(handler)
{
//Timeout = TimeSpan.FromSeconds(30),
};

// Enable compression
httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new System.Net.Http.Headers.StringWithQualityHeaderValue("gzip"));
httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new System.Net.Http.Headers.StringWithQualityHeaderValue("deflate"));

return httpClient;
}
}
}
83 changes: 83 additions & 0 deletions src/ScreepsUserTracker/API/HttpHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Shared.Models;
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace API
{
public static class HttpHelper
{
public static string DecodeGzip(string encodedString)
{
byte[] compressedBytes = Convert.FromBase64String(encodedString);
using (MemoryStream memoryStream = new MemoryStream(compressedBytes))
{
using (GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
{
using (StreamReader streamReader = new StreamReader(gzipStream))
{
return streamReader.ReadToEnd();
}
}
}
}
public static async Task<HttpResponseResult> GetAsync(string url, string? proxyUrl = null)
{
var result = new HttpResponseResult();

var client = HttpClientManager.CreateHttpClient(proxyUrl);
try
{
var response = await client.GetAsync(url);
result.StatusCode = response.StatusCode;
result.IsSuccessStatusCode = response.IsSuccessStatusCode;

if (response.IsSuccessStatusCode)
{
if (response.Content.Headers.ContentType?.MediaType == "application/octet-stream")
{
var contentBytes = await response.Content.ReadAsByteArrayAsync();
var decodedContent = DecodeGzip(Convert.ToBase64String(contentBytes));
result.Content = decodedContent;
}
else
{
result.Content = await response.Content.ReadAsStringAsync();
}
}
else
{
result.Content = $"Error: {response.ReasonPhrase}";
}
}
catch (HttpRequestException e)
{
// Handle network-related errors
result.StatusCode = HttpStatusCode.ServiceUnavailable;
result.Content = $"Request error: {e.Message}";
result.IsSuccessStatusCode = false;
}
catch (TaskCanceledException e)
{
// Handle timeout errors
result.StatusCode = HttpStatusCode.RequestTimeout;
result.Content = $"Request timeout: {e.Message}";
result.IsSuccessStatusCode = false;
}
catch (Exception e)
{
// Handle all other errors
result.StatusCode = HttpStatusCode.InternalServerError;
result.Content = $"Unexpected error: {e.Message}";
result.IsSuccessStatusCode = false;
}

client.Dispose();
return result;
}
}
}
22 changes: 22 additions & 0 deletions src/ScreepsUserTracker/API/Proxies.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace API
{
public class Proxies
{
public static List<string> GetProxies()
{
var proxies = new List<string>();
string[] proxiesArray = File.ReadAllLines("proxies.txt");
foreach (string proxy in proxiesArray)
{
proxies.Add($"{proxy}");
}
return proxies;
}
}
}
24 changes: 24 additions & 0 deletions src/ScreepsUserTracker/API/ScreepsAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Shared.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace API
{
public class ScreepsAPI
{
public static int Y {get;set;}
public async static Task<HttpResponseResult> GetScreepsHistory(int i, string url, string? proxyUrl = null)
{
var responseResult = await HttpHelper.GetAsync(url, proxyUrl);

Console.WriteLine($"{Y}-{i}: Is Success: {responseResult.IsSuccessStatusCode}");
Y += 1;
return responseResult;
}
}
}
37 changes: 37 additions & 0 deletions src/ScreepsUserTracker/ScreepsUserTracker.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34607.119
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ScreepsUserTracker", "ScreepsUserTracker\ScreepsUserTracker.csproj", "{46426535-024D-4EF0-A08C-24F7B05F0DA0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API", "API\API.csproj", "{242E638D-7C01-4B41-8912-EF2D2A7A4E1D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csproj", "{4B6246B2-819F-40B8-BA26-654CB12032C5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{46426535-024D-4EF0-A08C-24F7B05F0DA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{46426535-024D-4EF0-A08C-24F7B05F0DA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{46426535-024D-4EF0-A08C-24F7B05F0DA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{46426535-024D-4EF0-A08C-24F7B05F0DA0}.Release|Any CPU.Build.0 = Release|Any CPU
{242E638D-7C01-4B41-8912-EF2D2A7A4E1D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{242E638D-7C01-4B41-8912-EF2D2A7A4E1D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{242E638D-7C01-4B41-8912-EF2D2A7A4E1D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{242E638D-7C01-4B41-8912-EF2D2A7A4E1D}.Release|Any CPU.Build.0 = Release|Any CPU
{4B6246B2-819F-40B8-BA26-654CB12032C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4B6246B2-819F-40B8-BA26-654CB12032C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4B6246B2-819F-40B8-BA26-654CB12032C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4B6246B2-819F-40B8-BA26-654CB12032C5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2CAB7E7C-EF4C-4774-8A6E-11CC73044ED9}
EndGlobalSection
EndGlobal
31 changes: 31 additions & 0 deletions src/ScreepsUserTracker/ScreepsUserTracker/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using API;
using Shared.Models;
using System.Diagnostics;

var proxies = Proxies.GetProxies();

// 119KB
var url = "https://screeps.com/room-history/shard0/E67N17/61400100.json";
var proxyUrl2= "178.208.183.112:3128";
var result = await ScreepsAPI.GetScreepsHistory(0, url, proxyUrl2);
Console.WriteLine($"Status Code: {result.StatusCode}");
Console.WriteLine($"Is Success: {result.IsSuccessStatusCode}");

Stopwatch stopwatch = new Stopwatch();

stopwatch.Start();

List<Task<HttpResponseResult>> tasks = new List<Task<HttpResponseResult>>();
//for (int i = 0; i < proxies.Count; i++)
for (int i = 0; i < 240; i++)
{
var proxyUrl = proxies[i];
tasks.Add(ScreepsAPI.GetScreepsHistory(i, url, proxyUrl));
}
await Task.WhenAll(tasks);
stopwatch.Stop();
TimeSpan elapsedTime = stopwatch.Elapsed;
Console.WriteLine($"Elapsed Time: {elapsedTime}");

var results = tasks.Select(t => t.Result.IsSuccessStatusCode).ToList();
Console.WriteLine($"Success: {results.Count(r => r)}");
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\API\API.csproj" />
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>

</Project>
16 changes: 16 additions & 0 deletions src/ScreepsUserTracker/Shared/Models/HttpResponseResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Shared.Models
{
public class HttpResponseResult
{
public string Content { get; set; } = "";
public HttpStatusCode StatusCode { get; set; }
public bool IsSuccessStatusCode { get; set; }
}
}
12 changes: 12 additions & 0 deletions src/ScreepsUserTracker/Shared/Models/ScreepsHistoryModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shared.Models
{
public class ScreepsHistoryModel
{
}
}
9 changes: 9 additions & 0 deletions src/ScreepsUserTracker/Shared/Shared.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
49 changes: 0 additions & 49 deletions src/logger.py

This file was deleted.

Loading

0 comments on commit 1d91135

Please sign in to comment.