2024-09-17 03:32:17 +02:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2024-11-07 19:48:41 +01:00
|
|
|
"fmt"
|
2024-09-17 03:32:17 +02:00
|
|
|
"github.com/openziti/zrok/agent/agentGrpc"
|
|
|
|
"github.com/openziti/zrok/agent/proctree"
|
|
|
|
"github.com/openziti/zrok/environment"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2024-09-18 17:54:37 +02:00
|
|
|
func (i *agentGrpcImpl) AccessPrivate(_ context.Context, req *agentGrpc.AccessPrivateRequest) (*agentGrpc.AccessPrivateResponse, error) {
|
2024-09-17 03:32:17 +02:00
|
|
|
root, err := environment.LoadRoot()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !root.IsEnabled() {
|
|
|
|
return nil, errors.New("unable to load environment; did you 'zrok enable'?")
|
|
|
|
}
|
|
|
|
|
2024-09-25 17:16:56 +02:00
|
|
|
accCmd := []string{os.Args[0], "access", "private", "--subordinate", "-b", req.BindAddress, req.Token}
|
2024-11-07 19:48:41 +01:00
|
|
|
if req.AutoMode {
|
|
|
|
accCmd = append(accCmd, "--auto", "--auto-address", req.AutoAddress, "--auto-start-port", fmt.Sprintf("%v", req.AutoStartPort))
|
|
|
|
accCmd = append(accCmd, "--auto-end-port", fmt.Sprintf("%v", req.AutoEndPort))
|
|
|
|
}
|
|
|
|
logrus.Info(accCmd)
|
|
|
|
|
2024-09-17 03:32:17 +02:00
|
|
|
acc := &access{
|
|
|
|
token: req.Token,
|
|
|
|
bindAddress: req.BindAddress,
|
2024-11-07 19:48:41 +01:00
|
|
|
autoMode: req.AutoMode,
|
|
|
|
autoAddress: req.AutoAddress,
|
|
|
|
autoStartPort: uint16(req.AutoStartPort),
|
|
|
|
autoEndPort: uint16(req.AutoEndPort),
|
2024-09-17 03:32:17 +02:00
|
|
|
responseHeaders: req.ResponseHeaders,
|
|
|
|
bootComplete: make(chan struct{}),
|
2024-09-25 17:06:06 +02:00
|
|
|
agent: i.agent,
|
2024-09-17 03:32:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Infof("executing '%v'", accCmd)
|
|
|
|
|
|
|
|
acc.process, err = proctree.StartChild(acc.tail, accCmd...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
<-acc.bootComplete
|
|
|
|
|
|
|
|
if acc.bootErr == nil {
|
2024-11-12 18:51:41 +01:00
|
|
|
go acc.monitor()
|
2024-09-25 17:06:06 +02:00
|
|
|
i.agent.addAccess <- acc
|
2024-09-18 17:54:37 +02:00
|
|
|
return &agentGrpc.AccessPrivateResponse{FrontendToken: acc.frontendToken}, nil
|
2024-09-17 03:32:17 +02:00
|
|
|
}
|
|
|
|
return nil, acc.bootErr
|
|
|
|
}
|