2023-05-22 00:37:59 +08:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql/driver"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/netip"
|
|
|
|
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrCannotParsePrefix = errors.New("cannot parse prefix")
|
|
|
|
|
|
|
|
type IPPrefix netip.Prefix
|
|
|
|
|
|
|
|
func (i *IPPrefix) Scan(destination interface{}) error {
|
|
|
|
switch value := destination.(type) {
|
|
|
|
case string:
|
|
|
|
prefix, err := netip.ParsePrefix(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*i = IPPrefix(prefix)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("%w: unexpected data type %T", ErrCannotParsePrefix, destination)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value return json value, implement driver.Valuer interface.
|
|
|
|
func (i IPPrefix) Value() (driver.Value, error) {
|
|
|
|
prefixStr := netip.Prefix(i).String()
|
|
|
|
|
|
|
|
return prefixStr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type IPPrefixes []netip.Prefix
|
|
|
|
|
|
|
|
func (i *IPPrefixes) Scan(destination interface{}) error {
|
|
|
|
switch value := destination.(type) {
|
|
|
|
case []byte:
|
|
|
|
return json.Unmarshal(value, i)
|
|
|
|
|
|
|
|
case string:
|
|
|
|
return json.Unmarshal([]byte(value), i)
|
|
|
|
|
|
|
|
default:
|
2023-09-24 19:42:05 +08:00
|
|
|
return fmt.Errorf("%w: unexpected data type %T", ErrNodeAddressesInvalid, destination)
|
2023-05-22 00:37:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value return json value, implement driver.Valuer interface.
|
|
|
|
func (i IPPrefixes) Value() (driver.Value, error) {
|
|
|
|
bytes, err := json.Marshal(i)
|
|
|
|
|
|
|
|
return string(bytes), err
|
|
|
|
}
|
|
|
|
|
|
|
|
type StringList []string
|
|
|
|
|
|
|
|
func (i *StringList) Scan(destination interface{}) error {
|
|
|
|
switch value := destination.(type) {
|
|
|
|
case []byte:
|
|
|
|
return json.Unmarshal(value, i)
|
|
|
|
|
|
|
|
case string:
|
|
|
|
return json.Unmarshal([]byte(value), i)
|
|
|
|
|
|
|
|
default:
|
2023-09-24 19:42:05 +08:00
|
|
|
return fmt.Errorf("%w: unexpected data type %T", ErrNodeAddressesInvalid, destination)
|
2023-05-22 00:37:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value return json value, implement driver.Valuer interface.
|
|
|
|
func (i StringList) Value() (driver.Value, error) {
|
|
|
|
bytes, err := json.Marshal(i)
|
|
|
|
|
|
|
|
return string(bytes), err
|
|
|
|
}
|
2023-06-29 18:20:22 +08:00
|
|
|
|
|
|
|
type StateUpdateType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
StateFullUpdate StateUpdateType = iota
|
2023-12-10 01:09:24 +08:00
|
|
|
// StatePeerChanged is used for updates that needs
|
|
|
|
// to be calculated with all peers and all policy rules.
|
|
|
|
// This would typically be things that include tags, routes
|
|
|
|
// and similar.
|
2023-06-29 18:20:22 +08:00
|
|
|
StatePeerChanged
|
2023-12-10 01:09:24 +08:00
|
|
|
StatePeerChangedPatch
|
2023-06-29 18:20:22 +08:00
|
|
|
StatePeerRemoved
|
2024-01-05 17:41:56 +08:00
|
|
|
// StateSelfUpdate is used to indicate that the node
|
|
|
|
// has changed in control, and the client needs to be
|
|
|
|
// informed.
|
|
|
|
// The updated node is inside the ChangeNodes field
|
|
|
|
// which should have a length of one.
|
|
|
|
StateSelfUpdate
|
2023-06-29 18:20:22 +08:00
|
|
|
StateDERPUpdated
|
|
|
|
)
|
|
|
|
|
|
|
|
// StateUpdate is an internal message containing information about
|
|
|
|
// a state change that has happened to the network.
|
2023-12-10 01:09:24 +08:00
|
|
|
// If type is StateFullUpdate, all fields are ignored.
|
2023-06-29 18:20:22 +08:00
|
|
|
type StateUpdate struct {
|
|
|
|
// The type of update
|
|
|
|
Type StateUpdateType
|
|
|
|
|
2023-12-10 01:09:24 +08:00
|
|
|
// ChangeNodes must be set when Type is StatePeerAdded
|
|
|
|
// and StatePeerChanged and contains the full node
|
|
|
|
// object for added nodes.
|
|
|
|
ChangeNodes Nodes
|
|
|
|
|
|
|
|
// ChangePatches must be set when Type is StatePeerChangedPatch
|
|
|
|
// and contains a populated PeerChange object.
|
|
|
|
ChangePatches []*tailcfg.PeerChange
|
2023-06-29 18:20:22 +08:00
|
|
|
|
|
|
|
// Removed must be set when Type is StatePeerRemoved and
|
|
|
|
// contain a list of the nodes that has been removed from
|
|
|
|
// the network.
|
|
|
|
Removed []tailcfg.NodeID
|
|
|
|
|
|
|
|
// DERPMap must be set when Type is StateDERPUpdated and
|
|
|
|
// contain the new DERP Map.
|
2023-12-10 01:09:24 +08:00
|
|
|
DERPMap *tailcfg.DERPMap
|
|
|
|
|
|
|
|
// Additional message for tracking origin or what being
|
|
|
|
// updated, useful for ambiguous updates like StatePeerChanged.
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid reports if a StateUpdate is correctly filled and
|
|
|
|
// panics if the mandatory fields for a type is not
|
|
|
|
// filled.
|
|
|
|
// Reports true if valid.
|
|
|
|
func (su *StateUpdate) Valid() bool {
|
|
|
|
switch su.Type {
|
|
|
|
case StatePeerChanged:
|
|
|
|
if su.ChangeNodes == nil {
|
|
|
|
panic("Mandatory field ChangeNodes is not set on StatePeerChanged update")
|
|
|
|
}
|
|
|
|
case StatePeerChangedPatch:
|
|
|
|
if su.ChangePatches == nil {
|
|
|
|
panic("Mandatory field ChangePatches is not set on StatePeerChangedPatch update")
|
|
|
|
}
|
|
|
|
case StatePeerRemoved:
|
|
|
|
if su.Removed == nil {
|
|
|
|
panic("Mandatory field Removed is not set on StatePeerRemove update")
|
|
|
|
}
|
2024-01-05 17:41:56 +08:00
|
|
|
case StateSelfUpdate:
|
|
|
|
if su.ChangeNodes == nil || len(su.ChangeNodes) != 1 {
|
|
|
|
panic("Mandatory field ChangeNodes is not set for StateSelfUpdate or has more than one node")
|
|
|
|
}
|
2023-12-10 01:09:24 +08:00
|
|
|
case StateDERPUpdated:
|
|
|
|
if su.DERPMap == nil {
|
|
|
|
panic("Mandatory field DERPMap is not set on StateDERPUpdated update")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2023-06-29 18:20:22 +08:00
|
|
|
}
|