Skip to content

Commit d58e6bc

Browse files
committed
⚙ Allow configuration of User-Agent
- For OnlineConfigResolver and GeositeUpdater
1 parent 108025d commit d58e6bc

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

shadowsocks-csharp/Controller/Service/GeositeUpdater.cs

+2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ public static async Task UpdatePACFromGeosite()
102102
// because we can't change proxy on existing socketsHttpHandler instance
103103
httpClientHandler = new HttpClientHandler();
104104
httpClient = new HttpClient(httpClientHandler);
105+
if (!string.IsNullOrWhiteSpace(config.userAgentString))
106+
httpClient.DefaultRequestHeaders.Add("User-Agent", config.userAgentString);
105107
if (config.enabled)
106108
{
107109
httpClientHandler.Proxy = new WebProxy(

shadowsocks-csharp/Controller/Service/OnlineConfigResolver.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Shadowsocks.Controller.Service
1111
{
1212
public class OnlineConfigResolver
1313
{
14-
public static async Task<List<Server>> GetOnline(string url, IWebProxy proxy = null)
14+
public static async Task<List<Server>> GetOnline(string url, string userAgentString, IWebProxy proxy = null)
1515
{
1616
var httpClientHandler = new HttpClientHandler()
1717
{
@@ -21,9 +21,8 @@ public static async Task<List<Server>> GetOnline(string url, IWebProxy proxy = n
2121
{
2222
Timeout = TimeSpan.FromSeconds(15)
2323
};
24-
25-
string userAgent = "ShadowsocksWindows/" + UpdateChecker.Version;
26-
httpClient.DefaultRequestHeaders.Add("User-Agent", userAgent);
24+
if (!string.IsNullOrWhiteSpace(userAgentString))
25+
httpClient.DefaultRequestHeaders.Add("User-Agent", userAgentString);
2726

2827
string server_json = await httpClient.GetStringAsync(url);
2928

shadowsocks-csharp/Controller/ShadowsocksController.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ private void TrafficStatistics(int queueMaxSize)
691691

692692
public async Task<int> UpdateOnlineConfigInternal(string url)
693693
{
694-
var onlineServer = await OnlineConfigResolver.GetOnline(url, _config.WebProxy);
694+
var onlineServer = await OnlineConfigResolver.GetOnline(url, _config.userAgentString, _config.WebProxy);
695695
_config.configs = Configuration.SortByOnlineConfig(
696696
_config.configs
697697
.Where(c => c.group != url)

shadowsocks-csharp/Model/Configuration.cs

+8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class Configuration
4646
public string geositeUrl; // for custom geosite source (and rule group)
4747
public string geositeGroup;
4848
public bool geositeBlacklistMode;
49+
public string userAgent;
4950

5051
//public NLogConfig.LogLevel logLevel;
5152
public LogViewerConfig logViewer;
@@ -81,6 +82,7 @@ public Configuration()
8182
geositeUrl = "";
8283
geositeGroup = "geolocation-!cn";
8384
geositeBlacklistMode = true;
85+
userAgent = "ShadowsocksWindows/$version";
8486

8587
logViewer = new LogViewerConfig();
8688
proxy = new ProxyConfig();
@@ -92,6 +94,9 @@ public Configuration()
9294
onlineConfigSource = new List<string>();
9395
}
9496

97+
[JsonIgnore]
98+
public string userAgentString; // $version substituted with numeral version in it
99+
95100
[JsonIgnore]
96101
NLogConfig nLogConfig;
97102

@@ -153,6 +158,7 @@ public static Configuration Load()
153158
string configContent = File.ReadAllText(CONFIG_FILE);
154159
config = JsonConvert.DeserializeObject<Configuration>(configContent);
155160
config.isDefault = false;
161+
config.version = UpdateChecker.Version;
156162
if (UpdateChecker.Asset.CompareVersion(UpdateChecker.Version, config.version ?? "0") > 0)
157163
{
158164
config.updated = true;
@@ -201,6 +207,8 @@ public static Configuration Load()
201207
logger.Error(e, "Cannot get the log level from NLog config file. Please check if the nlog config file exists with corresponding XML nodes.");
202208
}
203209

210+
config.userAgentString = config.userAgent.Replace("$version", config.version);
211+
204212
return config;
205213
}
206214

0 commit comments

Comments
 (0)