Skip to content

OnlineConfigResolver: add User-Agent #2978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions shadowsocks-csharp/Controller/Service/GeositeUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public static async Task UpdatePACFromGeosite()
// 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -21,6 +21,8 @@ public static async Task<List<Server>> GetOnline(string url, IWebProxy proxy = n
{
Timeout = TimeSpan.FromSeconds(15)
};
if (!string.IsNullOrWhiteSpace(userAgentString))
httpClient.DefaultRequestHeaders.Add("User-Agent", userAgentString);

string server_json = await httpClient.GetStringAsync(url);

Expand Down
2 changes: 1 addition & 1 deletion shadowsocks-csharp/Controller/ShadowsocksController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ private void TrafficStatistics(int queueMaxSize)

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)
Expand Down
8 changes: 8 additions & 0 deletions shadowsocks-csharp/Model/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class Configuration
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;
Expand Down Expand Up @@ -81,6 +82,7 @@ public Configuration()
geositeUrl = "";
geositeGroup = "geolocation-!cn";
geositeBlacklistMode = true;
userAgent = "ShadowsocksWindows/$version";

logViewer = new LogViewerConfig();
proxy = new ProxyConfig();
Expand All @@ -92,6 +94,9 @@ public Configuration()
onlineConfigSource = new List<string>();
}

[JsonIgnore]
public string userAgentString; // $version substituted with numeral version in it

[JsonIgnore]
NLogConfig nLogConfig;

Expand Down Expand Up @@ -153,6 +158,7 @@ public static Configuration Load()
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;
Expand Down Expand Up @@ -201,6 +207,8 @@ public static Configuration Load()
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;
}

Expand Down