From 54ea40d3f1b856bd4a3a1d793564c263cf55da39 Mon Sep 17 00:00:00 2001 From: "Marcel M. Otte" Date: Mon, 2 Jan 2023 20:02:50 +0100 Subject: [PATCH] Proper project structure and architecture implementation --- fieldview.go | 7 +- go.mod | 5 +- go.work | 7 ++ go.work.sum | 14 ++++ packet/fieldvalue.go | 0 packet/packet.go | 0 packetctl/packetctl.go | 0 protocol/defaultfieldvalue.go | 6 ++ protocol/defaultvalue.go | 6 ++ protocol/dopmeta.go | 16 +++++ protocol/field.go | 11 +++ protocol/protocol.go | 8 +++ protocolctl/protocolctl.go | 130 ++++++++++++++++++++++++++++++++++ structure.go | 50 ++----------- 14 files changed, 213 insertions(+), 47 deletions(-) create mode 100644 packet/fieldvalue.go create mode 100644 packet/packet.go create mode 100644 packetctl/packetctl.go create mode 100644 protocol/defaultfieldvalue.go create mode 100644 protocol/defaultvalue.go create mode 100644 protocol/dopmeta.go create mode 100644 protocol/field.go create mode 100644 protocol/protocol.go create mode 100644 protocolctl/protocolctl.go diff --git a/fieldview.go b/fieldview.go index 899a709..ca0f5b2 100644 --- a/fieldview.go +++ b/fieldview.go @@ -1,8 +1,11 @@ package ppf -import "fyne.io/fyne/v2/widget" +import ( + "fyne.io/fyne/v2/widget" + "gitea.mmo.to/ProtocolPacketForger/ppf/protocol" +) -func CreateView(field Field) *widget.BaseWidget { +func CreateView(field protocol.Field) *widget.BaseWidget { return nil } diff --git a/go.mod b/go.mod index bb99864..2b2ff02 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module gitea.mmo.to/ProtocolPacketForger/ppf go 1.19 -require fyne.io/fyne/v2 v2.2.4 +require ( + fyne.io/fyne/v2 v2.2.4 + github.com/spf13/viper v1.10.1 +) require ( fyne.io/systray v1.10.1-0.20220621085403-9a2652634e93 // indirect diff --git a/go.work b/go.work index 52a3b7c..b4c3682 100644 --- a/go.work +++ b/go.work @@ -1,3 +1,10 @@ go 1.19 use . +//use protocol +//use protocolctl +//use packet +//use packetctl +//use dop +//use dopctl + diff --git a/go.work.sum b/go.work.sum index 589623f..b72cea3 100644 --- a/go.work.sum +++ b/go.work.sum @@ -1,2 +1,16 @@ github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1 h1:QbL/5oDUmRBzO9/Z7Seo6zf912W/a6Sr4Eu0G/3Jho0= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls= +github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs= +github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM= +github.com/spf13/afero v1.6.0 h1:xoax2sJ2DT8S8xA2paPFjDCScCNeWsg75VG0DLRreiY= +github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA= +github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/viper v1.10.1 h1:nuJZuYpG7gTj/XqiUwg8bA0cp1+M2mC3J4g5luUYBKk= +github.com/spf13/viper v1.10.1/go.mod h1:IGlFPqhNAPKRxohIzWpI5QEy4kuI7tcl5WvR+8qy1rU= +github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= +golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d h1:LO7XpTYMwTqxjLcGWPijK3vRXg1aWdlNOVOHRq45d7c= +golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +gopkg.in/ini.v1 v1.66.2 h1:XfR1dOYubytKy4Shzc2LHrrGhU0lDCfDGG1yLPmpgsI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= diff --git a/packet/fieldvalue.go b/packet/fieldvalue.go new file mode 100644 index 0000000..e69de29 diff --git a/packet/packet.go b/packet/packet.go new file mode 100644 index 0000000..e69de29 diff --git a/packetctl/packetctl.go b/packetctl/packetctl.go new file mode 100644 index 0000000..e69de29 diff --git a/protocol/defaultfieldvalue.go b/protocol/defaultfieldvalue.go new file mode 100644 index 0000000..2d655e6 --- /dev/null +++ b/protocol/defaultfieldvalue.go @@ -0,0 +1,6 @@ +package protocol + +type DefaultFieldValue struct { + FieldRef string + Value string +} diff --git a/protocol/defaultvalue.go b/protocol/defaultvalue.go new file mode 100644 index 0000000..a278359 --- /dev/null +++ b/protocol/defaultvalue.go @@ -0,0 +1,6 @@ +package protocol + +type DefaultValue struct { + Desc string + FieldValues []DefaultFieldValue +} diff --git a/protocol/dopmeta.go b/protocol/dopmeta.go new file mode 100644 index 0000000..a89e05a --- /dev/null +++ b/protocol/dopmeta.go @@ -0,0 +1,16 @@ +package protocol + +const DOPVersion string = "1.0" + +type DOPMeta struct { + // DOP metadata version 1.0 + DOPVersion string + Name string + Version string + TCPIPLayer uint + OSILayer uint + ExtensionTo string + Description string + RequiredJSFunctions []string + LowerLayerIdentification map[string]string +} diff --git a/protocol/field.go b/protocol/field.go new file mode 100644 index 0000000..75e1714 --- /dev/null +++ b/protocol/field.go @@ -0,0 +1,11 @@ +package protocol + +type Field struct { + Name string // Name of the Field + Desc string // Lengthy description + Regex string // Regex to recognize values + Size uint // Size in bits! + SubFields []Field // Possible sub-fields + Optional bool // Is this field required? + Payload bool // Is this field the payload or next protocol level? +} diff --git a/protocol/protocol.go b/protocol/protocol.go new file mode 100644 index 0000000..daf92d2 --- /dev/null +++ b/protocol/protocol.go @@ -0,0 +1,8 @@ +package protocol + +type ProtocolStructure struct { + Metadata DOPMeta + Structure []Field + DefaultValues []DefaultValue + JavaScript string +} diff --git a/protocolctl/protocolctl.go b/protocolctl/protocolctl.go new file mode 100644 index 0000000..6135a84 --- /dev/null +++ b/protocolctl/protocolctl.go @@ -0,0 +1,130 @@ +package protocolctl + +import ( + "encoding/json" + "io/fs" + "os" + + "gitea.mmo.to/ProtocolPacketForger/ppf/protocol" +) + +func NewProtocolStructure() *protocol.ProtocolStructure { + p := protocol.ProtocolStructure{} + return &p +} + +func UpdateMetaData( + prot *protocol.ProtocolStructure, + name string, + version string, + tcpiplayer uint, + osilayer uint, + extensionTo string, + desc string, + requiredJSfunctions []string, + lowerLayerIdent map[string]string, +) { + prot.Metadata.Name = name + prot.Metadata.Description = desc + prot.Metadata.ExtensionTo = extensionTo + prot.Metadata.Version = version + prot.Metadata.OSILayer = osilayer + prot.Metadata.TCPIPLayer = tcpiplayer + prot.Metadata.RequiredJSFunctions = requiredJSfunctions + prot.Metadata.LowerLayerIdentification = lowerLayerIdent +} + +func NewField( + name string, + desc string, + regex string, + size uint, + subfields []protocol.Field, + optional bool, + payload bool, +) *protocol.Field { + f := protocol.Field{ + Name: name, + Desc: desc, + Regex: regex, + Size: size, + SubFields: subfields, + Optional: optional, + Payload: payload, + } + return &f +} + +func AppendField(prot *protocol.ProtocolStructure, field *protocol.Field) { + prot.Structure = append(prot.Structure, *field) +} + +func UpdateFieldByRef(prot *protocol.ProtocolStructure, field *protocol.Field) { + for _, f := range prot.Structure { + if f.Name == field.Name { + f.Desc = field.Desc + f.Optional = field.Optional + f.Payload = field.Payload + f.Regex = field.Regex + f.Size = field.Size + f.SubFields = field.SubFields + } + } +} + +func UpdateFieldByElement(prot *protocol.ProtocolStructure, element int, field *protocol.Field) { + for i, f := range prot.Structure { + if i == element { + f.Desc = field.Desc + f.Optional = field.Optional + f.Payload = field.Payload + f.Regex = field.Regex + f.Size = field.Size + f.SubFields = field.SubFields + } + } +} + +func RemoveFieldByRef(prot *protocol.ProtocolStructure, field *protocol.Field) { + element := -1 + for i, f := range prot.Structure { + if f.Name == field.Name { + element = i + } + } + if element == -1 { + return + } + RemoveFieldByElement(prot, element) +} + +func RemoveFieldByElement(prot *protocol.ProtocolStructure, field int) { + ret := make([]protocol.Field, 0) + ret = append(ret, prot.Structure[:field]...) + ret = append(ret, prot.Structure[field+1:]...) + prot.Structure = ret +} + +func Load(prot *protocol.ProtocolStructure, path string) error { + data, err := os.ReadFile(path) + if err != nil { + return err + } + err = json.Unmarshal(data, prot) + return err +} + +func LoadNew(path string) (*protocol.ProtocolStructure, error) { + prot := NewProtocolStructure() + err := Load(prot, path) + return prot, err +} + +func Save(prot *protocol.ProtocolStructure, path string) error { + data, err := json.Marshal(*prot) + if err != nil { + return err + } + err = os.WriteFile(path, data, fs.ModeAppend) + return err +} diff --git a/structure.go b/structure.go index 2c2a85b..6897ffd 100644 --- a/structure.go +++ b/structure.go @@ -1,46 +1,8 @@ package ppf -type Field struct { - Name string // Name of the Field - Desc string // Lengthy description - Regex string // Regex to recognize values - Size uint // Size in bits! - SubFields []Field // Possible sub-fields - Optional bool // Is this field required? - Payload bool // Is this field the payload or next protocol level? - PredefinedValues []PredefinedFieldValue // Collection of predefined field values -} - -type PredefinedFieldValue struct { - Desc string - FieldValues []FieldValue -} - -type FieldValue struct { - FieldRef *Field - Value string -} - -type ProtocolStructure struct { - Metadata DOPMetadata - Structure []Field -} - -type DOPMetadata struct { - DOPVersion string - Name string - Version string - TCPIPLayer uint - OSILayer uint - ExtensionTo string - Description string - RequiredJSFunctions []string - LowerLayerIdentification map[string]string -} - -type DataModel struct { - Name string // User defined name of his current work (filename later) - Protocols []ProtocolStructure // List of protocols in order of the stack - Data []FieldValue // Data of the fields of all protocols - ProtocolCreation bool -} +//type DataModel struct { +// Name string // User defined name of his current work (filename later) +// Protocols []ProtocolStructure // List of protocols in order of the stack +// Data []FieldValue // Data of the fields of all protocols +// ProtocolCreation bool +//}