From 02c12cf90e9ca244e245a47af0d0cd703ec558d4 Mon Sep 17 00:00:00 2001 From: Samuel Lorch Date: Wed, 1 Mar 2023 18:17:52 +0100 Subject: [PATCH] add initial service definition --- pkg/definitions/service.go | 49 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 pkg/definitions/service.go diff --git a/pkg/definitions/service.go b/pkg/definitions/service.go new file mode 100644 index 0000000..165e6d2 --- /dev/null +++ b/pkg/definitions/service.go @@ -0,0 +1,49 @@ +package definitions + +import "encoding/json" + +type Service struct { + Type ServiceType `json:"type"` + Comment string `json:"comment,omitempty"` + SPortStart *uint32 `json:"sport_start,omitempty"` + SPortEnd *uint32 `json:"sport_end,omitempty"` + DPortStart *uint32 `json:"dport_start,omitempty"` + DPortEnd *uint32 `json:"dport_end,omitempty"` + ICMPCode *uint32 `json:"icmp_code,omitempty"` +} + +type ServiceType int + +const ( + TCP ServiceType = iota + UDP + TCPUDP + ICMP +) + +func (t ServiceType) String() string { + return [...]string{"tcp", "udp", "tcpudp", "icmp"}[t] +} + +func (t *ServiceType) FromString(input string) ServiceType { + return map[string]ServiceType{ + "tcp": TCP, + "udp": UDP, + "tcpudp": TCPUDP, + "icmp": ICMP, + }[input] +} + +func (t ServiceType) MarshalJSON() ([]byte, error) { + return json.Marshal(t.String()) +} + +func (t *ServiceType) UnmarshalJSON(b []byte) error { + var s string + err := json.Unmarshal(b, &s) + if err != nil { + return err + } + *t = t.FromString(s) + return nil +}