-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
396e2d3
commit 1d91135
Showing
14 changed files
with
309 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}"); |
15 changes: 15 additions & 0 deletions
15
src/ScreepsUserTracker/ScreepsUserTracker/ScreepsUserTracker.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
src/ScreepsUserTracker/Shared/Models/HttpResponseResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
src/ScreepsUserTracker/Shared/Models/ScreepsHistoryModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.