diff --git a/wintun-testing/ziti-tunnel/ipc/impl.go b/wintun-testing/ziti-tunnel/ipc/impl.go index 0e4b0547..9a21aaeb 100644 --- a/wintun-testing/ziti-tunnel/ipc/impl.go +++ b/wintun-testing/ziti-tunnel/ipc/impl.go @@ -18,7 +18,6 @@ import ( "golang.org/x/sys/windows/svc" "golang.org/x/sys/windows/svc/eventlog" - "wintun-testing/cziti" "wintun-testing/cziti/windns" "wintun-testing/ziti-tunnel/config" "wintun-testing/ziti-tunnel/dto" @@ -55,7 +54,7 @@ func SubMain(ops <- chan string, changes chan<- svc.Status) error { } // listen for log requests go acceptLogs(logs) - log.Info("log listener ready. pipe: %s", logsPipeName) + log.Infof("log listener ready. pipe: %s", logsPipeName) pc2 := winio.PipeConfig{ SecurityDescriptor: auth, @@ -70,7 +69,7 @@ func SubMain(ops <- chan string, changes chan<- svc.Status) error { // listen for ipc messages go acceptIPC(ipc) - log.Info("ipc listener ready pipe: %s", ipcPipeName) + log.Infof("ipc listener ready pipe: %s", ipcPipeName) // initialize the network interface err = initialize() @@ -81,6 +80,7 @@ func SubMain(ops <- chan string, changes chan<- svc.Status) error { // notify the service is running changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} _ = Elog.Info(20, SvcName + " status set to running") + log.Info(SvcName + " status set to running. starting cancel loop") loop: for { @@ -99,12 +99,6 @@ func SubMain(ops <- chan string, changes chan<- svc.Status) error { windns.ResetDNS() - if ! runtime.NoZiti { - cziti.Stop() - } else { - log.Warnf("NOZITI set to true. fix this ") - } - state.Close() _ = ipc.Close() _ = logs.Close() @@ -142,7 +136,7 @@ func acceptIPC(p net.Listener) { } func initialize() error { - log.Debugf("reading config file located at : %s", config.File()) + log.Debugf("reading config file located at: %s", config.File()) file, err := os.OpenFile(config.File(), os.O_RDONLY, 0640) if err != nil { // file does not exist or process has no rights to read the file - return leaving configuration empty @@ -155,15 +149,11 @@ func initialize() error { _ = dec.Decode(&state) - if ! runtime.NoZiti { - err := state.CreateTun() - if err != nil { - return err - } - setTunInfo() - } else { - log.Warnf("NOZITI set to true. fix this ") + err = state.CreateTun() + if err != nil { + return err } + setTunInfo() // decide if the tunnel should be active or not and if so - activate it setTunnelState(state.Active) @@ -540,31 +530,11 @@ func connectIdentity(id *dto.Identity) { //tell the c sdk to use the file from the id and connect log.Infof("Connecting identity: %s", id.Name) - if ! runtime.NoZiti { - if ctx, err := cziti.LoadZiti(id.Path()); err != nil { - log.Panic(err) - } else { - log.Infof("successfully loaded %s@%s\n", ctx.Name(), ctx.Controller()) - } - } else { - log.Warnf("NOZITI set to true. fix this ") - } - + state.LoadIdentity(id) id.Connected = true - - loadServices(id) log.Infof("Connecting identity: %s responded to", id.Name) } -func loadServices(id *dto.Identity) { - id.Services = make([]*dto.Service, 0) - id.Services = append(id.Services, - &dto.Service{Name: "ServiceOne", HostName: "MyServiceName", Port: 1111}, - &dto.Service{Name: "SecondOne", HostName: "SecondService", Port: 2222}, - &dto.Service{Name: "LastDummy Service With Spaces and is very very long", HostName: "10.10.10.10", Port: 3333}, - ) -} - func disconnectIdentity(id *dto.Identity) error { //tell the c sdk to disconnect the identity/services etc log.Infof("Disconnecting identity: %s", id.Name) diff --git a/wintun-testing/ziti-tunnel/runtime/package-variables.go b/wintun-testing/ziti-tunnel/runtime/package-variables.go index 635376ee..18163c02 100644 --- a/wintun-testing/ziti-tunnel/runtime/package-variables.go +++ b/wintun-testing/ziti-tunnel/runtime/package-variables.go @@ -25,4 +25,3 @@ const ipv6ip = "1" const ipv6mask = 64 const Ipv6dns = "::1" // must be in "ipv6ip/ipv6mask" CIDR block -const NoZiti = true \ No newline at end of file diff --git a/wintun-testing/ziti-tunnel/runtime/state.go b/wintun-testing/ziti-tunnel/runtime/state.go index 8baef9fc..8d2db758 100644 --- a/wintun-testing/ziti-tunnel/runtime/state.go +++ b/wintun-testing/ziti-tunnel/runtime/state.go @@ -6,6 +6,7 @@ import ( "fmt" "net" "os" + "strconv" "time" "golang.zx2c4.com/wireguard/tun" @@ -104,18 +105,6 @@ func (t TunnelerState) Clean() TunnelerState { func (t *TunnelerState) CreateTun() error { - wt, err := tun.WintunPool.GetInterface(TunName) - - if err != nil { - return err - } - - if wt != nil { - log.Infof("interface already exists with name: %s", TunName) - //todo: probably need to handle this better by ripping it down and recreating it. will need sdk testing to make sure that works - return nil - } - log.Infof("creating TUN device: %s", TunName) tunDevice, err := tun.CreateTUN(TunName, 64*1024) if err == nil { @@ -129,7 +118,7 @@ func (t *TunnelerState) CreateTun() error { } if name, err := tunDevice.Name(); err == nil { - fmt.Printf("created TUN device [%s]\n", name) + log.Debugf("created TUN device [%s]", name) } nativeTunDevice := tunDevice.(*tun.NativeTun) @@ -138,7 +127,7 @@ func (t *TunnelerState) CreateTun() error { if err != nil { log.Fatal(err) } - fmt.Printf("setting TUN interface address to [%s]\n", ip) + log.Debugf("setting TUN interface address to [%s]", ip) err = luid.SetIPAddresses([]net.IPNet{{ip, ipnet.Mask}}) if err != nil { log.Fatal(err) @@ -156,22 +145,24 @@ func (t *TunnelerState) CreateTun() error { if err != nil { log.Fatal(err) } - fmt.Println("dns servers = ", dns) + log.Debugf("dns servers = %s", dns) - fmt.Printf("routing destination [%s] through [%s]\n", *ipnet, ipnet.IP) + log.Infof("routing destination [%s] through [%s]", *ipnet, ipnet.IP) err = luid.SetRoutes([]*winipcfg.RouteData{{*ipnet, ipnet.IP, 0}}) if err != nil { return err } - fmt.Println("routing applied") + log.Info("routing applied") - fmt.Println("running") - cziti.DnsInit(Ipv4ip, 24) - - cziti.Start() - _, err = cziti.HookupTun(tunDevice, dns) - if err != nil { - panic(err) + if ! noZiti() { + cziti.DnsInit(Ipv4ip, 24) + cziti.Start() + _, err = cziti.HookupTun(tunDevice, dns) + if err != nil { + panic(err) + } + } else { + log.Warnf("NOZITI set to true. this should be only used for debugging") } return nil } @@ -179,10 +170,51 @@ func (t *TunnelerState) CreateTun() error { func (t *TunnelerState) Close() { if t.tun != nil { log.Warn("TODO: actually close the tun - or disable all the identies etc.") -/* err := t.tun.Close() +/* + cziti.Stop() + err := t.tun.Close() if err != nil { log.Fatalf("problem closing tunnel!") } */ } } + +func (t *TunnelerState) LoadIdentity(id *dto.Identity) { + if ! noZiti() { + if ctx, err := cziti.LoadZiti(id.Path()); err != nil { + log.Errorf("error when loading identity %v", err) + } else { + log.Infof("successfully loaded %s@%s", ctx.Name(), ctx.Controller()) + if *ctx.Services!=nil { + id.Services = make([]*dto.Service, 0) + for key, svc := range *ctx.Services { + id.Services = append(id.Services, &dto.Service{ + Name: svc.Name, + HostName: svc.InterceptHost, + Port: uint16(svc.InterceptPort)}) + log.Debugf("key is: %s. same as name: %s", key, svc.Name) + } + } else { + log.Debugf("no services to load for service name: %s", ctx.Name()) + } + } + } else { + log.Warnf("NOZITI set to true. this should be only used for debugging") + //loadDummyServices(id) + } +} + +func noZiti() bool { + v, _ := strconv.ParseBool(os.Getenv("NOZITI")) + return v +} + + +func loadDummyServices(id *dto.Identity) { + id.Services = append(id.Services, + &dto.Service{Name: "ServiceOne", HostName: "MyServiceName", Port: 1111}, + &dto.Service{Name: "SecondOne", HostName: "SecondService", Port: 2222}, + &dto.Service{Name: "LastDummy Service With Spaces and is very very long", HostName: "10.10.10.10", Port: 3333}, + ) +} \ No newline at end of file diff --git a/wintun-testing/ziti-tunnel/service/service.go b/wintun-testing/ziti-tunnel/service/service.go index 928488d0..559046bf 100644 --- a/wintun-testing/ziti-tunnel/service/service.go +++ b/wintun-testing/ziti-tunnel/service/service.go @@ -25,7 +25,7 @@ func (m *zitiService) Execute(args []string, r <-chan svc.ChangeRequest, changes go func() { err := ipc.SubMain(control, changes) if err != nil { - log.Errorf("unexpected error received from ipc subroutine: %v", err) + log.Errorf("the main loop exited with an unexpected error: %v", err) } }() loop: