diff --git a/ZitiTunneler/MainWindow.xaml.cs b/ZitiTunneler/MainWindow.xaml.cs index 7b48ae27..22f55faf 100644 --- a/ZitiTunneler/MainWindow.xaml.cs +++ b/ZitiTunneler/MainWindow.xaml.cs @@ -72,7 +72,7 @@ namespace ZitiTunneler { // add a new service client serviceClient = new Client(); serviceClient.OnTunnelStatusUpdate += ServiceClient_OnTunnelStatusUpdated; - serviceClient.OnMetricsUpdate += ServiceClient_OnMetricsUpdate; + Application.Current.Properties.Add("ServiceClient", serviceClient); Application.Current.Properties.Add("Identities", new List()); MainMenu.OnAttachmentChange += AttachmentChanged; diff --git a/ZitiTunneler/ServiceClient/ClientImpl.cs b/ZitiTunneler/ServiceClient/ClientImpl.cs index 53694563..183af891 100644 --- a/ZitiTunneler/ServiceClient/ClientImpl.cs +++ b/ZitiTunneler/ServiceClient/ClientImpl.cs @@ -24,7 +24,6 @@ namespace ZitiTunneler.ServiceClient internal class Client { public event EventHandler OnTunnelStatusUpdate; - public event EventHandler OnMetricsUpdate; protected virtual void ZitiTunnelStatusUpdate(TunnelStatus e) { @@ -34,14 +33,6 @@ namespace ZitiTunneler.ServiceClient handler(this, e); } } - protected virtual void MetricsUpdate(Metrics e) - { - EventHandler handler = OnMetricsUpdate; - if (handler != null) - { - handler(this, e); - } - } JsonSerializer serializer = new JsonSerializer(); @@ -123,10 +114,6 @@ namespace ZitiTunneler.ServiceClient { ZitiTunnelStatusUpdate(r.Payload.Status); } - if (r?.Payload?.Metrics != null) - { - MetricsUpdate(r.Payload.Metrics); - } } Console.WriteLine("THREAD DONE"); }); diff --git a/ZitiTunneler/ServiceClient/DataStructures.cs b/ZitiTunneler/ServiceClient/DataStructures.cs index 9a868f53..bc6b0ebe 100644 --- a/ZitiTunneler/ServiceClient/DataStructures.cs +++ b/ZitiTunneler/ServiceClient/DataStructures.cs @@ -28,7 +28,8 @@ namespace ZitiTunneler.ServiceClient { public string Operation { get; set; } public TunnelStatus Status { get; set; } - public Metrics Metrics { get; set; } + + public Identity NewIdentity { get; set; } } public class NewIdentity diff --git a/wintun-testing/build.bat b/wintun-testing/build.bat index a14f0285..dd5458ce 100644 --- a/wintun-testing/build.bat +++ b/wintun-testing/build.bat @@ -13,5 +13,8 @@ echo COPIED dlls to . set CGO_CFLAGS=-DNOGDI -I %TUNNELER_SDK_DIR%\install\include set CGO_LDFLAGS=-L %TUNNELER_SDK_DIR%\install\lib -go build -a ./ziti-wintun +REM go build -a ./ziti-wintun go build -a ./ziti-tunnel + +copy *.dll c:\temp\ziti-windows-tunneler\ +copy ziti-tunnel.exe c:\temp\ziti-windows-tunneler\ \ No newline at end of file diff --git a/wintun-testing/cziti/dns-proxy.go b/wintun-testing/cziti/dns-proxy.go index 98372738..56f7ca67 100644 --- a/wintun-testing/cziti/dns-proxy.go +++ b/wintun-testing/cziti/dns-proxy.go @@ -196,7 +196,7 @@ func runDNSproxy(dnsServers []string) { // fmt.Printf("proxy resolved %+v for %v\n\n", reply, req.peer) req.s.WriteMsgUDP(rep, nil, req.peer) } else { - log.Debug("matching request was not found for ", + log.Trace("matching request was not found for ", dns.Type(reply.Question[0].Qtype), reply.Question[0].Name) } } diff --git a/wintun-testing/cziti/sdk.go b/wintun-testing/cziti/sdk.go index 612fe748..3bfae059 100644 --- a/wintun-testing/cziti/sdk.go +++ b/wintun-testing/cziti/sdk.go @@ -19,11 +19,24 @@ import ( "unsafe" ) +const ( + ADDED = "added" + REMOVED = "removed" +) + +var ServiceChanges = make(chan ServiceChange) var log = pfxlog.Logger() type sdk struct { libuvCtx *C.libuv_ctx } +type ServiceChange struct { + Operation string + Servicename string + Host string + Port int + NFContext *CZitiCtx +} var _impl sdk @@ -108,6 +121,11 @@ func serviceCB(nf C.nf_context, service *C.ziti_service, status C.int, data unsa if status == C.ZITI_SERVICE_UNAVAILABLE { DNS.DeregisterService(ctx, name) delete(*ctx.Services, name) + ServiceChanges <- ServiceChange{ + Operation: REMOVED, + Servicename: name, + NFContext: ctx, + } } else if status == C.ZITI_OK { cfg := C.ziti_service_get_raw_config(service, tunCfgName) @@ -136,6 +154,13 @@ func serviceCB(nf C.nf_context, service *C.ziti_service, status C.int, data unsa for _, t := range devMap { t.AddIntercept(name, ip.String(), port, unsafe.Pointer(ctx.nf)) } + ServiceChanges <- ServiceChange{ + Operation: ADDED, + Servicename: name, + Host: ip.String(), + Port: port, + NFContext: ctx, + } } } } diff --git a/wintun-testing/ziti-tunnel/service/ipc.go b/wintun-testing/ziti-tunnel/service/ipc.go index 84bf5418..0c196804 100644 --- a/wintun-testing/ziti-tunnel/service/ipc.go +++ b/wintun-testing/ziti-tunnel/service/ipc.go @@ -36,6 +36,8 @@ func (p *Pipes) Close() { p.events.Close() } +var shutdown = make(chan bool) //a channel informing go routines to exit + func SubMain(ops <-chan string, changes chan<- svc.Status) error { defer close(top.Broadcast) log.Info("============================== service begins ==============================") @@ -73,11 +75,10 @@ func SubMain(ops <-chan string, changes chan<- svc.Status) error { // setup metrics notifier every5s := time.NewTicker(5 * time.Second) - every5sDone := make(chan bool) go func() { for { select { - case <-every5sDone: + case <-shutdown: return case <-every5s.C: /* @@ -100,6 +101,9 @@ func SubMain(ops <-chan string, changes chan<- svc.Status) error { } }() + //listen for services that show up + go acceptServices() + // notify the service is running changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} _ = globals.Elog.Info(InformationEvent, SvcName+" status set to running") @@ -107,8 +111,8 @@ func SubMain(ops <-chan string, changes chan<- svc.Status) error { waitForStopRequest(ops) - // stop the metrics ticker - every5sDone <- true + shutdown <- true // stop the metrics ticker + shutdown <- true // stop the service change listener pipes.shutdownConnections() @@ -669,4 +673,40 @@ func logsPipeName() string { func eventsPipeName() string { return pipeName("events") +} + +func acceptServices() { + for { + select { + case <-shutdown: + return + case c := <-cziti.ServiceChanges: + matched := false + //find the id using the context + for _, id := range activeIds { + if id.NFContext == c.NFContext { + matched = true + switch c.Operation { + case cziti.ADDED: + //add the service to the identity + id.Services = append(id.Services, &dto.Service{ + Name: c.Servicename, + HostName: c.Host, + Port: uint16(c.Port), + }) + case cziti.REMOVED: + for idx, svc := range id.Services { + if svc.Name == c.Servicename { + id.Services = append(id.Services[:idx], id.Services[idx+1:]...) + } + } + } + } + } + + if !matched { + log.Warnf("service update received but matched no context. this is unexpected. service name: %s", c.Servicename) + } + } + } } \ No newline at end of file diff --git a/wintun-testing/ziti-tunnel/service/package-variables.go b/wintun-testing/ziti-tunnel/service/package-variables.go deleted file mode 100644 index 85f5ec4c..00000000 --- a/wintun-testing/ziti-tunnel/service/package-variables.go +++ /dev/null @@ -1,3 +0,0 @@ -package service - - diff --git a/wintun-testing/ziti-tunnel/service/state.go b/wintun-testing/ziti-tunnel/service/state.go index 222ad630..a699151f 100644 --- a/wintun-testing/ziti-tunnel/service/state.go +++ b/wintun-testing/ziti-tunnel/service/state.go @@ -85,7 +85,7 @@ func (t *RuntimeState) ToStatus() dto.TunnelStatus { IpInfo: t.state.IpInfo, } for i, id := range t.state.Identities { - log.Debugf("returning clean identity: %s", id.Name) + log.Tracef("returning clean identity: %s", id.Name) cid := idutil.Clean(*id) clean.Identities[i] = &cid } @@ -176,19 +176,8 @@ func (t *RuntimeState) LoadIdentity(id *dto.Identity) { log.Debugf("name changed from %s to %s", id.Name, ctx.Name()) id.Name = ctx.Name() - - if ctx.Services != nil { - log.Debug("ranging over services...") - id.Services = make([]*dto.Service, 0) - for _, svc := range *ctx.Services { - id.Services = append(id.Services, &dto.Service{ - Name: svc.Name, - HostName: svc.InterceptHost, - Port: uint16(svc.InterceptPort)}) - } - } else { - log.Warnf("no services to load for service name: %s", ctx.Name()) - } + id.Services = make([]*dto.Service, 0) + } else { log.Warnf("NOZITI set to true. this should be only used for debugging") }