Skip to content

Commit

Permalink
feat(plc4xanalyzer): exposed cli options to the ui
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Nov 1, 2022
1 parent 34bc1d5 commit 00d418f
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 91 deletions.
2 changes: 1 addition & 1 deletion plc4go/tools/plc4xpcapanalyzer/config/AnalyzeConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package config

type AnalyzeConfig struct {
*PcapConfig
*PcapConfig `json:"-"`
NoFilter, OnlyParse, NoBytesCompare, NoCustomMapping bool
}

Expand Down
4 changes: 2 additions & 2 deletions plc4go/tools/plc4xpcapanalyzer/config/BacnetConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
package config

type BacnetConfig struct {
*AnalyzeConfig
BacnetFilter string
*AnalyzeConfig `json:"-"`
BacnetFilter string
}

var BacnetConfigInstance = BacnetConfig{}
Expand Down
4 changes: 2 additions & 2 deletions plc4go/tools/plc4xpcapanalyzer/config/CBusConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
package config

type CBusConfig struct {
*AnalyzeConfig
CBusFilter string
*AnalyzeConfig `json:"-"`
CBusFilter string

Connect bool
Smart bool
Expand Down
2 changes: 1 addition & 1 deletion plc4go/tools/plc4xpcapanalyzer/config/ExtractConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
)

type ExtractConfig struct {
*PcapConfig
*PcapConfig `json:"-"`
ShowDirectionalIndicators bool
ExtractType ExtractType
}
Expand Down
2 changes: 1 addition & 1 deletion plc4go/tools/plc4xpcapanalyzer/config/PcapConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package config

type PcapConfig struct {
*RootConfig
*RootConfig `json:"-"`
Filter string
Client string
StartPackageNumber uint
Expand Down
21 changes: 11 additions & 10 deletions plc4go/tools/plc4xpcapanalyzer/internal/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,7 @@ func AnalyzeWithOutput(pcapFile, protocolType string, stdout, stderr io.Writer)
}

