latest metric stuff, allow callbacks to fill out the services available. start plumbing services that show up back to the ui

This commit is contained in:
dovholuknf
2020-05-07 23:51:25 -04:00
parent bd8c2bd1f7
commit d3e63cc5fe
9 changed files with 80 additions and 38 deletions
+1 -1
View File
@@ -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<ZitiIdentity>());
MainMenu.OnAttachmentChange += AttachmentChanged;
-13
View File
@@ -24,7 +24,6 @@ namespace ZitiTunneler.ServiceClient
internal class Client
{
public event EventHandler<TunnelStatus> OnTunnelStatusUpdate;
public event EventHandler<Metrics> OnMetricsUpdate;
protected virtual void ZitiTunnelStatusUpdate(TunnelStatus e)
{
@@ -34,14 +33,6 @@ namespace ZitiTunneler.ServiceClient
handler(this, e);
}
}
protected virtual void MetricsUpdate(Metrics e)
{
EventHandler<Metrics> 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");
});
+2 -1
View File
@@ -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
+4 -1
View File
@@ -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\
+1 -1
View File
@@ -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)
}
}
+25
View File
@@ -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,
}
}
}
}
+44 -4
View File
@@ -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)
}
}
}
}
@@ -1,3 +0,0 @@
package service
+3 -14
View File
@@ -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")
}