2024-08-22 17:02:33 +02:00
|
|
|
package agent
|
2024-08-21 20:48:02 +02:00
|
|
|
|
|
|
|
import (
|
2024-09-12 20:05:17 +02:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2024-09-13 20:40:55 +02:00
|
|
|
"errors"
|
2024-09-12 20:05:17 +02:00
|
|
|
"github.com/michaelquigley/pfxlog"
|
|
|
|
"github.com/openziti/zrok/agent/proctree"
|
2024-08-21 20:48:02 +02:00
|
|
|
"github.com/openziti/zrok/sdk/golang/sdk"
|
2024-09-16 20:21:23 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
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
|
2024-09-12 20:05:17 +02:00
|
|
|
frontendEndpoints []string
|
2024-09-12 19:07:27 +02:00
|
|
|
target string
|
2024-08-21 20:48:02 +02:00
|
|
|
basicAuth []string
|
|
|
|
frontendSelection []string
|
2024-08-26 19:13:59 +02:00
|
|
|
shareMode sdk.ShareMode
|
2024-08-21 20:48:02 +02:00
|
|
|
backendMode sdk.BackendMode
|
2024-08-26 19:13:59 +02:00
|
|
|
reserved bool
|
2024-08-21 20:48:02 +02:00
|
|
|
insecure bool
|
|
|
|
oauthProvider string
|
|
|
|
oauthEmailAddressPatterns []string
|
|
|
|
oauthCheckInterval time.Duration
|
|
|
|
closed bool
|
|
|
|
accessGrants []string
|
2024-08-26 20:18:17 +02:00
|
|
|
|
2024-09-13 20:39:30 +02:00
|
|
|
process *proctree.Child
|
|
|
|
readBuffer bytes.Buffer
|
|
|
|
booted bool
|
|
|
|
bootComplete chan struct{}
|
|
|
|
bootErr error
|
|
|
|
|
2024-09-25 17:06:06 +02:00
|
|
|
agent *Agent
|
2024-09-13 20:39:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *share) monitor() {
|
|
|
|
if err := proctree.WaitChild(s.process); err != nil {
|
|
|
|
pfxlog.ChannelLogger(s.token).Error(err)
|
|
|
|
}
|
2024-09-25 17:06:06 +02:00
|
|
|
s.agent.rmShare <- s
|
2024-09-12 20:05:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *share) tail(data []byte) {
|
2024-09-16 20:21:23 +02:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
logrus.Errorf("recovering: %v", r)
|
|
|
|
}
|
|
|
|
}()
|
2024-09-12 20:05:17 +02:00
|
|
|
s.readBuffer.Write(data)
|
|
|
|
if line, err := s.readBuffer.ReadString('\n'); err == nil {
|
2024-09-13 20:39:30 +02:00
|
|
|
line = strings.Trim(line, "\n")
|
|
|
|
if !s.booted {
|
2024-09-12 20:05:17 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2024-09-17 18:54:17 +02:00
|
|
|
if v, found := in["backend_mode"]; found {
|
|
|
|
if str, ok := v.(string); ok {
|
|
|
|
s.backendMode = sdk.BackendMode(str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v, found := in["share_mode"]; found {
|
|
|
|
if str, ok := v.(string); ok {
|
|
|
|
s.shareMode = sdk.ShareMode(str)
|
|
|
|
}
|
|
|
|
}
|
2024-09-12 20:05:17 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-17 18:54:17 +02:00
|
|
|
if v, found := in["target"]; found {
|
|
|
|
if str, ok := v.(string); ok {
|
|
|
|
s.target = str
|
|
|
|
}
|
|
|
|
}
|
2024-09-13 20:39:30 +02:00
|
|
|
s.booted = true
|
|
|
|
} else {
|
|
|
|
s.bootErr = errors.New(line)
|
2024-09-12 20:05:17 +02:00
|
|
|
}
|
2024-09-13 20:39:30 +02:00
|
|
|
close(s.bootComplete)
|
|
|
|
|
2024-09-12 20:05:17 +02:00
|
|
|
} 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"))
|
|
|
|
}
|
2024-09-12 20:05:17 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s.readBuffer.WriteString(line)
|
|
|
|
}
|
2024-08-21 20:48:02 +02:00
|
|
|
}
|