From 6da771305d166af6f10396dbb327f63ffbec4b24 Mon Sep 17 00:00:00 2001 From: dovholuknf <46322585+dovholuknf@users.noreply.github.com> Date: Fri, 9 Oct 2020 13:11:06 -0400 Subject: [PATCH] refactor globals -> constants/logging change ip and mask to 100.64.0.0/10 --- service/cziti/dns.go | 7 ++- service/ziti-tunnel/constants/consts.go | 7 +++ service/ziti-tunnel/main.go | 16 +++---- service/ziti-tunnel/service/ipc.go | 46 ++++++++++--------- service/ziti-tunnel/service/pkg-vars.go | 8 ++-- service/ziti-tunnel/service/service.go | 24 +++++----- service/ziti-tunnel/service/state.go | 11 +++-- .../{ipchanges => util}/detect-ip-changes.go | 2 +- .../ziti-tunnel/{ => util}/idutil/identity.go | 0 .../{globals => util/logging}/loghelper.go | 2 +- 10 files changed, 66 insertions(+), 57 deletions(-) create mode 100644 service/ziti-tunnel/constants/consts.go rename service/ziti-tunnel/{ipchanges => util}/detect-ip-changes.go (99%) rename service/ziti-tunnel/{ => util}/idutil/identity.go (100%) rename service/ziti-tunnel/{globals => util/logging}/loghelper.go (99%) diff --git a/service/cziti/dns.go b/service/cziti/dns.go index 1eb2834e..43c4a030 100644 --- a/service/cziti/dns.go +++ b/service/cziti/dns.go @@ -21,6 +21,8 @@ import "C" import ( "encoding/binary" "fmt" + "github.com/openziti/desktop-edge-win/service/ziti-tunnel/constants" + "net" "strings" "sync" @@ -35,9 +37,6 @@ type DnsManager interface { GetService(ip net.IP, port uint16) (*CZitiCtx, string, error) } -const defaultCidr = "169.254.0.0" -const defaultMaskBits = 16 - var initOnce = sync.Once{} var DNS = &dnsImpl{} @@ -80,7 +79,7 @@ func normalizeDnsName(dnsName string) string { // assigned to a hostname an error will also be returned indicating why. func (dns *dnsImpl) RegisterService(svcId string, dnsNameToReg string, port uint16, ctx *CZitiCtx, svcName string) (net.IP, error) { log.Infof("adding DNS entry for service name %s@%s:%d", svcName, dnsNameToReg, port) - DnsInit(defaultCidr, defaultMaskBits) + DnsInit(constants.Ipv4ip, constants.Ipv4DefaultMask) dnsName := normalizeDnsName(dnsNameToReg) key := fmt.Sprint(dnsName, ':', port) diff --git a/service/ziti-tunnel/constants/consts.go b/service/ziti-tunnel/constants/consts.go new file mode 100644 index 00000000..809cb599 --- /dev/null +++ b/service/ziti-tunnel/constants/consts.go @@ -0,0 +1,7 @@ +package constants + +const( + Ipv4ip = "100.64.0.0" + Ipv4MaxMask = 10 + Ipv4DefaultMask = 24 +) \ No newline at end of file diff --git a/service/ziti-tunnel/main.go b/service/ziti-tunnel/main.go index 1f868bdc..ac96141f 100644 --- a/service/ziti-tunnel/main.go +++ b/service/ziti-tunnel/main.go @@ -21,17 +21,17 @@ package main import ( "fmt" "github.com/openziti/desktop-edge-win/service/ziti-tunnel/dto" + "github.com/openziti/desktop-edge-win/service/ziti-tunnel/util/logging" "golang.org/x/sys/windows/svc/debug" "golang.org/x/sys/windows/svc/eventlog" "os" "strings" - "github.com/openziti/desktop-edge-win/service/ziti-tunnel/globals" - "golang.org/x/sys/windows/svc" "github.com/openziti/desktop-edge-win/service/ziti-tunnel/service" + "golang.org/x/sys/windows/svc" ) -var log = globals.Logger() +var log = logging.Logger() func main() { service.Version = dto.ServiceVersion{ @@ -39,7 +39,7 @@ func main() { Revision: Revision, BuildDate: BuildDate, } - globals.InitLogger("info") + logging.InitLogger("info") //if len(os.Args) < 2 { // // if no args supplied - expect to run as service @@ -57,9 +57,9 @@ func main() { var err error if hasArgs { - globals.Elog = debug.New(service.SvcName) + logging.Elog = debug.New(service.SvcName) } else { - globals.Elog, err = eventlog.Open(service.SvcName) + logging.Elog, err = eventlog.Open(service.SvcName) if err != nil { return } @@ -74,9 +74,9 @@ func main() { usage("no command specified") } - defer globals.Elog.Close() + defer logging.Elog.Close() - elog := globals.Elog + elog := logging.Elog cmd := strings.ToLower(os.Args[1]) switch cmd { diff --git a/service/ziti-tunnel/service/ipc.go b/service/ziti-tunnel/service/ipc.go index 96b39d0c..037c01bb 100644 --- a/service/ziti-tunnel/service/ipc.go +++ b/service/ziti-tunnel/service/ipc.go @@ -23,14 +23,16 @@ import ( "encoding/json" "fmt" "github.com/Microsoft/go-winio" + "github.com/openziti/desktop-edge-win/service/ziti-tunnel/constants" + "github.com/openziti/desktop-edge-win/service/ziti-tunnel/util/idutil" + "github.com/openziti/desktop-edge-win/service/ziti-tunnel/util/logging" "github.com/openziti/foundation/identity/identity" idcfg "github.com/openziti/sdk-golang/ziti/config" "github.com/openziti/sdk-golang/ziti/enroll" "github.com/openziti/desktop-edge-win/service/cziti" "github.com/openziti/desktop-edge-win/service/ziti-tunnel/config" "github.com/openziti/desktop-edge-win/service/ziti-tunnel/dto" - "github.com/openziti/desktop-edge-win/service/ziti-tunnel/globals" - "github.com/openziti/desktop-edge-win/service/ziti-tunnel/idutil" + "github.com/sirupsen/logrus" "golang.org/x/sys/windows/svc" "golang.zx2c4.com/wireguard/tun" "io" @@ -63,11 +65,11 @@ func SubMain(ops chan string, changes chan<- svc.Status) error { rts.LoadConfig() l := rts.state.LogLevel - _, czitiLevel := globals.ParseLevel(l) + _, czitiLevel := logging.ParseLevel(l) - globals.InitLogger(l) + logging.InitLogger(l ) - _ = globals.Elog.Info(InformationEvent, SvcName+" starting. log file located at "+config.LogFile()) + _ = logging.Elog.Info(InformationEvent, SvcName+" starting. log file located at "+config.LogFile()) // create a channel for notifying any connections that they are to be interrupted interrupt = make(chan struct{}, 8) @@ -100,7 +102,7 @@ func SubMain(ops chan string, changes chan<- svc.Status) error { // notify the service is running changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} - _ = globals.Elog.Info(InformationEvent, SvcName+" status set to running") + _ = logging.Elog.Info(InformationEvent, SvcName+" status set to running") log.Info(SvcName + " status set to running. starting cancel loop") waitForStopRequest(ops) @@ -245,12 +247,12 @@ func initialize(ipv4 string, ipv4mask int) error { func setTunInfo(s *dto.TunnelStatus, ipv4 string, ipv4mask int) { if strings.TrimSpace(ipv4) == "" { log.Infof("ip not provided using default: %v", ipv4) - ipv4 = Ipv4ip + ipv4 = constants.Ipv4ip rts.UpdateIpv4(ipv4) } - if ipv4mask < 8 || ipv4mask > 24 { - log.Warnf("provided mask is invalid: %d. using default value: %d", ipv4mask, Ipv4mask) - ipv4mask = Ipv4mask + if ipv4mask < 8 || ipv4mask > constants.Ipv4MaxMask { + log.Warnf("provided mask is invalid: %d. using default value: %d", ipv4mask, constants.Ipv4DefaultMask) + ipv4mask = constants.Ipv4DefaultMask rts.UpdateIpv4Mask(ipv4mask) } _, ipnet, err := net.ParseCIDR(fmt.Sprintf("%s/%d", ipv4, ipv4mask)) @@ -295,7 +297,7 @@ func accept(p net.Listener, serveFunction func(net.Conn), debug string) { c, err := p.Accept() if err != nil { if err != winio.ErrPipeListenerClosed { - log.Errorf("unexpected error while accepting a connection. exiting loop. %v", err) + log.Errorf("%v unexpected error while accepting a connection. exiting loop. %v", debug, err) } return } @@ -332,7 +334,7 @@ func serveIpc(conn net.Conn) { select { case <-interrupt: log.Info("request to interrupt read loop received") - conn.Close() + _ = conn.Close() log.Info("read loop interrupted") case <-done: log.Debug("loop finished normally") @@ -430,8 +432,10 @@ func serveIpc(conn net.Conn) { } func setLogLevel(out *json.Encoder, level string) { - goLevel, cLevel := globals.ParseLevel(level) - globals.SetLogLevel(goLevel, cLevel) + goLevel, cLevel := logging.ParseLevel(level) + logrus.Infof("Setting logger levels to %s", goLevel) + logrus.SetLevel(goLevel) + cziti.SetLogLevel(cLevel) rts.state.LogLevel = goLevel.String() respond(out, dto.Response{Message: "log level set", Code: SUCCESS, Error: "", Payload: nil}) } @@ -600,7 +604,7 @@ func toggleIdentity(out *json.Encoder, fingerprint string, onOff bool) { if onOff { connectIdentity(id) } else { - disconnectIdentity(id) + _ = disconnectIdentity(id) } respond(out, dto.Response{Message: "identity toggled", Code: SUCCESS, Error: "", Payload: idutil.Clean(*id)}) @@ -846,7 +850,7 @@ func acceptServices() { switch c.Operation { case cziti.ADDED: //add the service to the identity - svc := dto.Service{ + s := dto.Service{ Name: c.Service.Name, InterceptHost: c.Service.InterceptHost, InterceptPort: c.Service.InterceptPort, @@ -854,22 +858,22 @@ func acceptServices() { AssignedIP: c.Service.AssignedIP, OwnsIntercept: c.Service.OwnsIntercept, } - id.Services = append(id.Services, &svc) + id.Services = append(id.Services, &s) events.broadcast <- dto.ServiceEvent{ ActionEvent: SERVICE_ADDED, Fingerprint: id.FingerPrint, - Service: svc, + Service: s, } log.Debug("dispatched added service change event") case cziti.REMOVED: - for idx, svc := range id.Services { - if svc.Name == c.Service.Name { + for idx, s := range id.Services { + if s.Name == c.Service.Name { id.Services = append(id.Services[:idx], id.Services[idx+1:]...) events.broadcast <- dto.ServiceEvent{ ActionEvent: SERVICE_REMOVED, Fingerprint: id.FingerPrint, - Service: *svc, + Service: *s, } log.Debug(" dispatched remove service change event") } diff --git a/service/ziti-tunnel/service/pkg-vars.go b/service/ziti-tunnel/service/pkg-vars.go index e757f2cc..de1934da 100644 --- a/service/ziti-tunnel/service/pkg-vars.go +++ b/service/ziti-tunnel/service/pkg-vars.go @@ -18,11 +18,11 @@ package service import ( + "github.com/openziti/desktop-edge-win/service/ziti-tunnel/dto" + "github.com/openziti/desktop-edge-win/service/ziti-tunnel/util/logging" "golang.org/x/sys/windows/svc" "sync" "time" - "github.com/openziti/desktop-edge-win/service/ziti-tunnel/dto" - "github.com/openziti/desktop-edge-win/service/ziti-tunnel/globals" ) var Version dto.ServiceVersion var pipeBase = `\\.\pipe\NetFoundry\tunneler\` @@ -39,7 +39,7 @@ var eventsConnections int var Debug bool var TunStarted time.Time -var log = globals.Logger() +var log = logging.Logger() var events = newTopic() @@ -87,8 +87,6 @@ const ( NF_GROUP_NAME = "NetFoundry Tunneler Users" TunName = "ZitiTUN" - Ipv4ip = "169.254.1.1" - Ipv4mask = 24 Ipv4dns = "127.0.0.1" // use lo -- don't pass DNS queries through tunneler SDK // IPv6 CIDR fe80:6e66:7a69:7469::/64 diff --git a/service/ziti-tunnel/service/service.go b/service/ziti-tunnel/service/service.go index 97b5e2a6..a9b52130 100644 --- a/service/ziti-tunnel/service/service.go +++ b/service/ziti-tunnel/service/service.go @@ -21,9 +21,9 @@ package service import ( "fmt" + "github.com/openziti/desktop-edge-win/service/ziti-tunnel/util/logging" "golang.org/x/sys/windows/svc" "golang.org/x/sys/windows/svc/debug" - "github.com/openziti/desktop-edge-win/service/ziti-tunnel/globals" ) type zitiService struct{} @@ -51,26 +51,26 @@ loop: switch c.Cmd { case svc.Interrogate: changes <- c.CurrentStatus - _ = globals.Elog.Info(InterrogateEvent, "interrogate request received") + _ = logging.Elog.Info(InterrogateEvent, "interrogate request received") changes <- c.CurrentStatus case svc.Stop: - _ = globals.Elog.Info(StopEvent, "issuing stop") + _ = logging.Elog.Info(StopEvent, "issuing stop") control <- "stop" - _ = globals.Elog.Info(StopEvent, "stop issued") + _ = logging.Elog.Info(StopEvent, "stop issued") break loop case svc.Shutdown: - _ = globals.Elog.Info(ShutdownEvent, "issuing shutdown") + _ = logging.Elog.Info(ShutdownEvent, "issuing shutdown") control <- "stop" - _ = globals.Elog.Info(ShutdownEvent, "shutdown issued") + _ = logging.Elog.Info(ShutdownEvent, "shutdown issued") break loop case svc.Pause: changes <- svc.Status{State: svc.Paused, Accepts: cmdsAccepted} - _ = globals.Elog.Info(PauseEvent, "request to pause service received. todo - unimplemented") + _ = logging.Elog.Info(PauseEvent, "request to pause service received. todo - unimplemented") case svc.Continue: changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} - _ = globals.Elog.Info(ContinueEvent, "request to continue service received. todo - unimplemented") + _ = logging.Elog.Info(ContinueEvent, "request to continue service received. todo - unimplemented") default: - _ = globals.Elog.Error(1, fmt.Sprintf("unexpected control request #%d", c)) + _ = logging.Elog.Error(1, fmt.Sprintf("unexpected control request #%d", c)) } } } @@ -85,7 +85,7 @@ loop: } func RunService(isDebug bool) { - _ = globals.Elog.Info(InformationEvent, fmt.Sprintf("starting %s service", SvcStartName)) + _ = logging.Elog.Info(InformationEvent, fmt.Sprintf("starting %s service", SvcStartName)) run := svc.Run if isDebug { log.Info("debug specified. using debug.run") @@ -94,8 +94,8 @@ func RunService(isDebug bool) { err := run(SvcStartName, &zitiService{}) if err != nil { - _ = globals.Elog.Error(ErrorEvent, fmt.Sprintf("%s service failed: %v", SvcStartName, err)) + _ = logging.Elog.Error(ErrorEvent, fmt.Sprintf("%s service failed: %v", SvcStartName, err)) return } - _ = globals.Elog.Info(StopEvent, fmt.Sprintf("%s service stopped", SvcStartName)) + _ = logging.Elog.Info(StopEvent, fmt.Sprintf("%s service stopped", SvcStartName)) } diff --git a/service/ziti-tunnel/service/state.go b/service/ziti-tunnel/service/state.go index 5956a3ec..d7a3aa90 100644 --- a/service/ziti-tunnel/service/state.go +++ b/service/ziti-tunnel/service/state.go @@ -23,8 +23,9 @@ import ( "fmt" "github.com/openziti/desktop-edge-win/service/cziti" "github.com/openziti/desktop-edge-win/service/ziti-tunnel/config" + "github.com/openziti/desktop-edge-win/service/ziti-tunnel/constants" "github.com/openziti/desktop-edge-win/service/ziti-tunnel/dto" - "github.com/openziti/desktop-edge-win/service/ziti-tunnel/idutil" + "github.com/openziti/desktop-edge-win/service/ziti-tunnel/util/idutil" "golang.org/x/sys/windows/registry" "golang.zx2c4.com/wireguard/tun" "golang.zx2c4.com/wireguard/windows/tunnel/winipcfg" @@ -147,11 +148,11 @@ func (t *RuntimeState) CreateTun(ipv4 string, ipv4mask int) error { if strings.TrimSpace(ipv4) == "" { log.Infof("ip not provided using default: %v", ipv4) - ipv4 = Ipv4ip + ipv4 = constants.Ipv4ip } - if ipv4mask < 8 || ipv4mask > 24 { - log.Warnf("provided mask is invalid: %d. using default value: %d", ipv4mask, Ipv4mask) - ipv4mask = Ipv4mask + if ipv4mask < 8 || ipv4mask > constants.Ipv4MaxMask { + log.Warnf("provided mask is invalid: %d. using default value: %d", ipv4mask, constants.Ipv4DefaultMask) + ipv4mask = constants.Ipv4DefaultMask rts.UpdateIpv4Mask(ipv4mask) } ip, ipnet, err := net.ParseCIDR(fmt.Sprintf("%s/%d", ipv4, ipv4mask)) diff --git a/service/ziti-tunnel/ipchanges/detect-ip-changes.go b/service/ziti-tunnel/util/detect-ip-changes.go similarity index 99% rename from service/ziti-tunnel/ipchanges/detect-ip-changes.go rename to service/ziti-tunnel/util/detect-ip-changes.go index 4c7511cb..51623cb2 100644 --- a/service/ziti-tunnel/ipchanges/detect-ip-changes.go +++ b/service/ziti-tunnel/util/detect-ip-changes.go @@ -1,4 +1,4 @@ -package ipchanges +package util import ( "github.com/michaelquigley/pfxlog" diff --git a/service/ziti-tunnel/idutil/identity.go b/service/ziti-tunnel/util/idutil/identity.go similarity index 100% rename from service/ziti-tunnel/idutil/identity.go rename to service/ziti-tunnel/util/idutil/identity.go diff --git a/service/ziti-tunnel/globals/loghelper.go b/service/ziti-tunnel/util/logging/loghelper.go similarity index 99% rename from service/ziti-tunnel/globals/loghelper.go rename to service/ziti-tunnel/util/logging/loghelper.go index 3259555a..28cf343f 100644 --- a/service/ziti-tunnel/globals/loghelper.go +++ b/service/ziti-tunnel/util/logging/loghelper.go @@ -15,7 +15,7 @@ * */ -package globals +package logging import ( rotatelogs "github.com/lestrrat-go/file-rotatelogs"