add sd for tunnel
This commit is contained in:
@ -11,7 +11,7 @@ import (
|
||||
xnet "github.com/go-gost/x/internal/net"
|
||||
)
|
||||
|
||||
func (h *tunnelHandler) handleConnect(ctx context.Context, conn net.Conn, network, srcAddr string, dstAddr string, tunnelID relay.TunnelID, log logger.Logger) error {
|
||||
func (h *tunnelHandler) handleConnect(ctx context.Context, req *relay.Request, conn net.Conn, network, srcAddr string, dstAddr string, tunnelID relay.TunnelID, log logger.Logger) error {
|
||||
log = log.WithFields(map[string]any{
|
||||
"dst": fmt.Sprintf("%s/%s", dstAddr, network),
|
||||
"cmd": "connect",
|
||||
@ -33,58 +33,68 @@ func (h *tunnelHandler) handleConnect(ctx context.Context, conn net.Conn, networ
|
||||
host, _, _ := net.SplitHostPort(dstAddr)
|
||||
|
||||
// client is a public entrypoint.
|
||||
if tunnelID.Equal(h.md.entryPointID) && !h.md.entryPointID.IsZero() {
|
||||
if tunnelID.Equal(h.md.entryPointID) {
|
||||
resp.WriteTo(conn)
|
||||
return h.ep.handle(ctx, conn)
|
||||
}
|
||||
|
||||
var tid relay.TunnelID
|
||||
if ingress := h.md.ingress; ingress != nil && host != "" {
|
||||
tid = parseTunnelID(ingress.Get(ctx, host))
|
||||
if !h.md.directTunnel {
|
||||
var tid relay.TunnelID
|
||||
if ingress := h.md.ingress; ingress != nil && host != "" {
|
||||
tid = parseTunnelID(ingress.Get(ctx, host))
|
||||
}
|
||||
if !tid.Equal(tunnelID) {
|
||||
resp.Status = relay.StatusHostUnreachable
|
||||
resp.WriteTo(conn)
|
||||
err := fmt.Errorf("no route to host %s", host)
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// direct routing
|
||||
if h.md.directTunnel {
|
||||
tid = tunnelID
|
||||
} else if !tid.Equal(tunnelID) {
|
||||
resp.Status = relay.StatusHostUnreachable
|
||||
resp.WriteTo(conn)
|
||||
err := fmt.Errorf("no route to host %s", host)
|
||||
log.Error(err)
|
||||
return err
|
||||
d := Dialer{
|
||||
node: h.id,
|
||||
pool: h.pool,
|
||||
sd: h.md.sd,
|
||||
retry: 3,
|
||||
timeout: 15 * time.Second,
|
||||
log: log,
|
||||
}
|
||||
|
||||
cc, _, err := getTunnelConn(network, h.pool, tid, 3, log)
|
||||
cc, node, cid, err := d.Dial(ctx, network, tunnelID.String())
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
resp.Status = relay.StatusServiceUnavailable
|
||||
resp.WriteTo(conn)
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
defer cc.Close()
|
||||
|
||||
log.Debugf("%s >> %s", conn.RemoteAddr(), cc.RemoteAddr())
|
||||
log.Debugf("new connection to tunnel: %s, connector: %s", tunnelID, cid)
|
||||
|
||||
if _, err := resp.WriteTo(conn); err != nil {
|
||||
log.Error(err)
|
||||
return err
|
||||
if node == h.id {
|
||||
if _, err := resp.WriteTo(conn); err != nil {
|
||||
log.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
resp = relay.Response{
|
||||
Version: relay.Version1,
|
||||
Status: relay.StatusOK,
|
||||
}
|
||||
|
||||
af := &relay.AddrFeature{}
|
||||
af.ParseFrom(srcAddr)
|
||||
resp.Features = append(resp.Features, af) // src address
|
||||
|
||||
af = &relay.AddrFeature{}
|
||||
af.ParseFrom(dstAddr)
|
||||
resp.Features = append(resp.Features, af) // dst address
|
||||
|
||||
resp.WriteTo(cc)
|
||||
} else {
|
||||
req.WriteTo(cc)
|
||||
}
|
||||
|
||||
resp = relay.Response{
|
||||
Version: relay.Version1,
|
||||
Status: relay.StatusOK,
|
||||
}
|
||||
|
||||
af := &relay.AddrFeature{}
|
||||
af.ParseFrom(srcAddr)
|
||||
resp.Features = append(resp.Features, af) // src address
|
||||
|
||||
af = &relay.AddrFeature{}
|
||||
af.ParseFrom(dstAddr)
|
||||
resp.Features = append(resp.Features, af) // dst address
|
||||
|
||||
resp.WriteTo(cc)
|
||||
|
||||
t := time.Now()
|
||||
log.Debugf("%s <-> %s", conn.RemoteAddr(), cc.RemoteAddr())
|
||||
xnet.Transport(conn, cc)
|
||||
|
Reference in New Issue
Block a user