2024-09-13 20:40:55 +02:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2024-09-17 03:32:17 +02:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"github.com/michaelquigley/pfxlog"
|
2024-09-13 20:40:55 +02:00
|
|
|
"github.com/openziti/zrok/agent/proctree"
|
2024-09-17 03:32:17 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"strings"
|
2024-09-13 20:40:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type access struct {
|
2024-09-17 03:32:17 +02:00
|
|
|
frontendToken string
|
|
|
|
token string
|
2024-09-13 20:40:55 +02:00
|
|
|
bindAddress string
|
|
|
|
responseHeaders []string
|
|
|
|
|
2024-09-17 03:32:17 +02:00
|
|
|
process *proctree.Child
|
|
|
|
readBuffer bytes.Buffer
|
|
|
|
booted bool
|
|
|
|
bootComplete chan struct{}
|
|
|
|
bootErr error
|
2024-09-13 20:40:55 +02:00
|
|
|
|
|
|
|
a *Agent
|
|
|
|
}
|
2024-09-17 03:32:17 +02:00
|
|
|
|
|
|
|
func (a *access) monitor() {
|
|
|
|
if err := proctree.WaitChild(a.process); err != nil {
|
|
|
|
pfxlog.ChannelLogger(a.token).Error(err)
|
|
|
|
}
|
|
|
|
a.a.outAccesses <- a
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *access) tail(data []byte) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
logrus.Errorf("recovering: %v", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
a.readBuffer.Write(data)
|
|
|
|
if line, err := a.readBuffer.ReadString('\n'); err == nil {
|
|
|
|
line = strings.Trim(line, "\n")
|
|
|
|
if !a.booted {
|
|
|
|
in := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal([]byte(line), &in); err == nil {
|
|
|
|
if v, found := in["frontend-token"]; found {
|
|
|
|
if str, ok := v.(string); ok {
|
|
|
|
a.frontendToken = str
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a.booted = true
|
|
|
|
} else {
|
|
|
|
a.bootErr = errors.New(line)
|
|
|
|
}
|
|
|
|
close(a.bootComplete)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if strings.HasPrefix(line, "{") {
|
|
|
|
in := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal([]byte(line), &in); err == nil {
|
|
|
|
pfxlog.ChannelLogger(a.token).Info(in)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pfxlog.ChannelLogger(a.token).Info(strings.Trim(line, "\n"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
a.readBuffer.WriteString(line)
|
|
|
|
}
|
|
|
|
}
|