add observePeriod option for observer

This commit is contained in:
ginuerzh 2024-06-13 21:22:29 +08:00
parent 784e4b2b01
commit 6d819a0c06
13 changed files with 105 additions and 101 deletions

View File

@ -67,15 +67,17 @@ func ParseHop(cfg *config.HopConfig, log logger.Logger) (hop.Hop, error) {
} }
if v.Connector == nil { if v.Connector == nil {
v.Connector = &config.ConnectorConfig{ v.Connector = &config.ConnectorConfig{}
Type: "http",
} }
if strings.TrimSpace(v.Connector.Type) == "" {
v.Connector.Type = "http"
} }
if v.Dialer == nil { if v.Dialer == nil {
v.Dialer = &config.DialerConfig{ v.Dialer = &config.DialerConfig{}
Type: "tcp",
} }
if strings.TrimSpace(v.Dialer.Type) == "" {
v.Dialer.Type = "tcp"
} }
node, err := node_parser.ParseNode(cfg.Name, v, log) node, err := node_parser.ParseNode(cfg.Name, v, log)

View File

@ -3,6 +3,7 @@ package service
import ( import (
"fmt" "fmt"
"strings" "strings"
"time"
"github.com/go-gost/core/admission" "github.com/go-gost/core/admission"
"github.com/go-gost/core/auth" "github.com/go-gost/core/auth"
@ -37,14 +38,14 @@ func ParseService(cfg *config.ServiceConfig) (service.Service, error) {
if cfg.Listener == nil { if cfg.Listener == nil {
cfg.Listener = &config.ListenerConfig{} cfg.Listener = &config.ListenerConfig{}
} }
if lt := strings.TrimSpace(cfg.Listener.Type); lt == "" { if strings.TrimSpace(cfg.Listener.Type) == "" {
cfg.Listener.Type = "tcp" cfg.Listener.Type = "tcp"
} }
if cfg.Handler == nil { if cfg.Handler == nil {
cfg.Handler = &config.HandlerConfig{} cfg.Handler = &config.HandlerConfig{}
} }
if ht := strings.TrimSpace(cfg.Handler.Type); ht == "" { if strings.TrimSpace(cfg.Handler.Type) == "" {
cfg.Handler.Type = "auto" cfg.Handler.Type = "auto"
} }
@ -102,6 +103,7 @@ func ParseService(cfg *config.ServiceConfig) (service.Service, error) {
var preUp, preDown, postUp, postDown []string var preUp, preDown, postUp, postDown []string
var ignoreChain bool var ignoreChain bool
var pStats *stats.Stats var pStats *stats.Stats
var observePeriod time.Duration
if cfg.Metadata != nil { if cfg.Metadata != nil {
md := metadata.NewMetadata(cfg.Metadata) md := metadata.NewMetadata(cfg.Metadata)
ppv = mdutil.GetInt(md, parsing.MDKeyProxyProtocol) ppv = mdutil.GetInt(md, parsing.MDKeyProxyProtocol)
@ -122,6 +124,7 @@ func ParseService(cfg *config.ServiceConfig) (service.Service, error) {
if mdutil.GetBool(md, parsing.MDKeyEnableStats) { if mdutil.GetBool(md, parsing.MDKeyEnableStats) {
pStats = &stats.Stats{} pStats = &stats.Stats{}
} }
observePeriod = mdutil.GetDuration(md, "observePeriod")
} }
listenOpts := []listener.Option{ listenOpts := []listener.Option{
@ -263,6 +266,7 @@ func ParseService(cfg *config.ServiceConfig) (service.Service, error) {
xservice.RecordersOption(recorders...), xservice.RecordersOption(recorders...),
xservice.StatsOption(pStats), xservice.StatsOption(pStats),
xservice.ObserverOption(registry.ObserverRegistry().Get(cfg.Observer)), xservice.ObserverOption(registry.ObserverRegistry().Get(cfg.Observer)),
xservice.ObservePeriodOption(observePeriod),
xservice.LoggerOption(serviceLogger), xservice.LoggerOption(serviceLogger),
) )

View File

@ -452,7 +452,11 @@ func (h *httpHandler) observeStats(ctx context.Context) {
return return
} }
ticker := time.NewTicker(5 * time.Second) d := h.md.observePeriod
if d < time.Millisecond {
d = 5 * time.Second
}
ticker := time.NewTicker(d)
defer ticker.Stop() defer ticker.Stop()
for { for {

View File

@ -3,6 +3,7 @@ package http
import ( import (
"net/http" "net/http"
"strings" "strings"
"time"
mdata "github.com/go-gost/core/metadata" mdata "github.com/go-gost/core/metadata"
mdutil "github.com/go-gost/core/metadata/util" mdutil "github.com/go-gost/core/metadata/util"
@ -18,20 +19,11 @@ type metadata struct {
header http.Header header http.Header
hash string hash string
authBasicRealm string authBasicRealm string
observePeriod time.Duration
} }
func (h *httpHandler) parseMetadata(md mdata.Metadata) error { func (h *httpHandler) parseMetadata(md mdata.Metadata) error {
const ( if m := mdutil.GetStringMapString(md, "http.header", "header"); len(m) > 0 {
header = "header"
probeResistKey = "probeResistance"
probeResistKeyX = "probe_resist"
knock = "knock"
enableUDP = "udp"
hash = "hash"
authBasicRealm = "authBasicRealm"
)
if m := mdutil.GetStringMapString(md, header); len(m) > 0 {
hd := http.Header{} hd := http.Header{}
for k, v := range m { for k, v := range m {
hd.Add(k, v) hd.Add(k, v)
@ -39,22 +31,20 @@ func (h *httpHandler) parseMetadata(md mdata.Metadata) error {
h.md.header = hd h.md.header = hd
} }
pr := mdutil.GetString(md, probeResistKey) if pr := mdutil.GetString(md, "probeResist", "probe_resist"); pr != "" {
if pr == "" {
pr = mdutil.GetString(md, probeResistKeyX)
}
if pr != "" {
if ss := strings.SplitN(pr, ":", 2); len(ss) == 2 { if ss := strings.SplitN(pr, ":", 2); len(ss) == 2 {
h.md.probeResistance = &probeResistance{ h.md.probeResistance = &probeResistance{
Type: ss[0], Type: ss[0],
Value: ss[1], Value: ss[1],
Knock: mdutil.GetString(md, knock), Knock: mdutil.GetString(md, "knock"),
} }
} }
} }
h.md.enableUDP = mdutil.GetBool(md, enableUDP) h.md.enableUDP = mdutil.GetBool(md, "udp")
h.md.hash = mdutil.GetString(md, hash) h.md.hash = mdutil.GetString(md, "hash")
h.md.authBasicRealm = mdutil.GetString(md, authBasicRealm) h.md.authBasicRealm = mdutil.GetString(md, "authBasicRealm")
h.md.observePeriod = mdutil.GetDuration(md, "observePeriod")
return nil return nil
} }

View File

@ -419,7 +419,11 @@ func (h *http2Handler) observeStats(ctx context.Context) {
return return
} }
ticker := time.NewTicker(5 * time.Second) d := h.md.observePeriod
if d < time.Millisecond {
d = 5 * time.Second
}
ticker := time.NewTicker(d)
defer ticker.Stop() defer ticker.Stop()
for { for {

View File

@ -3,6 +3,7 @@ package http2
import ( import (
"net/http" "net/http"
"strings" "strings"
"time"
mdata "github.com/go-gost/core/metadata" mdata "github.com/go-gost/core/metadata"
mdutil "github.com/go-gost/core/metadata/util" mdutil "github.com/go-gost/core/metadata/util"
@ -17,19 +18,11 @@ type metadata struct {
header http.Header header http.Header
hash string hash string
authBasicRealm string authBasicRealm string
observePeriod time.Duration
} }
func (h *http2Handler) parseMetadata(md mdata.Metadata) error { func (h *http2Handler) parseMetadata(md mdata.Metadata) error {
const ( if m := mdutil.GetStringMapString(md, "http.header", "header"); len(m) > 0 {
header = "header"
probeResistKey = "probeResistance"
probeResistKeyX = "probe_resist"
knock = "knock"
hash = "hash"
authBasicRealm = "authBasicRealm"
)
if m := mdutil.GetStringMapString(md, header); len(m) > 0 {
hd := http.Header{} hd := http.Header{}
for k, v := range m { for k, v := range m {
hd.Add(k, v) hd.Add(k, v)
@ -37,21 +30,19 @@ func (h *http2Handler) parseMetadata(md mdata.Metadata) error {
h.md.header = hd h.md.header = hd
} }
pr := mdutil.GetString(md, probeResistKey) if pr := mdutil.GetString(md, "probeResist", "probe_resist"); pr != "" {
if pr == "" {
pr = mdutil.GetString(md, probeResistKeyX)
}
if pr != "" {
if ss := strings.SplitN(pr, ":", 2); len(ss) == 2 { if ss := strings.SplitN(pr, ":", 2); len(ss) == 2 {
h.md.probeResistance = &probeResistance{ h.md.probeResistance = &probeResistance{
Type: ss[0], Type: ss[0],
Value: ss[1], Value: ss[1],
Knock: mdutil.GetString(md, knock), Knock: mdutil.GetString(md, "knock"),
} }
} }
} }
h.md.hash = mdutil.GetString(md, hash) h.md.hash = mdutil.GetString(md, "hash")
h.md.authBasicRealm = mdutil.GetString(md, authBasicRealm) h.md.authBasicRealm = mdutil.GetString(md, "authBasicRealm")
h.md.observePeriod = mdutil.GetDuration(md, "observePeriod")
return nil return nil
} }

View File

@ -204,7 +204,11 @@ func (h *relayHandler) observeStats(ctx context.Context) {
return return
} }
ticker := time.NewTicker(5 * time.Second) d := h.md.observePeriod
if d < time.Millisecond {
d = 5 * time.Second
}
ticker := time.NewTicker(d)
defer ticker.Stop() defer ticker.Stop()
for { for {

View File

@ -16,28 +16,21 @@ type metadata struct {
noDelay bool noDelay bool
hash string hash string
muxCfg *mux.Config muxCfg *mux.Config
observePeriod time.Duration
} }
func (h *relayHandler) parseMetadata(md mdata.Metadata) (err error) { func (h *relayHandler) parseMetadata(md mdata.Metadata) (err error) {
const ( h.md.readTimeout = mdutil.GetDuration(md, "readTimeout")
readTimeout = "readTimeout" h.md.enableBind = mdutil.GetBool(md, "bind")
enableBind = "bind" h.md.noDelay = mdutil.GetBool(md, "nodelay")
udpBufferSize = "udpBufferSize"
noDelay = "nodelay"
hash = "hash"
)
h.md.readTimeout = mdutil.GetDuration(md, readTimeout) if bs := mdutil.GetInt(md, "udpBufferSize"); bs > 0 {
h.md.enableBind = mdutil.GetBool(md, enableBind)
h.md.noDelay = mdutil.GetBool(md, noDelay)
if bs := mdutil.GetInt(md, udpBufferSize); bs > 0 {
h.md.udpBufferSize = int(math.Min(math.Max(float64(bs), 512), 64*1024)) h.md.udpBufferSize = int(math.Min(math.Max(float64(bs), 512), 64*1024))
} else { } else {
h.md.udpBufferSize = 4096 h.md.udpBufferSize = 4096
} }
h.md.hash = mdutil.GetString(md, hash) h.md.hash = mdutil.GetString(md, "hash")
h.md.muxCfg = &mux.Config{ h.md.muxCfg = &mux.Config{
Version: mdutil.GetInt(md, "mux.version"), Version: mdutil.GetInt(md, "mux.version"),
@ -49,5 +42,7 @@ func (h *relayHandler) parseMetadata(md mdata.Metadata) (err error) {
MaxStreamBuffer: mdutil.GetInt(md, "mux.maxStreamBuffer"), MaxStreamBuffer: mdutil.GetInt(md, "mux.maxStreamBuffer"),
} }
h.md.observePeriod = mdutil.GetDuration(md, "observePeriod")
return return
} }

View File

@ -14,9 +14,9 @@ import (
"github.com/go-gost/gosocks4" "github.com/go-gost/gosocks4"
ctxvalue "github.com/go-gost/x/ctx" ctxvalue "github.com/go-gost/x/ctx"
netpkg "github.com/go-gost/x/internal/net" netpkg "github.com/go-gost/x/internal/net"
stats_util "github.com/go-gost/x/internal/util/stats"
"github.com/go-gost/x/limiter/traffic/wrapper" "github.com/go-gost/x/limiter/traffic/wrapper"
"github.com/go-gost/x/registry" "github.com/go-gost/x/registry"
stats_util "github.com/go-gost/x/internal/util/stats"
"github.com/go-gost/x/stats" "github.com/go-gost/x/stats"
stats_wrapper "github.com/go-gost/x/stats/wrapper" stats_wrapper "github.com/go-gost/x/stats/wrapper"
) )
@ -218,7 +218,11 @@ func (h *socks4Handler) observeStats(ctx context.Context) {
return return
} }
ticker := time.NewTicker(5 * time.Second) d := h.md.observePeriod
if d < time.Millisecond {
d = 5 * time.Second
}
ticker := time.NewTicker(d)
defer ticker.Stop() defer ticker.Stop()
for { for {

View File

@ -10,15 +10,12 @@ import (
type metadata struct { type metadata struct {
readTimeout time.Duration readTimeout time.Duration
hash string hash string
observePeriod time.Duration
} }
func (h *socks4Handler) parseMetadata(md mdata.Metadata) (err error) { func (h *socks4Handler) parseMetadata(md mdata.Metadata) (err error) {
const ( h.md.readTimeout = mdutil.GetDuration(md, "readTimeout")
readTimeout = "readTimeout" h.md.hash = mdutil.GetString(md, "hash")
hash = "hash" h.md.observePeriod = mdutil.GetDuration(md, "observePeriod")
)
h.md.readTimeout = mdutil.GetDuration(md, readTimeout)
h.md.hash = mdutil.GetString(md, hash)
return return
} }

View File

@ -160,7 +160,11 @@ func (h *socks5Handler) observeStats(ctx context.Context) {
return return
} }
ticker := time.NewTicker(5 * time.Second) d := h.md.observePeriod
if d < time.Millisecond {
d = 5 * time.Second
}
ticker := time.NewTicker(d)
defer ticker.Stop() defer ticker.Stop()
for { for {

View File

@ -18,32 +18,23 @@ type metadata struct {
compatibilityMode bool compatibilityMode bool
hash string hash string
muxCfg *mux.Config muxCfg *mux.Config
observePeriod time.Duration
} }
func (h *socks5Handler) parseMetadata(md mdata.Metadata) (err error) { func (h *socks5Handler) parseMetadata(md mdata.Metadata) (err error) {
const ( h.md.readTimeout = mdutil.GetDuration(md, "readTimeout")
readTimeout = "readTimeout" h.md.noTLS = mdutil.GetBool(md, "notls")
noTLS = "notls" h.md.enableBind = mdutil.GetBool(md, "bind")
enableBind = "bind" h.md.enableUDP = mdutil.GetBool(md, "udp")
enableUDP = "udp"
udpBufferSize = "udpBufferSize"
compatibilityMode = "comp"
hash = "hash"
)
h.md.readTimeout = mdutil.GetDuration(md, readTimeout) if bs := mdutil.GetInt(md, "udpBufferSize"); bs > 0 {
h.md.noTLS = mdutil.GetBool(md, noTLS)
h.md.enableBind = mdutil.GetBool(md, enableBind)
h.md.enableUDP = mdutil.GetBool(md, enableUDP)
if bs := mdutil.GetInt(md, udpBufferSize); bs > 0 {
h.md.udpBufferSize = int(math.Min(math.Max(float64(bs), 512), 64*1024)) h.md.udpBufferSize = int(math.Min(math.Max(float64(bs), 512), 64*1024))
} else { } else {
h.md.udpBufferSize = 4096 h.md.udpBufferSize = 4096
} }
h.md.compatibilityMode = mdutil.GetBool(md, compatibilityMode) h.md.compatibilityMode = mdutil.GetBool(md, "comp")
h.md.hash = mdutil.GetString(md, hash) h.md.hash = mdutil.GetString(md, "hash")
h.md.muxCfg = &mux.Config{ h.md.muxCfg = &mux.Config{
Version: mdutil.GetInt(md, "mux.version"), Version: mdutil.GetInt(md, "mux.version"),
@ -55,5 +46,7 @@ func (h *socks5Handler) parseMetadata(md mdata.Metadata) (err error) {
MaxStreamBuffer: mdutil.GetInt(md, "mux.maxStreamBuffer"), MaxStreamBuffer: mdutil.GetInt(md, "mux.maxStreamBuffer"),
} }
h.md.observePeriod = mdutil.GetDuration(md, "observePeriod")
return nil return nil
} }

View File

@ -32,6 +32,7 @@ type options struct {
postDown []string postDown []string
stats *stats.Stats stats *stats.Stats
observer observer.Observer observer observer.Observer
observePeriod time.Duration
logger logger.Logger logger logger.Logger
} }
@ -85,6 +86,12 @@ func ObserverOption(observer observer.Observer) Option {
} }
} }
func ObservePeriodOption(period time.Duration) Option {
return func(opts *options) {
opts.observePeriod = period
}
}
func LoggerOption(logger logger.Logger) Option { func LoggerOption(logger logger.Logger) Option {
return func(opts *options) { return func(opts *options) {
opts.logger = logger opts.logger = logger
@ -294,7 +301,12 @@ func (s *defaultService) observeStats(ctx context.Context) {
return return
} }
ticker := time.NewTicker(5 * time.Second) d := s.options.observePeriod
if d < time.Millisecond {
d = 5 * time.Second
}
ticker := time.NewTicker(d)
defer ticker.Stop() defer ticker.Stop()
for { for {