Browse Source

⚙ Allow configuration of User-Agent

- For OnlineConfigResolver and GeositeUpdater
tags/4.2.1.0
database64128 4 years ago
parent
commit
608675f96e
4 changed files with 14 additions and 5 deletions
  1. +2
    -0
      shadowsocks-csharp/Controller/Service/GeositeUpdater.cs
  2. +3
    -4
      shadowsocks-csharp/Controller/Service/OnlineConfigResolver.cs
  3. +1
    -1
      shadowsocks-csharp/Controller/ShadowsocksController.cs
  4. +8
    -0
      shadowsocks-csharp/Model/Configuration.cs

+ 2
- 0
shadowsocks-csharp/Controller/Service/GeositeUpdater.cs View File

@@ -102,6 +102,8 @@ namespace Shadowsocks.Controller
// because we can't change proxy on existing socketsHttpHandler instance
httpClientHandler = new HttpClientHandler();
httpClient = new HttpClient(httpClientHandler);
if (!string.IsNullOrWhiteSpace(config.userAgentString))
httpClient.DefaultRequestHeaders.Add("User-Agent", config.userAgentString);
if (config.enabled)
{
httpClientHandler.Proxy = new WebProxy(


+ 3
- 4
shadowsocks-csharp/Controller/Service/OnlineConfigResolver.cs View File

@@ -11,7 +11,7 @@ namespace Shadowsocks.Controller.Service
{
public class OnlineConfigResolver
{
public static async Task<List<Server>> GetOnline(string url, IWebProxy proxy = null)
public static async Task<List<Server>> GetOnline(string url, string userAgentString, IWebProxy proxy = null)
{
var httpClientHandler = new HttpClientHandler()
{
@@ -21,9 +21,8 @@ namespace Shadowsocks.Controller.Service
{
Timeout = TimeSpan.FromSeconds(15)
};

string userAgent = "ShadowsocksWindows/" + UpdateChecker.Version;
httpClient.DefaultRequestHeaders.Add("User-Agent", userAgent);
if (!string.IsNullOrWhiteSpace(userAgentString))
httpClient.DefaultRequestHeaders.Add("User-Agent", userAgentString);

string server_json = await httpClient.GetStringAsync(url);



+ 1
- 1
shadowsocks-csharp/Controller/ShadowsocksController.cs View File

@@ -691,7 +691,7 @@ namespace Shadowsocks.Controller
public async Task<int> UpdateOnlineConfigInternal(string url)
{
var onlineServer = await OnlineConfigResolver.GetOnline(url, _config.WebProxy);
var onlineServer = await OnlineConfigResolver.GetOnline(url, _config.userAgentString, _config.WebProxy);
_config.configs = Configuration.SortByOnlineConfig(
_config.configs
.Where(c => c.group != url)


+ 8
- 0
shadowsocks-csharp/Model/Configuration.cs View File

@@ -46,6 +46,7 @@ namespace Shadowsocks.Model
public string geositeUrl; // for custom geosite source (and rule group)
public string geositeGroup;
public bool geositeBlacklistMode;
public string userAgent;
//public NLogConfig.LogLevel logLevel;
public LogViewerConfig logViewer;
@@ -81,6 +82,7 @@ namespace Shadowsocks.Model
geositeUrl = "";
geositeGroup = "geolocation-!cn";
geositeBlacklistMode = true;
userAgent = "ShadowsocksWindows/$version";
logViewer = new LogViewerConfig();
proxy = new ProxyConfig();
@@ -92,6 +94,9 @@ namespace Shadowsocks.Model
onlineConfigSource = new List<string>();
}
[JsonIgnore]
public string userAgentString; // $version substituted with numeral version in it
[JsonIgnore]
NLogConfig nLogConfig;
@@ -153,6 +158,7 @@ namespace Shadowsocks.Model
string configContent = File.ReadAllText(CONFIG_FILE);
config = JsonConvert.DeserializeObject<Configuration>(configContent);
config.isDefault = false;
config.version = UpdateChecker.Version;
if (UpdateChecker.Asset.CompareVersion(UpdateChecker.Version, config.version ?? "0") > 0)
{
config.updated = true;
@@ -201,6 +207,8 @@ namespace Shadowsocks.Model
logger.Error(e, "Cannot get the log level from NLog config file. Please check if the nlog config file exists with corresponding XML nodes.");
}
config.userAgentString = config.userAgent.Replace("$version", config.version);
return config;
}


Loading…
Cancel
Save