diff --git a/service/ziti-tunnel/cli/constants.go b/service/ziti-tunnel/cli/constants.go index e6e49187..150d45af 100644 --- a/service/ziti-tunnel/cli/constants.go +++ b/service/ziti-tunnel/cli/constants.go @@ -1,59 +1,59 @@ -package cli - -/* - * Copyright NetFoundry, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -import ( - "github.com/openziti/desktop-edge-win/service/ziti-tunnel/dto" - "github.com/openziti/desktop-edge-win/service/ziti-tunnel/util/logging" -) - -var GET_STATUS = dto.CommandMsg{ - Function: "Status", -} - -var ONOFF_IDENTITY = dto.CommandMsg{ - Function: "IdentityOnOff", -} - -var SET_LOGLEVEL = dto.CommandMsg{ - Function: "SetLogLevel", -} - -var NOTIFY_LOGLEVEL_UI_MONITOR = dto.CommandMsg{ - Function: "NotifyLogLevelUIAndUpdateService", -} - -var NOTIFY_IDENTITY_UI = dto.CommandMsg{ - Function: "NotifyIdentityUI", -} - -var UPDATE_TUN_IPV4 = dto.CommandMsg{ - Function: "UpdateTunIpv4", -} - -var monitorIpcPipe = `\\.\pipe\OpenZiti\ziti-monitor\ipc` - -var templateIdentity = `{{printf "%40s" "Name"}} | {{printf "%41s" "FingerPrint"}} | {{printf "%6s" "Active"}} | {{printf "%30s" "Config"}} | {{"Status"}} -{{range .}}{{printf "%40s" .Name}} | {{printf "%41s" .FingerPrint}} | {{printf "%6t" .Active}} | {{printf "%30s" .Config}} | {{.Status}} -{{end}}` - -var templateService = `{{printf "%40s" "Name"}} | {{printf "%15s" "Id"}} | {{printf "%9s" "Protocols"}} | {{printf "%14s" "Ports"}} | {{printf "%60s" "Addresses"}} -{{range .}}{{printf "%40s" .Name}} | {{printf "%15s" .Id}} | {{printf "%9s" .Protocols}} | {{printf "%14s" .Ports}} | {{printf "%60s" .Addresses}} -{{end}}` - -var log = logging.Logger() +package cli + +/* + * Copyright NetFoundry, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +import ( + "github.com/openziti/desktop-edge-win/service/ziti-tunnel/dto" + "github.com/openziti/desktop-edge-win/service/ziti-tunnel/util/logging" +) + +var GET_STATUS = dto.CommandMsg{ + Function: "Status", +} + +var ONOFF_IDENTITY = dto.CommandMsg{ + Function: "IdentityOnOff", +} + +var SET_LOGLEVEL = dto.CommandMsg{ + Function: "SetLogLevel", +} + +var NOTIFY_LOGLEVEL_UI_MONITOR = dto.CommandMsg{ + Function: "NotifyLogLevelUIAndUpdateService", +} + +var NOTIFY_IDENTITY_UI = dto.CommandMsg{ + Function: "NotifyIdentityUI", +} + +var UPDATE_TUN_IPV4 = dto.CommandMsg{ + Function: "UpdateTunIpv4", +} + +var monitorIpcPipe = `\\.\pipe\OpenZiti\ziti-monitor\ipc` + +var templateIdentity = `{{printf "%40s" "Name"}} | {{printf "%41s" "FingerPrint"}} | {{printf "%6s" "Active"}} | {{printf "%30s" "Config"}} | {{"Status"}} +{{range .}}{{printf "%40s" .Name}} | {{printf "%41s" .FingerPrint}} | {{printf "%6t" .Active}} | {{printf "%30s" .Config}} | {{.Status}} +{{end}}` + +var templateService = `{{printf "%40s" "Name"}} | {{printf "%15s" "Id"}} | {{printf "%9s" "Protocols"}} | {{printf "%14s" "Ports"}} | {{printf "%60s" "Addresses"}} +{{range .}}{{printf "%40s" .Name}} | {{printf "%15s" .Id}} | {{printf "%9s" .Protocols}} | {{printf "%14s" .Ports}} | {{printf "%60s" .Addresses}} +{{end}}` + +var log = logging.Logger() diff --git a/service/ziti-tunnel/cli/responseGenerator.go b/service/ziti-tunnel/cli/responseGenerator.go index e736c7f4..2b19d6ad 100644 --- a/service/ziti-tunnel/cli/responseGenerator.go +++ b/service/ziti-tunnel/cli/responseGenerator.go @@ -1,243 +1,242 @@ -package cli - -/* - * Copyright NetFoundry, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -import ( - "bytes" - "encoding/json" - "fmt" - "strings" - "text/template" - - "github.com/openziti/desktop-edge-win/service/ziti-tunnel/dto" - "github.com/openziti/desktop-edge-win/service/ziti-tunnel/service" -) - -func convertToIdentityCli(id *dto.Identity) dto.IdentityCli { - return dto.IdentityCli{ - Name: id.Name, - FingerPrint: id.FingerPrint, - Active: id.Active, - Config: id.Config.ZtAPI, - Status: id.Status, - } -} - -func convertToServiceCli(svc dto.Service) dto.ServiceCli { - cliPorts := "" - for _, val := range svc.Ports { - if len(cliPorts) != 0 { - cliPorts = fmt.Sprintf("%s, %d-%d", cliPorts, val.Low, val.High) - } else { - cliPorts = fmt.Sprintf("%d-%d", val.Low, val.High) - } - } - cliAddresses := "" - for _, val := range svc.Addresses { - if len(cliAddresses) != 0 { - cliAddresses = cliAddresses + ", " + val.HostName + "/" + val.IP - } else { - cliAddresses = val.HostName + "/" + val.IP - } - } - - return dto.ServiceCli{ - Name: svc.Name, - Id: svc.Id, - Protocols: strings.Join(svc.Protocols, ","), - Ports: cliPorts, - Addresses: cliAddresses, - } -} - -// GetIdentitiesFromRTS is to get identities from the RTS -func GetIdentitiesFromRTS(args []string, status *dto.TunnelStatus, flags map[string]bool) dto.Response { - - var filteredIdentities []dto.IdentityCli - - if flags["services"] { - return filterServicesByIdentity(args, status, flags) - } - - for _, val := range args { - if val == "all" { - for _, id := range status.Identities { - filteredIdentities = append(filteredIdentities, convertToIdentityCli(id)) - } - break - } else { - for _, id := range status.Identities { - if strings.Compare(id.Name, val) == 0 { - filteredIdentities = append(filteredIdentities, convertToIdentityCli(id)) - } - } - } - } - - if len(filteredIdentities) == 0 { - errMsg := fmt.Sprintf("Could not find identities matching %s", args) - return dto.Response{Message: "", Code: service.ERROR, Error: errMsg, Payload: nil} - } - message := fmt.Sprintf("Got %d identities - %s", len(filteredIdentities), args) - return generateResponse("identities", message, filteredIdentities, flags, templateIdentity) -} - -func filterServicesByIdentity(identity []string, status *dto.TunnelStatus, flags map[string]bool) dto.Response { - var filteredServices []dto.ServiceCli - - for _, id := range status.Identities { - for _, filterID := range identity { - if (filterID == "all" || id.Name == filterID) && len(id.Services) > 0 { - for _, svc := range id.Services { - filteredServices = append(filteredServices, convertToServiceCli(*svc)) - } - // if the filterId array has all or matching string, then fetch all the services and break from the filter loop - break - } - } - - } - - if len(filteredServices) == 0 { - errMsg := fmt.Sprintf("Could not find services for identity %s", identity) - return dto.Response{Message: "", Code: service.ERROR, Error: errMsg, Payload: nil} - } - message := fmt.Sprintf("Got %d services for identity - %s", len(filteredServices), identity) - return generateResponse("services", message, filteredServices, flags, templateService) - -} - -func GetServicesFromRTS(args []string, status *dto.TunnelStatus, flags map[string]bool) dto.Response { - - var filteredServices []dto.ServiceCli - - for _, val := range args { - if val == "all" { - for _, id := range status.Identities { - if len(id.Services) > 0 { - for _, svc := range id.Services { - filteredServices = append(filteredServices, convertToServiceCli(*svc)) - } - } - } - break - } else { - for _, id := range status.Identities { - if len(id.Services) > 0 { - for _, svc := range id.Services { - if strings.Compare(val, svc.Name) == 0 { - filteredServices = append(filteredServices, convertToServiceCli(*svc)) - } - } - } - } - } - } - - if len(filteredServices) == 0 { - errMsg := fmt.Sprintf("Could not find services matching %s", args) - return dto.Response{Message: "", Code: service.ERROR, Error: errMsg, Payload: nil} - } - message := fmt.Sprintf("Got %d services - %s", len(filteredServices), args) - - return generateResponse("services", message, filteredServices, flags, templateService) - -} - -func generateResponse(dataType string, message string, filteredData interface{}, flags map[string]bool, templateStr string) dto.Response { - - var bytesData []byte - var err error - var responseBuffer bytes.Buffer - var responseStr string - - if flags["prettyJSON"] == true { - bytesData, err = json.MarshalIndent(filteredData, "", " ") - - if err != nil { - log.Error(err) - return dto.Response{Message: message, Code: service.ERROR, Error: "Could not fetch " + dataType + " from Runtime", Payload: nil} - } - responseStr = string(bytesData) - - } else { - it, err := template.New("filteredData").Parse(templateStr) - - if err != nil { - log.Error(err) - return dto.Response{Message: message, Code: service.ERROR, Error: "Could not parse " + dataType + " from Runtime", Payload: nil} - } - - err = it.Execute(&responseBuffer, filteredData) - - if err != nil { - log.Error(err) - return dto.Response{Message: message, Code: service.ERROR, Error: "Could not print " + dataType + " from Runtime", Payload: nil} - } - responseStr = responseBuffer.String() - } - - return dto.Response{Message: message, Code: service.SUCCESS, Error: "", Payload: responseStr} -} - -func GetLogLevelFromRTS(args []string, status *dto.TunnelStatus, flags map[string]bool) dto.Response { - - if flags["query"] == true { - message := fmt.Sprintf("Loglevel is currently set to %s", status.LogLevel) - return dto.Response{Message: message, Code: service.SUCCESS, Error: "", Payload: ""} - } - errMsg := fmt.Sprintf("Unknown error: args %s flag %v", args, flags) - return dto.Response{Message: "", Code: service.ERROR, Error: errMsg, Payload: ""} - -} - -// GetIdentityResponseObjectFromRTS is to get identity info from the RTS -func GetIdentityResponseObjectFromRTS(args []string, status dto.Response, flags map[string]bool) dto.Response { - log.Debugf("Message from ziti-tunnel : %v", status.Message) - if status.Error == "" && status.Payload != nil { - log.Debugf("Payload from RTS %v", status.Payload) - payloadData := status.Payload.(map[string]interface{}) - identityStatus := make(map[string]interface{}) - identityStatus["FingerPrint"] = payloadData["FingerPrint"] - identityStatus["Active"] = payloadData["Active"] - identityStatus["Name"] = payloadData["Name"] - return dto.Response{Message: status.Message, Code: service.SUCCESS, Error: "", Payload: identityStatus} - } else { - return status - } -} - -// GetResponseObjectFromRTS is to get response object info from the RTS -func GetResponseObjectFromRTS(args []string, status dto.Response, flags map[string]bool) dto.Response { - return status -} - -func GetStatusFromRTS(args []string, status *dto.TunnelStatus, flags map[string]bool) dto.Response { - - if status == nil { - return dto.Response{Message: "Stopped / Unknown State", Code: service.SUCCESS, Error: "", Payload: ""} - } - - if status.Active { - return dto.Response{Message: "Running", Code: service.SUCCESS, Error: "", Payload: ""} - } else { - return dto.Response{Message: "Stopped", Code: service.SUCCESS, Error: "", Payload: ""} - } - -} - +package cli + +/* + * Copyright NetFoundry, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +import ( + "bytes" + "encoding/json" + "fmt" + "strings" + "text/template" + + "github.com/openziti/desktop-edge-win/service/ziti-tunnel/dto" + "github.com/openziti/desktop-edge-win/service/ziti-tunnel/service" +) + +func convertToIdentityCli(id *dto.Identity) dto.IdentityCli { + return dto.IdentityCli{ + Name: id.Name, + FingerPrint: id.FingerPrint, + Active: id.Active, + Config: id.Config.ZtAPI, + Status: id.Status, + } +} + +func convertToServiceCli(svc dto.Service) dto.ServiceCli { + cliPorts := "" + for _, val := range svc.Ports { + if len(cliPorts) != 0 { + cliPorts = fmt.Sprintf("%s, %d-%d", cliPorts, val.Low, val.High) + } else { + cliPorts = fmt.Sprintf("%d-%d", val.Low, val.High) + } + } + cliAddresses := "" + for _, val := range svc.Addresses { + if len(cliAddresses) != 0 { + cliAddresses = cliAddresses + ", " + val.HostName + "/" + val.IP + } else { + cliAddresses = val.HostName + "/" + val.IP + } + } + + return dto.ServiceCli{ + Name: svc.Name, + Id: svc.Id, + Protocols: strings.Join(svc.Protocols, ","), + Ports: cliPorts, + Addresses: cliAddresses, + } +} + +// GetIdentitiesFromRTS is to get identities from the RTS +func GetIdentitiesFromRTS(args []string, status *dto.TunnelStatus, flags map[string]bool) dto.Response { + + var filteredIdentities []dto.IdentityCli + + if flags["services"] { + return filterServicesByIdentity(args, status, flags) + } + + for _, val := range args { + if val == "all" { + for _, id := range status.Identities { + filteredIdentities = append(filteredIdentities, convertToIdentityCli(id)) + } + break + } else { + for _, id := range status.Identities { + if strings.Compare(id.Name, val) == 0 { + filteredIdentities = append(filteredIdentities, convertToIdentityCli(id)) + } + } + } + } + + if len(filteredIdentities) == 0 { + errMsg := fmt.Sprintf("Could not find identities matching %s", args) + return dto.Response{Message: "", Code: service.ERROR, Error: errMsg, Payload: nil} + } + message := fmt.Sprintf("Got %d identities - %s", len(filteredIdentities), args) + return generateResponse("identities", message, filteredIdentities, flags, templateIdentity) +} + +func filterServicesByIdentity(identity []string, status *dto.TunnelStatus, flags map[string]bool) dto.Response { + var filteredServices []dto.ServiceCli + + for _, id := range status.Identities { + for _, filterID := range identity { + if (filterID == "all" || id.Name == filterID) && len(id.Services) > 0 { + for _, svc := range id.Services { + filteredServices = append(filteredServices, convertToServiceCli(*svc)) + } + // if the filterId array has all or matching string, then fetch all the services and break from the filter loop + break + } + } + + } + + if len(filteredServices) == 0 { + errMsg := fmt.Sprintf("Could not find services for identity %s", identity) + return dto.Response{Message: "", Code: service.ERROR, Error: errMsg, Payload: nil} + } + message := fmt.Sprintf("Got %d services for identity - %s", len(filteredServices), identity) + return generateResponse("services", message, filteredServices, flags, templateService) + +} + +func GetServicesFromRTS(args []string, status *dto.TunnelStatus, flags map[string]bool) dto.Response { + + var filteredServices []dto.ServiceCli + + for _, val := range args { + if val == "all" { + for _, id := range status.Identities { + if len(id.Services) > 0 { + for _, svc := range id.Services { + filteredServices = append(filteredServices, convertToServiceCli(*svc)) + } + } + } + break + } else { + for _, id := range status.Identities { + if len(id.Services) > 0 { + for _, svc := range id.Services { + if strings.Compare(val, svc.Name) == 0 { + filteredServices = append(filteredServices, convertToServiceCli(*svc)) + } + } + } + } + } + } + + if len(filteredServices) == 0 { + errMsg := fmt.Sprintf("Could not find services matching %s", args) + return dto.Response{Message: "", Code: service.ERROR, Error: errMsg, Payload: nil} + } + message := fmt.Sprintf("Got %d services - %s", len(filteredServices), args) + + return generateResponse("services", message, filteredServices, flags, templateService) + +} + +func generateResponse(dataType string, message string, filteredData interface{}, flags map[string]bool, templateStr string) dto.Response { + + var bytesData []byte + var err error + var responseBuffer bytes.Buffer + var responseStr string + + if flags["prettyJSON"] == true { + bytesData, err = json.MarshalIndent(filteredData, "", " ") + + if err != nil { + log.Error(err) + return dto.Response{Message: message, Code: service.ERROR, Error: "Could not fetch " + dataType + " from Runtime", Payload: nil} + } + responseStr = string(bytesData) + + } else { + it, err := template.New("filteredData").Parse(templateStr) + + if err != nil { + log.Error(err) + return dto.Response{Message: message, Code: service.ERROR, Error: "Could not parse " + dataType + " from Runtime", Payload: nil} + } + + err = it.Execute(&responseBuffer, filteredData) + + if err != nil { + log.Error(err) + return dto.Response{Message: message, Code: service.ERROR, Error: "Could not print " + dataType + " from Runtime", Payload: nil} + } + responseStr = responseBuffer.String() + } + + return dto.Response{Message: message, Code: service.SUCCESS, Error: "", Payload: responseStr} +} + +func GetLogLevelFromRTS(args []string, status *dto.TunnelStatus, flags map[string]bool) dto.Response { + + if flags["query"] == true { + message := fmt.Sprintf("Loglevel is currently set to %s", status.LogLevel) + return dto.Response{Message: message, Code: service.SUCCESS, Error: "", Payload: ""} + } + errMsg := fmt.Sprintf("Unknown error: args %s flag %v", args, flags) + return dto.Response{Message: "", Code: service.ERROR, Error: errMsg, Payload: ""} + +} + +// GetIdentityResponseObjectFromRTS is to get identity info from the RTS +func GetIdentityResponseObjectFromRTS(args []string, status dto.Response, flags map[string]bool) dto.Response { + log.Debugf("Message from ziti-tunnel : %v", status.Message) + if status.Error == "" && status.Payload != nil { + log.Debugf("Payload from RTS %v", status.Payload) + payloadData := status.Payload.(map[string]interface{}) + identityStatus := make(map[string]interface{}) + identityStatus["FingerPrint"] = payloadData["FingerPrint"] + identityStatus["Active"] = payloadData["Active"] + identityStatus["Name"] = payloadData["Name"] + return dto.Response{Message: status.Message, Code: service.SUCCESS, Error: "", Payload: identityStatus} + } else { + return status + } +} + +// GetResponseObjectFromRTS is to get response object info from the RTS +func GetResponseObjectFromRTS(args []string, status dto.Response, flags map[string]bool) dto.Response { + return status +} + +func GetStatusFromRTS(args []string, status *dto.TunnelStatus, flags map[string]bool) dto.Response { + + if status == nil { + return dto.Response{Message: "Stopped / Unknown State", Code: service.SUCCESS, Error: "", Payload: ""} + } + + if status.Active { + return dto.Response{Message: "Running", Code: service.SUCCESS, Error: "", Payload: ""} + } else { + return dto.Response{Message: "Stopped", Code: service.SUCCESS, Error: "", Payload: ""} + } + +} diff --git a/service/ziti-tunnel/cli/service.go b/service/ziti-tunnel/cli/service.go index be81a09a..3b9a9e03 100644 --- a/service/ziti-tunnel/cli/service.go +++ b/service/ziti-tunnel/cli/service.go @@ -1,117 +1,116 @@ -package cli - -/* - * Copyright NetFoundry, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -import ( - "strings" - "strconv" - "github.com/openziti/desktop-edge-win/service/ziti-tunnel/dto" -) - -//GetIdentities is to fetch identities through cmdline -func GetIdentities(args []string, flags map[string]bool) { - GetDataFromIpcPipe(&GET_STATUS, GetIdentitiesFromRTS, nil, args, flags) -} - -//GetServices is to fetch services through cmdline -func GetServices(args []string, flags map[string]bool) { - GetDataFromIpcPipe(&GET_STATUS, GetServicesFromRTS, nil, args, flags) -} - -//OnOffIdentity is to enable or disable the identity through cmdline -func OnOffIdentity(args []string, flags map[string]bool) { - identityPayload := make(map[string]interface{}) - identityPayload["OnOff"] = strings.EqualFold(args[1], "on") - identityPayload["Fingerprint"] = args[0] - ONOFF_IDENTITY.Payload = identityPayload - log.Debugf("OnOffIdentity Payload %v", ONOFF_IDENTITY) - status := GetDataFromIpcPipe(&ONOFF_IDENTITY, nil, GetIdentityResponseObjectFromRTS, args, flags) - if status { - NOTIFY_IDENTITY_UI.Payload = identityPayload - log.Infof("Notifying the Identity Status to UI %v", identityPayload) - GetDataFromIpcPipe(&NOTIFY_IDENTITY_UI, nil, GetResponseObjectFromRTS, args, flags) - } -} - -//SetLogLevel is to change the loglevel through cmdline -func SetLogLevel(args []string, flags map[string]bool) { - if flags["query"] == true { - GetDataFromIpcPipe(&GET_STATUS, GetLogLevelFromRTS, nil, args, flags) - } else { - loglevelPayload := make(map[string]interface{}) - loglevelPayload["Level"] = args[0] - SET_LOGLEVEL.Payload = loglevelPayload - log.Debugf("LogLevel Payload %v", SET_LOGLEVEL) - status := GetDataFromIpcPipe(&SET_LOGLEVEL, nil, GetResponseObjectFromRTS, args, flags) - if status { - NOTIFY_LOGLEVEL_UI_MONITOR.Payload = loglevelPayload - log.Infof("Notifying the LogLevel to UI and Ziti monitor service %v", loglevelPayload) - GetDataFromIpcPipe(&NOTIFY_LOGLEVEL_UI_MONITOR, nil, GetResponseObjectFromRTS, args, flags) - } - } -} - -//GetFeedback is to create logs zip through cmdline -func GetFeedback(args []string, flags map[string]bool) { - GetDataFromMonitorIpcPipe(&dto.FEEDBACK_REQUEST, args, flags) -} - -func UpdateConfigIPSubnet(args []string, flags map[string]interface{}) { - CIDRstring := flags["CIDR"] - AddDns := flags["AddDns"] - log.Info("Updating the config file") - var cidr []string - var ipMask int - var err error - - updateTunIpv4Payload := make(map[string]interface{}) - if CIDRstring != "" { - cidr = strings.Split(CIDRstring.(string), "/") - if len(cidr) != 2 { - log.Error("Incorrect cidr") - } - ipMask, err = strconv.Atoi(cidr[1]) - if err != nil { - log.Errorf("Incorrect ipv4 mask, %s", cidr[1]) - return - } - updateTunIpv4Payload["TunIPv4"] = cidr[0] - updateTunIpv4Payload["TunIPv4Mask"] = ipMask - } - if AddDns != "" { - var addDnsBool bool - addDnsBool, err = strconv.ParseBool(AddDns.(string)) - - if err != nil { - log.Errorf("Incorrect addDns %v", err) - return - } - updateTunIpv4Payload["AddDns"] = addDnsBool - } - - UPDATE_TUN_IPV4.Payload = updateTunIpv4Payload - log.Debugf("updateTunIpv4 Payload %v", UPDATE_TUN_IPV4) - - status := GetDataFromIpcPipe(&UPDATE_TUN_IPV4, nil, GetResponseObjectFromRTS, args, nil) - - if !status { - log.Infof("Config can be updated only if the tunnel is started") - } - -} - +package cli + +/* + * Copyright NetFoundry, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +import ( + "github.com/openziti/desktop-edge-win/service/ziti-tunnel/dto" + "strconv" + "strings" +) + +//GetIdentities is to fetch identities through cmdline +func GetIdentities(args []string, flags map[string]bool) { + GetDataFromIpcPipe(&GET_STATUS, GetIdentitiesFromRTS, nil, args, flags) +} + +//GetServices is to fetch services through cmdline +func GetServices(args []string, flags map[string]bool) { + GetDataFromIpcPipe(&GET_STATUS, GetServicesFromRTS, nil, args, flags) +} + +//OnOffIdentity is to enable or disable the identity through cmdline +func OnOffIdentity(args []string, flags map[string]bool) { + identityPayload := make(map[string]interface{}) + identityPayload["OnOff"] = strings.EqualFold(args[1], "on") + identityPayload["Fingerprint"] = args[0] + ONOFF_IDENTITY.Payload = identityPayload + log.Debugf("OnOffIdentity Payload %v", ONOFF_IDENTITY) + status := GetDataFromIpcPipe(&ONOFF_IDENTITY, nil, GetIdentityResponseObjectFromRTS, args, flags) + if status { + NOTIFY_IDENTITY_UI.Payload = identityPayload + log.Infof("Notifying the Identity Status to UI %v", identityPayload) + GetDataFromIpcPipe(&NOTIFY_IDENTITY_UI, nil, GetResponseObjectFromRTS, args, flags) + } +} + +//SetLogLevel is to change the loglevel through cmdline +func SetLogLevel(args []string, flags map[string]bool) { + if flags["query"] == true { + GetDataFromIpcPipe(&GET_STATUS, GetLogLevelFromRTS, nil, args, flags) + } else { + loglevelPayload := make(map[string]interface{}) + loglevelPayload["Level"] = args[0] + SET_LOGLEVEL.Payload = loglevelPayload + log.Debugf("LogLevel Payload %v", SET_LOGLEVEL) + status := GetDataFromIpcPipe(&SET_LOGLEVEL, nil, GetResponseObjectFromRTS, args, flags) + if status { + NOTIFY_LOGLEVEL_UI_MONITOR.Payload = loglevelPayload + log.Infof("Notifying the LogLevel to UI and Ziti monitor service %v", loglevelPayload) + GetDataFromIpcPipe(&NOTIFY_LOGLEVEL_UI_MONITOR, nil, GetResponseObjectFromRTS, args, flags) + } + } +} + +//GetFeedback is to create logs zip through cmdline +func GetFeedback(args []string, flags map[string]bool) { + GetDataFromMonitorIpcPipe(&dto.FEEDBACK_REQUEST, args, flags) +} + +func UpdateConfigIPSubnet(args []string, flags map[string]interface{}) { + CIDRstring := flags["CIDR"] + AddDns := flags["AddDns"] + log.Info("Updating the config file") + var cidr []string + var ipMask int + var err error + + updateTunIpv4Payload := make(map[string]interface{}) + if CIDRstring != "" { + cidr = strings.Split(CIDRstring.(string), "/") + if len(cidr) != 2 { + log.Error("Incorrect cidr") + } + ipMask, err = strconv.Atoi(cidr[1]) + if err != nil { + log.Errorf("Incorrect ipv4 mask, %s", cidr[1]) + return + } + updateTunIpv4Payload["TunIPv4"] = cidr[0] + updateTunIpv4Payload["TunIPv4Mask"] = ipMask + } + if AddDns != "" { + var addDnsBool bool + addDnsBool, err = strconv.ParseBool(AddDns.(string)) + + if err != nil { + log.Errorf("Incorrect addDns %v", err) + return + } + updateTunIpv4Payload["AddDns"] = addDnsBool + } + + UPDATE_TUN_IPV4.Payload = updateTunIpv4Payload + log.Debugf("updateTunIpv4 Payload %v", UPDATE_TUN_IPV4) + + status := GetDataFromIpcPipe(&UPDATE_TUN_IPV4, nil, GetResponseObjectFromRTS, args, nil) + + if !status { + log.Infof("Config can be updated only if the tunnel is started") + } + +}