2024-08-22 17:02:33 +02:00
|
|
|
package agent
|
2024-08-21 20:48:02 +02:00
|
|
|
|
2024-08-23 18:05:05 +02:00
|
|
|
import (
|
2024-08-23 18:31:02 +02:00
|
|
|
"github.com/openziti/zrok/agent/agentGrpc"
|
2024-08-23 18:05:05 +02:00
|
|
|
"github.com/openziti/zrok/environment/env_core"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"net"
|
2024-08-23 18:35:19 +02:00
|
|
|
"os"
|
2024-08-23 18:05:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Agent struct {
|
2024-08-23 18:35:19 +02:00
|
|
|
root env_core.Root
|
|
|
|
agentSocket string
|
|
|
|
shares map[string]*share
|
|
|
|
accesses map[string]*access
|
2024-08-21 20:48:02 +02:00
|
|
|
}
|
|
|
|
|
2024-08-23 18:05:05 +02:00
|
|
|
func NewAgent(root env_core.Root) (*Agent, error) {
|
|
|
|
if !root.IsEnabled() {
|
|
|
|
return nil, errors.Errorf("unable to load environment; did you 'zrok enable'?")
|
|
|
|
}
|
|
|
|
return &Agent{
|
|
|
|
root: root,
|
2024-08-21 20:48:02 +02:00
|
|
|
shares: make(map[string]*share),
|
|
|
|
accesses: make(map[string]*access),
|
2024-08-23 18:05:05 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Agent) Run() error {
|
|
|
|
logrus.Infof("started")
|
2024-08-23 18:35:19 +02:00
|
|
|
|
2024-08-23 18:05:05 +02:00
|
|
|
agentSocket, err := a.root.AgentSocket()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
l, err := net.Listen("unix", agentSocket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-08-23 18:35:19 +02:00
|
|
|
a.agentSocket = agentSocket
|
|
|
|
|
2024-08-23 18:05:05 +02:00
|
|
|
srv := grpc.NewServer()
|
2024-08-26 19:13:59 +02:00
|
|
|
agentGrpc.RegisterAgentServer(srv, &agentGrpcImpl{a: a})
|
2024-08-23 18:05:05 +02:00
|
|
|
if err := srv.Serve(l); err != nil {
|
|
|
|
return err
|
2024-08-21 20:48:02 +02:00
|
|
|
}
|
2024-08-23 18:35:19 +02:00
|
|
|
|
2024-08-23 18:05:05 +02:00
|
|
|
return nil
|
2024-08-21 20:48:02 +02:00
|
|
|
}
|
2024-08-23 18:35:19 +02:00
|
|
|
|
|
|
|
func (a *Agent) Shutdown() {
|
|
|
|
if err := os.Remove(a.agentSocket); err != nil {
|
|
|
|
logrus.Warnf("unable to remove agent socket: %v", err)
|
|
|
|
}
|
|
|
|
}
|