diff --git a/ZitiTunneler/MainWindow.xaml b/ZitiTunneler/MainWindow.xaml index d2f1c26a..e777c23d 100644 --- a/ZitiTunneler/MainWindow.xaml +++ b/ZitiTunneler/MainWindow.xaml @@ -7,7 +7,7 @@ mc:Ignorable="d" ShowInTaskbar="False" x:Name="MainUI" - Title="MainWindow" Height="520" + Title="ZitiTunneler" Height="520" Width="350" ResizeMode="NoResize" ClipToBounds="False" Icon="Assets/Images/icon.png" AllowsTransparency="True" diff --git a/ZitiTunneler/MainWindow.xaml.cs b/ZitiTunneler/MainWindow.xaml.cs index ef3a22e1..413e15bc 100644 --- a/ZitiTunneler/MainWindow.xaml.cs +++ b/ZitiTunneler/MainWindow.xaml.cs @@ -310,8 +310,11 @@ namespace ZitiTunneler { LoadIdentities(); } private void SetNotifyIcon(string iconPrefix) { - System.IO.Stream iconStream = System.Windows.Application.GetResourceStream(new Uri("pack://application:,,/Assets/Images/ziti-"+iconPrefix+".ico")).Stream; + var iconUri = new Uri("pack://application:,,/Assets/Images/ziti-" + iconPrefix + ".ico"); + System.IO.Stream iconStream = System.Windows.Application.GetResourceStream(iconUri).Stream; notifyIcon.Icon = new System.Drawing.Icon(iconStream); + + Application.Current.MainWindow.Icon = System.Windows.Media.Imaging.BitmapFrame.Create(iconUri); } private void LoadIdentities() { diff --git a/ZitiTunneler/ServiceClient/ClientImpl.cs b/ZitiTunneler/ServiceClient/ClientImpl.cs index 5a443635..d4ce7a6b 100644 --- a/ZitiTunneler/ServiceClient/ClientImpl.cs +++ b/ZitiTunneler/ServiceClient/ClientImpl.cs @@ -340,6 +340,7 @@ namespace ZitiTunneler.ServiceClient public void SetTunnelState(bool onOff) { + /* checkConnected(); try { @@ -352,6 +353,7 @@ namespace ZitiTunneler.ServiceClient //setupPipe(); throw ioe; } + */ } public string GetLogs() diff --git a/get-ziti-ci.bat b/get-ziti-ci.bat index 82f786f1..1d602bac 100644 --- a/get-ziti-ci.bat +++ b/get-ziti-ci.bat @@ -3,12 +3,12 @@ IF "x%ZITI_CI_VERSION%"=="x" GOTO DEFAULT echo Fetching ziti-ci@%ZITI_CI_VERSION% -go get github.com/netfoundry/ziti-ci@%ZITI_CI_VERSION% +go get github.com/netfoundry/ziti-ci@%ZITI_CI_VERSION% 2> NUL GOTO END :DEFAULT echo Fetching default ziti-ci -go get github.com/netfoundry/ziti-ci +go get github.com/netfoundry/ziti-ci 2> NUL GOTO END :END diff --git a/service/build.bat b/service/build.bat index 08e68bb4..c10854ce 100644 --- a/service/build.bat +++ b/service/build.bat @@ -7,6 +7,7 @@ set GO111MODULE=on cd /d %ZITI_TUNNEL_WIN_ROOT% IF "%BUILD_VERSION%"=="" GOTO BUILD_VERSION_ERROR +IF "%1"=="clean" GOTO CLEAN IF "%1"=="quick" GOTO QUICK echo fetching ziti-ci @@ -15,101 +16,115 @@ ziti-ci version echo generating version info - this will pushed from publish.bat in CI ziti-ci generate-build-info --useVersion=false %SVC_ROOT_DIR%/ziti-tunnel/version.go main +echo version info generated +goto QUICK + +:CLEAN +rmdir /s /q deps +del /q %SVC_ROOT_DIR%ziti.dll :QUICK cd %SVC_ROOT_DIR% SET REPO_URL=https://github.com/openziti/ziti-tunneler-sdk-c.git -SET ZITI_TUNNEL_REPO_BRANCH=5s-moving-avg +SET ZITI_TUNNEL_REPO_BRANCH=master SET TUNNELER_SDK_DIR=%SVC_ROOT_DIR%deps\ziti-tunneler-sdk-c\ set CGO_CFLAGS=-DNOGDI -I %TUNNELER_SDK_DIR%install\include set CGO_LDFLAGS=-L %TUNNELER_SDK_DIR%install\lib -if not exist %SVC_ROOT_DIR%ziti.dll ( - set BEFORE_GIT=%cd% - if exist %TUNNELER_SDK_DIR% ( - echo ------------------------------------------------------------------------------ - echo issuing git pull to pick up any changes - echo ------------------------------------------------------------------------------ - git pull - git submodule update --init --recursive - echo ------------------------------------------------------------------------------ - ) else ( - echo ------------------------------------------------------------------------------ - echo cloning %REPO_URL% - echo ------------------------------------------------------------------------------ - mkdir %TUNNELER_SDK_DIR% - git clone %REPO_URL% %TUNNELER_SDK_DIR% --recurse-submodules - ) - IF %ERRORLEVEL% NEQ 0 ( - SET ACTUAL_ERR=%ERRORLEVEL% - echo. - echo Could not pull or clone git repo:%REPO_URL% - echo. - goto FAIL - ) - - echo changing to %TUNNELER_SDK_DIR% - cd %TUNNELER_SDK_DIR% - echo current directory is %CD% - should be %TUNNELER_SDK_DIR% - echo checking out branch: %ZITI_TUNNEL_REPO_BRANCH% - git checkout %ZITI_TUNNEL_REPO_BRANCH% - IF %ERRORLEVEL% NEQ 0 ( - SET ACTUAL_ERR=%ERRORLEVEL% - echo. - echo Could not checkout branch :%ZITI_TUNNEL_REPO_BRANCH% - echo. - goto FAIL - ) - - echo ------------------------------------------------------------------------------ - type %TUNNELER_SDK_DIR%.gitmodules - echo. - echo updating any ssh submodules to https using: - echo powershell "(get-content -path %TUNNELER_SDK_DIR%.gitmodules) -replace 'git@(.*)\:(.*)\.git', 'https://$1/$2.git' | Set-Content -Path %TUNNELER_SDK_DIR%.gitmodules" - echo. - powershell "(get-content -path %TUNNELER_SDK_DIR%.gitmodules) -replace 'git@(.*)\:(.*)\.git', 'https://$1/$2.git' | Set-Content -Path %TUNNELER_SDK_DIR%.gitmodules" - type %TUNNELER_SDK_DIR%.gitmodules - echo ------------------------------------------------------------------------------ - - echo Updating submodules... - git config --get remote.origin.url - git submodule update --init --recursive - IF %ERRORLEVEL% NEQ 0 ( - SET ACTUAL_ERR=%ERRORLEVEL% - echo. - echo Could not update submodules - echo. - goto FAIL - ) - - if not exist %TUNNELER_SDK_DIR%build mkdir %TUNNELER_SDK_DIR%build - if not exist %TUNNELER_SDK_DIR%install mkdir %TUNNELER_SDK_DIR%install - - cmake -G Ninja -S %TUNNELER_SDK_DIR% -B %TUNNELER_SDK_DIR%build -DCMAKE_INSTALL_PREFIX=%TUNNELER_SDK_DIR%install - cmake --build %TUNNELER_SDK_DIR%build --target install - SET ACTUAL_ERR=%ERRORLEVEL% - if %ACTUAL_ERR% NEQ 0 ( - echo. - echo Build of %TUNNELER_SDK_DIR%build failed - echo. - goto FAIL - ) else ( - echo. - echo result of ninja build: %ACTUAL_ERR% - ) - - cp %TUNNELER_SDK_DIR%install\lib\ziti.dll %SVC_ROOT_DIR% - cp %TUNNELER_SDK_DIR%install\lib\libuv.dll %SVC_ROOT_DIR% - - echo COPIED dlls to %SVC_ROOT_DIR% - cd %SVC_ROOT_DIR% -) else ( +if exist %SVC_ROOT_DIR%ziti.dll ( echo ------------------------------------------------------------------------------ echo SKIPPED BUILDING ziti.dll because ziti.dll was found at %SVC_ROOT_DIR%ziti.dll echo ------------------------------------------------------------------------------ + GOTO GOBUILD ) +echo ------------------------------------------------------------------------------ +echo BUILDING ziti.dll begins +echo ------------------------------------------------------------------------------ + +set BEFORE_GIT=%cd% +if exist %TUNNELER_SDK_DIR% ( + echo ------------------------------------------------------------------------------ + echo issuing git pull to pick up any changes + echo ------------------------------------------------------------------------------ + git pull + git submodule update --init --recursive + SET ACTUAL_ERR=%ERRORLEVEL% + echo ------------------------------------------------------------------------------ +) else ( + echo ------------------------------------------------------------------------------ + echo cloning %REPO_URL% + echo ------------------------------------------------------------------------------ + mkdir %TUNNELER_SDK_DIR% + git clone %REPO_URL% %TUNNELER_SDK_DIR% --recurse-submodules + SET ACTUAL_ERR=%ERRORLEVEL% +) +IF %ACTUAL_ERR% NEQ 0 ( + echo. + echo Could not pull or clone git repo:%REPO_URL% + echo. + goto FAIL +) + +echo changing to %TUNNELER_SDK_DIR% +cd %TUNNELER_SDK_DIR% +echo current directory is %CD% - should be %TUNNELER_SDK_DIR% + +echo checking out branch: %ZITI_TUNNEL_REPO_BRANCH% +git checkout %ZITI_TUNNEL_REPO_BRANCH% +IF %ERRORLEVEL% NEQ 0 ( + SET ACTUAL_ERR=%ERRORLEVEL% + echo. + echo Could not checkout branch :%ZITI_TUNNEL_REPO_BRANCH% + echo. + goto FAIL +) + +echo ------------------------------------------------------------------------------ +type %TUNNELER_SDK_DIR%.gitmodules +echo. +echo updating any ssh submodules to https using: +echo powershell "(get-content -path %TUNNELER_SDK_DIR%.gitmodules) -replace 'git@(.*)\:(.*)\.git', 'https://$1/$2.git' | Set-Content -Path %TUNNELER_SDK_DIR%.gitmodules" +echo. +powershell "(get-content -path %TUNNELER_SDK_DIR%.gitmodules) -replace 'git@(.*)\:(.*)\.git', 'https://$1/$2.git' | Set-Content -Path %TUNNELER_SDK_DIR%.gitmodules" +type %TUNNELER_SDK_DIR%.gitmodules +echo ------------------------------------------------------------------------------ + +echo Updating submodules... +git config --get remote.origin.url +git submodule update --init --recursive +IF %ERRORLEVEL% NEQ 0 ( + SET ACTUAL_ERR=%ERRORLEVEL% + echo. + echo Could not update submodules + echo. + goto FAIL +) + +if not exist %TUNNELER_SDK_DIR%build mkdir %TUNNELER_SDK_DIR%build +if not exist %TUNNELER_SDK_DIR%install mkdir %TUNNELER_SDK_DIR%install + +cmake -G Ninja -S %TUNNELER_SDK_DIR% -B %TUNNELER_SDK_DIR%build -DCMAKE_INSTALL_PREFIX=%TUNNELER_SDK_DIR%install +cmake --build %TUNNELER_SDK_DIR%build --target install +SET ACTUAL_ERR=%ERRORLEVEL% +if %ACTUAL_ERR% NEQ 0 ( + echo. + echo Build of %TUNNELER_SDK_DIR%build failed + echo. + goto FAIL +) else ( + echo. + echo result of ninja build: %ACTUAL_ERR% +) + +cp %TUNNELER_SDK_DIR%install\lib\ziti.dll %SVC_ROOT_DIR% +cp %TUNNELER_SDK_DIR%install\lib\libuv.dll %SVC_ROOT_DIR% + +echo COPIED dlls to %SVC_ROOT_DIR% +cd %SVC_ROOT_DIR% + +:GOBUILD echo building the go program go build -a ./ziti-tunnel SET ACTUAL_ERR=%ERRORLEVEL% diff --git a/service/build.sh b/service/build.sh deleted file mode 100644 index e327b06f..00000000 --- a/service/build.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env bash -die() { - echo "Error: $1" - exit 1 -} - -[ -z "$TUNNELER_SDK_DIR" ] && die 'set TUNNELER_SDK_DIR' - -if [[ $(type -P cmake) ]]; then - CMAKE=cmake -elif [ -x "/c/Program Files/Cmake/bin/cmake.exe" ]; then - echo "cmake found in standard location" - CMAKE='/c/Program Files/Cmake/bin/cmake.exe' -fi - -mkdir build -mkdir install - -if [[ ! -f ./build/CMakeCache.txt ]]; then - "${CMAKE}" -G Ninja -S ${TUNNELER_SDK_DIR} -B ./build -DCMAKE_INSTALL_PREFIX=./install -fi -"${CMAKE}" --build ./build --target install - -cp ./install/lib/ziti.dll . -cp ./install/lib/libuv.dll . - -export CGO_CFLAGS="-DNOGDI -I $(cygpath -a -m ./install/include)" -export CGO_LDFLAGS="-L $(cygpath -a -m ./install/lib)" -rm ./ziti-wintun.exe -go build -v ./ziti-wintun - diff --git a/service/cziti/ctun.go b/service/cziti/ctun.go index 1a78512f..3ea04c7c 100644 --- a/service/cziti/ctun.go +++ b/service/cziti/ctun.go @@ -55,7 +55,7 @@ import ( ) type Tunnel interface { - AddIntercept(service string, hostname string, port int, ctx unsafe.Pointer) + AddIntercept(svcId string, service string, hostname string, port int, ctx unsafe.Pointer) } type tunnel struct { @@ -246,23 +246,23 @@ func (t *tunnel) runWriteLoop() { } } -func (t *tunnel) AddIntercept(service string, host string, port int, ctx unsafe.Pointer) { - log.Debug("about to add intercept for: %s, %s, %d", service, host, port) - res := C.ziti_tunneler_intercept_v1(t.tunCtx, ctx, C.CString(service), C.CString(host), C.int(port)) - log.Debug("intercept added", res) +func (t *tunnel) AddIntercept(svcId string, service string, host string, port int, ctx unsafe.Pointer) { + log.Debugf("about to add intercept for: %s, %s, %d", service, host, port) + res := C.ziti_tunneler_intercept_v1(t.tunCtx, ctx, C.CString(svcId), C.CString(service), C.CString(host), C.int(port)) + log.Debugf("intercept added", res) } -func RemoveIntercept(service string) { +func RemoveIntercept(svcvId string) { for _, t := range devMap { - log.Infof("issuing stop intercepting for %s", service) - C.ziti_tunneler_stop_intercepting(t.tunCtx, C.CString(service)) + log.Infof("issuing stop intercepting for service id: %s", svcvId) + C.ziti_tunneler_stop_intercepting(t.tunCtx, C.CString(svcvId)) } } -func AddIntercept(service string, host string, port uint16, ctx *CZitiCtx) { +func AddIntercept(svcId string, service string, host string, port uint16, ctx *CZitiCtx) { for _, t := range devMap { log.Debug("adding intercept for: %s, %s, %d", service, host, port) - res := C.ziti_tunneler_intercept_v1(t.tunCtx, unsafe.Pointer(ctx.nf), C.CString(service), C.CString(host), C.int(port)) - log.Debug("intercept added", res) + res := C.ziti_tunneler_intercept_v1(t.tunCtx, unsafe.Pointer(ctx.nf), C.CString(svcId), C.CString(service), C.CString(host), C.int(port)) + log.Debugf("intercept added", res) } } \ No newline at end of file diff --git a/service/cziti/sdk.go b/service/cziti/sdk.go index 3b2d760f..ae5f6e49 100644 --- a/service/cziti/sdk.go +++ b/service/cziti/sdk.go @@ -49,11 +49,9 @@ type sdk struct { libuvCtx *C.libuv_ctx } type ServiceChange struct { - Operation string - Servicename string - Host string - Port int - NFContext *CZitiCtx + Operation string + Service *Service + NFContext *CZitiCtx } var _impl sdk @@ -137,14 +135,21 @@ func serviceCB(nf C.ziti_context, service *C.ziti_service, status C.int, data un } name := C.GoString(service.name) - log.Tracef("============ INSIDE serviceCB - status: %s - %v, %v, %v ============", name, status, C.ZITI_SERVICE_UNAVAILABLE, C.ZITI_OK) + id := C.GoString(service.id) + log.Debugf("============ INSIDE serviceCB - status: %s:%s - %v, %v, %v ============", name, id, status, C.ZITI_SERVICE_UNAVAILABLE, C.ZITI_OK) if status == C.ZITI_SERVICE_UNAVAILABLE { - DNS.DeregisterService(ctx, name) - ctx.Services.Delete(name) - ServiceChanges <- ServiceChange{ - Operation: REMOVED, - Servicename: name, - NFContext: ctx, + found, ok := ctx.Services.Load(id) + fs := found.(Service) + if ok { + DNS.DeregisterService(ctx, name) + ctx.Services.Delete(id) + ServiceChanges <- ServiceChange{ + Operation: REMOVED, + Service: &fs, + NFContext: ctx, + } + } else { + log.Warnf("could not find a service with id: %s, name: %s", service.id, service.name) } } else if status == C.ZITI_OK { cfg := C.ziti_service_get_raw_config(service, tunCfgName) @@ -159,26 +164,25 @@ func serviceCB(nf C.ziti_context, service *C.ziti_service, status C.int, data un port = int(c["port"].(float64)) } } - ctx.Services.Store(name, Service{ + added := Service{ Name: name, - Id: C.GoString(service.id), + Id: id, InterceptHost: host, InterceptPort: port, - }) + } + ctx.Services.Store(id, added) if host != "" && port != -1 { ip, err := DNS.RegisterService(host, uint16(port), ctx, name) if err != nil { - log.Error(err) + log.Warn(err) } else { log.Infof("service[%s] is mapped to <%s:%d>", name, ip.String(), port) for _, t := range devMap { - t.AddIntercept(name, ip.String(), port, unsafe.Pointer(ctx.nf)) + t.AddIntercept(id, name, ip.String(), port, unsafe.Pointer(ctx.nf)) } ServiceChanges <- ServiceChange{ Operation: ADDED, - Servicename: name, - Host: host, - Port: port, + Service: &added, NFContext: ctx, } } @@ -217,10 +221,8 @@ func LoadZiti(cfg string) *CZitiCtx { ctx.options.config = C.CString(cfg) ctx.options.init_cb = C.ziti_init_cb(C.initCB) ctx.options.service_cb = C.ziti_service_cb(C.serviceCB) - //TODO don't commit this - ctx.options.refresh_interval = C.long(600) - ctx.options.refresh_interval = C.long(6) + ctx.options.refresh_interval = C.long(15) ctx.options.config_types = C.all_configs - //ctx.options.ctx = unsafe.Pointer(&ctx) ch := make(chan *CZitiCtx) initMap[cfg] = ch diff --git a/service/cziti/windns/windns.go b/service/cziti/windns/windns.go index 2900a400..1a27a84b 100644 --- a/service/cziti/windns/windns.go +++ b/service/cziti/windns/windns.go @@ -19,18 +19,18 @@ package windns import ( "bytes" - "fmt" + "github.com/michaelquigley/pfxlog" "net" "os" "os/exec" "strings" - - "github.com/michaelquigley/pfxlog" ) var log = pfxlog.Logger() func ResetDNS() { + log.Infof("skipping ResetDNS for now") + log.Infof("resetting dns...") log.Info("restoring dns to original-ish state") @@ -77,14 +77,85 @@ func GetUpstreamDNS() []string { } func ReplaceDNS(ips []net.IP) { + + /* + ifaces, err := net.Interfaces() + if err != nil { + fmt.Print(fmt.Errorf("localAddresses: %+v\n", err.Error())) + return + } + for _, i := range ifaces { + addrs, err := i.Addrs() + if err != nil { + fmt.Print(fmt.Errorf("localAddresses: %+v\n", err.Error())) + continue + } + for _, a := range addrs { + switch v := a.(type) { + case *net.IPAddr: + fmt.Printf("%v : %s (%s)\n", i.Name, v, v.IP.DefaultMask()) + + case *net.IPNet: + fmt.Printf("%v : %s [%v/%v]\n", i.Name, v, v.IP, v.Mask) + } + + } + } + var names []string for _, i := range ips { names = append(names, i.String()) } addresses := strings.Join(names, ",") + + */ + /* script := fmt.Sprintf( `Get-NetIPInterface | ForEach-Object { Set-DnsClientServerAddress -InterfaceIndex $_.ifIndex -ServerAddresses %s }`, addresses) + */ + + + + script := `$dnsinfo=Get-DnsClientServerAddress + +# see https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.addressfamily +$IPv4=2 +$IPv6=23 + +$dnsUpdates = @{} + +foreach ($dns in $dnsinfo) +{ + if($dnsUpdates[$dns.InterfaceIndex] -eq $null) { $dnsUpdates[$dns.InterfaceIndex]=[System.Collections.ArrayList]@() } + if($dns.AddressFamily -eq $IPv6) { + $dnsServers=$dns.ServerAddresses + $ArrList=[System.Collections.ArrayList]@($dnsServers) + if(($dnsServers -ne $null) -and ($dnsServers.Contains("::1")) ) { + echo ($dns.InterfaceAlias + " IPv6 already contains ::1") + } else { + $ArrList.Insert(0,"::1") + } + $dnsUpdates[$dns.InterfaceIndex].AddRange($ArrList) + } + elseif($dns.AddressFamily -eq $IPv4){ + $dnsServers=$dns.ServerAddresses + $ArrList=[System.Collections.ArrayList]@($dnsServers) + if(($dnsServers -ne $null) -and ($dnsServers.Contains("127.0.0.1")) ) { + echo ($dns.InterfaceAlias + " IPv4 already contains 127.0.0.1") + } else { + $ArrList.Insert(0,"127.0.0.1") + } + $dnsUpdates[$dns.InterfaceIndex].AddRange($ArrList) + } +} + +foreach ($key in $dnsUpdates.Keys) +{ + $dnsServers=$dnsUpdates[$key] + Set-DnsClientServerAddress -InterfaceIndex $key -ServerAddresses ($dnsServers) +}` + cmd := exec.Command("powershell", "-Command", script) cmd.Stderr = os.Stderr cmd.Stdout = os.Stdout diff --git a/service/publish.bat b/service/publish.bat index 4a8948fc..af4fa99b 100644 --- a/service/publish.bat +++ b/service/publish.bat @@ -6,6 +6,16 @@ set /p BUILD_VERSION=<%SVC_ROOT_DIR%..\version IF "%BUILD_VERSION%"=="" GOTO BUILD_VERSION_ERROR call %SVC_ROOT_DIR%\build.bat +SET ACTUAL_ERR=%ERRORLEVEL% +if %ACTUAL_ERR% NEQ 0 ( + echo. + echo call to build.bat failed with %ACTUAL_ERR% + echo. + exit /b 1 +) else ( + echo. + echo result of ninja build: %ACTUAL_ERR% +) IF "%GIT_BRANCH%"=="master" GOTO RELEASE @echo Publishing to snapshot repo @@ -22,18 +32,40 @@ GOTO END @echo The build version environment variable was not set - cannot publish exit /b 1 +:FAIL +IF %~1 NEQ 0 ( + echo ================================================================ + echo. + echo FAILURE: + echo %~2 + echo. + echo ================================================================ + exit /b %~1 +) +exit /b 0 + :END echo configuring git - relies on build.bat successfully grabbing ziti-ci -ziti-ci configure-git +ziti-ci configure-git 2>1 @echo publishing complete - committing version.go as ci -git add service/ziti-tunnel/version.go + +git stash 2>1 +git checkout %GIT_BRANCH% 2>1 +git stash pop 2>1 +CALL :FAIL %ERRORLEVEL% "checkout failed" +@echo git checkout %GIT_BRANCH% complete: %ERRORLEVEL% + +git add service/ziti-tunnel/version.go 2>1 +CALL :FAIL %ERRORLEVEL% "git add failed" @echo git add service/ziti-tunnel/version.go complete: %ERRORLEVEL% -git commit -m "[ci skip] committing updated version information" +git commit -m "[ci skip] committing updated version information" 2>1 +CALL :FAIL %ERRORLEVEL% "git commit failed" @echo git commit -m "[ci skip] committing updated version information" complete: %ERRORLEVEL% -git push +git push 2>1 +CALL :FAIL %ERRORLEVEL% "git push failed" @echo git push complete: %ERRORLEVEL% cd %CURDIR% diff --git a/service/ziti-tunnel/dto/dtos.go b/service/ziti-tunnel/dto/dtos.go index d2f6ed9d..a41f9c08 100644 --- a/service/ziti-tunnel/dto/dtos.go +++ b/service/ziti-tunnel/dto/dtos.go @@ -20,8 +20,8 @@ package dto import ( idcfg "github.com/netfoundry/ziti-sdk-golang/ziti/config" "github.com/netfoundry/ziti-sdk-golang/ziti/enroll" - "log" "github.com/netfoundry/ziti-tunnel-win/service/cziti" + "log" "github.com/netfoundry/ziti-tunnel-win/service/ziti-tunnel/config" ) @@ -34,6 +34,7 @@ type Service struct { Name string HostName string Port uint16 + Id string } type Identity struct { @@ -78,11 +79,13 @@ func (id *Identity) Path() string { } type TunnelStatus struct { - Active bool - Duration int64 - Identities []*Identity - IpInfo *TunIpInfo `json:"IpInfo,omitempty"` - LogLevel string + Active bool + Duration int64 + Identities []*Identity + IpInfo *TunIpInfo `json:"IpInfo,omitempty"` + LogLevel string + TunIpv4 string + TunIpv4Mask int } type ZitiTunnelStatus struct { diff --git a/service/ziti-tunnel/service/ipc.go b/service/ziti-tunnel/service/ipc.go index 8db39a12..100f406a 100644 --- a/service/ziti-tunnel/service/ipc.go +++ b/service/ziti-tunnel/service/ipc.go @@ -80,7 +80,7 @@ func SubMain(ops chan string, changes chan<- svc.Status) error { } // initialize the network interface - err = initialize() + err = initialize(rts.state.TunIpv4, rts.state.TunIpv4Mask) if err != nil { log.Errorf("unexpected err: %v", err) @@ -219,12 +219,12 @@ func (p *Pipes) shutdownConnections() { log.Info("all events connections closed") } -func initialize() error { - err := rts.CreateTun() +func initialize(ipv4 string, ipv4mask int) error { + err := rts.CreateTun(ipv4, ipv4mask) if err != nil { return err } - setTunInfo(rts.state) + setTunInfo(rts.state, ipv4, ipv4mask) s := rts.state // decide if the tunnel should be active or not and if so - activate it @@ -239,16 +239,37 @@ func initialize() error { return nil } -func setTunInfo(s *dto.TunnelStatus) { +func setTunInfo(s *dto.TunnelStatus, ipv4 string, ipv4mask int) { + if strings.TrimSpace(ipv4) == "" { + log.Infof("ip not provided using default: %d", ipv4) + ipv4 = Ipv4ip + } + if ipv4mask < 8 || ipv4mask > 24 { + log.Warnf("provided mask is invalid: %d. using default value: %d", ipv4mask, Ipv4mask) + ipv4mask = Ipv4mask + } + _, ipnet, err := net.ParseCIDR(fmt.Sprintf("%s/%d", ipv4, ipv4mask)) + if err != nil { + log.Errorf("error parsing CIDR block: (%v)", err) + return + } //set the tun info into the state s.IpInfo = &dto.TunIpInfo{ - Ip: Ipv4ip, + Ip: ipv4, DNS: Ipv4dns, MTU: 1400, - Subnet: "255.255.255.0", + Subnet: ipv4MaskString(ipnet.Mask), } } +func ipv4MaskString(m []byte) string { + if len(m) != 4 { + panic("ipv4Mask: len must be 4 bytes") + } + + return fmt.Sprintf("%d.%d.%d.%d", m[0], m[1], m[2], m[3]) +} + func closeConn(conn net.Conn) { err := conn.Close() if err != nil { @@ -384,6 +405,10 @@ func serveIpc(conn net.Conn) { log.Warnf("Unknown operation: %s. Returning error on pipe", cmd.Function) respondWithError(enc, "Something unexpected has happened", UNKNOWN_ERROR, nil) } + + //save the state + rts.SaveState() + _ = rw.Flush() } } @@ -496,7 +521,6 @@ func tunnelState(onOff bool, out *json.Encoder) { } setTunnelState(onOff) state.Active = onOff - SaveState(&rts) respond(out, dto.Response{Message: "tunnel state updated successfully", Code: SUCCESS, Error: "", Payload: nil}) log.Debugf("toggle ziti on/off: %t responded to", onOff) @@ -539,7 +563,6 @@ func toggleIdentity(out *json.Encoder, fingerprint string, onOff bool) { respond(out, dto.Response{Message: "identity toggled", Code: SUCCESS, Error: "", Payload: idutil.Clean(*id)}) log.Debugf("toggle ziti on/off for %s: %t responded to", fingerprint, onOff) - SaveState(&rts) } func removeTempFile(file os.File) { @@ -638,9 +661,6 @@ func newIdentity(newId dto.AddIdentity, out *json.Encoder) { //if successful parse the output and add the config to the identity state.Identities = append(state.Identities, &newId.Id) - //save the state - SaveState(&rts) - //return successful message resp := dto.Response{Message: "success", Code: SUCCESS, Error: "", Payload: idutil.Clean(newId.Id)} @@ -667,12 +687,11 @@ func connectIdentity(id *dto.Identity) { } else { log.Debugf("id [%s] is already connected - not reconnecting", id.Name) for _, s := range id.Services { - cziti.AddIntercept(s.Name, s.HostName, s.Port, id.NFContext) + cziti.AddIntercept(s.Id, s.Name, s.HostName, s.Port, id.NFContext) } id.Connected = true } id.Active = true - SaveState(&rts) log.Infof("identity [%s] connected [%t] and set to active [%t]", id.Name, id.Connected, id.Active) } @@ -681,15 +700,18 @@ func disconnectIdentity(id *dto.Identity) error { id.Active = false if id.Connected { + log.Debugf("ranging over services all services to remove intercept and deregister the service") + if len(id.Services) < 1 { + log.Errorf("identity with fingerprint %s has no services?", id.FingerPrint) + } for _, s := range id.Services { - cziti.RemoveIntercept(s.Name) + cziti.RemoveIntercept(s.Id) cziti.DNS.DeregisterService(id.NFContext, s.Name) } id.Connected = false } else { log.Debugf("id: %s is already disconnected - not attempting to disconnected again fingerprint:%s", id.Name, id.FingerPrint) } - SaveState(&rts) return nil } @@ -716,8 +738,6 @@ func removeIdentity(out *json.Encoder, fingerprint string) { log.Warn("could not remove file: %s", config.Path()+id.FingerPrint+".json") } - SaveState(&rts) - resp := dto.Response{Message: "success", Code: SUCCESS, Error: "", Payload: nil} respond(out, resp) log.Infof("request to remove identity by fingerprint: %s responded to", fingerprint) @@ -755,7 +775,7 @@ func acceptServices() { case <-shutdown: return case c := <-cziti.ServiceChanges: - log.Debug("processing service change event") + log.Debugf("processing service change event. id:%s name:%s", c.Service.Id, c.Service.Name) matched := false //find the id using the context for _, id := range activeIds { @@ -765,9 +785,10 @@ func acceptServices() { case cziti.ADDED: //add the service to the identity svc := dto.Service{ - Name: c.Servicename, - HostName: c.Host, - Port: uint16(c.Port), + Name: c.Service.Name, + HostName: c.Service.InterceptHost, + Port: uint16(c.Service.InterceptPort), + Id: c.Service.Id, } id.Services = append(id.Services, &svc) @@ -776,10 +797,10 @@ func acceptServices() { Fingerprint: id.FingerPrint, Service: svc, } - log.Debug(" dispatched added service change event") + log.Debug("dispatched added service change event") case cziti.REMOVED: for idx, svc := range id.Services { - if svc.Name == c.Servicename { + if svc.Name == c.Service.Name { id.Services = append(id.Services[:idx], id.Services[idx+1:]...) events.broadcast <- dto.ServiceEvent{ ActionEvent: SERVICE_REMOVED, @@ -794,7 +815,7 @@ func acceptServices() { } if !matched { - log.Warnf("service update received but matched no context. this is unexpected. service name: %s", c.Servicename) + log.Warnf("service update received but matched no context. this is unexpected. service name: %s", c.Service.Name) } } } diff --git a/service/ziti-tunnel/service/state.go b/service/ziti-tunnel/service/state.go index e69c9c75..9d919cdd 100644 --- a/service/ziti-tunnel/service/state.go +++ b/service/ziti-tunnel/service/state.go @@ -31,6 +31,7 @@ import ( "net" "os" "strconv" + "strings" "time" ) @@ -64,7 +65,7 @@ func (t *RuntimeState) FindByIdentity(id dto.Identity) (int, *dto.Identity) { return t.Find(id.FingerPrint) } -func SaveState(t *RuntimeState) { +func (t *RuntimeState) SaveState() { // overwrite file if it exists _ = os.MkdirAll(config.Path(), 0644) @@ -75,10 +76,6 @@ func SaveState(t *RuntimeState) { w := bufio.NewWriter(bufio.NewWriter(cfg)) enc := json.NewEncoder(w) enc.SetIndent("", " ") - t.state.IpInfo = nil - for _, id := range t.state.Identities { - id.Services = nil - } _ = enc.Encode(t.state) _ = w.Flush() @@ -116,7 +113,7 @@ func (t *RuntimeState) ToStatus() dto.TunnelStatus { return clean } -func (t *RuntimeState) CreateTun() error { +func (t *RuntimeState) CreateTun(ipv4 string, ipv4mask int) error { if noZiti() { log.Warnf("NOZITI set to true. this should be only used for debugging") return nil @@ -140,10 +137,20 @@ func (t *RuntimeState) CreateTun() error { nativeTunDevice := tunDevice.(*tun.NativeTun) luid := winipcfg.LUID(nativeTunDevice.LUID()) - ip, ipnet, err := net.ParseCIDR(fmt.Sprintf("%s/%d", Ipv4ip, Ipv4mask)) + + if strings.TrimSpace(ipv4) == "" { + log.Infof("ip not provided using default: %d", ipv4) + ipv4 = Ipv4ip + } + if ipv4mask < 8 || ipv4mask > 24 { + log.Warnf("provided mask is invalid: %d. using default value: %d", ipv4mask, Ipv4mask) + ipv4mask = Ipv4mask + } + ip, ipnet, err := net.ParseCIDR(fmt.Sprintf("%s/%d", ipv4, ipv4mask)) if err != nil { return fmt.Errorf("error parsing CIDR block: (%v)", err) } + log.Infof("setting TUN interface address to [%s]", ip) err = luid.SetIPAddresses([]net.IPNet{{ip, ipnet.Mask}}) if err != nil { @@ -171,7 +178,7 @@ func (t *RuntimeState) CreateTun() error { } log.Info("routing applied") - cziti.DnsInit(Ipv4ip, 24) + cziti.DnsInit(ipv4, ipv4mask) cziti.Start() _, err = cziti.HookupTun(tunDevice, dns) if err != nil { @@ -236,10 +243,7 @@ func (t *RuntimeState) Close() { log.Fatalf("problem closing tunnel!") } } else { - log.Warn("the TUN WAS NULL? ") - log.Warn("the TUN WAS NULL? ") - log.Warn("the TUN WAS NULL? ") - log.Warn("the TUN WAS NULL? ") + log.Warn("unexpected situation. the TUN was null? ") } } diff --git a/version b/version index d169b2f2..7c1886bb 100644 --- a/version +++ b/version @@ -1 +1 @@ -0.0.8 +0.0.10