diff --git a/ZitiTunneler/MainWindow.xaml.cs b/ZitiTunneler/MainWindow.xaml.cs index 1e44b336..f6b97939 100644 --- a/ZitiTunneler/MainWindow.xaml.cs +++ b/ZitiTunneler/MainWindow.xaml.cs @@ -18,7 +18,7 @@ namespace ZitiTunneler { public System.Windows.Forms.NotifyIcon notifyIcon; private DateTime _startDate; private System.Windows.Forms.Timer _timer; - private ServiceClient.Client serviceClient = null; + private Client serviceClient = null; private List identities { @@ -65,10 +65,18 @@ namespace ZitiTunneler { this.Top = desktopWorkingArea.Bottom-this.Height-25; // add a new service client - serviceClient = new ServiceClient.Client(); + serviceClient = new Client(); Application.Current.Properties.Add("ServiceClient", serviceClient); Application.Current.Properties.Add("Identities", new List()); - LoadStatusFromService(); + try + { + LoadStatusFromService(); + } + catch + { + //probably some kind of problem with the service... + MessageBox.Show("oh my goodness - problem with the service. Almost certainly means the service is NOT RUNNING... Jeremy make this pretty"); + } LoadIdentities(); } @@ -125,7 +133,32 @@ namespace ZitiTunneler { jwtDialog.Filter = "Ziti Identities (*.jwt)|*.jwt"; if (jwtDialog.ShowDialog() == true) { string fileContent = File.ReadAllText(jwtDialog.FileName); - // Clint!! AxedaBuddy - What to do with the jwt file? + + // Jeremy!! AxedaBuddy - DUN! + try + { + Identity createdId = serviceClient.AddIdentity(System.IO.Path.GetFileName(jwtDialog.FileName), false, fileContent); + if (createdId != null) + { + identities.Add(ZitiIdentity.FromClient(createdId)); + MessageBox.Show("New identity added with fingerprint: " + createdId.FingerPrint); + updateViewWithIdentity(createdId); + } + else + { + // Jeremy buddy - error popup here + MessageBox.Show("created id was null - wtf jeremy. your fault"); + } + } + catch (ServiceException se) + { + MessageBox.Show(se.AdditionalInfo, se.Message); + } + catch (Exception ex) + { + MessageBox.Show("Unexpected error", ex.Message); + } + LoadIdentities(); } } @@ -144,7 +177,7 @@ namespace ZitiTunneler { { serviceClient.SetTunnelState(true); } - catch (ServiceClient.ServiceException se) + catch (ServiceException se) { MessageBox.Show(se.AdditionalInfo, se.Message); } @@ -175,7 +208,7 @@ namespace ZitiTunneler { { serviceClient.SetTunnelState(false); } - catch (ServiceClient.ServiceException se) + catch (ServiceException se) { MessageBox.Show(se.AdditionalInfo, se.Message); } diff --git a/ZitiTunneler/ServiceClient/ClientImpl.cs b/ZitiTunneler/ServiceClient/ClientImpl.cs index e807f22f..75d4ab08 100644 --- a/ZitiTunneler/ServiceClient/ClientImpl.cs +++ b/ZitiTunneler/ServiceClient/ClientImpl.cs @@ -24,7 +24,7 @@ namespace ZitiTunneler.ServiceClient const string logPipe = @"NetFoundry\tunneler\logs"; const string localPipeServer = "."; const PipeDirection inOut = PipeDirection.InOut; - const int ServiceConnectTimeout = 5000; + const int ServiceConnectTimeout = 500; NamedPipeClientStream pipeClient = null; StreamWriter writer = null; @@ -40,22 +40,52 @@ namespace ZitiTunneler.ServiceClient { lock (namedPipeSyncLock) { - if(pipeClient != null) + if (pipeClient != null) { pipeClient.Dispose(); } pipeClient = new NamedPipeClientStream(localPipeServer, ipcPipe, inOut); writer = new StreamWriter(pipeClient); reader = new StreamReader(pipeClient); - pipeClient.Connect(ServiceConnectTimeout); + try + { + pipeClient.Connect(ServiceConnectTimeout); + } + catch + { + //todo: better error + Debug.WriteLine("There was a problem connecting to the service"); + reader.Close(); + try + { + writer.Close(); + } + catch + { + //intentionally ignored + } + pipeClient.Close(); + reader = null; + writer = null; + pipeClient = null; + } } } public ZitiTunnelStatus GetStatus() { - send(new ServiceFunction() { Function = "Status" }); - var rtn = read(); - return rtn; + try + { + send(new ServiceFunction() { Function = "Status" }); + var rtn = read(); + return rtn; + } + catch (IOException ioe) + { + //almost certainly a problem with the pipe - recreate the pipe... + setupPipe(); + throw ioe; + } } ServiceFunction AddIdentityFunction = new ServiceFunction() { Function = "AddIdentity" }; @@ -78,21 +108,18 @@ namespace ZitiTunneler.ServiceClient } }; - - //serializer.Serialize(Console.Out, newId); send(AddIdentityFunction); send(newId); var resp = read(); - //NewIdentityResponse nidresp = (NewIdentityResponse)serializer.Deserialize(sr, typeof(NewIdentityResponse)); Debug.WriteLine(resp.ToString()); return resp.Payload; } - catch(IOException ioe) + catch (IOException ioe) { //almost certainly a problem with the pipe - recreate the pipe... setupPipe(); - throw ioe; + throw; } } @@ -140,26 +167,34 @@ namespace ZitiTunneler.ServiceClient public string GetLogs() { - NamedPipeClientStream logClient = new NamedPipeClientStream(localPipeServer, logPipe, PipeDirection.In); - reader = new StreamReader(logClient); - logClient.Connect(ServiceConnectTimeout); - /* - string line = null; - System.Text.StringBuilder sb = new System.Text.StringBuilder(); - do + try { - line = reader.ReadLine(); - sb.Append(line); - sb.Append("\r\n"); - Debug.WriteLine(line); - } while (line != null); - */ - string content = reader.ReadToEnd(); - //string content = sb.ToString(); - //ugly hack to turn ansi escaping to not... _bleck_ - //todo: fix this :point_up: - content = new System.Text.RegularExpressions.Regex(@"\x1B\[[^@-~]*[@-~]").Replace(content, ""); - return content; + NamedPipeClientStream logClient = new NamedPipeClientStream(localPipeServer, logPipe, PipeDirection.In); + reader = new StreamReader(logClient); + logClient.Connect(ServiceConnectTimeout); + /* + string line = null; + System.Text.StringBuilder sb = new System.Text.StringBuilder(); + do + { + line = reader.ReadLine(); + sb.Append(line); + sb.Append("\r\n"); + Debug.WriteLine(line); + } while (line != null); + */ + string content = reader.ReadToEnd(); + //string content = sb.ToString(); + //ugly hack to turn ansi escaping to not... _bleck_ + //todo: fix this :point_up: + content = new System.Text.RegularExpressions.Regex(@"\x1B\[[^@-~]*[@-~]").Replace(content, ""); + return content; + } + catch + { + //almost certainly a problem with the pipe - probably means the service is NOT running + return "Error fetching logs from service. Is it running?"; + } } public void IdentityOnOff(Identity id) @@ -320,14 +355,23 @@ namespace ZitiTunneler.ServiceClient string json = JsonConvert.SerializeObject(objToSend, Formatting.Indented); Debug.WriteLine(json); - serializer.Serialize(writer, objToSend); + try + { + serializer.Serialize(writer, objToSend); + } + catch + { + //if this fails it's usually because the writer is null/invalid. throwing IOException + //will trigger the pipe to rebuild + throw new IOException("Unexpected error when sending data to service"); + } writer.Flush(); } private T read() where T : SvcResponse { T resp = (T)serializer.Deserialize(reader, typeof(T)); - if(resp.Code != 0) + if (resp.Code != 0) { throw new ServiceException(resp.Message, resp.Code, resp.Error); } diff --git a/wintun-testing/winio/goland.run/go build wintun-testing main.go.run.xml b/wintun-testing/winio/goland.run/go build wintun-testing main.go.run.xml index a872cf1e..2a60b33a 100644 --- a/wintun-testing/winio/goland.run/go build wintun-testing main.go.run.xml +++ b/wintun-testing/winio/goland.run/go build wintun-testing main.go.run.xml @@ -1,7 +1,7 @@ - +