Files
ginuerzh bc06c7f5da x: wire MetadataFeature (relay v0.7.0) for tunnel recording mode negotiation
Tunnel connectors send record.mode via MetadataFeature in relay requests.
The handler parses MetadataFeature, stores on tunnel during bind, and
propagates to entrypoint services where recorder context applies it
(per-request Gost-Record header still takes precedence).

- connector/tunnel: send record.mode MetadataFeature in Connect/Bind
- connector/tunnel: parse record.mode from metadata config
- handler/tunnel: parse MetadataFeature from relay request features
- handler/tunnel: pass connMetadata through handleBind → pool.SetMetadata
- handler/tunnel: SetMetadata/GetMetadata on ConnectorPool + Tunnel
- handler/tunnel/entrypointsvc: apply pool metadata to recorder context
- deps: bump relay v0.6.2 → v0.7.0
- sniffer_sse.go: whitespace gofmt fixes
2026-07-24 22:19:41 +08:00

119 lines
2.8 KiB
Go

package tunnel
import (
"context"
"fmt"
"net"
"strconv"
"github.com/go-gost/core/connector"
"github.com/go-gost/relay"
"github.com/go-gost/x/internal/util/mux"
)
// Bind implements connector.Binder.
func (c *tunnelConnector) Bind(ctx context.Context, conn net.Conn, network, address string, opts ...connector.BindOption) (net.Listener, error) {
addr, cid, err := c.initTunnel(conn, network, address)
if err != nil {
return nil, err
}
endpoint := addr.String()
if v, _, _ := net.SplitHostPort(addr.String()); v != "" {
endpoint = v
}
log := c.options.Logger.WithFields(map[string]any{
"endpoint": endpoint,
"tunnel": c.md.tunnelID.String(),
})
log.Infof("create tunnel on %s/%s OK, tunnel=%s, connector=%s, weight=%d",
addr, network, c.md.tunnelID.String(), cid, cid.Weight())
session, err := mux.ServerSession(conn, c.md.muxCfg)
if err != nil {
return nil, err
}
return &bindListener{
network: network,
addr: addr,
session: session,
logger: log,
}, nil
}
func (c *tunnelConnector) initTunnel(conn net.Conn, network, address string) (addr net.Addr, cid relay.ConnectorID, err error) {
req := relay.Request{
Version: relay.Version1,
Cmd: relay.CmdBind,
}
if network == "udp" {
req.Cmd |= relay.FUDP
req.Features = append(req.Features, &relay.NetworkFeature{
Network: relay.NetworkUDP,
})
}
if c.options.Auth != nil {
pwd, _ := c.options.Auth.Password()
req.Features = append(req.Features, &relay.UserAuthFeature{
Username: c.options.Auth.Username(),
Password: pwd,
})
}
af := &relay.AddrFeature{}
af.ParseFrom(conn.LocalAddr().String()) // src address
req.Features = append(req.Features, af)
af = &relay.AddrFeature{}
if h, p, e := net.SplitHostPort(address); e == nil && h == "" {
address = net.JoinHostPort("0.0.0.0", p)
}
af.ParseFrom(address)
req.Features = append(req.Features, af) // dst address
req.Features = append(req.Features, &relay.TunnelFeature{
ID: c.md.tunnelID,
})
if c.md.recordMode != "" {
req.Features = append(req.Features, &relay.MetadataFeature{
KVs: map[string]string{"record.mode": c.md.recordMode},
})
}
if _, err = req.WriteTo(conn); err != nil {
return
}
// first reply, bind status
resp := relay.Response{}
if _, err = resp.ReadFrom(conn); err != nil {
return
}
if resp.Status != relay.StatusOK {
err = fmt.Errorf("%d: create tunnel %s failed", resp.Status, c.md.tunnelID.String())
return
}
for _, f := range resp.Features {
switch f.Type() {
case relay.FeatureAddr:
if feature, _ := f.(*relay.AddrFeature); feature != nil {
addr = &bindAddr{
network: network,
addr: net.JoinHostPort(feature.Host, strconv.Itoa(int(feature.Port))),
}
}
case relay.FeatureTunnel:
if feature, _ := f.(*relay.TunnelFeature); feature != nil {
cid = feature.ID
}
}
}
return
}