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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Agent struct {
|
|
|
|
root env_core.Root
|
2024-08-21 20:48:02 +02:00
|
|
|
shares map[string]*share
|
|
|
|
accesses map[string]*access
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
agentSocket, err := a.root.AgentSocket()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
l, err := net.Listen("unix", agentSocket)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
srv := grpc.NewServer()
|
|
|
|
agentGrpc.RegisterAgentServer(srv, &agentGrpcImpl{})
|
|
|
|
if err := srv.Serve(l); err != nil {
|
|
|
|
return err
|
2024-08-21 20:48:02 +02:00
|
|
|
}
|
2024-08-23 18:05:05 +02:00
|
|
|
return nil
|
2024-08-21 20:48:02 +02:00
|
|
|
}
|