mirror of
https://github.com/Ylianst/MeshCentralRouter
synced 2025-12-06 00:13:33 +00:00
Manual proxy improvements.
This commit is contained in:
@@ -285,8 +285,8 @@ namespace MeshCentralRouter
|
||||
ushort tile_x = (ushort)((buffer[off + 4] << 8) + buffer[off + 5]);
|
||||
ushort tile_y = (ushort)((buffer[off + 6] << 8) + buffer[off + 7]);
|
||||
try { newtile = Image.FromStream(new System.IO.MemoryStream(buffer, off + 8, blen - 8)); } catch (Exception) { return blen; }
|
||||
Rectangle r = new Rectangle((int)tile_x, (int)tile_y, newtile.Width, newtile.Height);
|
||||
Rectangle r3 = new Rectangle((int)Math.Round((double)tile_x / (double)scalefactor) - 2, (int)Math.Round((double)tile_y / (double)scalefactor) - 2, (int)Math.Round((double)newtile.Width / (double)scalefactor) + 4, (int)Math.Round((double)newtile.Height / (double)scalefactor) + 4);
|
||||
//Rectangle r = new Rectangle((int)tile_x, (int)tile_y, newtile.Width, newtile.Height);
|
||||
//Rectangle r3 = new Rectangle((int)Math.Round((double)tile_x / (double)scalefactor) - 2, (int)Math.Round((double)tile_y / (double)scalefactor) - 2, (int)Math.Round((double)newtile.Width / (double)scalefactor) + 4, (int)Math.Round((double)newtile.Height / (double)scalefactor) + 4);
|
||||
tilecount++;
|
||||
|
||||
// Winform mode
|
||||
@@ -949,6 +949,9 @@ namespace MeshCentralRouter
|
||||
private void KVMControl_Load(object sender, EventArgs e)
|
||||
{
|
||||
controlLoaded = true;
|
||||
ProcessModule objCurrentModule = Process.GetCurrentProcess().MainModule;
|
||||
objKeyboardProcess = new LowLevelKeyboardProc(captureKey);
|
||||
ptrHook = SetWindowsHookEx(13, objKeyboardProcess, GetModuleHandle(objCurrentModule.ModuleName), 0);
|
||||
}
|
||||
|
||||
//Declaring Global objects
|
||||
@@ -984,14 +987,5 @@ namespace MeshCentralRouter
|
||||
return (flags & 0x20) == 0x20;
|
||||
}
|
||||
|
||||
private void KVMControl_Load(object sender, EventArgs e)
|
||||
{
|
||||
ProcessModule objCurrentModule = Process.GetCurrentProcess().MainModule;
|
||||
objKeyboardProcess = new LowLevelKeyboardProc(captureKey);
|
||||
ptrHook = SetWindowsHookEx(13, objKeyboardProcess, GetModuleHandle(objCurrentModule.ModuleName), 0);
|
||||
}
|
||||
|
||||
/* Code to Disable WinKey, Alt+Tab, Ctrl+Esc Ends Here */
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
88
MainForm.Designer.cs
generated
88
MainForm.Designer.cs
generated
@@ -84,14 +84,12 @@
|
||||
this.sortByGroupToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.toolStripMenuItem4 = new System.Windows.Forms.ToolStripSeparator();
|
||||
this.settingsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.customAppsToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.searchTextBox = new System.Windows.Forms.TextBox();
|
||||
this.devicesTabControl = new System.Windows.Forms.TabControl();
|
||||
this.devicesTabPage = new System.Windows.Forms.TabPage();
|
||||
this.devicesPanel = new System.Windows.Forms.Panel();
|
||||
this.cancelAutoCloseButton1 = new System.Windows.Forms.Button();
|
||||
this.devicesListView = new ListViewExtended();
|
||||
this.nameColumnHeader = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
|
||||
this.stateColumnHeader = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
|
||||
this.devicesContextMenuStrip = new System.Windows.Forms.ContextMenuStrip(this.components);
|
||||
this.addMapToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.addRelayMapToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||
@@ -135,10 +133,12 @@
|
||||
this.saveMappingsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.toolStripMenuItem5 = new System.Windows.Forms.ToolStripSeparator();
|
||||
this.settingsToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.customAppsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.openMapFileDialog = new System.Windows.Forms.OpenFileDialog();
|
||||
this.saveMapFileDialog = new System.Windows.Forms.SaveFileDialog();
|
||||
this.customAppsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.customAppsToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
|
||||
this.devicesListView = new MeshCentralRouter.ListViewExtended();
|
||||
this.nameColumnHeader = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
|
||||
this.stateColumnHeader = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
|
||||
this.panel5.SuspendLayout();
|
||||
this.mainPanel.SuspendLayout();
|
||||
this.mainTabControl.SuspendLayout();
|
||||
@@ -575,6 +575,12 @@
|
||||
resources.ApplyResources(this.settingsToolStripMenuItem, "settingsToolStripMenuItem");
|
||||
this.settingsToolStripMenuItem.Click += new System.EventHandler(this.settingsToolStripMenuItem_Click);
|
||||
//
|
||||
// customAppsToolStripMenuItem1
|
||||
//
|
||||
this.customAppsToolStripMenuItem1.Name = "customAppsToolStripMenuItem1";
|
||||
resources.ApplyResources(this.customAppsToolStripMenuItem1, "customAppsToolStripMenuItem1");
|
||||
this.customAppsToolStripMenuItem1.Click += new System.EventHandler(this.customAppsToolStripMenuItem_Click);
|
||||
//
|
||||
// searchTextBox
|
||||
//
|
||||
resources.ApplyResources(this.searchTextBox, "searchTextBox");
|
||||
@@ -618,37 +624,6 @@
|
||||
this.cancelAutoCloseButton1.Click += new System.EventHandler(this.cancelAutoCloseButton_Click);
|
||||
this.cancelAutoCloseButton1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.MainForm_KeyPress);
|
||||
//
|
||||
// devicesListView
|
||||
//
|
||||
this.devicesListView.BackColor = System.Drawing.SystemColors.Window;
|
||||
this.devicesListView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
|
||||
this.nameColumnHeader,
|
||||
this.stateColumnHeader});
|
||||
this.devicesListView.ContextMenuStrip = this.devicesContextMenuStrip;
|
||||
this.devicesListView.FullRowSelect = true;
|
||||
this.devicesListView.GridLines = true;
|
||||
this.devicesListView.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
|
||||
this.devicesListView.HideSelection = false;
|
||||
this.devicesListView.LargeImageList = this.devicesImageList;
|
||||
resources.ApplyResources(this.devicesListView, "devicesListView");
|
||||
this.devicesListView.MultiSelect = false;
|
||||
this.devicesListView.Name = "devicesListView";
|
||||
this.devicesListView.SmallImageList = this.devicesImageList;
|
||||
this.devicesListView.Sorting = System.Windows.Forms.SortOrder.Ascending;
|
||||
this.devicesListView.UseCompatibleStateImageBehavior = false;
|
||||
this.devicesListView.View = System.Windows.Forms.View.Details;
|
||||
this.devicesListView.Click += new System.EventHandler(this.devicesListView_Click);
|
||||
this.devicesListView.DoubleClick += new System.EventHandler(this.devicesListView_DoubleClick);
|
||||
this.devicesListView.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.MainForm_KeyPress);
|
||||
//
|
||||
// nameColumnHeader
|
||||
//
|
||||
resources.ApplyResources(this.nameColumnHeader, "nameColumnHeader");
|
||||
//
|
||||
// stateColumnHeader
|
||||
//
|
||||
resources.ApplyResources(this.stateColumnHeader, "stateColumnHeader");
|
||||
//
|
||||
// devicesContextMenuStrip
|
||||
//
|
||||
this.devicesContextMenuStrip.ImageScalingSize = new System.Drawing.Size(24, 24);
|
||||
@@ -968,6 +943,12 @@
|
||||
resources.ApplyResources(this.settingsToolStripMenuItem1, "settingsToolStripMenuItem1");
|
||||
this.settingsToolStripMenuItem1.Click += new System.EventHandler(this.settingsPictureBox_Click);
|
||||
//
|
||||
// customAppsToolStripMenuItem
|
||||
//
|
||||
this.customAppsToolStripMenuItem.Name = "customAppsToolStripMenuItem";
|
||||
resources.ApplyResources(this.customAppsToolStripMenuItem, "customAppsToolStripMenuItem");
|
||||
this.customAppsToolStripMenuItem.Click += new System.EventHandler(this.customAppsToolStripMenuItem_Click);
|
||||
//
|
||||
// openMapFileDialog
|
||||
//
|
||||
this.openMapFileDialog.DefaultExt = "mcrouter";
|
||||
@@ -978,17 +959,36 @@
|
||||
this.saveMapFileDialog.DefaultExt = "mcrouter";
|
||||
resources.ApplyResources(this.saveMapFileDialog, "saveMapFileDialog");
|
||||
//
|
||||
// customAppsToolStripMenuItem
|
||||
// devicesListView
|
||||
//
|
||||
this.customAppsToolStripMenuItem.Name = "customAppsToolStripMenuItem";
|
||||
resources.ApplyResources(this.customAppsToolStripMenuItem, "customAppsToolStripMenuItem");
|
||||
this.customAppsToolStripMenuItem.Click += new System.EventHandler(this.customAppsToolStripMenuItem_Click);
|
||||
this.devicesListView.BackColor = System.Drawing.SystemColors.Window;
|
||||
this.devicesListView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
|
||||
this.nameColumnHeader,
|
||||
this.stateColumnHeader});
|
||||
this.devicesListView.ContextMenuStrip = this.devicesContextMenuStrip;
|
||||
this.devicesListView.FullRowSelect = true;
|
||||
this.devicesListView.GridLines = true;
|
||||
this.devicesListView.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
|
||||
this.devicesListView.HideSelection = false;
|
||||
this.devicesListView.LargeImageList = this.devicesImageList;
|
||||
resources.ApplyResources(this.devicesListView, "devicesListView");
|
||||
this.devicesListView.MultiSelect = false;
|
||||
this.devicesListView.Name = "devicesListView";
|
||||
this.devicesListView.SmallImageList = this.devicesImageList;
|
||||
this.devicesListView.Sorting = System.Windows.Forms.SortOrder.Ascending;
|
||||
this.devicesListView.UseCompatibleStateImageBehavior = false;
|
||||
this.devicesListView.View = System.Windows.Forms.View.Details;
|
||||
this.devicesListView.Click += new System.EventHandler(this.devicesListView_Click);
|
||||
this.devicesListView.DoubleClick += new System.EventHandler(this.devicesListView_DoubleClick);
|
||||
this.devicesListView.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.MainForm_KeyPress);
|
||||
//
|
||||
// customAppsToolStripMenuItem1
|
||||
// nameColumnHeader
|
||||
//
|
||||
this.customAppsToolStripMenuItem1.Name = "customAppsToolStripMenuItem1";
|
||||
resources.ApplyResources(this.customAppsToolStripMenuItem1, "customAppsToolStripMenuItem1");
|
||||
this.customAppsToolStripMenuItem1.Click += new System.EventHandler(this.customAppsToolStripMenuItem_Click);
|
||||
resources.ApplyResources(this.nameColumnHeader, "nameColumnHeader");
|
||||
//
|
||||
// stateColumnHeader
|
||||
//
|
||||
resources.ApplyResources(this.stateColumnHeader, "stateColumnHeader");
|
||||
//
|
||||
// MainForm
|
||||
//
|
||||
|
||||
577
MainForm.resx
577
MainForm.resx
File diff suppressed because it is too large
Load Diff
@@ -129,6 +129,7 @@
|
||||
</Compile>
|
||||
<Compile Include="ProxySettings.Designer.cs">
|
||||
<DependentUpon>ProxySettings.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="ListViewExtended.cs">
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
|
||||
1156
MeshCentralServer-new.cs
Normal file
1156
MeshCentralServer-new.cs
Normal file
File diff suppressed because it is too large
Load Diff
@@ -97,6 +97,31 @@ 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()
|
||||
@@ -734,424 +759,8 @@ namespace MeshCentralRouter
|
||||
|
||||
public string Base64Encode(string plainText)
|
||||
{
|
||||
private MeshCentralServer parent = null;
|
||||
private TcpClient wsclient = null;
|
||||
private SslStream wsstream = null;
|
||||
private NetworkStream wsrawstream = null;
|
||||
private int state = 0;
|
||||
private Uri url = null;
|
||||
private byte[] readBuffer = new Byte[500];
|
||||
private int readBufferLen = 0;
|
||||
private int accopcodes = 0;
|
||||
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;
|
||||
public bool xdebug = false;
|
||||
public bool xtlsdump = false;
|
||||
public bool xignoreCert = false;
|
||||
|
||||
public void Dispose() {
|
||||
try { wsstream.Close(); } catch (Exception) { }
|
||||
try { wsstream.Dispose(); } catch (Exception) { }
|
||||
wsstream = null;
|
||||
wsclient = null;
|
||||
state = -1;
|
||||
parent.changeState(0);
|
||||
parent.wshash = null;
|
||||
}
|
||||
|
||||
public void Debug(string msg) { if (xdebug) { try { File.AppendAllText("debug.log", "Debug-" + msg + "\r\n"); } catch (Exception) { } } }
|
||||
public void TlsDump(string direction, byte[] data, int offset, int len) { if (xtlsdump) { try { File.AppendAllText("debug.log", direction + ": " + BitConverter.ToString(data, offset, len).Replace("-", string.Empty) + "\r\n"); } catch (Exception) { } } }
|
||||
|
||||
public bool Start(MeshCentralServer parent, Uri url, string user, string pass, string token, string fingerprint)
|
||||
{
|
||||
if (state != 0) return false;
|
||||
parent.changeState(1);
|
||||
state = 1;
|
||||
this.parent = parent;
|
||||
this.url = url;
|
||||
this.user = user;
|
||||
this.pass = pass;
|
||||
this.token = token;
|
||||
this.proxyUri = null;
|
||||
|
||||
this.proxyUri = Win32Api.GetProxy(url);
|
||||
|
||||
if (proxyUri != null)
|
||||
{
|
||||
// Proxy in use
|
||||
proxyInUse = true;
|
||||
wsclient = new TcpClient();
|
||||
// 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
|
||||
{
|
||||
// No proxy in use
|
||||
proxyInUse = false;
|
||||
wsclient = new TcpClient();
|
||||
Debug("Connecting without proxy");
|
||||
wsclient.BeginConnect(url.Host, url.Port, new AsyncCallback(OnConnectSink), this);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void OnConnectSink(IAsyncResult ar)
|
||||
{
|
||||
if (wsclient == null) return;
|
||||
|
||||
// Accept the connection
|
||||
try
|
||||
{
|
||||
wsclient.EndConnect(ar);
|
||||
} catch (Exception ex) {
|
||||
Debug("Websocket TCP failed to connect: " + ex.ToString());
|
||||
Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
if (proxyInUse == true)
|
||||
{
|
||||
// Send proxy connection request
|
||||
wsrawstream = wsclient.GetStream();
|
||||
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);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Start TLS connection
|
||||
Debug("Websocket TCP connected, doing TLS...");
|
||||
wsstream = new SslStream(wsclient.GetStream(), false, VerifyServerCertificate, null);
|
||||
wsstream.BeginAuthenticateAsClient(url.Host, null, System.Security.Authentication.SslProtocols.Tls12, false, new AsyncCallback(OnTlsSetupSink), this);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnProxyResponseSink(IAsyncResult ar)
|
||||
{
|
||||
if (wsrawstream == null) return;
|
||||
|
||||
int len = 0;
|
||||
try { len = wsrawstream.EndRead(ar); } catch (Exception) { }
|
||||
if (len == 0)
|
||||
{
|
||||
// Disconnect
|
||||
Debug("Websocket proxy disconnected, length = 0.");
|
||||
Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
TlsDump("InRaw", readBuffer, 0, readBufferLen);
|
||||
|
||||
readBufferLen += len;
|
||||
string proxyResponse = UTF8Encoding.UTF8.GetString(readBuffer, 0, readBufferLen);
|
||||
if (proxyResponse.IndexOf("\r\n\r\n") >= 0)
|
||||
{
|
||||
// We get a full proxy response, we should get something like "HTTP/1.1 200 Connection established\r\n\r\n"
|
||||
if (proxyResponse.StartsWith("HTTP/1.1 200 "))
|
||||
{
|
||||
// All good, start TLS setup.
|
||||
readBufferLen = 0;
|
||||
Debug("Websocket TCP connected, doing TLS...");
|
||||
wsstream = new SslStream(wsrawstream, false, VerifyServerCertificate, null);
|
||||
wsstream.BeginAuthenticateAsClient(url.Host, null, System.Security.Authentication.SslProtocols.Tls12, false, new AsyncCallback(OnTlsSetupSink), this);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Invalid response
|
||||
Debug("Proxy connection failed: " + proxyResponse);
|
||||
Dispose();
|
||||
}
|
||||
} else {
|
||||
if (readBufferLen == readBuffer.Length)
|
||||
{
|
||||
// Buffer overflow
|
||||
Debug("Proxy connection failed");
|
||||
Dispose();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Read more proxy data
|
||||
wsrawstream.BeginRead(readBuffer, readBufferLen, readBuffer.Length - readBufferLen, new AsyncCallback(OnProxyResponseSink), this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string Base64Encode(string plainText)
|
||||
{
|
||||
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
|
||||
return System.Convert.ToBase64String(plainTextBytes);
|
||||
}
|
||||
|
||||
public string Base64Decode(string base64EncodedData)
|
||||
{
|
||||
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
|
||||
return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
|
||||
}
|
||||
|
||||
private void OnTlsSetupSink(IAsyncResult ar)
|
||||
{
|
||||
if (wsstream == null) return;
|
||||
|
||||
// Accept the connection
|
||||
try
|
||||
{
|
||||
wsstream.EndAuthenticateAsClient(ar);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Disconnect
|
||||
if (ex.InnerException != null) {
|
||||
MessageBox.Show(ex.Message + ", Inner: " + ex.InnerException.ToString(), "MeshCentral Router");
|
||||
} else {
|
||||
MessageBox.Show(ex.Message, "MeshCentral Router");
|
||||
}
|
||||
Debug("Websocket TLS failed: " + ex.ToString());
|
||||
Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
// Fetch remote certificate
|
||||
parent.wshash = wsstream.RemoteCertificate.GetCertHashString();
|
||||
|
||||
// Setup extra headers if needed
|
||||
string extraHeaders = "";
|
||||
if (user != null && pass != null && token != null) { extraHeaders = "x-meshauth: " + Base64Encode(user) + "," + Base64Encode(pass) + "," + Base64Encode(token) + "\r\n"; }
|
||||
else if (user != null && pass != null) { extraHeaders = "x-meshauth: " + Base64Encode(user) + "," + Base64Encode(pass) + "\r\n"; }
|
||||
|
||||
// Send the HTTP headers
|
||||
Debug("Websocket TLS setup, sending HTTP header...");
|
||||
string header = "GET " + url.PathAndQuery + " HTTP/1.1\r\nHost: " + url.Host + "\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nSec-WebSocket-Version: 13\r\n" + extraHeaders + "\r\n";
|
||||
try { wsstream.Write(UTF8Encoding.UTF8.GetBytes(header)); } catch (Exception ex) { Debug(ex.ToString()); }
|
||||
|
||||
// Start receiving data
|
||||
wsstream.BeginRead(readBuffer, readBufferLen, readBuffer.Length - readBufferLen, new AsyncCallback(OnTlsDataSink), this);
|
||||
}
|
||||
|
||||
private void OnTlsDataSink(IAsyncResult ar)
|
||||
{
|
||||
if (wsstream == null) return;
|
||||
|
||||
int len = 0;
|
||||
try { len = wsstream.EndRead(ar); } catch (Exception) { }
|
||||
if (len == 0)
|
||||
{
|
||||
// Disconnect
|
||||
Debug("Websocket disconnected, length = 0.");
|
||||
Dispose();
|
||||
return;
|
||||
}
|
||||
//parent.Debug("#" + counter + ": Websocket got new data: " + len);
|
||||
readBufferLen += len;
|
||||
TlsDump("In", readBuffer, 0, len);
|
||||
|
||||
// Consume all of the data
|
||||
int consumed = 0;
|
||||
int ptr = 0;
|
||||
do
|
||||
{
|
||||
consumed = ProcessBuffer(readBuffer, ptr, readBufferLen - ptr);
|
||||
if (consumed < 0) { Dispose(); return; } // Error, close the connection
|
||||
ptr += consumed;
|
||||
} while ((consumed > 0) && ((readBufferLen - consumed) > 0));
|
||||
|
||||
// Move the data forward
|
||||
if ((ptr > 0) && (readBufferLen - ptr) > 0) {
|
||||
//Console.Write("MOVE FORWARD\r\n");
|
||||
Array.Copy(readBuffer, ptr, readBuffer, 0, (readBufferLen - ptr));
|
||||
}
|
||||
readBufferLen = (readBufferLen - ptr);
|
||||
|
||||
// If the buffer is too small, double the size here.
|
||||
if (readBuffer.Length - readBufferLen == 0)
|
||||
{
|
||||
Debug("Increasing the read buffer size from " + readBuffer.Length + " to " + (readBuffer.Length * 2) + ".");
|
||||
byte[] readBuffer2 = new byte[readBuffer.Length * 2];
|
||||
Array.Copy(readBuffer, 0, readBuffer2, 0, readBuffer.Length);
|
||||
readBuffer = readBuffer2;
|
||||
}
|
||||
|
||||
// Receive more data
|
||||
try { wsstream.BeginRead(readBuffer, readBufferLen, readBuffer.Length - readBufferLen, new AsyncCallback(OnTlsDataSink), this); } catch (Exception) { }
|
||||
}
|
||||
|
||||
private int ProcessBuffer(byte[] buffer, int offset, int len)
|
||||
{
|
||||
string ss = UTF8Encoding.UTF8.GetString(buffer, offset, len);
|
||||
|
||||
if (state == 1)
|
||||
{
|
||||
// Look for the end of the http header
|
||||
string header = UTF8Encoding.UTF8.GetString(buffer, offset, len);
|
||||
int i = header.IndexOf("\r\n\r\n");
|
||||
if (i == -1) return 0;
|
||||
Dictionary<string, string> parsedHeader = ParseHttpHeader(header.Substring(0, i));
|
||||
if ((parsedHeader == null) || (parsedHeader["_Path"] != "101")) { Debug("Websocket bad header."); return -1; } // Bad header, close the connection
|
||||
Debug("Websocket got setup upgrade header.");
|
||||
state = 2;
|
||||
return len; // TODO: Technically we need to return the header length before UTF8 convert.
|
||||
} else if (state == 2) {
|
||||
// Parse a websocket fragment header
|
||||
if (len < 2) return 0;
|
||||
int headsize = 2;
|
||||
accopcodes = buffer[offset];
|
||||
accmask = ((buffer[offset + 1] & 0x80) != 0);
|
||||
acclen = (buffer[offset + 1] & 0x7F);
|
||||
|
||||
if ((accopcodes & 0x0F) == 8)
|
||||
{
|
||||
// Close the websocket
|
||||
Debug("Websocket got closed fragment.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (acclen == 126)
|
||||
{
|
||||
if (len < 4) return 0;
|
||||
headsize = 4;
|
||||
acclen = (buffer[offset + 2] << 8) + (buffer[offset + 3]);
|
||||
}
|
||||
else if (acclen == 127)
|
||||
{
|
||||
if (len < 10) return 0;
|
||||
headsize = 10;
|
||||
acclen = (buffer[offset + 6] << 24) + (buffer[offset + 7] << 16) + (buffer[offset + 8] << 8) + (buffer[offset + 9]);
|
||||
Debug("Websocket receive large fragment: " + acclen);
|
||||
}
|
||||
if (accmask == true)
|
||||
{
|
||||
// TODO: Do unmasking here.
|
||||
headsize += 4;
|
||||
}
|
||||
//parent.Debug("#" + counter + ": Websocket frag header - FIN: " + ((accopcodes & 0x80) != 0) + ", OP: " + (accopcodes & 0x0F) + ", LEN: " + acclen + ", MASK: " + accmask);
|
||||
state = 3;
|
||||
return headsize;
|
||||
}
|
||||
else if (state == 3)
|
||||
{
|
||||
// Parse a websocket fragment data
|
||||
if (len < acclen) return 0;
|
||||
//Console.Write("WSREAD: " + acclen + "\r\n");
|
||||
ProcessWsBuffer(buffer, offset, acclen, accopcodes);
|
||||
state = 2;
|
||||
return acclen;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private void ProcessWsBuffer(byte[] data, int offset, int len, int op)
|
||||
{
|
||||
Debug("Websocket got data.");
|
||||
//try { parent.processServerData(UTF8Encoding.UTF8.GetString(data, offset, len)); } catch (Exception ex) { }
|
||||
parent.processServerData(UTF8Encoding.UTF8.GetString(data, offset, len));
|
||||
}
|
||||
|
||||
private Dictionary<string, string> ParseHttpHeader(string header)
|
||||
{
|
||||
string[] lines = header.Replace("\r\n", "\r").Split('\r');
|
||||
if (lines.Length < 2) { return null; }
|
||||
string[] directive = lines[0].Split(' ');
|
||||
Dictionary<string, string> values = new Dictionary<string, string>();
|
||||
values["_Action"] = directive[0];
|
||||
values["_Path"] = directive[1];
|
||||
values["_Protocol"] = directive[2];
|
||||
for (int i = 1; i < lines.Length; i++)
|
||||
{
|
||||
var j = lines[i].IndexOf(":");
|
||||
values[lines[i].Substring(0, j).ToLower()] = lines[i].Substring(j + 1).Trim();
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
// Return a modified base64 SHA384 hash string of the certificate public key
|
||||
public static string GetMeshKeyHash(X509Certificate cert)
|
||||
{
|
||||
return ByteArrayToHexString(new SHA384Managed().ComputeHash(cert.GetPublicKey()));
|
||||
}
|
||||
|
||||
// Return a modified base64 SHA384 hash string of the certificate
|
||||
public static string GetMeshCertHash(X509Certificate cert)
|
||||
{
|
||||
return ByteArrayToHexString(new SHA384Managed().ComputeHash(cert.GetRawCertData()));
|
||||
}
|
||||
|
||||
public static string ByteArrayToHexString(byte[] Bytes)
|
||||
{
|
||||
StringBuilder Result = new StringBuilder(Bytes.Length * 2);
|
||||
string HexAlphabet = "0123456789ABCDEF";
|
||||
foreach (byte B in Bytes) { Result.Append(HexAlphabet[(int)(B >> 4)]); Result.Append(HexAlphabet[(int)(B & 0xF)]); }
|
||||
return Result.ToString();
|
||||
}
|
||||
|
||||
private bool VerifyServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
||||
{
|
||||
parent.certHash = GetMeshKeyHash(certificate);
|
||||
Debug("Verify cert: " + parent.certHash);
|
||||
if (xignoreCert) return true;
|
||||
if (chain.Build(new X509Certificate2(certificate)) == true) return true;
|
||||
|
||||
// Check that the remote certificate is the expected one
|
||||
if ((parent.okCertHash != null) && (parent.okCertHash == certificate.GetCertHashString())) return true;
|
||||
|
||||
// Check that the remote certificate is the expected one
|
||||
if ((parent.okCertHash2 != null) && ((parent.okCertHash2 == GetMeshKeyHash(certificate)) || (parent.okCertHash2 == GetMeshCertHash(certificate)))) { return true; }
|
||||
|
||||
parent.certHash = null;
|
||||
parent.disconnectMsg = "cert";
|
||||
parent.disconnectCert = new X509Certificate2(certificate);
|
||||
return false;
|
||||
}
|
||||
|
||||
public void WriteStringWebSocket(string data)
|
||||
{
|
||||
// Convert the string into a buffer with 4 byte of header space.
|
||||
int len = UTF8Encoding.UTF8.GetByteCount(data);
|
||||
byte[] buf = new byte[4 + len];
|
||||
UTF8Encoding.UTF8.GetBytes(data, 0, data.Length, buf, 4);
|
||||
len = buf.Length - 4;
|
||||
|
||||
// Check that everything is ok
|
||||
if ((state < 2) || (len < 1) || (len > 65535)) { Dispose(); return; }
|
||||
|
||||
//Console.Write("Length: " + len + "\r\n");
|
||||
//System.Threading.Thread.Sleep(0);
|
||||
|
||||
if (len < 126)
|
||||
{
|
||||
// Small fragment
|
||||
buf[2] = 130; // Fragment op code (129 = text, 130 = binary)
|
||||
buf[3] = (byte)(len & 0x7F);
|
||||
//try { wsstream.BeginWrite(buf, 2, len + 2, new AsyncCallback(WriteWebSocketAsyncDone), args); } catch (Exception) { Dispose(); return; }
|
||||
TlsDump("Out", buf, 2, len + 2);
|
||||
try { wsstream.Write(buf, 2, len + 2); } catch (Exception ex) { Debug(ex.ToString()); }
|
||||
}
|
||||
else
|
||||
{
|
||||
// Large fragment
|
||||
buf[0] = 130; // Fragment op code (129 = text, 130 = binary)
|
||||
buf[1] = 126;
|
||||
buf[2] = (byte)((len >> 8) & 0xFF);
|
||||
buf[3] = (byte)(len & 0xFF);
|
||||
//try { wsstream.BeginWrite(buf, 0, len + 4, new AsyncCallback(WriteWebSocketAsyncDone), args); } catch (Exception) { Dispose(); return; }
|
||||
TlsDump("Out", buf, 0, len + 4);
|
||||
try { wsstream.Write(buf, 0, len + 4); } catch (Exception ex) { Debug(ex.ToString()); }
|
||||
}
|
||||
}
|
||||
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
|
||||
return System.Convert.ToBase64String(plainTextBytes);
|
||||
}
|
||||
|
||||
public string Base64Decode(string base64EncodedData)
|
||||
|
||||
258
ProxySettings.Designer.cs
generated
258
ProxySettings.Designer.cs
generated
@@ -28,167 +28,221 @@
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.SaveProxyConfig = new System.Windows.Forms.Button();
|
||||
this.manualHttpProxyHost = new System.Windows.Forms.TextBox();
|
||||
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ProxySettings));
|
||||
this.okButton = new System.Windows.Forms.Button();
|
||||
this.hostTextBox = 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.usernameTextBox = new System.Windows.Forms.TextBox();
|
||||
this.label4 = new System.Windows.Forms.Label();
|
||||
this.manualHttpProxyPassword = new System.Windows.Forms.TextBox();
|
||||
this.passwordTextBox = 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.manualProxyCheckBox = new System.Windows.Forms.CheckBox();
|
||||
this.cancelButton = new System.Windows.Forms.Button();
|
||||
this.groupBox1 = new System.Windows.Forms.GroupBox();
|
||||
this.label2 = new System.Windows.Forms.Label();
|
||||
this.authComboBox = new System.Windows.Forms.ComboBox();
|
||||
this.groupBox1.SuspendLayout();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// SaveProxyConfig
|
||||
// okButton
|
||||
//
|
||||
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);
|
||||
this.okButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.okButton.Location = new System.Drawing.Point(164, 203);
|
||||
this.okButton.Margin = new System.Windows.Forms.Padding(2);
|
||||
this.okButton.Name = "okButton";
|
||||
this.okButton.Size = new System.Drawing.Size(95, 23);
|
||||
this.okButton.TabIndex = 1;
|
||||
this.okButton.Text = "OK";
|
||||
this.okButton.UseVisualStyleBackColor = true;
|
||||
this.okButton.Click += new System.EventHandler(this.okButton_Click);
|
||||
//
|
||||
// manualHttpProxyHost
|
||||
// hostTextBox
|
||||
//
|
||||
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;
|
||||
this.hostTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
|
||||
| System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.hostTextBox.Location = new System.Drawing.Point(145, 39);
|
||||
this.hostTextBox.Margin = new System.Windows.Forms.Padding(2);
|
||||
this.hostTextBox.Name = "hostTextBox";
|
||||
this.hostTextBox.Size = new System.Drawing.Size(192, 20);
|
||||
this.hostTextBox.TabIndex = 2;
|
||||
this.hostTextBox.TextChanged += new System.EventHandler(this.manualProxyCheckBox_CheckedChanged);
|
||||
//
|
||||
// label1
|
||||
//
|
||||
this.label1.AutoSize = true;
|
||||
this.label1.Location = new System.Drawing.Point(75, 114);
|
||||
this.label1.Location = new System.Drawing.Point(6, 42);
|
||||
this.label1.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
|
||||
this.label1.Name = "label1";
|
||||
this.label1.Size = new System.Drawing.Size(103, 17);
|
||||
this.label1.Size = new System.Drawing.Size(106, 13);
|
||||
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";
|
||||
this.label1.Text = "Proxy Hostname:Port";
|
||||
//
|
||||
// label3
|
||||
//
|
||||
this.label3.AutoSize = true;
|
||||
this.label3.Location = new System.Drawing.Point(75, 257);
|
||||
this.label3.Location = new System.Drawing.Point(5, 93);
|
||||
this.label3.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
|
||||
this.label3.Name = "label3";
|
||||
this.label3.Size = new System.Drawing.Size(139, 17);
|
||||
this.label3.Size = new System.Drawing.Size(55, 13);
|
||||
this.label3.TabIndex = 7;
|
||||
this.label3.Text = "Http proxy username";
|
||||
this.label3.Text = "Username";
|
||||
//
|
||||
// manualHttpProxyUsername
|
||||
// usernameTextBox
|
||||
//
|
||||
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;
|
||||
this.usernameTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
|
||||
| System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.usernameTextBox.Location = new System.Drawing.Point(145, 90);
|
||||
this.usernameTextBox.Margin = new System.Windows.Forms.Padding(2);
|
||||
this.usernameTextBox.Name = "usernameTextBox";
|
||||
this.usernameTextBox.Size = new System.Drawing.Size(192, 20);
|
||||
this.usernameTextBox.TabIndex = 6;
|
||||
this.usernameTextBox.TextChanged += new System.EventHandler(this.manualProxyCheckBox_CheckedChanged);
|
||||
//
|
||||
// label4
|
||||
//
|
||||
this.label4.AutoSize = true;
|
||||
this.label4.Location = new System.Drawing.Point(78, 332);
|
||||
this.label4.Location = new System.Drawing.Point(6, 117);
|
||||
this.label4.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
|
||||
this.label4.Name = "label4";
|
||||
this.label4.Size = new System.Drawing.Size(136, 17);
|
||||
this.label4.Size = new System.Drawing.Size(53, 13);
|
||||
this.label4.TabIndex = 9;
|
||||
this.label4.Text = "Http proxy password";
|
||||
this.label4.Text = "Password";
|
||||
//
|
||||
// manualHttpProxyPassword
|
||||
// passwordTextBox
|
||||
//
|
||||
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;
|
||||
this.passwordTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
|
||||
| System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.passwordTextBox.Location = new System.Drawing.Point(145, 114);
|
||||
this.passwordTextBox.Margin = new System.Windows.Forms.Padding(2);
|
||||
this.passwordTextBox.Name = "passwordTextBox";
|
||||
this.passwordTextBox.PasswordChar = '*';
|
||||
this.passwordTextBox.Size = new System.Drawing.Size(192, 20);
|
||||
this.passwordTextBox.TabIndex = 8;
|
||||
this.passwordTextBox.TextChanged += new System.EventHandler(this.manualProxyCheckBox_CheckedChanged);
|
||||
//
|
||||
// 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.Location = new System.Drawing.Point(12, 7);
|
||||
this.label5.Name = "label5";
|
||||
this.label5.Size = new System.Drawing.Size(396, 69);
|
||||
this.label5.Size = new System.Drawing.Size(346, 44);
|
||||
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.Text = "By default, the system\'s proxy is used. You can override this by manually configu" +
|
||||
"ring your own proxy settings here.\r\n";
|
||||
this.label5.UseMnemonic = false;
|
||||
//
|
||||
// useManualProxySettings
|
||||
// manualProxyCheckBox
|
||||
//
|
||||
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);
|
||||
this.manualProxyCheckBox.AutoSize = true;
|
||||
this.manualProxyCheckBox.Location = new System.Drawing.Point(145, 18);
|
||||
this.manualProxyCheckBox.Margin = new System.Windows.Forms.Padding(2);
|
||||
this.manualProxyCheckBox.Name = "manualProxyCheckBox";
|
||||
this.manualProxyCheckBox.Size = new System.Drawing.Size(131, 17);
|
||||
this.manualProxyCheckBox.TabIndex = 11;
|
||||
this.manualProxyCheckBox.Text = "Manual Proxy Settings";
|
||||
this.manualProxyCheckBox.UseVisualStyleBackColor = true;
|
||||
this.manualProxyCheckBox.CheckedChanged += new System.EventHandler(this.manualProxyCheckBox_CheckedChanged);
|
||||
//
|
||||
// cancel
|
||||
// cancelButton
|
||||
//
|
||||
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);
|
||||
this.cancelButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
|
||||
this.cancelButton.Location = new System.Drawing.Point(263, 203);
|
||||
this.cancelButton.Margin = new System.Windows.Forms.Padding(2);
|
||||
this.cancelButton.Name = "cancelButton";
|
||||
this.cancelButton.Size = new System.Drawing.Size(95, 23);
|
||||
this.cancelButton.TabIndex = 12;
|
||||
this.cancelButton.Text = "Cancel";
|
||||
this.cancelButton.UseVisualStyleBackColor = true;
|
||||
this.cancelButton.Click += new System.EventHandler(this.cancelButton_Click);
|
||||
//
|
||||
// groupBox1
|
||||
//
|
||||
this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
|
||||
| System.Windows.Forms.AnchorStyles.Left)
|
||||
| System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.groupBox1.Controls.Add(this.label2);
|
||||
this.groupBox1.Controls.Add(this.authComboBox);
|
||||
this.groupBox1.Controls.Add(this.hostTextBox);
|
||||
this.groupBox1.Controls.Add(this.usernameTextBox);
|
||||
this.groupBox1.Controls.Add(this.manualProxyCheckBox);
|
||||
this.groupBox1.Controls.Add(this.passwordTextBox);
|
||||
this.groupBox1.Controls.Add(this.label4);
|
||||
this.groupBox1.Controls.Add(this.label1);
|
||||
this.groupBox1.Controls.Add(this.label3);
|
||||
this.groupBox1.Location = new System.Drawing.Point(12, 54);
|
||||
this.groupBox1.Name = "groupBox1";
|
||||
this.groupBox1.Size = new System.Drawing.Size(342, 144);
|
||||
this.groupBox1.TabIndex = 13;
|
||||
this.groupBox1.TabStop = false;
|
||||
this.groupBox1.Text = "Proxy Settings";
|
||||
//
|
||||
// label2
|
||||
//
|
||||
this.label2.AutoSize = true;
|
||||
this.label2.Location = new System.Drawing.Point(6, 67);
|
||||
this.label2.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
|
||||
this.label2.Name = "label2";
|
||||
this.label2.Size = new System.Drawing.Size(104, 13);
|
||||
this.label2.TabIndex = 13;
|
||||
this.label2.Text = "Proxy Authentication";
|
||||
//
|
||||
// authComboBox
|
||||
//
|
||||
this.authComboBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
|
||||
| System.Windows.Forms.AnchorStyles.Right)));
|
||||
this.authComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
|
||||
this.authComboBox.FormattingEnabled = true;
|
||||
this.authComboBox.Items.AddRange(new object[] {
|
||||
"None",
|
||||
"Basic Authentication"});
|
||||
this.authComboBox.Location = new System.Drawing.Point(145, 64);
|
||||
this.authComboBox.Name = "authComboBox";
|
||||
this.authComboBox.Size = new System.Drawing.Size(192, 21);
|
||||
this.authComboBox.TabIndex = 12;
|
||||
this.authComboBox.SelectedIndexChanged += new System.EventHandler(this.manualProxyCheckBox_CheckedChanged);
|
||||
//
|
||||
// ProxySettings
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
|
||||
this.AcceptButton = this.okButton;
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
|
||||
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.CancelButton = this.cancelButton;
|
||||
this.ClientSize = new System.Drawing.Size(369, 237);
|
||||
this.Controls.Add(this.groupBox1);
|
||||
this.Controls.Add(this.cancelButton);
|
||||
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.Controls.Add(this.okButton);
|
||||
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
|
||||
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
|
||||
this.Margin = new System.Windows.Forms.Padding(2);
|
||||
this.MaximizeBox = false;
|
||||
this.MinimizeBox = false;
|
||||
this.Name = "ProxySettings";
|
||||
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
|
||||
this.Text = "Proxy Settings";
|
||||
this.groupBox1.ResumeLayout(false);
|
||||
this.groupBox1.PerformLayout();
|
||||
this.ResumeLayout(false);
|
||||
this.PerformLayout();
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
private System.Windows.Forms.Button SaveProxyConfig;
|
||||
private System.Windows.Forms.TextBox manualHttpProxyHost;
|
||||
private System.Windows.Forms.Button okButton;
|
||||
private System.Windows.Forms.TextBox hostTextBox;
|
||||
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.TextBox usernameTextBox;
|
||||
private System.Windows.Forms.Label label4;
|
||||
private System.Windows.Forms.TextBox manualHttpProxyPassword;
|
||||
private System.Windows.Forms.TextBox passwordTextBox;
|
||||
private System.Windows.Forms.Label label5;
|
||||
private System.Windows.Forms.CheckBox useManualProxySettings;
|
||||
private System.Windows.Forms.Button cancel;
|
||||
private System.Windows.Forms.CheckBox manualProxyCheckBox;
|
||||
private System.Windows.Forms.Button cancelButton;
|
||||
private System.Windows.Forms.GroupBox groupBox1;
|
||||
private System.Windows.Forms.Label label2;
|
||||
private System.Windows.Forms.ComboBox authComboBox;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,4 @@
|
||||
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
|
||||
@@ -15,53 +8,66 @@ namespace MeshCentralRouter
|
||||
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();
|
||||
manualProxyCheckBox.Checked = Settings.GetRegValue("ManualProxy", false);
|
||||
string host = Settings.GetRegValue("ProxyHost", "");
|
||||
if (host != "") { host += ":" + Settings.GetRegValue("ProxyPort", 443); }
|
||||
hostTextBox.Text = host;
|
||||
authComboBox.SelectedIndex = Settings.GetRegValue("ProxyAuth", 0);
|
||||
usernameTextBox.Text = Settings.GetRegValue("ProxyUsername", "");
|
||||
passwordTextBox.Text = Settings.GetRegValue("ProxyPassword", "");
|
||||
UpdateInfo();
|
||||
}
|
||||
|
||||
private void label1_Click(object sender, EventArgs e)
|
||||
private void UpdateInfo()
|
||||
{
|
||||
hostTextBox.Enabled = manualProxyCheckBox.Checked;
|
||||
usernameTextBox.Enabled = passwordTextBox.Enabled = (manualProxyCheckBox.Checked && (authComboBox.SelectedIndex == 1));
|
||||
|
||||
bool ok = true;
|
||||
if (manualProxyCheckBox.Checked == true)
|
||||
{
|
||||
string portStr = "";
|
||||
string hostStr = hostTextBox.Text;
|
||||
int i = hostStr.IndexOf(':');
|
||||
if (i >= 0) { portStr = hostStr.Substring(i + 1); hostStr = hostStr.Substring(0, i); }
|
||||
int port = 0;
|
||||
int.TryParse(portStr, out port);
|
||||
if ((hostStr.Length == 0) || (port < 1) || (port > 65535)) { ok = false; }
|
||||
}
|
||||
okButton.Enabled = ok;
|
||||
}
|
||||
|
||||
private void SaveProxyConfig_Click(object sender, EventArgs e)
|
||||
private void manualProxyCheckBox_CheckedChanged(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);
|
||||
UpdateInfo();
|
||||
}
|
||||
|
||||
private void okButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
Settings.SetRegValue("ManualProxy", manualProxyCheckBox.Checked);
|
||||
if (manualProxyCheckBox.Checked == true) {
|
||||
string hostStr = hostTextBox.Text;
|
||||
string portStr = "";
|
||||
int i = hostStr.IndexOf(':');
|
||||
if (i >= 0) { portStr = hostStr.Substring(i + 1); hostStr = hostStr.Substring(0, i); }
|
||||
int port = 0;
|
||||
int.TryParse(portStr, out port);
|
||||
Settings.SetRegValue("ProxyHost", hostStr);
|
||||
Settings.SetRegValue("ProxyPort", port);
|
||||
Settings.SetRegValue("ProxyAuth", authComboBox.SelectedIndex);
|
||||
Settings.SetRegValue("ProxyUsername", (authComboBox.SelectedIndex == 1) ? usernameTextBox.Text : "");
|
||||
Settings.SetRegValue("ProxyPassword", (authComboBox.SelectedIndex == 1) ? passwordTextBox.Text : "");
|
||||
} else {
|
||||
Settings.SetRegValue("ProxyHost", "");
|
||||
Settings.SetRegValue("ProxyPort", "");
|
||||
Settings.SetRegValue("ProxyAuth", 0);
|
||||
Settings.SetRegValue("ProxyUsername", "");
|
||||
Settings.SetRegValue("ProxyPassword", "");
|
||||
}
|
||||
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)
|
||||
private void cancelButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
DialogResult = DialogResult.Cancel;
|
||||
}
|
||||
|
||||
1695
ProxySettings.resx
1695
ProxySettings.resx
File diff suppressed because it is too large
Load Diff
@@ -30,6 +30,10 @@ namespace MeshCentralRouter
|
||||
{
|
||||
try { Registry.SetValue(@"HKEY_CURRENT_USER\SOFTWARE\Open Source\MeshCentral Router", name, value.ToString()); } catch (Exception) { }
|
||||
}
|
||||
public static void SetRegValue(string name, int value)
|
||||
{
|
||||
try { Registry.SetValue(@"HKEY_CURRENT_USER\SOFTWARE\Open Source\MeshCentral Router", name, value.ToString()); } catch (Exception) { }
|
||||
}
|
||||
/// <summary>
|
||||
/// This function querys the registry. If the key is found it returns the value as a string
|
||||
/// </summary>
|
||||
@@ -55,6 +59,11 @@ namespace MeshCentralRouter
|
||||
try { return bool.Parse(GetRegValue(name, value.ToString())); } catch (Exception) { return value; }
|
||||
}
|
||||
|
||||
public static int GetRegValue(string name, int value)
|
||||
{
|
||||
try { return int.Parse(GetRegValue(name, value.ToString())); } catch (Exception) { return value; }
|
||||
}
|
||||
|
||||
public static void SetApplications(List<string[]> apps)
|
||||
{
|
||||
ClearApplications();
|
||||
|
||||
@@ -161,7 +161,7 @@ namespace MeshCentralRouter
|
||||
|
||||
private async Task ConnectAsync(Uri url)
|
||||
{
|
||||
if (state != ConnectionStates.Disconnected) return false;
|
||||
if (state != ConnectionStates.Disconnected) return;
|
||||
SetState(ConnectionStates.Connecting);
|
||||
this.url = url;
|
||||
if (tlsCertFingerprint != null) { this.tlsCertFingerprint = tlsCertFingerprint.ToUpper(); }
|
||||
@@ -500,7 +500,8 @@ namespace MeshCentralRouter
|
||||
|
||||
private void PongTimerCallback(object state) { SendPong(null, 0, 0); }
|
||||
|
||||
private void ConnectTimerCallback(object state) {
|
||||
private void ConnectTimerCallback(object state)
|
||||
{
|
||||
// Switch from C# sockets to native sockets
|
||||
if ((nativeWebSocketFirst == false) && (this.state == ConnectionStates.Connecting))
|
||||
{
|
||||
|
||||
29
Win32Api.cs
29
Win32Api.cs
@@ -78,24 +78,28 @@ 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))
|
||||
// Manual http proxy with optional basic auth
|
||||
if (Settings.GetRegValue("ManualProxy", 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)
|
||||
string proxyUserName = null;
|
||||
string proxyPassword = null;
|
||||
if (Settings.GetRegValue("ProxyAuth", 0) == 1)
|
||||
{
|
||||
proxyStr = "http://" + proxyUserName + ":" + proxyPassword + "@" + Settings.GetRegValue("Manual_Http_proxy_host", "") + ":" + Settings.GetRegValue("Manual_Http_proxy_port", "");
|
||||
proxyUserName = Settings.GetRegValue("ProxyUsername", "");
|
||||
proxyPassword = Settings.GetRegValue("ProxyPassword", "");
|
||||
}
|
||||
if ((proxyUserName != null) && (proxyPassword != null))
|
||||
{
|
||||
proxyStr = "http://" + proxyUserName + ":" + proxyPassword + "@" + Settings.GetRegValue("ProxyHost", "") + ":" + Settings.GetRegValue("ProxyPort", 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
proxyStr = "http://" + Settings.GetRegValue("Manual_Http_proxy_host", "") + ":" + Settings.GetRegValue("Manual_Http_proxy_port", "");
|
||||
|
||||
proxyStr = "http://" + Settings.GetRegValue("ProxyHost", "") + ":" + Settings.GetRegValue("ProxyPort", "");
|
||||
}
|
||||
return new Uri(proxyStr);
|
||||
}
|
||||
|
||||
// Check if we need to use a HTTP proxy (Auto-proxy way)
|
||||
try
|
||||
{
|
||||
@@ -104,10 +108,7 @@ namespace MeshCentralRouter
|
||||
if ((x != null) && (x.GetType() == typeof(string)))
|
||||
{
|
||||
string proxyStr = GetProxyForUrlUsingPac("http" + ((url.Port == 80) ? "" : "s") + "://" + url.Host + ":" + url.Port, x.ToString());
|
||||
if (proxyStr != null)
|
||||
{
|
||||
return new Uri("http://" + proxyStr);
|
||||
}
|
||||
if (proxyStr != null) { return new Uri("http://" + proxyStr); }
|
||||
}
|
||||
}
|
||||
catch (Exception) { }
|
||||
@@ -150,5 +151,5 @@ namespace MeshCentralRouter
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user