1
0
mirror of https://github.com/Ylianst/MeshCentralRouter synced 2025-12-06 00:13:33 +00:00

Proxy related changes

Removed some duplicate proxy related code
Added manual http proxy settings with basic auth support
This commit is contained in:
jbfuzier
2021-10-19 18:43:00 +02:00
parent 20bb296dcb
commit cdb04cf032
11 changed files with 888 additions and 285 deletions

30
MainForm.Designer.cs generated
View File

@@ -35,6 +35,7 @@
this.mainTabControl = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.panel1 = new System.Windows.Forms.Panel();
this.proxySettings = new System.Windows.Forms.Button();
this.installButton = new System.Windows.Forms.Button();
this.stateLabel = new System.Windows.Forms.Label();
this.label28 = new System.Windows.Forms.Label();
@@ -48,6 +49,7 @@
this.nextButton1 = new System.Windows.Forms.Button();
this.pictureBox3 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.panel2 = new System.Windows.Forms.Panel();
this.tokenRememberCheckBox = new System.Windows.Forms.CheckBox();
@@ -135,13 +137,13 @@
this.settingsToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.openMapFileDialog = new System.Windows.Forms.OpenFileDialog();
this.saveMapFileDialog = new System.Windows.Forms.SaveFileDialog();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.panel5.SuspendLayout();
this.mainPanel.SuspendLayout();
this.mainTabControl.SuspendLayout();
this.tabPage1.SuspendLayout();
this.panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
this.tabPage2.SuspendLayout();
this.panel2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox6)).BeginInit();
@@ -164,7 +166,6 @@
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.trayIconContextMenuStrip.SuspendLayout();
this.mappingsContextMenuStrip.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
this.SuspendLayout();
//
// panel5
@@ -200,6 +201,7 @@
//
// panel1
//
this.panel1.Controls.Add(this.proxySettings);
this.panel1.Controls.Add(this.installButton);
this.panel1.Controls.Add(this.stateLabel);
this.panel1.Controls.Add(this.label28);
@@ -217,6 +219,13 @@
resources.ApplyResources(this.panel1, "panel1");
this.panel1.Name = "panel1";
//
// proxySettings
//
resources.ApplyResources(this.proxySettings, "proxySettings");
this.proxySettings.Name = "proxySettings";
this.proxySettings.UseVisualStyleBackColor = true;
this.proxySettings.Click += new System.EventHandler(this.button1_Click);
//
// installButton
//
resources.ApplyResources(this.installButton, "installButton");
@@ -304,6 +313,13 @@
this.label1.Name = "label1";
this.label1.UseMnemonic = false;
//
// pictureBox2
//
resources.ApplyResources(this.pictureBox2, "pictureBox2");
this.pictureBox2.Image = global::MeshCentralRouter.Properties.Resources.MeshCentral;
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.TabStop = false;
//
// tabPage2
//
this.tabPage2.Controls.Add(this.panel2);
@@ -958,13 +974,6 @@
this.saveMapFileDialog.DefaultExt = "mcrouter";
resources.ApplyResources(this.saveMapFileDialog, "saveMapFileDialog");
//
// pictureBox2
//
resources.ApplyResources(this.pictureBox2, "pictureBox2");
this.pictureBox2.Image = global::MeshCentralRouter.Properties.Resources.MeshCentral;
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.TabStop = false;
//
// MainForm
//
resources.ApplyResources(this, "$this");
@@ -984,6 +993,7 @@
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
this.tabPage2.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.panel2.PerformLayout();
@@ -1009,7 +1019,6 @@
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.trayIconContextMenuStrip.ResumeLayout(false);
this.mappingsContextMenuStrip.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
this.ResumeLayout(false);
}
@@ -1121,6 +1130,7 @@
private System.Windows.Forms.ToolStripMenuItem askConsentToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem privacyBarToolStripMenuItem;
private System.Windows.Forms.PictureBox pictureBox2;
private System.Windows.Forms.Button proxySettings;
}
}

View File

