Skip to content

Commit

Permalink
Merge pull request #46 from chi-rei-den/squid
Browse files Browse the repository at this point in the history
Update mirrors, adjust request UserAgent
  • Loading branch information
sgkoishi authored May 2, 2020
2 parents 4c8c34e + dd95c7c commit 155abf0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
7 changes: 6 additions & 1 deletion Localizer/Helpers/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,19 @@ public static void LogDebug(object o)

#region Network

internal static string UserAgent() {
return $"Localizer ({Environment.OSVersion}; rv: {Localizer.Instance.Version}; {(Environment.Is64BitOperatingSystem ? "x64" : "x86")}) tModLoader/{ModLoader.versionTag} ({(Environment.Is64BitOperatingSystem ? "x64" : "x86")})";
}

public static HttpWebResponse GET(string url)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/json;charset=UTF-8";
request.Accept = "application/vnd.github.v3+json";
request.UserAgent = "Localizer";
request.UserAgent = UserAgent();
request.Timeout = 9000;
request.Headers["Accept-Language"] = Terraria.Localization.LanguageManager.Instance.ActiveCulture.CultureInfo.ToString();

return (HttpWebResponse)request.GetResponse();
}
Expand Down
17 changes: 5 additions & 12 deletions Localizer/ModBrowser/Patches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using Harmony;
using Terraria.ModLoader;

Expand Down Expand Up @@ -51,22 +52,20 @@ public static void Patch()
}
}

private static readonly Regex _defaultMirror = new Regex(@"mirror(?:\d*)?\.sgkoi\.dev");
private static string GetModListURL()
{
var mirror = Localizer.Config.ModListMirror[0];
switch (mirror)
{
case "mirror.sgkoi.dev":
case "mirror5.sgkoi.dev":
return $"https://{mirror}/tModLoader/listmods.php";
case "mirror2.sgkoi.dev":
return "http://www.mb.axeel.moe/tModLoader/listmods.php";
case "mirror3.sgkoi.dev":
return "https://trbbs.cc/trmod/listmods.php";
case "mirror4.sgkoi.dev":
return "http://www.mb2.axeel.moe:25555/tModLoader/listmods.php";
default:
return mirror;
return _defaultMirror.IsMatch(mirror) ? $"https://{mirror}/tModLoader/listmods.php" : mirror;
}
}

Expand All @@ -75,17 +74,14 @@ private static string GetModDownloadURL()
var mirror = Localizer.Config.ModDownloadMirror[0];
switch (mirror)
{
case "mirror.sgkoi.dev":
case "mirror5.sgkoi.dev":
return $"https://{mirror}/tModLoader/download.php?Down=mods/";
case "mirror2.sgkoi.dev":
return "http://www.mb.axeel.moe/tModLoader/download.php?Down=mods/";
case "mirror3.sgkoi.dev":
return "https://trbbs.cc/trmod/";
case "mirror4.sgkoi.dev":
return "http://www.mb2.axeel.moe:25555/tModLoader/download.php?Down=mods/";
default:
return mirror;
return _defaultMirror.IsMatch(mirror) ? $"https://{mirror}/tModLoader/download.php?Down=mods/" : mirror;
}
}

Expand All @@ -94,15 +90,12 @@ private static string GetModDescURL()
var mirror = Localizer.Config.ModDescMirror[0];
switch (mirror)
{
case "mirror.sgkoi.dev":
case "mirror5.sgkoi.dev":
return $"https://{mirror}/tModLoader/moddescription.php";
case "mirror2.sgkoi.dev":
return "http://www.mb.axeel.moe/tModLoader/moddescription.php";
case "mirror4.sgkoi.dev":
return "http://www.mb2.axeel.moe:25555/tModLoader/moddescription.php";
default:
return mirror;
return _defaultMirror.IsMatch(mirror) ? $"https://{mirror}/tModLoader/moddescription.php" : mirror;
}
}

Expand Down
5 changes: 4 additions & 1 deletion Localizer/Network/DownloadManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ public class DownloadManager : IDownloadManagerService
{
public void Download(string url, string path)
{
new WebClient().DownloadFile(new Uri(url), path);
var wc= new WebClient();
wc.Headers[HttpRequestHeader.UserAgent] = Utils.UserAgent();
wc.Headers[HttpRequestHeader.AcceptLanguage] = Terraria.Localization.LanguageManager.Instance.ActiveCulture.CultureInfo.ToString();
wc.DownloadFile(new Uri(url), path);
}

public void Dispose()
Expand Down
2 changes: 1 addition & 1 deletion Localizer/build.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
author = Chireiden Team
version = 1.5.0.15
version = 1.5.0.16
displayName = Localizer
hideCode = true
hideResources = false
Expand Down

0 comments on commit 155abf0

Please sign in to comment.