31878a72ec
- Add package-level architecture doc in handler.go covering: - NAT traversal reverse proxy architecture - CmdBind/CmdConnect roles and data flow - Connector lifecycle and waitClose semantics - Entrypoint protocol dispatch (first-byte sniffing) - SD fallback behavior - Add data-flow-oriented doc comments to all business files: - entrypoint.go: protocol dispatch, dial flow - connector.go: Connector/ConnectorPool semantics - dialer.go: two-phase dial strategy - tunnel.go: MaxWeight semantics, selection algorithm - bind.go: 6-step CmdBind flow - connect.go: CmdConnect flow with relay framing - ephttp.go, eptls.go, eprelay.go: per-protocol entrypoint flow - Fix observeStats: after successful error retry, also flush new events instead of skipping the current tick (handler.go:261-271) - Add test for observeStats retry-then-flush (handler_test.go)
94 lines
2.0 KiB
Go
94 lines
2.0 KiB
Go
package tunnel
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/go-gost/core/logger"
|
|
"github.com/go-gost/core/sd"
|
|
)
|
|
|
|
// Dialer resolves a tunnel connector and returns a stream to it.
|
|
//
|
|
// Dial strategy (two-phase):
|
|
// 1. Local pool: try ConnectorPool.Get() up to retry times. Each attempt
|
|
// calls GetConn() (= mux.OpenStream) on the same connector — if the
|
|
// connector is dead, all retries fail until Tunnel.clean() removes it
|
|
// (up to TTL, default 15s).
|
|
// 2. SD fallback: if pool returns nil AND sd is configured, query service
|
|
// discovery for remote nodes. Filter out self (d.node) and mismatched
|
|
// networks. Establish a raw TCP connection to the remote address,
|
|
// bypassing the mux layer entirely.
|
|
//
|
|
// The returned node and connector ID identify which node/hop the stream
|
|
// is connected to — used by callers to decide the relay protocol framing.
|
|
type Dialer struct {
|
|
node string
|
|
pool *ConnectorPool
|
|
sd sd.SD
|
|
retry int
|
|
timeout time.Duration
|
|
log logger.Logger
|
|
}
|
|
|
|
func (d *Dialer) Dial(ctx context.Context, network string, tid string) (conn net.Conn, node string, cid string, err error) {
|
|
retry := d.retry
|
|
if retry <= 0 {
|
|
retry = 1
|
|
}
|
|
|
|
for i := 0; i < retry; i++ {
|
|
c := d.pool.Get(network, tid)
|
|
if c == nil {
|
|
err = nil // clear stale err so SD fallback is not masked
|
|
break
|
|
}
|
|
|
|
conn, err = c.GetConn()
|
|
if err != nil {
|
|
d.log.Error(err)
|
|
continue
|
|
}
|
|
node = d.node
|
|
cid = c.id.String()
|
|
|
|
break
|
|
}
|
|
if conn != nil || err != nil {
|
|
return
|
|
}
|
|
|
|
if d.sd == nil {
|
|
err = ErrTunnelNotAvailable
|
|
return
|
|
}
|
|
|
|
ss, err := d.sd.Get(ctx, tid)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
var service *sd.Service
|
|
for _, s := range ss {
|
|
d.log.Debugf("%+v", s)
|
|
if s.Node != d.node && s.Network == network {
|
|
service = s
|
|
break
|
|
}
|
|
}
|
|
if service == nil || service.Address == "" {
|
|
err = ErrTunnelNotAvailable
|
|
return
|
|
}
|
|
|
|
node = service.Node
|
|
cid = service.ID
|
|
|
|
dialer := net.Dialer{
|
|
Timeout: d.timeout,
|
|
}
|
|
conn, err = dialer.DialContext(ctx, "tcp", service.Address)
|
|
return
|
|
}
|