@@ -296,6 +296,7 @@ namespace MeshCentralRouter
if (File.Exists(Path.Combine(selfExe.Directory.FullName, @"customization\logo.png"))) { try { pictureBox2.Image = pictureBox6.Image = (Bitmap)Image.FromFile(Path.Combine(selfExe.Directory.FullName, @"customization\logo.png")); showLicense = false; } catch (Exception) { } }
if (File.Exists(Path.Combine(selfExe.Directory.FullName, @"customization\bottombanner.png"))) { try { pictureBox3.Image = pictureBox4.Image = pictureBox5.Image = pictureBox7.Image = (Bitmap)Image.FromFile(Path.Combine(selfExe.Directory.FullName, @"customization\bottombanner.png")); showLicense = false; } catch (Exception) { } }
licenseLinkLabel.Visible = showLicense;
proxySettings.Visible = true;
try
{
if (File.Exists(Path.Combine(selfExe.Directory.FullName, @"customization\customize.txt")))
@@ -1974,6 +1975,12 @@ namespace MeshCentralRouter
cancelAutoClose();
}
private void button1_Click(object sender, EventArgs e)
{
ProxySettings form = new ProxySettings();
if (form.ShowDialog(this) == DialogResult.OK) { }
}
/*
private delegate void displayMessageHandler(string msg, int buttons, string extra, int progress);
private void displayMessage(string msg, int buttons = 0, string extra = "", int progress = 0)

File diff suppressed because it is too large Load Diff

View File

@@ -18,6 +18,10 @@
</FileUpgradeFlags>
<OldToolsVersion>3.5</OldToolsVersion>
<UpgradeBackupLocation />
<IsWebBootstrapper>false</IsWebBootstrapper>
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
@@ -30,12 +34,8 @@
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@@ -106,6 +106,12 @@
<Compile Include="FileDialogMsgForm.Designer.cs">
<DependentUpon>FileDialogMsgForm.cs</DependentUpon>
</Compile>
<Compile Include="ProxySettings.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="ProxySettings.Designer.cs">
<DependentUpon>ProxySettings.cs</DependentUpon>
</Compile>
<Compile Include="LocalPipe.cs" />
<Compile Include="SshUsernameForm.cs">
<SubType>Form</SubType>
@@ -274,6 +280,9 @@
<EmbeddedResource Include="FileDialogMsgForm.resx">
<DependentUpon>FileDialogMsgForm.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="ProxySettings.resx">
<DependentUpon>ProxySettings.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="SshUsernameForm.resx">
<DependentUpon>SshUsernameForm.cs</DependentUpon>
</EmbeddedResource>

View File

@@ -99,31 +99,6 @@ namespace MeshCentralRouter
try { return Registry.GetValue(@"HKEY_CURRENT_USER\SOFTWARE\OpenSource\MeshRouter", name, "").ToString(); } catch (Exception) { return ""; }
}
public static string GetProxyForUrlUsingPac(string DestinationUrl, string PacUri)
{
IntPtr WinHttpSession = Win32Api.WinHttpOpen("User", Win32Api.WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, IntPtr.Zero, IntPtr.Zero, 0);
Win32Api.WINHTTP_AUTOPROXY_OPTIONS ProxyOptions = new Win32Api.WINHTTP_AUTOPROXY_OPTIONS();
Win32Api.WINHTTP_PROXY_INFO ProxyInfo = new Win32Api.WINHTTP_PROXY_INFO();
ProxyOptions.dwFlags = Win32Api.WINHTTP_AUTOPROXY_CONFIG_URL;
ProxyOptions.dwAutoDetectFlags = (Win32Api.WINHTTP_AUTO_DETECT_TYPE_DHCP | Win32Api.WINHTTP_AUTO_DETECT_TYPE_DNS_A);
ProxyOptions.lpszAutoConfigUrl = PacUri;
// Get Proxy
bool IsSuccess = Win32Api.WinHttpGetProxyForUrl(WinHttpSession, DestinationUrl, ref ProxyOptions, ref ProxyInfo);
Win32Api.WinHttpCloseHandle(WinHttpSession);
if (IsSuccess)
{
return ProxyInfo.lpszProxy;
}
else
{
Console.WriteLine("Error: {0}", Win32Api.GetLastError());
return null;
}
}
// Parse the URL query parameters and returns a collection
public static NameValueCollection GetQueryStringParameters()
@@ -722,6 +697,7 @@ namespace MeshCentralRouter
private bool accmask = false;
private int acclen = 0;
private bool proxyInUse = false;
private Uri proxyUri = null;
private string user = null;
private string pass = null;
private string token = null;
@@ -752,31 +728,17 @@ namespace MeshCentralRouter
this.user = user;
this.pass = pass;
this.token = token;
Uri proxyUri = null;
this.proxyUri = null;
// Check if we need to use a HTTP proxy (Auto-proxy way)
try {
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
Object x = registryKey.GetValue("AutoConfigURL", null);
if ((x != null) && (x.GetType() == typeof(string))) {
string proxyStr = GetProxyForUrlUsingPac("http" + ((url.Port == 80) ? "" : "s") + "://" + url.Host + ":" + url.Port, x.ToString());
if (proxyStr != null) { proxyUri = new Uri("http://" + proxyStr); }
}
} catch (Exception) { proxyUri = null; }
// Check if we need to use a HTTP proxy (Normal way)
if (proxyUri == null) {
var proxy = System.Net.HttpWebRequest.GetSystemWebProxy();
proxyUri = proxy.GetProxy(url);
if ((url.Host.ToLower() == proxyUri.Host.ToLower()) && (url.Port == proxyUri.Port)) { proxyUri = null; }
}
this.proxyUri = Win32Api.GetProxy(url);
if (proxyUri != null)
{
// Proxy in use
proxyInUse = true;
wsclient = new TcpClient();
Debug("Connecting with proxy in use: " + proxyUri.ToString());
// This may log proxy password to debug log
Debug("Connecting with proxy in use: " + proxyUri.ToString());
wsclient.BeginConnect(proxyUri.Host, proxyUri.Port, new AsyncCallback(OnConnectSink), this);
}
else
@@ -808,7 +770,16 @@ namespace MeshCentralRouter
{
// Send proxy connection request
wsrawstream = wsclient.GetStream();
byte[] proxyRequestBuf = UTF8Encoding.UTF8.GetBytes("CONNECT " + url.Host + ":" + url.Port + " HTTP/1.1\r\nHost: " + url.Host + ":" + url.Port + "\r\n\r\n");
string userCreds = proxyUri.UserInfo;
string basicAuth = "";
if (userCreds.Length > 0)
{
// Base64 encode for basic authentication
userCreds = System.Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(userCreds));
basicAuth = "\r\nProxy-Authorization: Basic " + userCreds;
}
byte[] proxyRequestBuf = UTF8Encoding.UTF8.GetBytes("CONNECT " + url.Host + ":" + url.Port + " HTTP/1.1\r\nHost: " + url.Host + ":" + url.Port + basicAuth + "\r\n\r\n");
TlsDump("OutRaw", proxyRequestBuf, 0, proxyRequestBuf.Length);
try { wsrawstream.Write(proxyRequestBuf, 0, proxyRequestBuf.Length); } catch (Exception ex) { Debug(ex.ToString()); }
wsrawstream.BeginRead(readBuffer, readBufferLen, readBuffer.Length - readBufferLen, new AsyncCallback(OnProxyResponseSink), this);

View File

@@ -50,31 +50,7 @@ namespace MeshCentralRouter
public delegate void onStateMsgChangedHandler(string statemsg);
public event onStateMsgChangedHandler onStateMsgChanged;
public static string GetProxyForUrlUsingPac(string DestinationUrl, string PacUri)
{
IntPtr WinHttpSession = Win32Api.WinHttpOpen("User", Win32Api.WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, IntPtr.Zero, IntPtr.Zero, 0);
Win32Api.WINHTTP_AUTOPROXY_OPTIONS ProxyOptions = new Win32Api.WINHTTP_AUTOPROXY_OPTIONS();
Win32Api.WINHTTP_PROXY_INFO ProxyInfo = new Win32Api.WINHTTP_PROXY_INFO();
ProxyOptions.dwFlags = Win32Api.WINHTTP_AUTOPROXY_CONFIG_URL;
ProxyOptions.dwAutoDetectFlags = (Win32Api.WINHTTP_AUTO_DETECT_TYPE_DHCP | Win32Api.WINHTTP_AUTO_DETECT_TYPE_DNS_A);
ProxyOptions.lpszAutoConfigUrl = PacUri;
// Get Proxy
bool IsSuccess = Win32Api.WinHttpGetProxyForUrl(WinHttpSession, DestinationUrl, ref ProxyOptions, ref ProxyInfo);
Win32Api.WinHttpCloseHandle(WinHttpSession);
if (IsSuccess)
{
return ProxyInfo.lpszProxy;
}
else
{
Console.WriteLine("Error: {0}", Win32Api.GetLastError());
return null;
}
}
// Starts the routing server, called when the start button is pressed
public void start(MeshCentralServer parent, int protocol, int localPort, string url, int remotePort, string remoteIP)

194
ProxySettings.Designer.cs generated Normal file
View File

@@ -0,0 +1,194 @@
namespace MeshCentralRouter
{
partial class ProxySettings
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SaveProxyConfig = new System.Windows.Forms.Button();
this.manualHttpProxyHost = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.manualHttpProxyPort = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.manualHttpProxyUsername = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.manualHttpProxyPassword = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.useManualProxySettings = new System.Windows.Forms.CheckBox();
this.cancel = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// SaveProxyConfig
//
this.SaveProxyConfig.Location = new System.Drawing.Point(124, 404);
this.SaveProxyConfig.Name = "SaveProxyConfig";
this.SaveProxyConfig.Size = new System.Drawing.Size(75, 23);
this.SaveProxyConfig.TabIndex = 1;
this.SaveProxyConfig.Text = "Ok";
this.SaveProxyConfig.UseVisualStyleBackColor = true;
this.SaveProxyConfig.Click += new System.EventHandler(this.SaveProxyConfig_Click);
//
// manualHttpProxyHost
//
this.manualHttpProxyHost.Location = new System.Drawing.Point(104, 141);
this.manualHttpProxyHost.Name = "manualHttpProxyHost";
this.manualHttpProxyHost.Size = new System.Drawing.Size(203, 22);
this.manualHttpProxyHost.TabIndex = 2;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(75, 114);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(103, 17);
this.label1.TabIndex = 3;
this.label1.Text = "Http proxy host";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// manualHttpProxyPort
//
this.manualHttpProxyPort.Location = new System.Drawing.Point(104, 208);
this.manualHttpProxyPort.Name = "manualHttpProxyPort";
this.manualHttpProxyPort.Size = new System.Drawing.Size(203, 22);
this.manualHttpProxyPort.TabIndex = 4;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(75, 181);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(101, 17);
this.label2.TabIndex = 5;
this.label2.Text = "Http proxy port";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(75, 257);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(139, 17);
this.label3.TabIndex = 7;
this.label3.Text = "Http proxy username";
//
// manualHttpProxyUsername
//
this.manualHttpProxyUsername.Location = new System.Drawing.Point(104, 286);
this.manualHttpProxyUsername.Name = "manualHttpProxyUsername";
this.manualHttpProxyUsername.Size = new System.Drawing.Size(203, 22);
this.manualHttpProxyUsername.TabIndex = 6;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(78, 332);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(136, 17);
this.label4.TabIndex = 9;
this.label4.Text = "Http proxy password";
//
// manualHttpProxyPassword
//
this.manualHttpProxyPassword.Location = new System.Drawing.Point(104, 362);
this.manualHttpProxyPassword.Name = "manualHttpProxyPassword";
this.manualHttpProxyPassword.PasswordChar = '*';
this.manualHttpProxyPassword.Size = new System.Drawing.Size(203, 22);
this.manualHttpProxyPassword.TabIndex = 8;
//
// label5
//
this.label5.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.label5.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.label5.Location = new System.Drawing.Point(13, 9);
this.label5.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(396, 69);
this.label5.TabIndex = 10;
this.label5.Text = "Manually configure an HTTP proxy server to use. Username and password are optiona" +
"l. Only basic auth is supported.";
this.label5.UseMnemonic = false;
//
// useManualProxySettings
//
this.useManualProxySettings.AutoSize = true;
this.useManualProxySettings.Location = new System.Drawing.Point(43, 81);
this.useManualProxySettings.Name = "useManualProxySettings";
this.useManualProxySettings.Size = new System.Drawing.Size(194, 21);
this.useManualProxySettings.TabIndex = 11;
this.useManualProxySettings.Text = "use manual proxy settings";
this.useManualProxySettings.UseVisualStyleBackColor = true;
this.useManualProxySettings.CheckedChanged += new System.EventHandler(this.useManualProxySettings_CheckedChanged);
//
// cancel
//
this.cancel.Location = new System.Drawing.Point(232, 404);
this.cancel.Name = "cancel";
this.cancel.Size = new System.Drawing.Size(75, 23);
this.cancel.TabIndex = 12;
this.cancel.Text = "Cancel";
this.cancel.UseVisualStyleBackColor = true;
this.cancel.Click += new System.EventHandler(this.cancel_Click);
//
// ProxySettings
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(482, 450);
this.Controls.Add(this.cancel);
this.Controls.Add(this.useManualProxySettings);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.manualHttpProxyPassword);
this.Controls.Add(this.label3);
this.Controls.Add(this.manualHttpProxyUsername);
this.Controls.Add(this.label2);
this.Controls.Add(this.manualHttpProxyPort);
this.Controls.Add(this.label1);
this.Controls.Add(this.manualHttpProxyHost);
this.Controls.Add(this.SaveProxyConfig);
this.Name = "ProxySettings";
this.Text = "Proxy Settings";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button SaveProxyConfig;
private System.Windows.Forms.TextBox manualHttpProxyHost;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox manualHttpProxyPort;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox manualHttpProxyUsername;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox manualHttpProxyPassword;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.CheckBox useManualProxySettings;
private System.Windows.Forms.Button cancel;
}
}

69
ProxySettings.cs Normal file
View File

@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MeshCentralRouter
{
public partial class ProxySettings : Form
{
public ProxySettings()
{
InitializeComponent();
useManualProxySettings.Checked = Settings.GetRegValue("Use_Manual_Http_proxy", false);
manualHttpProxyHost.Text = Settings.GetRegValue("Manual_Http_proxy_host", "");
manualHttpProxyPort.Text = Settings.GetRegValue("Manual_Http_proxy_port", "");
manualHttpProxyUsername.Text = Settings.GetRegValue("Manual_Http_proxy_username", "");
manualHttpProxyPassword.Text = Settings.GetRegValue("Manual_Http_proxy_password", "");
checkbox_refresh_form();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void SaveProxyConfig_Click(object sender, EventArgs e)
{
Settings.SetRegValue("Use_Manual_Http_proxy", useManualProxySettings.Checked);
Settings.SetRegValue("Manual_Http_proxy_host", manualHttpProxyHost.Text);
Settings.SetRegValue("Manual_Http_proxy_port", manualHttpProxyPort.Text);
Settings.SetRegValue("Manual_Http_proxy_username", manualHttpProxyUsername.Text);
Settings.SetRegValue("Manual_Http_proxy_password", manualHttpProxyPassword.Text);
DialogResult = DialogResult.OK;
}
private void checkbox_refresh_form()
{
if (useManualProxySettings.Checked)
{
manualHttpProxyHost.ReadOnly = false;
manualHttpProxyPort.ReadOnly = false;
manualHttpProxyUsername.ReadOnly = false;
manualHttpProxyPassword.ReadOnly = false;
}
else
{
manualHttpProxyHost.ReadOnly = true;
manualHttpProxyPort.ReadOnly = true;
manualHttpProxyUsername.ReadOnly = true;
manualHttpProxyPassword.ReadOnly = true;
}
}
private void useManualProxySettings_CheckedChanged(object sender, EventArgs e)
{
checkbox_refresh_form();
}
private void cancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
}
}
}

120
ProxySettings.resx Normal file
View File

@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@@ -43,6 +43,7 @@ namespace MeshCentralRouter
private bool accmask = false;
private int acclen = 0;
private bool proxyInUse = false;
private Uri proxyUri = null;
private string tlsCertFingerprint = null;
//private ConnectionErrors lastError = ConnectionErrors.NoError;
public bool debug = false;
@@ -134,30 +135,11 @@ namespace MeshCentralRouter
SetState(ConnectionStates.Connecting);
this.url = url;
if (tlsCertFingerprint != null) { this.tlsCertFingerprint = tlsCertFingerprint.ToUpper(); }
Uri proxyUri = null;
Log("Websocket Start, URL=" + ((url == null) ? "(NULL)" : url.ToString()));
// Check if we need to use a HTTP proxy (Auto-proxy way)
try
{
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
Object x = registryKey.GetValue("AutoConfigURL", null);
if ((x != null) && (x.GetType() == typeof(string)))
{
string proxyStr = GetProxyForUrlUsingPac("http" + ((url.Port == 80) ? "" : "s") + "://" + url.Host + ":" + url.Port, x.ToString());
if (proxyStr != null) { proxyUri = new Uri("http://" + proxyStr); }
}
}
catch (Exception) { proxyUri = null; }
// Check if we need to use a HTTP proxy (Normal way)
if (proxyUri == null)
{
var proxy = System.Net.HttpWebRequest.GetSystemWebProxy();
proxyUri = proxy.GetProxy(url);
if ((url.Host.ToLower() == proxyUri.Host.ToLower()) && (url.Port == proxyUri.Port)) { proxyUri = null; }
}
proxyUri = Win32Api.GetProxy(url);
if (proxyUri != null)
{
@@ -201,7 +183,17 @@ namespace MeshCentralRouter
{
// Send proxy connection request
wsrawstream = wsclient.GetStream();
byte[] proxyRequestBuf = UTF8Encoding.UTF8.GetBytes("CONNECT " + url.Host + ":" + url.Port + " HTTP/1.1\r\nHost: " + url.Host + ":" + url.Port + "\r\n\r\n");
string userCreds = proxyUri.UserInfo;
string basicAuth = "";
if (userCreds.Length > 0)
{
// Base64 encode for basic authentication
userCreds = System.Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(userCreds));
basicAuth = "\r\nProxy-Authorization: Basic " + userCreds;
}
byte[] proxyRequestBuf = UTF8Encoding.UTF8.GetBytes("CONNECT " + url.Host + ":" + url.Port + " HTTP/1.1\r\nHost: " + url.Host + ":" + url.Port + basicAuth + "\r\n\r\n");
wsrawstream.Write(proxyRequestBuf, 0, proxyRequestBuf.Length);
wsrawstream.BeginRead(readBuffer, readBufferLen, readBuffer.Length - readBufferLen, new AsyncCallback(OnProxyResponseSink), this);
}

View File

@@ -78,6 +78,24 @@ namespace MeshCentralRouter
public static Uri GetProxy(Uri url)
{
// Manual http proxy with optional basic auth. Other auth type would be difficult to support as proxy authentication is done manually within the websocketclient
if(Settings.GetRegValue("Use_Manual_Http_proxy", false))
{
string proxyStr = "";
string proxyUserName = Settings.GetRegValue("Manual_Http_proxy_username", "");
string proxyPassword = Settings.GetRegValue("Manual_Http_proxy_password", "");
if (proxyUserName.Length > 0 && proxyPassword.Length > 0)
{
proxyStr = "http://" + proxyUserName + ":" + proxyPassword + "@" + Settings.GetRegValue("Manual_Http_proxy_host", "") + ":" + Settings.GetRegValue("Manual_Http_proxy_port", "");
}
else
{
proxyStr = "http://" + Settings.GetRegValue("Manual_Http_proxy_host", "") + ":" + Settings.GetRegValue("Manual_Http_proxy_port", "");
}
return new Uri(proxyStr);
}
// Check if we need to use a HTTP proxy (Auto-proxy way)
try
{
@@ -86,7 +104,10 @@ namespace MeshCentralRouter
if ((x != null) && (x.GetType() == typeof(string)))
{
string proxyStr = GetProxyForUrlUsingPac("http" + ((url.Port == 80) ? "" : "s") + "://" + url.Host + ":" + url.Port, x.ToString());
return new Uri("http://" + proxyStr);
if (proxyStr != null)
{
return new Uri("http://" + proxyStr);
}
}
}
catch (Exception) { }