mirror of
https://github.com/speatzle/nfsense.git
synced 2025-09-13 15:19:08 +00:00
Seperate Definitions by Sub System
This commit is contained in:
parent
14c8da64cc
commit
72bf96295d
31 changed files with 120 additions and 108 deletions
54
internal/definitions/object/address.go
Normal file
54
internal/definitions/object/address.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
package object
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/netip"
|
||||
|
||||
"go4.org/netipx"
|
||||
"nfsense.net/nfsense/internal/definitions/common"
|
||||
)
|
||||
|
||||
type Address struct {
|
||||
Type AddressType `json:"type" validate:"min=0,max=3"`
|
||||
Comment string `json:"comment,omitempty"`
|
||||
Host *netip.Addr `json:"host,omitempty" validate:"excluded_unless=Type 0"`
|
||||
Range *netipx.IPRange `json:"range,omitempty" validate:"excluded_unless=Type 1"`
|
||||
NetworkAddress *common.IPNet `json:"network,omitempty" validate:"excluded_unless=Type 2"`
|
||||
Children *[]string `json:"children,omitempty"`
|
||||
}
|
||||
|
||||
type AddressType int
|
||||
|
||||
const (
|
||||
Host AddressType = iota
|
||||
Range
|
||||
NetworkAddress
|
||||
AddressGroup
|
||||
)
|
||||
|
||||
func (t AddressType) String() string {
|
||||
return [...]string{"host", "range", "network", "group"}[t]
|
||||
}
|
||||
|
||||
func (t *AddressType) FromString(input string) AddressType {
|
||||
return map[string]AddressType{
|
||||
"host": Host,
|
||||
"range": Range,
|
||||
"network": NetworkAddress,
|
||||
"group": AddressGroup,
|
||||
}[input]
|
||||
}
|
||||
|
||||
func (t AddressType) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(t.String())
|
||||
}
|
||||
|
||||
func (t *AddressType) UnmarshalJSON(b []byte) error {
|
||||
var s string
|
||||
err := json.Unmarshal(b, &s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*t = t.FromString(s)
|
||||
return nil
|
||||
}
|
6
internal/definitions/object/object.go
Normal file
6
internal/definitions/object/object.go
Normal file
|
@ -0,0 +1,6 @@
|
|||
package object
|
||||
|
||||
type Object struct {
|
||||
Addresses map[string]Address `json:"addresses" validate:"required,dive"`
|
||||
Services map[string]Service `json:"services" validate:"required,dive"`
|
||||
}
|
71
internal/definitions/object/service.go
Normal file
71
internal/definitions/object/service.go
Normal file
|
@ -0,0 +1,71 @@
|
|||
package object
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
Type ServiceType `json:"type" validate:"min=0,max=3"`
|
||||
Comment string `json:"comment,omitempty"`
|
||||
SPortStart *uint32 `json:"sport_start,omitempty" validate:"excluded_unless=Type 0|excluded_unless=Type 1"`
|
||||
SPortEnd *uint32 `json:"sport_end,omitempty"`
|
||||
DPortStart *uint32 `json:"dport_start,omitempty" validate:"excluded_unless=Type 0|excluded_unless=Type 1"`
|
||||
DPortEnd *uint32 `json:"dport_end,omitempty"`
|
||||
ICMPCode *uint32 `json:"icmp_code,omitempty" validate:"excluded_unless=Type 2"`
|
||||
Children *[]string `json:"children,omitempty"`
|
||||
}
|
||||
|
||||
func (s Service) GetSPort() string {
|
||||
if s.SPortStart == nil || *s.SPortStart == 0 {
|
||||
return ""
|
||||
} else if s.SPortEnd == nil || *s.SPortEnd == 0 {
|
||||
return fmt.Sprintf("%d", *s.SPortStart)
|
||||
}
|
||||
return fmt.Sprintf("%d - %d", *s.SPortStart, *s.SPortEnd)
|
||||
}
|
||||
|
||||
func (s Service) GetDPort() string {
|
||||
if s.DPortStart == nil || *s.DPortStart == 0 {
|
||||
return ""
|
||||
} else if s.DPortEnd == nil || *s.DPortEnd == 0 {
|
||||
return fmt.Sprintf("%d", *s.DPortStart)
|
||||
}
|
||||
return fmt.Sprintf("%d - %d", *s.DPortStart, *s.DPortEnd)
|
||||
}
|
||||
|
||||
type ServiceType int
|
||||
|
||||
const (
|
||||
TCP ServiceType = iota
|
||||
UDP
|
||||
ICMP
|
||||
ServiceGroup
|
||||
)
|
||||
|
||||
func (t ServiceType) String() string {
|
||||
return [...]string{"tcp", "udp", "icmp", "group"}[t]
|
||||
}
|
||||
|
||||
func (t *ServiceType) FromString(input string) ServiceType {
|
||||
return map[string]ServiceType{
|
||||
"tcp": TCP,
|
||||
"udp": UDP,
|
||||
"icmp": ICMP,
|
||||
"group": ServiceGroup,
|
||||
}[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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue