From fe1c043b8e5b6cc359f543d86b700eb7e963a63d Mon Sep 17 00:00:00 2001 From: mojpangr26 Date: Wed, 1 Feb 2023 17:07:36 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89UA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- v2rayN/v2rayN/Global.cs | 9 ++ v2rayN/v2rayN/Handler/CoreConfigHandler.cs | 24 +++- v2rayN/v2rayN/Mode/Config.cs | 7 + v2rayN/v2rayN/Mode/V2rayConfig.cs | 10 +- v2rayN/v2rayN/Resx/ResUI.Designer.cs | 18 +++ v2rayN/v2rayN/Resx/ResUI.fa-Ir.resx | 6 + v2rayN/v2rayN/Resx/ResUI.resx | 6 + v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx | 6 + v2rayN/v2rayN/Sample/SampleHttprequest | 2 +- .../ViewModels/OptionSettingViewModel.cs | 6 + v2rayN/v2rayN/Views/OptionSettingWindow.xaml | 124 +++++++++++------- .../v2rayN/Views/OptionSettingWindow.xaml.cs | 6 + 12 files changed, 169 insertions(+), 55 deletions(-) diff --git a/v2rayN/v2rayN/Global.cs b/v2rayN/v2rayN/Global.cs index 37604545..86579d78 100644 --- a/v2rayN/v2rayN/Global.cs +++ b/v2rayN/v2rayN/Global.cs @@ -96,6 +96,15 @@ public static readonly List domainStrategys = new List { "AsIs", "IPIfNonMatch", "IPOnDemand" }; public static readonly List domainMatchers = new List { "linear", "mph", "" }; public static readonly List fingerprints = new List { "chrome", "firefox", "safari", "randomized", "" }; + public static readonly List userAgent = new List { "chrome", "firefox", "safari", "edge", "none" }; + public static readonly Dictionary userAgentTxt = new Dictionary + { + {"chrome","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36" }, + {"firefox","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0" }, + {"safari","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15" }, + {"edge","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.70" }, + {"none",""} + }; public static readonly List allowInsecures = new List { "true", "false", "" }; public static readonly List domainStrategy4Freedoms = new List { "AsIs", "UseIP", "UseIPv4", "UseIPv6", "" }; public static readonly List Languages = new List { "zh-Hans", "en", "fa-Ir" }; diff --git a/v2rayN/v2rayN/Handler/CoreConfigHandler.cs b/v2rayN/v2rayN/Handler/CoreConfigHandler.cs index 454117a3..8ccfc38a 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigHandler.cs @@ -546,6 +546,15 @@ namespace v2rayN.Handler streamSettings.network = node.GetNetwork(); string host = node.requestHost.TrimEx(); string sni = node.sni; + string useragent = ""; + if (!config.customizeUserAgent.IsNullOrEmpty()) + { + useragent = config.customizeUserAgent; + } + else if (!config.defUserAgent.IsNullOrEmpty()) + { + useragent = Global.userAgentTxt[config.defUserAgent]; + } //if tls if (node.streamSecurity == Global.StreamSecurity) @@ -634,19 +643,22 @@ namespace v2rayN.Handler WsSettings wsSettings = new WsSettings { }; - + wsSettings.headers = new Headers + { + }; string path = node.path; if (!string.IsNullOrWhiteSpace(host)) { - wsSettings.headers = new Headers - { - Host = host - }; + wsSettings.headers.Host = host; } if (!string.IsNullOrWhiteSpace(path)) { wsSettings.path = path; } + if (!string.IsNullOrWhiteSpace(useragent)) + { + wsSettings.headers.UserAgent = useragent; + } streamSettings.wsSettings = wsSettings; //TlsSettings tlsSettings = new TlsSettings(); @@ -730,7 +742,7 @@ namespace v2rayN.Handler string host2 = string.Join("\",\"", arrHost); request = request.Replace("$requestHost$", $"\"{host2}\""); //request = request.Replace("$requestHost$", string.Format("\"{0}\"", config.requestHost())); - + request = request.Replace("$requestUserAgent$", $"\"{useragent}\""); //Path string pathHttp = @"/"; if (!Utils.IsNullOrEmpty(node.path)) diff --git a/v2rayN/v2rayN/Mode/Config.cs b/v2rayN/v2rayN/Mode/Config.cs index 11ed01fc..b6dc9fcc 100644 --- a/v2rayN/v2rayN/Mode/Config.cs +++ b/v2rayN/v2rayN/Mode/Config.cs @@ -94,6 +94,13 @@ public string defFingerprint { get; set; } + /// + /// 默认用户代理 + /// + public string defUserAgent { get; set; } + + public string customizeUserAgent { get; set; } + /// /// 域名解析策略 /// diff --git a/v2rayN/v2rayN/Mode/V2rayConfig.cs b/v2rayN/v2rayN/Mode/V2rayConfig.cs index e0247c7e..ec1fd03b 100644 --- a/v2rayN/v2rayN/Mode/V2rayConfig.cs +++ b/v2rayN/v2rayN/Mode/V2rayConfig.cs @@ -1,4 +1,6 @@ -namespace v2rayN.Mode +using Newtonsoft.Json; + +namespace v2rayN.Mode { /// /// v2ray配置文件实体类 @@ -505,6 +507,12 @@ /// /// public string Host { get; set; } + + /// + /// 用户代理 + /// + [JsonProperty("User-Agent")] + public string UserAgent { get; set; } } public class HttpSettings diff --git a/v2rayN/v2rayN/Resx/ResUI.Designer.cs b/v2rayN/v2rayN/Resx/ResUI.Designer.cs index eb4862c3..1ef9e227 100644 --- a/v2rayN/v2rayN/Resx/ResUI.Designer.cs +++ b/v2rayN/v2rayN/Resx/ResUI.Designer.cs @@ -2392,6 +2392,15 @@ namespace v2rayN.Resx { } } + /// + /// 查找类似 Customize User-Agent 的本地化字符串。 + /// + public static string TbSettingsCustomizeUserAgent { + get { + return ResourceManager.GetString("TbSettingsCustomizeUserAgent", resourceCulture); + } + } + /// /// 查找类似 AllowInsecure 的本地化字符串。 /// @@ -2410,6 +2419,15 @@ namespace v2rayN.Resx { } } + /// + /// 查找类似 User-Agent 的本地化字符串。 + /// + public static string TbSettingsDefUserAgent { + get { + return ResourceManager.GetString("TbSettingsDefUserAgent", resourceCulture); + } + } + /// /// 查找类似 Outbound Freedom domainStrategy 的本地化字符串。 /// diff --git a/v2rayN/v2rayN/Resx/ResUI.fa-Ir.resx b/v2rayN/v2rayN/Resx/ResUI.fa-Ir.resx index e6fc55b2..4b3744f6 100644 --- a/v2rayN/v2rayN/Resx/ResUI.fa-Ir.resx +++ b/v2rayN/v2rayN/Resx/ResUI.fa-Ir.resx @@ -1060,4 +1060,10 @@ نمایش کنسول + + Customize User-Agent + + + User-Agent + \ No newline at end of file diff --git a/v2rayN/v2rayN/Resx/ResUI.resx b/v2rayN/v2rayN/Resx/ResUI.resx index a96733f4..5dc3a280 100644 --- a/v2rayN/v2rayN/Resx/ResUI.resx +++ b/v2rayN/v2rayN/Resx/ResUI.resx @@ -1087,4 +1087,10 @@ Default TLS fingerprint + + Customize User-Agent + + + User-Agent + \ No newline at end of file diff --git a/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx b/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx index bda8b0ce..cbce7200 100644 --- a/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx +++ b/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx @@ -1087,4 +1087,10 @@ 默认TLS指纹(fingerprint) + + 自定义用户代理(UA) + + + 用户代理(UA) + \ No newline at end of file diff --git a/v2rayN/v2rayN/Sample/SampleHttprequest b/v2rayN/v2rayN/Sample/SampleHttprequest index 48a0be41..583b89eb 100644 --- a/v2rayN/v2rayN/Sample/SampleHttprequest +++ b/v2rayN/v2rayN/Sample/SampleHttprequest @@ -1 +1 @@ -{"version":"1.1","method":"GET","path":[$requestPath$],"headers":{"Host":[$requestHost$],"User-Agent":["Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36","Mozilla/5.0 (iPhone; CPU iPhone OS 10_0_2 like Mac OS X) AppleWebKit/601.1 (KHTML, like Gecko) CriOS/53.0.2785.109 Mobile/14A456 Safari/601.1.46"],"Accept-Encoding":["gzip, deflate"],"Connection":["keep-alive"],"Pragma":"no-cache"}} \ No newline at end of file +{"version":"1.1","method":"GET","path":[$requestPath$],"headers":{"Host":[$requestHost$],"User-Agent":[$requestUserAgent$],"Accept-Encoding":["gzip, deflate"],"Connection":["keep-alive"],"Pragma":"no-cache"}} \ No newline at end of file diff --git a/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs b/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs index eb157f6d..746f5189 100644 --- a/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs @@ -29,6 +29,8 @@ namespace v2rayN.ViewModels [Reactive] public string loglevel { get; set; } [Reactive] public bool defAllowInsecure { get; set; } [Reactive] public string defFingerprint { get; set; } + [Reactive] public string defUserAgent { get; set; } + [Reactive] public string customizeUserAgent { get; set; } #endregion #region Core DNS @@ -112,6 +114,8 @@ namespace v2rayN.ViewModels loglevel = _config.loglevel; defAllowInsecure = _config.defAllowInsecure; defFingerprint = _config.defFingerprint; + defUserAgent = _config.defUserAgent; + customizeUserAgent = _config.customizeUserAgent; #endregion #region Core DNS @@ -272,6 +276,8 @@ namespace v2rayN.ViewModels _config.muxEnabled = muxEnabled; _config.defAllowInsecure = defAllowInsecure; _config.defFingerprint = defFingerprint; + _config.defUserAgent = defUserAgent; + _config.customizeUserAgent = customizeUserAgent; //DNS diff --git a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml index 018214a9..6be18fe6 100644 --- a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml +++ b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml @@ -48,209 +48,239 @@ - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + diff --git a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs index 459207a4..eade2567 100644 --- a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs @@ -25,6 +25,10 @@ namespace v2rayN.Views { cmbdefFingerprint.Items.Add(it); }); + Global.userAgent.ForEach(it => + { + cmbdefUserAgent.Items.Add(it); + }); Global.domainStrategy4Freedoms.ForEach(it => { cmbdomainStrategy4Freedom.Items.Add(it); @@ -68,6 +72,8 @@ namespace v2rayN.Views this.Bind(ViewModel, vm => vm.loglevel, v => v.cmbloglevel.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.defAllowInsecure, v => v.togdefAllowInsecure.IsChecked).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.defFingerprint, v => v.cmbdefFingerprint.Text).DisposeWith(disposables); + this.Bind(ViewModel, vm => vm.defUserAgent, v => v.cmbdefUserAgent.Text).DisposeWith(disposables); + this.Bind(ViewModel, vm => vm.customizeUserAgent, v => v.txtcustomizeUserAgent.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.domainStrategy4Freedom, v => v.cmbdomainStrategy4Freedom.Text).DisposeWith(disposables); From 92e4de12fb83d59f4d8bd81a3f495b6f880f0837 Mon Sep 17 00:00:00 2001 From: mojpangr26 Date: Thu, 2 Feb 2023 09:41:23 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=AF=E7=BC=96?= =?UTF-8?q?=E8=BE=91cmb=20=E5=A2=9E=E5=8A=A0=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- v2rayN/v2rayN/Handler/CoreConfigHandler.cs | 15 +++++++++------ v2rayN/v2rayN/Mode/Config.cs | 2 -- v2rayN/v2rayN/Resx/ResUI.Designer.cs | 18 +++++++++--------- v2rayN/v2rayN/Resx/ResUI.fa-Ir.resx | 6 +++--- v2rayN/v2rayN/Resx/ResUI.resx | 6 +++--- v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx | 6 +++--- .../ViewModels/OptionSettingViewModel.cs | 3 --- v2rayN/v2rayN/Views/OptionSettingWindow.xaml | 17 +++++------------ .../v2rayN/Views/OptionSettingWindow.xaml.cs | 1 - 9 files changed, 32 insertions(+), 42 deletions(-) diff --git a/v2rayN/v2rayN/Handler/CoreConfigHandler.cs b/v2rayN/v2rayN/Handler/CoreConfigHandler.cs index 8ccfc38a..86617d8c 100644 --- a/v2rayN/v2rayN/Handler/CoreConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/CoreConfigHandler.cs @@ -547,13 +547,16 @@ namespace v2rayN.Handler string host = node.requestHost.TrimEx(); string sni = node.sni; string useragent = ""; - if (!config.customizeUserAgent.IsNullOrEmpty()) + if (!config.defUserAgent.IsNullOrEmpty()) { - useragent = config.customizeUserAgent; - } - else if (!config.defUserAgent.IsNullOrEmpty()) - { - useragent = Global.userAgentTxt[config.defUserAgent]; + try + { + useragent = Global.userAgentTxt[config.defUserAgent]; + } + catch (KeyNotFoundException) + { + useragent = config.defUserAgent; + } } //if tls diff --git a/v2rayN/v2rayN/Mode/Config.cs b/v2rayN/v2rayN/Mode/Config.cs index b6dc9fcc..0166d894 100644 --- a/v2rayN/v2rayN/Mode/Config.cs +++ b/v2rayN/v2rayN/Mode/Config.cs @@ -99,8 +99,6 @@ /// public string defUserAgent { get; set; } - public string customizeUserAgent { get; set; } - /// /// 域名解析策略 /// diff --git a/v2rayN/v2rayN/Resx/ResUI.Designer.cs b/v2rayN/v2rayN/Resx/ResUI.Designer.cs index 1ef9e227..8597560f 100644 --- a/v2rayN/v2rayN/Resx/ResUI.Designer.cs +++ b/v2rayN/v2rayN/Resx/ResUI.Designer.cs @@ -2392,15 +2392,6 @@ namespace v2rayN.Resx { } } - /// - /// 查找类似 Customize User-Agent 的本地化字符串。 - /// - public static string TbSettingsCustomizeUserAgent { - get { - return ResourceManager.GetString("TbSettingsCustomizeUserAgent", resourceCulture); - } - } - /// /// 查找类似 AllowInsecure 的本地化字符串。 /// @@ -2428,6 +2419,15 @@ namespace v2rayN.Resx { } } + /// + /// 查找类似 This parameter is valid only for tcp/http and ws 的本地化字符串。 + /// + public static string TbSettingsDefUserAgentTips { + get { + return ResourceManager.GetString("TbSettingsDefUserAgentTips", resourceCulture); + } + } + /// /// 查找类似 Outbound Freedom domainStrategy 的本地化字符串。 /// diff --git a/v2rayN/v2rayN/Resx/ResUI.fa-Ir.resx b/v2rayN/v2rayN/Resx/ResUI.fa-Ir.resx index 4b3744f6..9008d82d 100644 --- a/v2rayN/v2rayN/Resx/ResUI.fa-Ir.resx +++ b/v2rayN/v2rayN/Resx/ResUI.fa-Ir.resx @@ -1060,10 +1060,10 @@ نمایش کنسول - - Customize User-Agent - User-Agent + + This parameter is valid only for tcp/http and ws + \ No newline at end of file diff --git a/v2rayN/v2rayN/Resx/ResUI.resx b/v2rayN/v2rayN/Resx/ResUI.resx index 5dc3a280..cfa575dd 100644 --- a/v2rayN/v2rayN/Resx/ResUI.resx +++ b/v2rayN/v2rayN/Resx/ResUI.resx @@ -1087,10 +1087,10 @@ Default TLS fingerprint - - Customize User-Agent - User-Agent + + This parameter is valid only for tcp/http and ws + \ No newline at end of file diff --git a/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx b/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx index cbce7200..f27e18bd 100644 --- a/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx +++ b/v2rayN/v2rayN/Resx/ResUI.zh-Hans.resx @@ -1087,10 +1087,10 @@ 默认TLS指纹(fingerprint) - - 自定义用户代理(UA) - 用户代理(UA) + + 仅对tcp/http、ws协议生效 + \ No newline at end of file diff --git a/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs b/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs index 746f5189..d532dba0 100644 --- a/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/OptionSettingViewModel.cs @@ -30,7 +30,6 @@ namespace v2rayN.ViewModels [Reactive] public bool defAllowInsecure { get; set; } [Reactive] public string defFingerprint { get; set; } [Reactive] public string defUserAgent { get; set; } - [Reactive] public string customizeUserAgent { get; set; } #endregion #region Core DNS @@ -115,7 +114,6 @@ namespace v2rayN.ViewModels defAllowInsecure = _config.defAllowInsecure; defFingerprint = _config.defFingerprint; defUserAgent = _config.defUserAgent; - customizeUserAgent = _config.customizeUserAgent; #endregion #region Core DNS @@ -277,7 +275,6 @@ namespace v2rayN.ViewModels _config.defAllowInsecure = defAllowInsecure; _config.defFingerprint = defFingerprint; _config.defUserAgent = defUserAgent; - _config.customizeUserAgent = customizeUserAgent; //DNS diff --git a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml index 6be18fe6..72dd0f6e 100644 --- a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml +++ b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml @@ -70,6 +70,7 @@ + - + Margin="{StaticResource SettingItemMargin}" IsEditable="True" /> - + Text="{x:Static resx:ResUI.TbSettingsDefUserAgentTips}" /> diff --git a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs index eade2567..639adb31 100644 --- a/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/OptionSettingWindow.xaml.cs @@ -73,7 +73,6 @@ namespace v2rayN.Views this.Bind(ViewModel, vm => vm.defAllowInsecure, v => v.togdefAllowInsecure.IsChecked).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.defFingerprint, v => v.cmbdefFingerprint.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.defUserAgent, v => v.cmbdefUserAgent.Text).DisposeWith(disposables); - this.Bind(ViewModel, vm => vm.customizeUserAgent, v => v.txtcustomizeUserAgent.Text).DisposeWith(disposables); this.Bind(ViewModel, vm => vm.domainStrategy4Freedom, v => v.cmbdomainStrategy4Freedom.Text).DisposeWith(disposables);