2023-05-21 16:37:59 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2024-02-08 16:28:19 +00:00
|
|
|
"context"
|
2023-05-21 16:37:59 +00:00
|
|
|
"database/sql/driver"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/netip"
|
2024-02-08 16:28:19 +00:00
|
|
|
"time"
|
2023-05-21 16:37:59 +00:00
|
|
|
|
|
|
|
"tailscale.com/tailcfg"
|
2024-04-27 08:47:39 +00:00
|
|
|
"tailscale.com/util/ctxkey"
|
2023-05-21 16:37:59 +00:00
|
|
|
)
|
|
|
|
|
2024-02-09 06:27:00 +00:00
|
|
|
const (
|
|
|
|
SelfUpdateIdentifier = "self-update"
|
|
|
|
DatabasePostgres = "postgres"
|
|
|
|
DatabaseSqlite = "sqlite3"
|
|
|
|
)
|
2024-02-08 16:28:19 +00:00
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
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 11:42:05 +00:00
|
|
|
return fmt.Errorf("%w: unexpected data type %T", ErrNodeAddressesInvalid, destination)
|
2023-05-21 16:37:59 +00: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 11:42:05 +00:00
|
|
|
return fmt.Errorf("%w: unexpected data type %T", ErrNodeAddressesInvalid, destination)
|
2023-05-21 16:37:59 +00: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 10:20:22 +00:00
|
|
|
|
|
|
|
type StateUpdateType int
|
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
func (su StateUpdateType) String() string {
|
|
|
|
switch su {
|
|
|
|
case StateFullUpdate:
|
|
|
|
return "StateFullUpdate"
|
|
|
|
case StatePeerChanged:
|
|
|
|
return "StatePeerChanged"
|
|
|
|
case StatePeerChangedPatch:
|
|
|
|
return "StatePeerChangedPatch"
|
|
|
|
case StatePeerRemoved:
|
|
|
|
return "StatePeerRemoved"
|
|
|
|
case StateSelfUpdate:
|
|
|
|
return "StateSelfUpdate"
|
|
|
|
case StateDERPUpdated:
|
|
|
|
return "StateDERPUpdated"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "unknown state update type"
|
|
|
|
}
|
|
|
|
|
2023-06-29 10:20:22 +00:00
|
|
|
const (
|
|
|
|
StateFullUpdate StateUpdateType = iota
|
2023-12-09 17:09:24 +00: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 10:20:22 +00:00
|
|
|
StatePeerChanged
|
2023-12-09 17:09:24 +00:00
|
|
|
StatePeerChangedPatch
|
2023-06-29 10:20:22 +00:00
|
|
|
StatePeerRemoved
|
2024-01-05 09:41:56 +00: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 10:20:22 +00:00
|
|
|
StateDERPUpdated
|
|
|
|
)
|
|
|
|
|
|
|
|
// StateUpdate is an internal message containing information about
|
|
|
|
// a state change that has happened to the network.
|
2023-12-09 17:09:24 +00:00
|
|
|
// If type is StateFullUpdate, all fields are ignored.
|
2023-06-29 10:20:22 +00:00
|
|
|
type StateUpdate struct {
|
|
|
|
// The type of update
|
|
|
|
Type StateUpdateType
|
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
// ChangeNodes must be set when Type is StatePeerAdded
|
|
|
|
// and StatePeerChanged and contains the full node
|
|
|
|
// object for added nodes.
|
2024-02-23 09:59:24 +00:00
|
|
|
ChangeNodes []NodeID
|
2023-12-09 17:09:24 +00:00
|
|
|
|
|
|
|
// ChangePatches must be set when Type is StatePeerChangedPatch
|
|
|
|
// and contains a populated PeerChange object.
|
|
|
|
ChangePatches []*tailcfg.PeerChange
|
2023-06-29 10:20:22 +00:00
|
|
|
|
|
|
|
// Removed must be set when Type is StatePeerRemoved and
|
|
|
|
// contain a list of the nodes that has been removed from
|
|
|
|
// the network.
|
2024-02-23 09:59:24 +00:00
|
|
|
Removed []NodeID
|
2023-06-29 10:20:22 +00:00
|
|
|
|
|
|
|
// DERPMap must be set when Type is StateDERPUpdated and
|
|
|
|
// contain the new DERP Map.
|
2023-12-09 17:09:24 +00:00
|
|
|
DERPMap *tailcfg.DERPMap
|
|
|
|
|
|
|
|
// Additional message for tracking origin or what being
|
|
|
|
// updated, useful for ambiguous updates like StatePeerChanged.
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
// Empty reports if there are any updates in the StateUpdate.
|
|
|
|
func (su *StateUpdate) Empty() bool {
|
|
|
|
switch su.Type {
|
|
|
|
case StatePeerChanged:
|
|
|
|
return len(su.ChangeNodes) == 0
|
|
|
|
case StatePeerChangedPatch:
|
|
|
|
return len(su.ChangePatches) == 0
|
|
|
|
case StatePeerRemoved:
|
|
|
|
return len(su.Removed) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-02-23 09:59:24 +00:00
|
|
|
func StateUpdateExpire(nodeID NodeID, expiry time.Time) StateUpdate {
|
2024-02-08 16:28:19 +00:00
|
|
|
return StateUpdate{
|
|
|
|
Type: StatePeerChangedPatch,
|
|
|
|
ChangePatches: []*tailcfg.PeerChange{
|
|
|
|
{
|
2024-02-23 09:59:24 +00:00
|
|
|
NodeID: nodeID.NodeID(),
|
2024-02-08 16:28:19 +00:00
|
|
|
KeyExpiry: &expiry,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-27 08:47:39 +00:00
|
|
|
var (
|
|
|
|
NotifyOriginKey = ctxkey.New("notify.origin", "")
|
|
|
|
NotifyHostnameKey = ctxkey.New("notify.hostname", "")
|
|
|
|
)
|
|
|
|
|
2024-02-08 16:28:19 +00:00
|
|
|
func NotifyCtx(ctx context.Context, origin, hostname string) context.Context {
|
2024-04-27 08:47:39 +00:00
|
|
|
ctx2, _ := context.WithTimeout(ctx, 3*time.Second)
|
|
|
|
ctx2 = NotifyOriginKey.WithValue(ctx2, origin)
|
|
|
|
ctx2 = NotifyHostnameKey.WithValue(ctx2, hostname)
|
2024-02-08 16:28:19 +00:00
|
|
|
return ctx2
|
|
|
|
}
|