2023-11-13 11:44:14 +01:00
|
|
|
package api
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
import (
|
|
|
|
"time"
|
2023-12-11 10:55:25 +01:00
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/tim-beatham/smegmesh/pkg/ipc"
|
|
|
|
"github.com/tim-beatham/smegmesh/pkg/what8words"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Route is an advertised route in the data store
|
2023-11-20 19:07:52 +01:00
|
|
|
type Route struct {
|
2024-01-04 14:10:08 +01:00
|
|
|
// Prefix is the advertised route prefix
|
|
|
|
Prefix string `json:"prefix"`
|
|
|
|
// Path is the hops the destination
|
|
|
|
Path []string `json:"path"`
|
2023-11-20 19:07:52 +01:00
|
|
|
}
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// SmegStats is the WireGuard stats that the underlying host
|
|
|
|
// has sent to the peer
|
2023-12-11 10:55:25 +01:00
|
|
|
type SmegStats struct {
|
2024-01-04 14:10:08 +01:00
|
|
|
// TotalTransmit number of bytes sent to the peer
|
|
|
|
TotalTransmit int64 `json:"totalTransmit"`
|
|
|
|
// TotalReceived number of bytes received from the peer
|
|
|
|
TotalReceived int64 `json:"totalReceived"`
|
|
|
|
// KeepAliveInterval WireGuard keepalive interval that is sent to the host
|
2023-12-11 10:55:25 +01:00
|
|
|
KeepAliveInterval time.Duration `json:"keepaliveInterval"`
|
2024-01-04 14:10:08 +01:00
|
|
|
// AllowsIps is the allowed path to the destination
|
|
|
|
AllowedIps []string `json:"allowedIps"`
|
2023-12-11 10:55:25 +01:00
|
|
|
}
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// SmegNode is a node in the mesh network
|
2023-11-13 11:44:14 +01:00
|
|
|
type SmegNode struct {
|
2024-01-04 14:10:08 +01:00
|
|
|
// Alias is the human readable name that the node is assocaited with
|
|
|
|
Alias string `json:"alias"`
|
|
|
|
// WgHost is the WireGuard IP address of the node. This is an IPv6
|
|
|
|
// address
|
|
|
|
WgHost string `json:"wgHost"`
|
|
|
|
// WgEndpoint is the physical endpoint of the host that packets
|
|
|
|
// are forwarded to
|
|
|
|
WgEndpoint string `json:"wgEndpoint"`
|
|
|
|
// Endpoint is the control plane endpoint of the host which
|
|
|
|
// grpc connections are to be sent along
|
|
|
|
Endpoint string `json:"endpoint"`
|
|
|
|
// Timestamp is the last time the signified it was alive.
|
|
|
|
// if the node is the leader this is evert heartBeatInterval
|
|
|
|
// otherwise this is the time the node joined the network
|
|
|
|
Timestamp int `json:"timestamp"`
|
|
|
|
// Description is the human readable description of the node
|
|
|
|
Description string `json:"description"`
|
|
|
|
// PublicKey is the WireGuard public key of the node
|
|
|
|
PublicKey string `json:"publicKey"`
|
|
|
|
// Routes is the routes that the node is advertising
|
|
|
|
Routes []Route `json:"routes"`
|
|
|
|
// Services is information about services that the node offers
|
|
|
|
Services map[string]string `json:"services"`
|
|
|
|
// Stats is the WireGuard stats of the node (if any)
|
|
|
|
Stats SmegStats `json:"stats"`
|
2023-11-13 11:44:14 +01:00
|
|
|
}
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// SmegMesh encapsulates a single mesh in the API
|
2023-11-13 11:44:14 +01:00
|
|
|
type SmegMesh struct {
|
2024-01-04 14:10:08 +01:00
|
|
|
// MeshId is the mesh id of the network
|
|
|
|
MeshId string `json:"meshid"`
|
|
|
|
// Nodes is the nodes in the network keyed by their public
|
|
|
|
// key
|
|
|
|
Nodes map[string]SmegNode `json:"nodes"`
|
2023-11-13 11:44:14 +01:00
|
|
|
}
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// CreateMeshRequest encapsulates a request to create a mesh network
|
2023-11-13 11:44:14 +01:00
|
|
|
type CreateMeshRequest struct {
|
2024-01-04 14:10:08 +01:00
|
|
|
// WgPort is the WireGuard to create the mesh in
|
2023-11-20 16:22:32 +01:00
|
|
|
WgPort int `json:"port" binding:"omitempty,gte=1024,lt=65535"`
|
2024-01-05 17:59:02 +01:00
|
|
|
// Role is the role to take on in the mesh
|
|
|
|
Role string `json:"role" binding:"required,eq=client|eq=peer"`
|
|
|
|
// AdvertiseRoutes: advertise thi mesh to other meshes
|
|
|
|
AdvertiseRoutes bool `json:"advertiseRoutes"`
|
|
|
|
// AdvertiseDefaults: advertise an exit point
|
|
|
|
AdvertiseDefaults bool `json:"advertiseDefaults"`
|
|
|
|
// Alias: alias of the node in the mesh
|
|
|
|
Alias string `json:"alias"`
|
|
|
|
// Description: description of the node in the mesh
|
|
|
|
Description string `json:"description"`
|
|
|
|
// PublicEndpoint: an alternative public endpoint to advertise
|
|
|
|
PublicEndpoint string `json:"publicEndpoint"`
|
2023-11-13 11:44:14 +01:00
|
|
|
}
|
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// JoinMeshRequests encapsulates a request to create a mesh network
|
2023-11-13 11:44:14 +01:00
|
|
|
type JoinMeshRequest struct {
|
2024-01-04 14:10:08 +01:00
|
|
|
// WgPort is the WireGuard port to run the service on
|
|
|
|
WgPort int `json:"port" binding:"omitempty,gte=1024,lt=65535"`
|
|
|
|
// Bootstrap is a bootstrap node to use to join the network
|
2023-11-13 11:44:14 +01:00
|
|
|
Bootstrap string `json:"bootstrap" binding:"required"`
|
2024-01-04 14:10:08 +01:00
|
|
|
// MeshId is the ID of the mesh to join
|
|
|
|
MeshId string `json:"meshid" binding:"required"`
|
2024-01-05 17:59:02 +01:00
|
|
|
// Role is the role to take on in the mesh
|
|
|
|
Role string `json:"role" binding:"required,eq=client|eq=peer"`
|
|
|
|
// AdvertiseRoutes: advertise thi mesh to other meshes
|
|
|
|
AdvertiseRoutes bool `json:"advertiseRoutes"`
|
|
|
|
// AdvertiseDefaults: advertise an exit point
|
|
|
|
AdvertiseDefaults bool `json:"advertiseDefaults"`
|
|
|
|
// Alias: alias of the node in the mesh
|
|
|
|
Alias string `json:"alias"`
|
|
|
|
// Description: description of the node in the mesh
|
|
|
|
Description string `json:"description"`
|
|
|
|
// PublicEndpoint: an alternative public endpoint to advertise
|
|
|
|
PublicEndpoint string `json:"publicEndpoint"`
|
2023-11-13 11:44:14 +01:00
|
|
|
}
|
2023-11-20 19:07:52 +01:00
|
|
|
|
2024-01-04 14:10:08 +01:00
|
|
|
// ApiServerConf configuration to instantiate the API server
|
2023-11-20 19:07:52 +01:00
|
|
|
type ApiServerConf struct {
|
2024-01-04 14:10:08 +01:00
|
|
|
// WordsFile to use to map IP to words
|
2023-11-20 19:07:52 +01:00
|
|
|
WordsFile string
|
|
|
|
}
|
2024-01-04 14:10:08 +01:00
|
|
|
|
|
|
|
// SmegSever is the GIN api server that runs the service
|
|
|
|
type SmegServer struct {
|
|
|
|
// gin router to use
|
|
|
|
router *gin.Engine
|
|
|
|
// client to invoke operations
|
|
|
|
client *ipc.SmegmeshIpc
|
|
|
|
// what8words to use to convert IP to an alias
|
|
|
|
words *what8words.What8Words
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApiSever absrtacts the API server
|
|
|
|
type ApiServer interface {
|
|
|
|
Run(addr string) error
|
|
|
|
}
|