zrok/agent/model.go

102 lines
2.3 KiB
Go
Raw Normal View History

2024-08-22 17:02:33 +02:00
package agent
2024-08-21 20:48:02 +02:00
import (
"bytes"
"encoding/json"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/zrok/agent/agentGrpc"
"github.com/openziti/zrok/agent/proctree"
2024-08-21 20:48:02 +02:00
"github.com/openziti/zrok/sdk/golang/sdk"
"github.com/pkg/errors"
2024-09-12 20:12:46 +02:00
"strings"
2024-08-21 20:48:02 +02:00
"time"
)
type share struct {
2024-09-12 19:07:27 +02:00
token string
frontendEndpoints []string
2024-09-12 19:07:27 +02:00
target string
2024-08-21 20:48:02 +02:00
basicAuth []string
frontendSelection []string
shareMode sdk.ShareMode
2024-08-21 20:48:02 +02:00
backendMode sdk.BackendMode
reserved bool
2024-08-21 20:48:02 +02:00
insecure bool
oauthProvider string
oauthEmailAddressPatterns []string
oauthCheckInterval time.Duration
closed bool
accessGrants []string
process *proctree.Child
readBuffer bytes.Buffer
booted bool
bootComplete chan struct{}
bootErr error
a *Agent
}
func (s *share) monitor() {
if err := proctree.WaitChild(s.process); err != nil {
pfxlog.ChannelLogger(s.token).Error(err)
}
s.a.outShares <- s
}
func (s *share) tail(data []byte) {
s.readBuffer.Write(data)
if line, err := s.readBuffer.ReadString('\n'); err == nil {
line = strings.Trim(line, "\n")
if !s.booted {
in := make(map[string]interface{})
if err := json.Unmarshal([]byte(line), &in); err == nil {
if v, found := in["token"]; found {
if str, ok := v.(string); ok {
s.token = str
}
}
if v, found := in["frontend_endpoints"]; found {
if vArr, ok := v.([]interface{}); ok {
for _, v := range vArr {
if str, ok := v.(string); ok {
s.frontendEndpoints = append(s.frontendEndpoints, str)
}
}
}
}
s.booted = true
} else {
s.bootErr = errors.New(line)
}
close(s.bootComplete)
} else {
2024-09-12 20:12:46 +02:00
if strings.HasPrefix(line, "{") {
in := make(map[string]interface{})
if err := json.Unmarshal([]byte(line), &in); err == nil {
pfxlog.ChannelLogger(s.token).Info(in)
}
} else {
pfxlog.ChannelLogger(s.token).Info(strings.Trim(line, "\n"))
}
}
} else {
s.readBuffer.WriteString(line)
}
2024-08-21 20:48:02 +02:00
}
type access struct {
token string
bindAddress string
responseHeaders []string
process *proctree.Child
2024-08-21 20:48:02 +02:00
}
type agentGrpcImpl struct {
agentGrpc.UnimplementedAgentServer
a *Agent
}