diff --git a/shadowsocks-csharp/Controller/Local.cs b/shadowsocks-csharp/Controller/Local.cs index 6700d234..4e1b7103 100644 --- a/shadowsocks-csharp/Controller/Local.cs +++ b/shadowsocks-csharp/Controller/Local.cs @@ -111,6 +111,7 @@ class HandlerConfig : ICloneable public Double TTL = 0; // Second public Double connect_timeout = 0; public int try_keep_alive = 0; + public string local_dns_servers; public string dns_servers; public bool fouce_local_dns_query = false; // Server proxy @@ -136,6 +137,7 @@ public object Clone() obj.TTL = TTL; obj.connect_timeout = connect_timeout; obj.try_keep_alive = try_keep_alive; + obj.local_dns_servers = local_dns_servers; obj.dns_servers = dns_servers; obj.fouce_local_dns_query = fouce_local_dns_query; obj.proxyType = proxyType; @@ -873,7 +875,7 @@ private void Connect() Logging.Info($"Connect {cfg.targetHost}:{cfg.targetPort.ToString()} via {server.server}:{server.server_port}"); ResetTimeout(cfg.TTL); - if (cfg.fouce_local_dns_query && cfg.targetHost != null) + if (cfg.targetHost != null) { IPAddress ipAddress; @@ -948,7 +950,7 @@ private void Connect() { if (serverHost.IndexOf('.') >= 0) { - ipAddress = Util.Utils.QueryDns(serverHost, cfg.dns_servers); + ipAddress = Util.Utils.QueryDns(serverHost, cfg.local_dns_servers); } else { diff --git a/shadowsocks-csharp/Controller/ProxyAuth.cs b/shadowsocks-csharp/Controller/ProxyAuth.cs index d41e8ca3..6c5341b1 100644 --- a/shadowsocks-csharp/Controller/ProxyAuth.cs +++ b/shadowsocks-csharp/Controller/ProxyAuth.cs @@ -548,6 +548,10 @@ private void Connect() handler.cfg.TTL = _config.TTL; handler.cfg.connect_timeout = _config.connectTimeout; handler.cfg.autoSwitchOff = _config.autoBan; + if (!string.IsNullOrEmpty(_config.localDnsServer)) + { + handler.cfg.local_dns_servers = _config.localDnsServer; + } if (!string.IsNullOrEmpty(_config.dnsServer)) { handler.cfg.dns_servers = _config.dnsServer; diff --git a/shadowsocks-csharp/Controller/Socks5Forwarder.cs b/shadowsocks-csharp/Controller/Socks5Forwarder.cs index 9cc0e9f6..a4b472ec 100644 --- a/shadowsocks-csharp/Controller/Socks5Forwarder.cs +++ b/shadowsocks-csharp/Controller/Socks5Forwarder.cs @@ -302,14 +302,14 @@ private void Connect() } if (ipAddress == null) { - ipAddress = Utils.DnsBuffer.Get(_remote_host); + ipAddress = Utils.LocalDnsBuffer.Get(_remote_host); } } if (ipAddress == null) { if (_remote_host.IndexOf('.') >= 0) { - ipAddress = Util.Utils.QueryDns(_remote_host, _config.dnsServer); + ipAddress = Util.Utils.QueryDns(_remote_host, _config.localDnsServer); } else { @@ -318,8 +318,8 @@ private void Connect() } if (ipAddress != null) { - Utils.DnsBuffer.Set(_remote_host, new IPAddress(ipAddress.GetAddressBytes())); - Utils.DnsBuffer.Sweep(); + Utils.LocalDnsBuffer.Set(_remote_host, new IPAddress(ipAddress.GetAddressBytes())); + Utils.LocalDnsBuffer.Sweep(); } else { diff --git a/shadowsocks-csharp/Model/Configuration.cs b/shadowsocks-csharp/Model/Configuration.cs index f209fcc4..46b167b8 100755 --- a/shadowsocks-csharp/Model/Configuration.cs +++ b/shadowsocks-csharp/Model/Configuration.cs @@ -111,6 +111,7 @@ public class Configuration public int localPort; public string localAuthPassword; + public string localDnsServer; public string dnsServer; public int reconnectTimes; public string balanceAlgorithm; @@ -389,6 +390,7 @@ public Configuration() keepVisitTime = 180; connectTimeout = 5; dnsServer = ""; + localDnsServer = ""; balanceAlgorithm = "LowException"; random = true; @@ -421,6 +423,7 @@ public void CopyFrom(Configuration config) TTL = config.TTL; connectTimeout = config.connectTimeout; dnsServer = config.dnsServer; + localDnsServer = config.localDnsServer; proxyEnable = config.proxyEnable; pacDirectGoProxy = config.pacDirectGoProxy; proxyType = config.proxyType; diff --git a/shadowsocks-csharp/Util/Util.cs b/shadowsocks-csharp/Util/Util.cs index e27ba2bf..22c32406 100755 --- a/shadowsocks-csharp/Util/Util.cs +++ b/shadowsocks-csharp/Util/Util.cs @@ -32,6 +32,14 @@ public static LRUCache DnsBuffer } } + public static LRUCache LocalDnsBuffer + { + get + { + return dnsBuffer; + } + } + static Process current_process = Process.GetCurrentProcess(); public static void ReleaseMemory() diff --git a/shadowsocks-csharp/View/SettingsForm.Designer.cs b/shadowsocks-csharp/View/SettingsForm.Designer.cs index 9acd5a05..98b1bf50 100644 --- a/shadowsocks-csharp/View/SettingsForm.Designer.cs +++ b/shadowsocks-csharp/View/SettingsForm.Designer.cs @@ -74,6 +74,8 @@ private void InitializeComponent() this.DNSText = new System.Windows.Forms.TextBox(); this.buttonDefault = new System.Windows.Forms.Button(); this.label2 = new System.Windows.Forms.Label(); + this.label3 = new System.Windows.Forms.Label(); + this.LocalDNSText = new System.Windows.Forms.TextBox(); this.tableLayoutPanel1.SuspendLayout(); this.tableLayoutPanel2.SuspendLayout(); this.Socks5ProxyGroup.SuspendLayout(); @@ -107,7 +109,7 @@ private void InitializeComponent() this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); - this.tableLayoutPanel1.Size = new System.Drawing.Size(581, 452); + this.tableLayoutPanel1.Size = new System.Drawing.Size(581, 479); this.tableLayoutPanel1.TabIndex = 0; // // tableLayoutPanel2 @@ -495,7 +497,7 @@ private void InitializeComponent() this.tableLayoutPanel10.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel10.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel10.RowStyles.Add(new System.Windows.Forms.RowStyle()); - this.tableLayoutPanel10.Size = new System.Drawing.Size(186, 191); + this.tableLayoutPanel10.Size = new System.Drawing.Size(186, 218); this.tableLayoutPanel10.TabIndex = 3; // // tableLayoutPanel3 @@ -509,7 +511,7 @@ private void InitializeComponent() this.tableLayoutPanel3.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 23F)); this.tableLayoutPanel3.Controls.Add(this.MyCancelButton, 1, 0); this.tableLayoutPanel3.Controls.Add(this.OKButton, 0, 0); - this.tableLayoutPanel3.Location = new System.Drawing.Point(3, 146); + this.tableLayoutPanel3.Location = new System.Drawing.Point(3, 173); this.tableLayoutPanel3.Margin = new System.Windows.Forms.Padding(3, 3, 0, 3); this.tableLayoutPanel3.Name = "tableLayoutPanel3"; this.tableLayoutPanel3.RowCount = 1; @@ -551,34 +553,37 @@ private void InitializeComponent() this.tableLayoutPanel5.ColumnCount = 2; this.tableLayoutPanel5.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel5.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); - this.tableLayoutPanel5.Controls.Add(this.ReconnectLabel, 0, 3); - this.tableLayoutPanel5.Controls.Add(this.NumReconnect, 1, 3); - this.tableLayoutPanel5.Controls.Add(this.TTLLabel, 0, 5); - this.tableLayoutPanel5.Controls.Add(this.NumTTL, 1, 5); - this.tableLayoutPanel5.Controls.Add(this.labelTimeout, 0, 4); - this.tableLayoutPanel5.Controls.Add(this.NumTimeout, 1, 4); + this.tableLayoutPanel5.Controls.Add(this.ReconnectLabel, 0, 4); + this.tableLayoutPanel5.Controls.Add(this.NumReconnect, 1, 4); + this.tableLayoutPanel5.Controls.Add(this.TTLLabel, 0, 6); + this.tableLayoutPanel5.Controls.Add(this.NumTTL, 1, 6); + this.tableLayoutPanel5.Controls.Add(this.labelTimeout, 0, 5); + this.tableLayoutPanel5.Controls.Add(this.NumTimeout, 1, 5); this.tableLayoutPanel5.Controls.Add(this.DNSText, 1, 1); this.tableLayoutPanel5.Controls.Add(this.buttonDefault, 1, 0); this.tableLayoutPanel5.Controls.Add(this.label2, 0, 1); + this.tableLayoutPanel5.Controls.Add(this.label3, 0, 2); + this.tableLayoutPanel5.Controls.Add(this.LocalDNSText, 1, 2); this.tableLayoutPanel5.Location = new System.Drawing.Point(0, 0); this.tableLayoutPanel5.Margin = new System.Windows.Forms.Padding(0); this.tableLayoutPanel5.Name = "tableLayoutPanel5"; this.tableLayoutPanel5.Padding = new System.Windows.Forms.Padding(3); - this.tableLayoutPanel5.RowCount = 6; + this.tableLayoutPanel5.RowCount = 7; this.tableLayoutPanel5.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel5.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel5.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel5.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel5.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel5.RowStyles.Add(new System.Windows.Forms.RowStyle()); - this.tableLayoutPanel5.Size = new System.Drawing.Size(186, 143); + this.tableLayoutPanel5.RowStyles.Add(new System.Windows.Forms.RowStyle()); + this.tableLayoutPanel5.Size = new System.Drawing.Size(186, 170); this.tableLayoutPanel5.TabIndex = 3; // // ReconnectLabel // this.ReconnectLabel.Anchor = System.Windows.Forms.AnchorStyles.Right; this.ReconnectLabel.AutoSize = true; - this.ReconnectLabel.Location = new System.Drawing.Point(6, 66); + this.ReconnectLabel.Location = new System.Drawing.Point(6, 93); this.ReconnectLabel.Name = "ReconnectLabel"; this.ReconnectLabel.Size = new System.Drawing.Size(59, 12); this.ReconnectLabel.TabIndex = 3; @@ -587,7 +592,7 @@ private void InitializeComponent() // NumReconnect // this.NumReconnect.ImeMode = System.Windows.Forms.ImeMode.Off; - this.NumReconnect.Location = new System.Drawing.Point(71, 62); + this.NumReconnect.Location = new System.Drawing.Point(71, 89); this.NumReconnect.Maximum = new decimal(new int[] { 20, 0, @@ -601,7 +606,7 @@ private void InitializeComponent() // this.TTLLabel.Anchor = System.Windows.Forms.AnchorStyles.Right; this.TTLLabel.AutoSize = true; - this.TTLLabel.Location = new System.Drawing.Point(42, 120); + this.TTLLabel.Location = new System.Drawing.Point(42, 147); this.TTLLabel.Name = "TTLLabel"; this.TTLLabel.Size = new System.Drawing.Size(23, 12); this.TTLLabel.TabIndex = 3; @@ -610,7 +615,7 @@ private void InitializeComponent() // NumTTL // this.NumTTL.ImeMode = System.Windows.Forms.ImeMode.Off; - this.NumTTL.Location = new System.Drawing.Point(71, 116); + this.NumTTL.Location = new System.Drawing.Point(71, 143); this.NumTTL.Maximum = new decimal(new int[] { 600, 0, @@ -624,7 +629,7 @@ private void InitializeComponent() // this.labelTimeout.Anchor = System.Windows.Forms.AnchorStyles.Right; this.labelTimeout.AutoSize = true; - this.labelTimeout.Location = new System.Drawing.Point(12, 93); + this.labelTimeout.Location = new System.Drawing.Point(12, 120); this.labelTimeout.Name = "labelTimeout"; this.labelTimeout.Size = new System.Drawing.Size(53, 12); this.labelTimeout.TabIndex = 3; @@ -633,7 +638,7 @@ private void InitializeComponent() // NumTimeout // this.NumTimeout.ImeMode = System.Windows.Forms.ImeMode.Off; - this.NumTimeout.Location = new System.Drawing.Point(71, 89); + this.NumTimeout.Location = new System.Drawing.Point(71, 116); this.NumTimeout.Maximum = new decimal(new int[] { 60, 0, @@ -673,6 +678,26 @@ private void InitializeComponent() this.label2.TabIndex = 3; this.label2.Text = "DNS"; // + // label3 + // + this.label3.Anchor = System.Windows.Forms.AnchorStyles.Right; + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(6, 66); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(59, 12); + this.label3.TabIndex = 3; + this.label3.Text = "Local DNS"; + // + // LocalDNSText + // + this.LocalDNSText.ImeMode = System.Windows.Forms.ImeMode.Off; + this.LocalDNSText.Location = new System.Drawing.Point(71, 62); + this.LocalDNSText.MaxLength = 0; + this.LocalDNSText.Name = "LocalDNSText"; + this.LocalDNSText.Size = new System.Drawing.Size(109, 21); + this.LocalDNSText.TabIndex = 17; + this.LocalDNSText.WordWrap = false; + // // SettingsForm // this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; @@ -763,5 +788,7 @@ private void InitializeComponent() private System.Windows.Forms.NumericUpDown NumTimeout; private System.Windows.Forms.Button buttonDefault; private System.Windows.Forms.CheckBox checkBalanceInGroup; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.TextBox LocalDNSText; } } \ No newline at end of file diff --git a/shadowsocks-csharp/View/SettingsForm.cs b/shadowsocks-csharp/View/SettingsForm.cs index 02ac4a59..a17da84b 100644 --- a/shadowsocks-csharp/View/SettingsForm.cs +++ b/shadowsocks-csharp/View/SettingsForm.cs @@ -52,6 +52,7 @@ public SettingsForm(ShadowsocksController controller) buttonDefault.Height = buttonDefault.Height * dpi_mul / 4; buttonDefault.Width = buttonDefault.Width * dpi_mul / 4; DNSText.Width = DNSText.Width * dpi_mul / 4; + LocalDNSText.Width = LocalDNSText.Width * dpi_mul / 4; NumReconnect.Width = NumReconnect.Width * dpi_mul / 4; NumTimeout.Width = NumTimeout.Width * dpi_mul / 4; NumTTL.Width = NumTTL.Width * dpi_mul / 4; @@ -141,6 +142,7 @@ private int SaveOldSelectedServer() _modifiedConfiguration.TTL = Convert.ToInt32(NumTTL.Value); _modifiedConfiguration.connectTimeout = Convert.ToInt32(NumTimeout.Value); _modifiedConfiguration.dnsServer = DNSText.Text; + _modifiedConfiguration.localDnsServer = LocalDNSText.Text; _modifiedConfiguration.proxyEnable = CheckSockProxy.Checked; _modifiedConfiguration.pacDirectGoProxy = checkBoxPacProxy.Checked; _modifiedConfiguration.proxyType = comboProxyType.SelectedIndex; @@ -185,6 +187,7 @@ private void LoadSelectedServer() NumTTL.Value = _modifiedConfiguration.TTL; NumTimeout.Value = _modifiedConfiguration.connectTimeout; DNSText.Text = _modifiedConfiguration.dnsServer; + LocalDNSText.Text = _modifiedConfiguration.localDnsServer; CheckSockProxy.Checked = _modifiedConfiguration.proxyEnable; checkBoxPacProxy.Checked = _modifiedConfiguration.pacDirectGoProxy;