Skip to content

Commit

Permalink
Merge pull request #2 from gardian12/master
Browse files Browse the repository at this point in the history
bugfix for proxy crash and general optimizations added
  • Loading branch information
smorks authored May 6, 2020
2 parents be3c7bc + 3226727 commit 857381a
Show file tree
Hide file tree
Showing 9 changed files with 303 additions and 46 deletions.
11 changes: 6 additions & 5 deletions Mono/Unix/UnixEndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public UnixEndPoint(string filename)
if (filename == null)
throw new ArgumentNullException(nameof(filename));

if (filename == "")
if (String.IsNullOrEmpty(filename))
throw new ArgumentException("Cannot be empty.", nameof(filename));
this.filename = filename;
}
Expand Down Expand Up @@ -83,8 +83,10 @@ public override EndPoint Create(SocketAddress socketAddress)
{
// Empty filename.
// Probably from RemoteEndPoint which on linux does not return the file name.
UnixEndPoint uep = new UnixEndPoint("a");
uep.filename = "";
UnixEndPoint uep = new UnixEndPoint("a")
{
filename = ""
};
return uep;
}
int size = socketAddress.Size - 2;
Expand Down Expand Up @@ -130,8 +132,7 @@ public override int GetHashCode()

public override bool Equals(object o)
{
UnixEndPoint other = o as UnixEndPoint;
if (other == null)
if (!(o is UnixEndPoint other))
return false;

return (other.filename == filename);
Expand Down
35 changes: 30 additions & 5 deletions PipeProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,55 @@ namespace KeePassNatMsgProxy
{
public class PipeProxy : ProxyBase
{
private NamedPipeClientStream _client;
private const int ConnectTimeout = 5000;

private NamedPipeClientStream _client;

protected override bool IsClientConnected => _client.IsConnected;

[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "<optimization>")]
protected override int ClientRead(byte[] buffer, int offset, int length)
{
return _client.Read(buffer, offset, length);
try
{
return _client.Read(buffer, offset, length);
}
catch (Exception)
{
return 0;
}
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "<optimization>")]
protected override void ClientWrite(byte[] data)
{
_client.Write(data, 0, data.Length);
try
{
_client.Write(data, 0, data.Length);
}
catch (Exception)
{
return;
}
}

protected override void Close()
{
_client.Close();
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "<optimization>")]
protected override void Connect()
{
_client = new NamedPipeClientStream(".", $"keepassxc\\{Environment.UserName}\\{ProxyName}", PipeDirection.InOut, PipeOptions.Asynchronous);
_client.Connect(ConnectTimeout);
try
{
_client = new NamedPipeClientStream(".", $"keepassxc\\{Environment.UserName}\\{ProxyName}", PipeDirection.InOut, PipeOptions.Asynchronous);
_client.Connect(ConnectTimeout);
}
catch (Exception)
{
return;
}
}
}
}
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("keepassnatmsg-proxy")]
[assembly: AssemblyCopyright("Copyright © Andy Brandt 2018")]
[assembly: AssemblyCopyright("Copyright © Andy Brandt 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -32,4 +32,4 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.0.6.*")]
[assembly: AssemblyVersion("0.0.7.*")]
61 changes: 50 additions & 11 deletions Proxy.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,67 @@
using System;
using System.Windows.Forms;

namespace KeePassNatMsgProxy
{
class Proxy
public class Proxy
{
static void Main(string[] args)
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "<optimization>")]
public static int Main(string[] args)
{
// check if we're running under Mono
var t = Type.GetType("Mono.Runtime");

if (t == null)
if (args.Length == 0)
{
// not Mono, assume Windows
var proxy = new PipeProxy();
proxy.Run();
DialogResult result = MessageBox.Show("No arguments received.", nameof(KeePassNatMsgProxy) + " Info");

// Set exit code of application
return 1;
}
else
{
if (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX)
string msg = "";

foreach (string item in args)
{
msg = msg + "\n" + item;
}

#if DEBUG
DialogResult result = MessageBox.Show("Attention: " + nameof(KeePassNatMsgProxy) + " runs in DEBUG mode.\n" +
"\n" +
"Arguments: " + msg,
nameof(KeePassNatMsgProxy) + " Info");
#endif
}


// check if we're running under Mono
var t = Type.GetType("Mono.Runtime");

try
{
if (t == null)
{
var proxy = new UnixSocketProxy();
// not Mono, assume Windows
var proxy = new PipeProxy();
proxy.Run();

}
else
{
if (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX)
{
var proxy = new UnixSocketProxy();
proxy.Run();

}
}
}
catch (Exception ex)
{
DialogResult result = MessageBox.Show(ex.Message, nameof(KeePassNatMsgProxy) + " Error");
}

// Set exit code of application
return 0;
}
}
}
Loading

0 comments on commit 857381a

Please sign in to comment.