func AnalyzeWithOutputAndCallback(ctx context.Context, pcapFile, protocolType string, stdout, stderr io.Writer, messageCallback func(parsed spi.Message)) error {
log.Info().Msgf("Analyzing pcap file '%s' with protocolType '%s' and filter '%s' now", pcapFile, protocolType, config.AnalyzeConfigInstance.Filter)

handle, numberOfPackage, timestampToIndexMap, err := pcaphandler.GetIndexedPcapHandle(pcapFile, config.AnalyzeConfigInstance.Filter)
if err != nil {
return errors.Wrap(err, "Error getting handle")
}
log.Info().Msgf("Starting to analyze %d packages", numberOfPackage)
defer handle.Close()
log.Debug().Interface("handle", handle).Int("numberOfPackage", numberOfPackage).Msg("got handle")
source := pcaphandler.GetPacketSource(handle)
var filterExpression = config.AnalyzeConfigInstance.Filter
var mapPackets = func(in chan gopacket.Packet, packetInformationCreator func(packet gopacket.Packet) common.PacketInformation) chan gopacket.Packet {
return in
}
Expand Down Expand Up @@ -88,6 +79,16 @@ func AnalyzeWithOutputAndCallback(ctx context.Context, pcapFile, protocolType st
default:
return errors.Errorf("Unsupported protocol type %s", protocolType)
}

log.Info().Msgf("Analyzing pcap file '%s' with protocolType '%s' and filter '%s' now", pcapFile, protocolType, filterExpression)
handle, numberOfPackage, timestampToIndexMap, err := pcaphandler.GetIndexedPcapHandle(pcapFile, filterExpression)
if err != nil {
return errors.Wrap(err, "Error getting handle")
}
log.Info().Msgf("Starting to analyze %d packages", numberOfPackage)
defer handle.Close()
log.Debug().Interface("handle", handle).Int("numberOfPackage", numberOfPackage).Msg("got handle")
source := pcaphandler.GetPacketSource(handle)
bar := progressbar.NewOptions(numberOfPackage, progressbar.OptionSetWriter(ansi.NewAnsiStderr()),
progressbar.OptionSetVisibility(!config.RootConfigInstance.HideProgressBar),
progressbar.OptionEnableColorCodes(true),
Expand Down
21 changes: 11 additions & 10 deletions plc4go/tools/plc4xpcapanalyzer/internal/extractor/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,6 @@ func Extract(pcapFile, protocolType string) error {
}

func ExtractWithOutput(ctx context.Context, pcapFile, protocolType string, stdout, stderr io.Writer) error {
log.Info().Msgf("Analyzing pcap file '%s' with protocolType '%s' and filter '%s' now", pcapFile, protocolType, config.ExtractConfigInstance.Filter)

handle, numberOfPackage, timestampToIndexMap, err := pcaphandler.GetIndexedPcapHandle(pcapFile, config.ExtractConfigInstance.Filter)
if err != nil {
return errors.Wrap(err, "Error getting handle")
}
log.Info().Msgf("Starting to analyze %d packages", numberOfPackage)
defer handle.Close()
log.Debug().Interface("handle", handle).Int("numberOfPackage", numberOfPackage).Msg("got handle")
source := pcaphandler.GetPacketSource(handle)
var printPayload = func(packetInformation common.PacketInformation, item []byte) {
_, _ = fmt.Fprintf(stdout, "%x\n", item)
}
Expand Down Expand Up @@ -91,6 +81,17 @@ func ExtractWithOutput(ctx context.Context, pcapFile, protocolType string, stdou
}
}
}
filterExpression := config.ExtractConfigInstance.Filter
log.Info().Msgf("Analyzing pcap file '%s' with protocolType '%s' and filter '%s' now", pcapFile, protocolType, filterExpression)

handle, numberOfPackage, timestampToIndexMap, err := pcaphandler.GetIndexedPcapHandle(pcapFile, filterExpression)
if err != nil {
return errors.Wrap(err, "Error getting handle")
}
log.Info().Msgf("Starting to analyze %d packages", numberOfPackage)
defer handle.Close()
log.Debug().Interface("handle", handle).Int("numberOfPackage", numberOfPackage).Msg("got handle")
source := pcaphandler.GetPacketSource(handle)
bar := progressbar.NewOptions(numberOfPackage, progressbar.OptionSetWriter(ansi.NewAnsiStderr()),
progressbar.OptionSetVisibility(!config.RootConfigInstance.HideProgressBar),
progressbar.OptionEnableColorCodes(true),
Expand Down
192 changes: 133 additions & 59 deletions plc4go/tools/plc4xpcapanalyzer/ui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/rs/zerolog/log"
"os"
"path"
"reflect"
"strings"
"time"
)
Expand Down Expand Up @@ -287,134 +288,207 @@ var rootCommand = Command{
},
},
{
Name: "plc4x-conf",
Description: "plc4x related settings",
Name: "conf",
Description: "Various settings for plc4xpcapanalyzer",
subCommands: []Command{
{
Name: "TraceTransactionManagerWorkers",
Description: "print information about transaction manager workers",
Name: "list",
Description: "list config values with their current settings",
action: func(_ context.Context, _ Command, _ string) error {
allCliConfigsValue := reflect.ValueOf(allCliConfigsInstances)
for i := 0; i < allCliConfigsValue.NumField(); i++ {
allConfigField := allCliConfigsValue.Field(i)
allConfigFieldType := allCliConfigsValue.Type().Field(i)
_, _ = fmt.Fprintf(commandOutput, "%s:\n", allConfigFieldType.Name)
configInstanceReflectValue := reflect.ValueOf(allConfigField.Interface())
if configInstanceReflectValue.Kind() == reflect.Ptr {
configInstanceReflectValue = configInstanceReflectValue.Elem()
}
for j := 0; j < configInstanceReflectValue.NumField(); j++ {
configField := configInstanceReflectValue.Field(j)
configFieldType := configInstanceReflectValue.Type().Field(j)
if configFieldType.Tag.Get("json") == "-" {
// Ignore those
continue
}
_, _ = fmt.Fprintf(commandOutput, " %s: %s\t= %v\n", configFieldType.Name, configFieldType.Type, configField.Interface())
}
}
return nil
},
},
{
Name: "set",
Description: "sets a config value",
subCommands: func() []Command {
var configCommand []Command
allCliConfigsValue := reflect.ValueOf(allCliConfigsInstances)
for i := 0; i < allCliConfigsValue.NumField(); i++ {
allConfigField := allCliConfigsValue.Field(i)
allConfigFieldType := allCliConfigsValue.Type().Field(i)
configCommand = append(configCommand, Command{
Name: allConfigFieldType.Name,
Description: fmt.Sprintf("Setting for %s", allConfigFieldType.Name),
subCommands: func() []Command {
var configElementCommands []Command
configInstanceReflectValue := reflect.ValueOf(allConfigField.Interface())
if configInstanceReflectValue.Kind() == reflect.Ptr {
configInstanceReflectValue = configInstanceReflectValue.Elem()
}
for i := 0; i < configInstanceReflectValue.NumField(); i++ {
field := configInstanceReflectValue.Field(i)
fieldOfType := configInstanceReflectValue.Type().Field(i)
if fieldOfType.Tag.Get("json") == "-" {
// Ignore those
continue
}
configElementCommands = append(configElementCommands, Command{
Name: fieldOfType.Name,
Description: fmt.Sprintf("Sets value for %s", fieldOfType.Name),
action: func(_ context.Context, _ Command, argument string) error {
field.SetString(argument)
return nil
},
})
}
return configElementCommands
}(),
})
}
return configCommand
}(),
},
{
Name: "plc4xpcapanalyzer-debug",
Description: "Prints out debug information of the pcap analyzer itself",
subCommands: []Command{
{
Name: "on",
Description: "trace on",
Description: "debug on",
action: func(_ context.Context, _ Command, _ string) error {
plc4x_config.TraceTransactionManagerWorkers = true
plc4xpcapanalyzerLog = zerolog.New(zerolog.ConsoleWriter{Out: tview.ANSIWriter(consoleOutput)})
return nil
},
},
{
Name: "off",
Description: "trace off",
Description: "debug off",
action: func(_ context.Context, _ Command, _ string) error {
plc4x_config.TraceTransactionManagerWorkers = false
plc4xpcapanalyzerLog = zerolog.Nop()
return nil
},
},
},
},
{
Name: "TraceTransactionManagerTransactions",
Description: "print information about transaction manager transactions",
Name: "auto-register",
Description: "autoregister driver at startup",
subCommands: []Command{
{
Name: "on",
Description: "trace on",
action: func(_ context.Context, _ Command, _ string) error {
plc4x_config.TraceTransactionManagerTransactions = true
Name: "list",
action: func(_ context.Context, currentCommand Command, argument string) error {
_, _ = fmt.Fprintf(commandOutput, "Auto-register enabled drivers:\n %s\n", strings.Join(config.AutoRegisterDrivers, "\n "))
return nil
},
},
{
Name: "off",
Description: "trace off",
action: func(_ context.Context, _ Command, _ string) error {
plc4x_config.TraceTransactionManagerTransactions = false
return nil
Name: "enable",
action: func(_ context.Context, _ Command, argument string) error {
return enableAutoRegister(argument)
},
parameterSuggestions: func(currentText string) (entries []string) {
for _, protocol := range protocolList {
if strings.HasPrefix(protocol, currentText) {
entries = append(entries, protocol)
}
}
return
},
},
{
Name: "disable",
action: func(_ context.Context, _ Command, argument string) error {
return disableAutoRegister(argument)
},
parameterSuggestions: func(currentText string) (entries []string) {
for _, protocol := range protocolList {
if strings.HasPrefix(protocol, currentText) {
entries = append(entries, protocol)
}
}
return
},
},
},
},
},
},
{
Name: "plc4x-conf",
Description: "plc4x related settings",
subCommands: []Command{
{
Name: "TraceDefaultMessageCodecWorker",
Description: "print information about message codec workers",
Name: "TraceTransactionManagerWorkers",
Description: "print information about transaction manager workers",
subCommands: []Command{
{
Name: "on",
Description: "trace on",
action: func(_ context.Context, _ Command, _ string) error {
plc4x_config.TraceDefaultMessageCodecWorker = true
plc4x_config.TraceTransactionManagerWorkers = true
return nil
},
},
{
Name: "off",
Description: "trace off",
action: func(_ context.Context, _ Command, _ string) error {
plc4x_config.TraceDefaultMessageCodecWorker = false
plc4x_config.TraceTransactionManagerWorkers = false
return nil
},
},
},
},
{
Name: "plc4xpcapanalyzer-debug",
Description: "Prints out debug information of the pcap analyzer itself",
Name: "TraceTransactionManagerTransactions",
Description: "print information about transaction manager transactions",
subCommands: []Command{
{
Name: "on",
Description: "debug on",
Description: "trace on",
action: func(_ context.Context, _ Command, _ string) error {
plc4xpcapanalyzerLog = zerolog.New(zerolog.ConsoleWriter{Out: tview.ANSIWriter(consoleOutput)})
plc4x_config.TraceTransactionManagerTransactions = true
return nil
},
},
{
Name: "off",
Description: "debug off",
Description: "trace off",
action: func(_ context.Context, _ Command, _ string) error {
plc4xpcapanalyzerLog = zerolog.Nop()
plc4x_config.TraceTransactionManagerTransactions = false
return nil
},
},
},
},
{
Name: "auto-register",
Description: "autoregister driver at startup",
Name: "TraceDefaultMessageCodecWorker",
Description: "print information about message codec workers",
subCommands: []Command{
{
Name: "list",
action: func(_ context.Context, currentCommand Command, argument string) error {
_, _ = fmt.Fprintf(commandOutput, "Auto-register enabled drivers:\n %s\n", strings.Join(config.AutoRegisterDrivers, "\n "))
Name: "on",
Description: "trace on",
action: func(_ context.Context, _ Command, _ string) error {
plc4x_config.TraceDefaultMessageCodecWorker = true
return nil
},
},
{
Name: "enable",
action: func(_ context.Context, _ Command, argument string) error {
return enableAutoRegister(argument)
},
parameterSuggestions: func(currentText string) (entries []string) {
for _, protocol := range protocolList {
if strings.HasPrefix(protocol, currentText) {
entries = append(entries, protocol)
}
}
return
},
},
{
Name: "disable",
action: func(_ context.Context, _ Command, argument string) error {
return disableAutoRegister(argument)
},
parameterSuggestions: func(currentText string) (entries []string) {
for _, protocol := range protocolList {
if strings.HasPrefix(protocol, currentText) {
entries = append(entries, protocol)
}
}
return
Name: "off",
Description: "trace off",
action: func(_ context.Context, _ Command, _ string) error {
plc4x_config.TraceDefaultMessageCodecWorker = false
return nil
},
},
},
Expand Down
Loading

0 comments on commit 00d418f

Please sign in to comment.